暫無描述
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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. [Test]
  85. public void FocusesOnSelect()
  86. {
  87. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  88. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  89. inputField.OnSelect(eventData);
  90. MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
  91. lateUpdate.Invoke(inputField, null);
  92. Assert.True(inputField.isFocused);
  93. }
  94. [Test]
  95. public void DoesNotFocusesOnSelectWhenShouldActivateOnSelect_IsFalse()
  96. {
  97. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  98. inputField.shouldActivateOnSelect = false;
  99. BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
  100. inputField.OnSelect(eventData);
  101. MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
  102. lateUpdate.Invoke(inputField, null);
  103. Assert.False(inputField.isFocused);
  104. }
  105. [Test]
  106. public void InputFieldSetTextWithoutNotifyWillNotNotify()
  107. {
  108. InputField i = m_PrefabRoot.GetComponentInChildren<InputField>();
  109. i.text = "Hello";
  110. bool calledOnValueChanged = false;
  111. i.onValueChanged.AddListener(s => { calledOnValueChanged = true; });
  112. i.SetTextWithoutNotify("Goodbye");
  113. Assert.IsTrue(i.text == "Goodbye");
  114. Assert.IsFalse(calledOnValueChanged);
  115. }
  116. [Test]
  117. public void ContentTypeSetsValues()
  118. {
  119. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  120. inputField.contentType = InputField.ContentType.Standard;
  121. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  122. Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
  123. Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
  124. inputField.contentType = InputField.ContentType.Autocorrected;
  125. Assert.AreEqual(InputField.InputType.AutoCorrect, inputField.inputType);
  126. Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
  127. Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
  128. inputField.contentType = InputField.ContentType.IntegerNumber;
  129. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  130. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  131. Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
  132. Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
  133. inputField.contentType = InputField.ContentType.DecimalNumber;
  134. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  135. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  136. Assert.AreEqual(TouchScreenKeyboardType.NumbersAndPunctuation, inputField.keyboardType);
  137. Assert.AreEqual(InputField.CharacterValidation.Decimal, inputField.characterValidation);
  138. inputField.contentType = InputField.ContentType.Alphanumeric;
  139. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  140. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  141. Assert.AreEqual(TouchScreenKeyboardType.ASCIICapable, inputField.keyboardType);
  142. Assert.AreEqual(InputField.CharacterValidation.Alphanumeric, inputField.characterValidation);
  143. inputField.contentType = InputField.ContentType.Name;
  144. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  145. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  146. Assert.AreEqual(TouchScreenKeyboardType.NamePhonePad, inputField.keyboardType);
  147. Assert.AreEqual(InputField.CharacterValidation.Name, inputField.characterValidation);
  148. inputField.contentType = InputField.ContentType.EmailAddress;
  149. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  150. Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
  151. Assert.AreEqual(TouchScreenKeyboardType.EmailAddress, inputField.keyboardType);
  152. Assert.AreEqual(InputField.CharacterValidation.EmailAddress, inputField.characterValidation);
  153. inputField.contentType = InputField.ContentType.Password;
  154. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  155. Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
  156. Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
  157. Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
  158. inputField.contentType = InputField.ContentType.Pin;
  159. Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
  160. Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
  161. Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
  162. Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
  163. }
  164. [Test]
  165. public void SettingLineTypeDoesNotChangesContentTypeToCustom([Values(InputField.ContentType.Standard, InputField.ContentType.Autocorrected)] InputField.ContentType type)
  166. {
  167. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  168. inputField.contentType = type;
  169. inputField.lineType = InputField.LineType.MultiLineNewline;
  170. Assert.AreEqual(type, inputField.contentType);
  171. }
  172. [Test]
  173. public void SettingLineTypeChangesContentTypeToCustom()
  174. {
  175. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  176. inputField.contentType = InputField.ContentType.Name;
  177. inputField.lineType = InputField.LineType.MultiLineNewline;
  178. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  179. }
  180. [Test]
  181. public void SettingInputChangesContentTypeToCustom()
  182. {
  183. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  184. inputField.contentType = InputField.ContentType.Name;
  185. inputField.inputType = InputField.InputType.Password;
  186. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  187. }
  188. [Test]
  189. public void SettingCharacterValidationChangesContentTypeToCustom()
  190. {
  191. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  192. inputField.contentType = InputField.ContentType.Name;
  193. inputField.characterValidation = InputField.CharacterValidation.None;
  194. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  195. }
  196. [Test]
  197. public void SettingKeyboardTypeChangesContentTypeToCustom()
  198. {
  199. InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
  200. inputField.contentType = InputField.ContentType.Name;
  201. inputField.keyboardType = TouchScreenKeyboardType.ASCIICapable;
  202. Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
  203. }
  204. [UnityTest]
  205. public IEnumerator CaretRectSameSizeAsTextRect()
  206. {
  207. InputField inputfield = m_PrefabRoot.GetComponentInChildren<InputField>();
  208. HorizontalLayoutGroup lg = inputfield.gameObject.AddComponent<HorizontalLayoutGroup>();
  209. lg.childControlWidth = true;
  210. lg.childControlHeight = false;
  211. lg.childForceExpandWidth = true;
  212. lg.childForceExpandHeight = true;
  213. ContentSizeFitter csf = inputfield.gameObject.AddComponent<ContentSizeFitter>();
  214. csf.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
  215. csf.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
  216. inputfield.text = "Hello World!";
  217. yield return new WaitForSeconds(1.0f);
  218. Rect prevTextRect = inputfield.textComponent.rectTransform.rect;
  219. Rect prevCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
  220. inputfield.text = "Hello World!Hello World!Hello World!";
  221. LayoutRebuilder.MarkLayoutForRebuild(inputfield.transform as RectTransform);
  222. yield return new WaitForSeconds(1.0f);
  223. Rect newTextRect = inputfield.textComponent.rectTransform.rect;
  224. Rect newCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
  225. Assert.IsFalse(prevTextRect == newTextRect);
  226. Assert.IsTrue(prevTextRect == prevCaretRect);
  227. Assert.IsFalse(prevCaretRect == newCaretRect);
  228. Assert.IsTrue(newTextRect == newCaretRect);
  229. }
  230. }
  231. }