TouchInputFieldTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. [UnityPlatform(exclude = new RuntimePlatform[]
  15. {
  16. RuntimePlatform.Android /* case 1094042 */
  17. })]
  18. public class TouchInputFieldTests : BaseInputFieldTests, IPrebuildSetup
  19. {
  20. protected const string kPrefabPath = "Assets/Resources/TouchInputFieldPrefab.prefab";
  21. public void Setup()
  22. {
  23. #if UNITY_EDITOR
  24. CreateInputFieldAsset(kPrefabPath);
  25. #endif
  26. }
  27. [SetUp]
  28. public void TestSetup()
  29. {
  30. m_PrefabRoot = UnityEngine.Object.Instantiate(Resources.Load("TouchInputFieldPrefab")) as GameObject;
  31. FieldInfo inputModule = typeof(EventSystem).GetField("m_CurrentInputModule", BindingFlags.NonPublic | BindingFlags.Instance);
  32. inputModule.SetValue(m_PrefabRoot.GetComponentInChildren<EventSystem>(), m_PrefabRoot.GetComponentInChildren<FakeInputModule>());
  33. }
  34. [TearDown]
  35. public void TearDown()
  36. {
  37. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  38. TouchScreenKeyboard.hideInput = false;
  39. FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
  40. GameObject.DestroyImmediate(m_PrefabRoot);
  41. }
  42. [OneTimeTearDown]
  43. public void OnetimeTearDown()
  44. {
  45. #if UNITY_EDITOR
  46. AssetDatabase.DeleteAsset(kPrefabPath);
  47. #endif
  48. }
  49. protected const string kDefaultInputStr = "foobar";
  50. const string kEmailSpecialCharacters = "!#$%&'*+-/=?^_`{|}~";
  51. public struct CharValidationTestData
  52. {
  53. public string input, output;
  54. public InputField.CharacterValidation validation;
  55. public CharValidationTestData(string input, string output, InputField.CharacterValidation validation)
  56. {
  57. this.input = input;
  58. this.output = output;
  59. this.validation = validation;
  60. }
  61. public override string ToString()
  62. {
  63. // these won't properly show up if test runners UI if we don't replace it
  64. string input = this.input.Replace(kEmailSpecialCharacters, "specialchars");
  65. string output = this.output.Replace(kEmailSpecialCharacters, "specialchars");
  66. return string.Format("input={0}, output={1}, validation={2}", input, output, validation);
  67. }
  68. }
  69. [Test]
  70. [TestCase("*Azé09", "*Azé09", InputField.CharacterValidation.None)]
  71. [TestCase("*Azé09?.", "Az09", InputField.CharacterValidation.Alphanumeric)]
  72. [TestCase("Abc10x", "10", InputField.CharacterValidation.Integer)]
  73. [TestCase("-10", "-10", InputField.CharacterValidation.Integer)]
  74. [TestCase("10.0", "100", InputField.CharacterValidation.Integer)]
  75. [TestCase("10.0", "10.0", InputField.CharacterValidation.Decimal)]
  76. [TestCase(" -10.0x", "-10.0", InputField.CharacterValidation.Decimal)]
  77. [TestCase("10,0", "10,0", InputField.CharacterValidation.Decimal)]
  78. [TestCase(" -10,0x", "-10,0", InputField.CharacterValidation.Decimal)]
  79. [TestCase("A10,0 ", "10,0", InputField.CharacterValidation.Decimal)]
  80. [TestCase("A'a aaa aaa", "A'a Aaa Aaa", InputField.CharacterValidation.Name)]
  81. [TestCase(" _JOHN* (Doe)", "John Doe", InputField.CharacterValidation.Name)]
  82. [TestCase("johndoe@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress)]
  83. [TestCase(">john doe\\@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress)]
  84. [TestCase(kEmailSpecialCharacters + "@unity3d.com", kEmailSpecialCharacters + "@unity3d.com", InputField.CharacterValidation.EmailAddress)]
  85. public void HonorsCharacterValidationSettingsAssignment(string input, string output, InputField.CharacterValidation validation)
  86. {
  87. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  88. inputField.characterValidation = validation;
  89. inputField.text = input;
  90. Assert.AreEqual(output, inputField.text, string.Format("Failed character validation: input ={0}, output ={1}, validation ={2}",
  91. input.Replace(kEmailSpecialCharacters, "specialchars"),
  92. output.Replace(kEmailSpecialCharacters, "specialchars"),
  93. validation));
  94. }
  95. [UnityTest]
  96. [TestCase("*Azé09", "*Azé09", InputField.CharacterValidation.None, ExpectedResult = null)]
  97. [TestCase("*Azé09?.", "Az09", InputField.CharacterValidation.Alphanumeric, ExpectedResult = null)]
  98. [TestCase("Abc10x", "10", InputField.CharacterValidation.Integer, ExpectedResult = null)]
  99. [TestCase("-10", "-10", InputField.CharacterValidation.Integer, ExpectedResult = null)]
  100. [TestCase("10.0", "100", InputField.CharacterValidation.Integer, ExpectedResult = null)]
  101. [TestCase("10.0", "10.0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  102. [TestCase(" -10.0x", "-10.0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  103. [TestCase("10,0", "10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  104. [TestCase(" -10,0x", "-10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  105. [TestCase("A10,0 ", "10,0", InputField.CharacterValidation.Decimal, ExpectedResult = null)]
  106. [TestCase("A'a aaa aaa", "A'a Aaa Aaa", InputField.CharacterValidation.Name, ExpectedResult = null)]
  107. [TestCase(" _JOHN* (Doe)", "John Doe", InputField.CharacterValidation.Name, ExpectedResult = null)]
  108. [TestCase("johndoe@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
  109. [TestCase(">john doe\\@unity3d.com", "johndoe@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
  110. [TestCase(kEmailSpecialCharacters + "@unity3d.com", kEmailSpecialCharacters + "@unity3d.com", InputField.CharacterValidation.EmailAddress, ExpectedResult = null)]
  111. public IEnumerator HonorsCharacterValidationSettingsTypingWithSelection(string input, string output, InputField.CharacterValidation validation)
  112. {
  113. if (!TouchScreenKeyboard.isSupported)
  114. yield break;
  115. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  116. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  117. inputField.characterValidation = validation;
  118. inputField.text = input;
  119. inputField.OnSelect(eventData);
  120. yield return null;
  121. Assert.AreEqual(output, inputField.text, string.Format("Failed character validation: input ={0}, output ={1}, validation ={2}",
  122. input.Replace(kEmailSpecialCharacters, "specialchars"),
  123. output.Replace(kEmailSpecialCharacters, "specialchars"),
  124. validation));
  125. }
  126. [Test]
  127. public void AssignmentAgainstCharacterLimit([Values("ABC", "abcdefghijkl")] string text)
  128. {
  129. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  130. // test assignment
  131. inputField.characterLimit = 5;
  132. inputField.text = text;
  133. Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
  134. }
  135. [Test] // regression test 793119
  136. public void AssignmentAgainstCharacterLimitWithContentType([Values("Abc", "Abcdefghijkl")] string text)
  137. {
  138. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  139. // test assignment
  140. inputField.characterLimit = 5;
  141. inputField.contentType = InputField.ContentType.Name;
  142. inputField.text = text;
  143. Assert.AreEqual(text.Substring(0, Math.Min(text.Length, inputField.characterLimit)), inputField.text);
  144. }
  145. [UnityTest]
  146. public IEnumerator SendsEndEditEventOnDeselect()
  147. {
  148. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  149. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  150. inputField.OnSelect(eventData);
  151. yield return null;
  152. var called = false;
  153. inputField.onEndEdit.AddListener((s) => { called = true; });
  154. inputField.OnDeselect(eventData);
  155. Assert.IsTrue(called, "Expected invocation of onEndEdit");
  156. }
  157. [Test]
  158. public void StripsNullCharacters2()
  159. {
  160. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  161. inputField.text = "a\0b";
  162. Assert.AreEqual("ab", inputField.text, "\\0 characters should be stripped");
  163. }
  164. [UnityTest]
  165. [UnityPlatform(exclude = new[]
  166. {
  167. RuntimePlatform.Android // case 1338327
  168. })]
  169. public IEnumerator FocusOpensTouchScreenKeyboard()
  170. {
  171. if (!TouchScreenKeyboard.isSupported)
  172. yield break;
  173. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  174. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  175. inputField.OnSelect(eventData);
  176. yield return null;
  177. Assert.NotNull(inputField.touchScreenKeyboard, "Expect a keyboard to be opened");
  178. }
  179. [UnityTest]
  180. public IEnumerator AssignsShouldHideInput()
  181. {
  182. if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
  183. {
  184. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  185. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  186. inputField.shouldHideMobileInput = false;
  187. inputField.OnSelect(eventData);
  188. yield return null;
  189. Assert.IsFalse(inputField.shouldHideMobileInput);
  190. Assert.IsFalse(TouchScreenKeyboard.hideInput, "Expect TouchScreenKeyboard.hideInput to be set");
  191. }
  192. }
  193. }
  194. }