Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TouchInputFieldTests.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 TouchInputFieldTests : BaseInputFieldTests, IPrebuildSetup
  15. {
  16. protected const string kPrefabPath = "Assets/Resources/TouchInputFieldPrefab.prefab";
  17. public void Setup()
  18. {
  19. #if UNITY_EDITOR
  20. CreateInputFieldAsset(kPrefabPath);
  21. #endif
  22. }
  23. [SetUp]
  24. public void TestSetup()
  25. {
  26. m_PrefabRoot = UnityEngine.Object.Instantiate(Resources.Load("TouchInputFieldPrefab")) 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 void TearDown()
  32. {
  33. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  34. TouchScreenKeyboard.hideInput = false;
  35. FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
  36. GameObject.DestroyImmediate(m_PrefabRoot);
  37. }
  38. [OneTimeTearDown]
  39. public void OnetimeTearDown()
  40. {
  41. #if UNITY_EDITOR
  42. AssetDatabase.DeleteAsset(kPrefabPath);
  43. #endif
  44. }
  45. protected const string kDefaultInputStr = "foobar";
  46. const string kEmailSpecialCharacters = "!#$%&'*+-/=?^_`{|}~";
  47. public struct CharValidationTestData
  48. {
  49. public string input, output;
  50. public InputField.CharacterValidation validation;
  51. public CharValidationTestData(string input, string output, InputField.CharacterValidation validation)
  52. {
  53. this.input = input;
  54. this.output = output;
  55. this.validation = validation;
  56. }
  57. public override string ToString()
  58. {
  59. // these won't properly show up if test runners UI if we don't replace it
  60. string input = this.input.Replace(kEmailSpecialCharacters, "specialchars");
  61. string output = this.output.Replace(kEmailSpecialCharacters, "specialchars");
  62. return string.Format("input={0}, output={1}, validation={2}", input, output, validation);
  63. }
  64. }
  65. [Test]
  66. [TestCase("*Azé09", "*Azé09", InputField.CharacterValidation.None)]
  67. [TestCase("*Azé09?.", "Az09", InputField.CharacterValidation.Alphanumeric)]
  68. [TestCase("Abc10x", "10", InputField.CharacterValidation.Integer)]
  69. [TestCase("-10", "-10", InputField.CharacterValidation.Integer)]
  70. [TestCase("10.0", "100", InputField.CharacterValidation.Integer)]
  71. [TestCase("10.0", "10.0", InputField.CharacterValidation.Decimal)]
  72. [TestCase(" -10.0x", "-10.0", InputField.CharacterValidation.Decimal)]
  73. [TestCase("10,0", "10,0", InputField.CharacterValidation.Decimal)]
  74. [TestCase(" -10,0x", "-10,0", InputField.CharacterValidation.Decimal)]
  75. [TestCase("A10,0 ", "10,0", InputField.CharacterValidation.Decimal)]
  76. [TestCase("A'a aaa aaa", "A'a Aaa Aaa", InputField.CharacterValidation.Name)]
  77. [TestCase("Unity-Editor", "Unity-Editor", InputField.CharacterValidation.Name)]
  78. [TestCase("Unity--Editor", "Unity-Editor", InputField.CharacterValidation.Name)]
  79. [TestCase("-UnityEditor", "Unityeditor", InputField.CharacterValidation.Name)]
  80. [TestCase(" _JOHN* (Doe)", "John Doe", InputField.CharacterValidation.Name)]
  81. [TestCase("johndoe@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress)]
  82. [TestCase(">john doe\\@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress)]
  83. [TestCase(kEmailSpecialCharacters + "@unity3d.com", kEmailSpecialCharacters + "@unity3d.com", InputField.CharacterValidation.EmailAddress)]
  84. public void HonorsCharacterValidationSettingsAssignment(string input, string output, InputField.CharacterValidation validation)
  85. {
  86. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  87. inputField.characterValidation = validation;
  88. inputField.text = input;
  89. Assert.AreEqual(output, inputField.text, string.Format("Failed character validation: input ={0}, output ={1}, validation ={2}",
  90. input.Replace(kEmailSpecialCharacters, "specialchars"),
  91. output.Replace(kEmailSpecialCharacters, "specialchars"),
  92. validation));
  93. }
  94. [UnityTest]
  95. [TestCase("*Azé09", "*Azé09", InputField.CharacterValidation.None, ExpectedResult = null)]
  96. [TestCase("*Azé09?.", "Az09", InputField.CharacterValidation.Alphanumeric, ExpectedResult = null)]
  97. [TestCase("Abc10x", "10", InputField.CharacterValidation.Integer, ExpectedResult = null)]
  98. [TestCase("-10", "-10", InputField.CharacterValidation.Integer, ExpectedResult = null)]
  99. [TestCase("10.0", "100", InputField.CharacterValidation.Integer, ExpectedResult = null)]
  100. [TestCase("10.0", "10.0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  101. [TestCase(" -10.0x", "-10.0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  102. [TestCase("10,0", "10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  103. [TestCase(" -10,0x", "-10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  104. [TestCase("A10,0 ", "10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  105. [TestCase("A'a aaa aaa", "A'a Aaa Aaa", InputField.CharacterValidation.Name, ExpectedResult = null)]
  106. [TestCase(" _JOHN* (Doe)", "John Doe", InputField.CharacterValidation.Name, ExpectedResult = null)]
  107. [TestCase("johndoe@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
  108. [TestCase(">john doe\\@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
  109. [TestCase(kEmailSpecialCharacters + "@unity3d.com", kEmailSpecialCharacters + "@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
  110. public IEnumerator HonorsCharacterValidationSettingsTypingWithSelection(string input, string output, InputField.CharacterValidation validation)
  111. {
  112. if (!TouchScreenKeyboard.isSupported)
  113. yield break;
  114. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  115. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  116. inputField.characterValidation = validation;
  117. inputField.text = input;
  118. inputField.OnSelect(eventData);
  119. #if UNITY_GAMECORE && !UNITY_EDITOR
  120. // On Xbox, the onScreenKeyboard is going to constrain the application and make it go out of focus.
  121. // We need to wait for the application to go out of focus before we can close the onScreenKeyboard.
  122. while (Application.isFocused)
  123. {
  124. yield return null;
  125. }
  126. #else
  127. yield return null;
  128. #endif
  129. Assert.AreEqual(output, inputField.text, string.Format("Failed character validation: input ={0}, output ={1}, validation ={2}",
  130. input.Replace(kEmailSpecialCharacters, "specialchars"),
  131. output.Replace(kEmailSpecialCharacters, "specialchars"),
  132. validation));
  133. #if UNITY_GAMECORE && !UNITY_EDITOR
  134. // On Xbox, we then need to close onScreenKeyboard and wait for the application to be focused again.
  135. // 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.
  136. while (!Application.isFocused)
  137. {
  138. if (inputField.touchScreenKeyboard != null)
  139. {
  140. inputField.touchScreenKeyboard.active = false;
  141. }
  142. yield return null;
  143. }
  144. #endif
  145. }
  146. [Test]
  147. public void AssignmentAgainstCharacterLimit([Values("ABC", "abcdefghijkl")] string text)
  148. {
  149. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  150. // test assignment
  151. inputField.characterLimit = 5;
  152. inputField.text = text;
  153. Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
  154. }
  155. [Test] // regression test 793119
  156. public void AssignmentAgainstCharacterLimitWithContentType([Values("Abc", "Abcdefghijkl")] string text)
  157. {
  158. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  159. // test assignment
  160. inputField.characterLimit = 5;
  161. inputField.contentType = InputField.ContentType.Name;
  162. inputField.text = text;
  163. Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
  164. }
  165. [UnityTest]
  166. public IEnumerator SendsEndEditEventOnDeselect()
  167. {
  168. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  169. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  170. inputField.OnSelect(eventData);
  171. #if UNITY_GAMECORE && !UNITY_EDITOR
  172. while (Application.isFocused)
  173. {
  174. yield return null;
  175. }
  176. #else
  177. yield return null;
  178. #endif
  179. var called = false;
  180. inputField.onEndEdit.AddListener((s) => { called = true; });
  181. inputField.OnDeselect(eventData);
  182. #if UNITY_GAMECORE && !UNITY_EDITOR
  183. while (!Application.isFocused)
  184. {
  185. if (inputField.touchScreenKeyboard != null)
  186. {
  187. inputField.touchScreenKeyboard.active = false;
  188. }
  189. yield return null;
  190. }
  191. #endif
  192. Assert.IsTrue(called, "Expected invocation of onEndEdit");
  193. }
  194. [Test]
  195. public void StripsNullCharacters2()
  196. {
  197. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  198. inputField.text = "a\0b";
  199. Assert.AreEqual("ab", inputField.text, "\\0 characters should be stripped");
  200. }
  201. [UnityTest]
  202. public IEnumerator FocusOpensTouchScreenKeyboard()
  203. {
  204. var isInPlaceEditingDisabled = typeof(TouchScreenKeyboard).GetProperty("disableInPlaceEditing",
  205. System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
  206. isInPlaceEditingDisabled.SetValue(null, true);
  207. if (!TouchScreenKeyboard.isSupported)
  208. yield break;
  209. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  210. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  211. inputField.OnSelect(eventData);
  212. #if UNITY_GAMECORE && !UNITY_EDITOR
  213. while (Application.isFocused)
  214. {
  215. yield return null;
  216. }
  217. #else
  218. yield return null;
  219. #endif
  220. Assert.NotNull(inputField.touchScreenKeyboard, "Expect a keyboard to be opened");
  221. #if UNITY_GAMECORE && !UNITY_EDITOR
  222. while (!Application.isFocused)
  223. {
  224. if (inputField.touchScreenKeyboard != null)
  225. {
  226. inputField.touchScreenKeyboard.active = false;
  227. }
  228. yield return null;
  229. }
  230. #endif
  231. }
  232. [UnityTest]
  233. public IEnumerator AssignsShouldHideInput()
  234. {
  235. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  236. {
  237. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  238. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  239. inputField.shouldHideMobileInput = false;
  240. inputField.OnSelect(eventData);
  241. yield return null;
  242. Assert.IsFalse(inputField.shouldHideMobileInput);
  243. Assert.IsFalse(TouchScreenKeyboard.hideInput, "Expect TouchScreenKeyboard.hideInput to be set");
  244. }
  245. }
  246. [UnityTest]
  247. [UnityPlatform(exclude = new[] { RuntimePlatform.IPhonePlayer, RuntimePlatform.PS4, RuntimePlatform.PS5, RuntimePlatform.VisionOS })] // disabled on visionOS due to UUM-61018
  248. public IEnumerator IsTouchScreenKeyboardVisible()
  249. {
  250. if (!TouchScreenKeyboard.isSupported)
  251. yield break;
  252. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  253. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  254. inputField.OnSelect(eventData);
  255. #if UNITY_GAMECORE && !UNITY_EDITOR
  256. while (Application.isFocused)
  257. {
  258. yield return null;
  259. }
  260. #else
  261. yield return null;
  262. #endif
  263. Assert.IsTrue(TouchScreenKeyboard.visible);
  264. inputField.OnDeselect(eventData);
  265. #if UNITY_GAMECORE && !UNITY_EDITOR
  266. while (!Application.isFocused)
  267. {
  268. if (inputField.touchScreenKeyboard != null)
  269. {
  270. inputField.touchScreenKeyboard.active = false;
  271. }
  272. yield return null;
  273. }
  274. #else
  275. yield return null;
  276. #endif
  277. Assert.IsFalse(TouchScreenKeyboard.visible);
  278. }
  279. }
  280. }