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.

GenericInputFieldTests.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 GenericInputFieldTests : BaseInputFieldTests, IPrebuildSetup
  15. {
  16. protected const string kPrefabPath = "Assets/Resources/GenericInputFieldPrefab.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("GenericInputFieldPrefab")) as GameObject;
  27. }
  28. [TearDown]
  29. public virtual void TearDown()
  30. {
  31. FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
  32. GameObject.DestroyImmediate(m_PrefabRoot);
  33. }
  34. [OneTimeTearDown]
  35. public void OnetimeTearDown()
  36. {
  37. #if UNITY_EDITOR
  38. AssetDatabase.DeleteAsset(kPrefabPath);
  39. #endif
  40. }
  41. [UnityTest]
  42. public IEnumerator CannotFocusIfNotTextComponent()
  43. {
  44. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  45. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  46. inputField.textComponent = null;
  47. inputField.OnSelect(eventData);
  48. yield return null;
  49. Assert.False(inputField.isFocused);
  50. }
  51. [UnityTest]
  52. public IEnumerator CannotFocusIfNullFont()
  53. {
  54. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  55. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  56. inputField.textComponent.font = null;
  57. inputField.OnSelect(eventData);
  58. yield return null;
  59. Assert.False(inputField.isFocused);
  60. }
  61. [UnityTest]
  62. public IEnumerator CannotFocusIfNotActive()
  63. {
  64. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  65. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  66. inputField.enabled = false;
  67. inputField.OnSelect(eventData);
  68. yield return null;
  69. Assert.False(inputField.isFocused);
  70. }
  71. [UnityTest]
  72. public IEnumerator CannotFocusWithoutEventSystem()
  73. {
  74. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  75. UnityEngine.Object.DestroyImmediate(m_PrefabRoot.GetComponentInChildren<FakeInputModule>());
  76. yield return null;
  77. UnityEngine.Object.DestroyImmediate(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  78. BaseEventData eventData = new BaseEventData(null);
  79. yield return null;
  80. inputField.OnSelect(eventData);
  81. yield return null;
  82. Assert.False(inputField.isFocused);
  83. }
  84. [UnityTest]
  85. [UnityPlatform(exclude = new[] { RuntimePlatform.Switch, RuntimePlatform.tvOS })] // Currently InputField.ActivateInputFieldInternal calls Switch SoftwareKeyboard screen ; without user input or a command to close the SoftwareKeyboard this blocks the tests suite. tvOS UUM-71764
  86. public IEnumerator FocusesOnSelect()
  87. {
  88. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  89. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  90. inputField.OnSelect(eventData);
  91. MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
  92. lateUpdate.Invoke(inputField, null);
  93. #if UNITY_GAMECORE && !UNITY_EDITOR
  94. if (TouchScreenKeyboard.isSupported)
  95. {
  96. // On Xbox, the onScreenKeyboard is going to constrain the application and make it go out of focus.
  97. // We need to wait for the application to go out of focus before we can close the onScreenKeyboard.
  98. while (Application.isFocused)
  99. {
  100. yield return null;
  101. }
  102. }
  103. #endif
  104. Assert.True(inputField.isFocused);
  105. #if UNITY_GAMECORE && !UNITY_EDITOR
  106. // On Xbox, we then need to close onScreenKeyboard and wait for the application to be focused again.
  107. // 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.
  108. if (!TouchScreenKeyboard.isSupported || !TouchScreenKeyboard.visible)
  109. {
  110. yield break;
  111. }
  112. while (!Application.isFocused)
  113. {
  114. if (inputField.touchScreenKeyboard != null)
  115. {
  116. inputField.touchScreenKeyboard.active = false;
  117. }
  118. yield return null;
  119. }
  120. #else
  121. yield break;
  122. #endif
  123. }
  124. [Test]
  125. public void DoesNotFocusesOnSelectWhenShouldActivateOnSelect_IsFalse()
  126. {
  127. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  128. inputField.shouldActivateOnSelect = false;
  129. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  130. inputField.OnSelect(eventData);
  131. MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
  132. lateUpdate.Invoke(inputField, null);
  133. Assert.False(inputField.isFocused);
  134. }
  135. [Test]
  136. public void InputFieldSetTextWithoutNotifyWillNotNotify()
  137. {
  138. InputField i = m_PrefabRoot.GetComponentInChildren<InputField>();
  139. i.text = "Hello";
  140. bool calledOnValueChanged = false;
  141. i.onValueChanged.AddListener(s => { calledOnValueChanged = true; });
  142. i.SetTextWithoutNotify("Goodbye");
  143. Assert.IsTrue(i.text == "Goodbye");
  144. Assert.IsFalse(calledOnValueChanged);
  145. }
  146. [Test]
  147. public void ContentTypeSetsValues()
  148. {
  149. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  150. inputField.contentType = InputField.ContentType.Standard;
  151. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  152. Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
  153. Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
  154. inputField.contentType = InputField.ContentType.Autocorrected;
  155. Assert.AreEqual(InputField.InputType.AutoCorrect, inputField.inputType);
  156. Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
  157. Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
  158. inputField.contentType = InputField.ContentType.IntegerNumber;
  159. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  160. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  161. Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
  162. Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
  163. inputField.contentType = InputField.ContentType.DecimalNumber;
  164. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  165. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  166. Assert.AreEqual(TouchScreenKeyboardType.NumbersAndPunctuation, inputField.keyboardType);
  167. Assert.AreEqual(InputField.CharacterValidation.Decimal, inputField.characterValidation);
  168. inputField.contentType = InputField.ContentType.Alphanumeric;
  169. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  170. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  171. Assert.AreEqual(TouchScreenKeyboardType.ASCIICapable, inputField.keyboardType);
  172. Assert.AreEqual(InputField.CharacterValidation.Alphanumeric, inputField.characterValidation);
  173. inputField.contentType = InputField.ContentType.Name;
  174. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  175. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  176. Assert.AreEqual(TouchScreenKeyboardType.NamePhonePad, inputField.keyboardType);
  177. Assert.AreEqual(InputField.CharacterValidation.Name, inputField.characterValidation);
  178. inputField.contentType = InputField.ContentType.EmailAddress;
  179. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  180. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  181. Assert.AreEqual(TouchScreenKeyboardType.EmailAddress, inputField.keyboardType);
  182. Assert.AreEqual(InputField.CharacterValidation.EmailAddress, inputField.characterValidation);
  183. inputField.contentType = InputField.ContentType.Password;
  184. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  185. Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
  186. Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
  187. Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
  188. inputField.contentType = InputField.ContentType.Pin;
  189. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  190. Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
  191. Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
  192. Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
  193. }
  194. [Test]
  195. public void SettingLineTypeDoesNotChangesContentTypeToCustom([Values(InputField.ContentType.Standard, InputField.ContentType.Autocorrected)] InputField.ContentType type)
  196. {
  197. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  198. inputField.contentType = type;
  199. inputField.lineType = InputField.LineType.MultiLineNewline;
  200. Assert.AreEqual(type, inputField.contentType);
  201. }
  202. [Test]
  203. public void SettingLineTypeChangesContentTypeToCustom()
  204. {
  205. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  206. inputField.contentType = InputField.ContentType.Name;
  207. inputField.lineType = InputField.LineType.MultiLineNewline;
  208. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  209. }
  210. [Test]
  211. public void SettingInputChangesContentTypeToCustom()
  212. {
  213. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  214. inputField.contentType = InputField.ContentType.Name;
  215. inputField.inputType = InputField.InputType.Password;
  216. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  217. }
  218. [Test]
  219. public void SettingCharacterValidationChangesContentTypeToCustom()
  220. {
  221. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  222. inputField.contentType = InputField.ContentType.Name;
  223. inputField.characterValidation = InputField.CharacterValidation.None;
  224. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  225. }
  226. [Test]
  227. public void SettingKeyboardTypeChangesContentTypeToCustom()
  228. {
  229. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  230. inputField.contentType = InputField.ContentType.Name;
  231. inputField.keyboardType = TouchScreenKeyboardType.ASCIICapable;
  232. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  233. }
  234. [UnityTest]
  235. public IEnumerator CaretRectSameSizeAsTextRect()
  236. {
  237. InputField inputfield = m_PrefabRoot.GetComponentInChildren<InputField>();
  238. HorizontalLayoutGroup lg = inputfield.gameObject.AddComponent<HorizontalLayoutGroup>();
  239. lg.childControlWidth = true;
  240. lg.childControlHeight = false;
  241. lg.childForceExpandWidth = true;
  242. lg.childForceExpandHeight = true;
  243. ContentSizeFitter csf = inputfield.gameObject.AddComponent<ContentSizeFitter>();
  244. csf.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
  245. csf.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
  246. inputfield.text = "Hello World!";
  247. yield return new WaitForSeconds(1.0f);
  248. Rect prevTextRect = inputfield.textComponent.rectTransform.rect;
  249. Rect prevCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
  250. inputfield.text = "Hello World!Hello World!Hello World!";
  251. LayoutRebuilder.MarkLayoutForRebuild(inputfield.transform as RectTransform);
  252. yield return new WaitForSeconds(1.0f);
  253. Rect newTextRect = inputfield.textComponent.rectTransform.rect;
  254. Rect newCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
  255. Assert.IsFalse(prevTextRect == newTextRect);
  256. Assert.IsTrue(prevTextRect == prevCaretRect);
  257. Assert.IsFalse(prevCaretRect == newCaretRect);
  258. Assert.IsTrue(newTextRect == newCaretRect);
  259. }
  260. }
  261. }