暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DesktopInputFieldTests.cs 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.TestTools;
  7. using System.Collections;
  8. using System.IO;
  9. using UnityEditor;
  10. using UnityEngine.UI;
  11. using System.Reflection;
  12. namespace InputfieldTests
  13. {
  14. public class DesktopInputFieldTests : BaseInputFieldTests, IPrebuildSetup
  15. {
  16. protected const string kPrefabPath = "Assets/Resources/DesktopInputFieldPrefab.prefab";
  17. public void Setup()
  18. {
  19. #if UNITY_EDITOR
  20. CreateInputFieldAsset(kPrefabPath);
  21. #endif
  22. }
  23. [SetUp]
  24. public virtual void TestSetup()
  25. {
  26. m_PrefabRoot = UnityEngine.Object.Instantiate(Resources.Load("DesktopInputFieldPrefab")) as GameObject;
  27. FieldInfo inputModule = typeof(EventSystem).GetField("m_CurrentInputModule", BindingFlags.NonPublic | BindingFlags.Instance);
  28. inputModule.SetValue(m_PrefabRoot.GetComponentInChildren<EventSystem>(), m_PrefabRoot.GetComponentInChildren<FakeInputModule>());
  29. }
  30. [TearDown]
  31. public virtual void TearDown()
  32. {
  33. GUIUtility.systemCopyBuffer = null;
  34. FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
  35. GameObject.DestroyImmediate(m_PrefabRoot);
  36. }
  37. [OneTimeTearDown]
  38. public void OnetimeTearDown()
  39. {
  40. #if UNITY_EDITOR
  41. AssetDatabase.DeleteAsset(kPrefabPath);
  42. #endif
  43. }
  44. [Test]
  45. public void FocusOnPointerClickWithLeftButton()
  46. {
  47. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  48. PointerEventData data = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  49. data.button = PointerEventData.InputButton.Left;
  50. inputField.OnPointerClick(data);
  51. MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
  52. lateUpdate.Invoke(inputField, null);
  53. Assert.IsTrue(inputField.isFocused);
  54. }
  55. [UnityTest]
  56. public IEnumerator DoesNotFocusOnPointerClickWithRightOrMiddleButton()
  57. {
  58. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  59. PointerEventData data = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  60. data.button = PointerEventData.InputButton.Middle;
  61. inputField.OnPointerClick(data);
  62. yield return null;
  63. data.button = PointerEventData.InputButton.Right;
  64. inputField.OnPointerClick(data);
  65. yield return null;
  66. Assert.IsFalse(inputField.isFocused);
  67. }
  68. }
  69. }