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.

TouchInputFieldTests.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. yield return null;
  120. Assert.AreEqual(output, inputField.text, string.Format("Failed character validation: input ={0}, output ={1}, validation ={2}",
  121. input.Replace(kEmailSpecialCharacters, "specialchars"),
  122. output.Replace(kEmailSpecialCharacters, "specialchars"),
  123. validation));
  124. }
  125. [Test]
  126. public void AssignmentAgainstCharacterLimit([Values("ABC", "abcdefghijkl")] string text)
  127. {
  128. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  129. // test assignment
  130. inputField.characterLimit = 5;
  131. inputField.text = text;
  132. Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
  133. }
  134. [Test] // regression test 793119
  135. public void AssignmentAgainstCharacterLimitWithContentType([Values("Abc", "Abcdefghijkl")] string text)
  136. {
  137. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  138. // test assignment
  139. inputField.characterLimit = 5;
  140. inputField.contentType = InputField.ContentType.Name;
  141. inputField.text = text;
  142. Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
  143. }
  144. [UnityTest]
  145. public IEnumerator SendsEndEditEventOnDeselect()
  146. {
  147. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  148. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  149. inputField.OnSelect(eventData);
  150. yield return null;
  151. var called = false;
  152. inputField.onEndEdit.AddListener((s) => { called = true; });
  153. inputField.OnDeselect(eventData);
  154. Assert.IsTrue(called, "Expected invocation of onEndEdit");
  155. }
  156. [Test]
  157. public void StripsNullCharacters2()
  158. {
  159. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  160. inputField.text = "a\0b";
  161. Assert.AreEqual("ab", inputField.text, "\\0 characters should be stripped");
  162. }
  163. [UnityTest]
  164. public IEnumerator FocusOpensTouchScreenKeyboard()
  165. {
  166. var isInPlaceEditingDisabled = typeof(TouchScreenKeyboard).GetProperty("disableInPlaceEditing",
  167. System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
  168. isInPlaceEditingDisabled.SetValue(null, true);
  169. if (!TouchScreenKeyboard.isSupported)
  170. yield break;
  171. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  172. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  173. inputField.OnSelect(eventData);
  174. yield return null;
  175. Assert.NotNull(inputField.touchScreenKeyboard, "Expect a keyboard to be opened");
  176. }
  177. [UnityTest]
  178. public IEnumerator AssignsShouldHideInput()
  179. {
  180. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  181. {
  182. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  183. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  184. inputField.shouldHideMobileInput = false;
  185. inputField.OnSelect(eventData);
  186. yield return null;
  187. Assert.IsFalse(inputField.shouldHideMobileInput);
  188. Assert.IsFalse(TouchScreenKeyboard.hideInput, "Expect TouchScreenKeyboard.hideInput to be set");
  189. }
  190. }
  191. }
  192. }