No Description
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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. [UnityTest]
  45. [UnityPlatform(exclude = new[] { RuntimePlatform.Switch })] // Currently InputField.ActivateInputFieldInternal calls Switch SoftwareKeyboard screen ; without user input or a command to close the SoftwareKeyboard this blocks the tests suite
  46. public IEnumerator FocusOnPointerClickWithLeftButton()
  47. {
  48. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  49. PointerEventData data = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  50. data.button = PointerEventData.InputButton.Left;
  51. inputField.OnPointerClick(data);
  52. MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
  53. lateUpdate.Invoke(inputField, null);
  54. #if UNITY_GAMECORE && !UNITY_EDITOR
  55. if (TouchScreenKeyboard.isSupported)
  56. {
  57. // On Xbox, the onScreenKeyboard is going to constrain the application and make it go out of focus.
  58. // We need to wait for the application to go out of focus before we can close the onScreenKeyboard.
  59. while (Application.isFocused)
  60. {
  61. yield return null;
  62. }
  63. }
  64. #endif
  65. Assert.IsTrue(inputField.isFocused);
  66. #if UNITY_GAMECORE && !UNITY_EDITOR
  67. // On Xbox, we then need to close onScreenKeyboard and wait for the application to be focused again.
  68. // If this is not done, it could have an impact on subsequent tests that require the application to be focused in order to function correctly.
  69. if (!TouchScreenKeyboard.isSupported || !TouchScreenKeyboard.visible)
  70. {
  71. yield break;
  72. }
  73. while (!Application.isFocused)
  74. {
  75. if (inputField.touchScreenKeyboard != null)
  76. {
  77. inputField.touchScreenKeyboard.active = false;
  78. }
  79. yield return null;
  80. }
  81. #else
  82. yield break;
  83. #endif
  84. }
  85. [UnityTest]
  86. public IEnumerator DoesNotFocusOnPointerClickWithRightOrMiddleButton()
  87. {
  88. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  89. PointerEventData data = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  90. data.button = PointerEventData.InputButton.Middle;
  91. inputField.OnPointerClick(data);
  92. yield return null;
  93. data.button = PointerEventData.InputButton.Right;
  94. inputField.OnPointerClick(data);
  95. yield return null;
  96. Assert.IsFalse(inputField.isFocused);
  97. }
  98. }
  99. }