A game about forced loneliness, made by TACStudios
1using System;
2using System.Linq;
3using NUnit.Framework;
4using UnityEngine;
5using UnityEngine.EventSystems;
6using UnityEngine.TestTools;
7using System.Collections;
8using System.IO;
9using UnityEditor;
10using UnityEngine.UI;
11using System.Reflection;
12
13namespace InputfieldTests
14{
15 public class GenericInputFieldTests : BaseInputFieldTests, IPrebuildSetup
16 {
17 protected const string kPrefabPath = "Assets/Resources/GenericInputFieldPrefab.prefab";
18
19 public void Setup()
20 {
21#if UNITY_EDITOR
22 CreateInputFieldAsset(kPrefabPath);
23#endif
24 }
25
26 [SetUp]
27 public virtual void TestSetup()
28 {
29 m_PrefabRoot = UnityEngine.Object.Instantiate(Resources.Load("GenericInputFieldPrefab")) as GameObject;
30 }
31
32 [TearDown]
33 public virtual void TearDown()
34 {
35 FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
36 GameObject.DestroyImmediate(m_PrefabRoot);
37 }
38
39 [OneTimeTearDown]
40 public void OnetimeTearDown()
41 {
42#if UNITY_EDITOR
43 AssetDatabase.DeleteAsset(kPrefabPath);
44#endif
45 }
46
47 [UnityTest]
48 public IEnumerator CannotFocusIfNotTextComponent()
49 {
50 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
51 BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
52 inputField.textComponent = null;
53
54 inputField.OnSelect(eventData);
55 yield return null;
56
57 Assert.False(inputField.isFocused);
58 }
59
60 [UnityTest]
61 public IEnumerator CannotFocusIfNullFont()
62 {
63 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
64 BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
65 inputField.textComponent.font = null;
66
67 inputField.OnSelect(eventData);
68 yield return null;
69
70 Assert.False(inputField.isFocused);
71 }
72
73 [UnityTest]
74 public IEnumerator CannotFocusIfNotActive()
75 {
76 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
77 BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
78 inputField.enabled = false;
79
80 inputField.OnSelect(eventData);
81 yield return null;
82
83 Assert.False(inputField.isFocused);
84 }
85
86 [UnityTest]
87 public IEnumerator CannotFocusWithoutEventSystem()
88 {
89 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
90 UnityEngine.Object.DestroyImmediate(m_PrefabRoot.GetComponentInChildren<FakeInputModule>());
91
92 yield return null;
93
94 UnityEngine.Object.DestroyImmediate(m_PrefabRoot.GetComponentInChildren<EventSystem>());
95 BaseEventData eventData = new BaseEventData(null);
96
97 yield return null;
98
99 inputField.OnSelect(eventData);
100 yield return null;
101
102 Assert.False(inputField.isFocused);
103 }
104
105 [UnityTest]
106 [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
107 public IEnumerator FocusesOnSelect()
108 {
109 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
110 BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
111 inputField.OnSelect(eventData);
112
113 MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
114 lateUpdate.Invoke(inputField, null);
115
116#if UNITY_GAMECORE && !UNITY_EDITOR
117 if (TouchScreenKeyboard.isSupported)
118 {
119 // On Xbox, the onScreenKeyboard is going to constrain the application and make it go out of focus.
120 // We need to wait for the application to go out of focus before we can close the onScreenKeyboard.
121 while (Application.isFocused)
122 {
123 yield return null;
124 }
125 }
126#endif
127
128 Assert.True(inputField.isFocused);
129
130#if UNITY_GAMECORE && !UNITY_EDITOR
131 // On Xbox, we then need to close onScreenKeyboard and wait for the application to be focused again.
132 // 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.
133 if (!TouchScreenKeyboard.isSupported || !TouchScreenKeyboard.visible)
134 {
135 yield break;
136 }
137
138 while (!Application.isFocused)
139 {
140 if (inputField.touchScreenKeyboard != null)
141 {
142 inputField.touchScreenKeyboard.active = false;
143 }
144 yield return null;
145 }
146#else
147 yield break;
148#endif
149 }
150
151 [Test]
152 public void DoesNotFocusesOnSelectWhenShouldActivateOnSelect_IsFalse()
153 {
154 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
155 inputField.shouldActivateOnSelect = false;
156 BaseEventData eventData = new BaseEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
157 inputField.OnSelect(eventData);
158
159 MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
160 lateUpdate.Invoke(inputField, null);
161
162 Assert.False(inputField.isFocused);
163 }
164
165 [Test]
166 public void InputFieldSetTextWithoutNotifyWillNotNotify()
167 {
168 InputField i = m_PrefabRoot.GetComponentInChildren<InputField>();
169 i.text = "Hello";
170
171 bool calledOnValueChanged = false;
172
173 i.onValueChanged.AddListener(s => { calledOnValueChanged = true; });
174
175 i.SetTextWithoutNotify("Goodbye");
176
177 Assert.IsTrue(i.text == "Goodbye");
178 Assert.IsFalse(calledOnValueChanged);
179 }
180
181 [Test]
182 public void ContentTypeSetsValues()
183 {
184 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
185 inputField.contentType = InputField.ContentType.Standard;
186 Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
187 Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
188 Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
189
190 inputField.contentType = InputField.ContentType.Autocorrected;
191 Assert.AreEqual(InputField.InputType.AutoCorrect, inputField.inputType);
192 Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
193 Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
194
195 inputField.contentType = InputField.ContentType.IntegerNumber;
196 Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
197 Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
198 Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
199 Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
200
201 inputField.contentType = InputField.ContentType.DecimalNumber;
202 Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
203 Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
204 Assert.AreEqual(TouchScreenKeyboardType.NumbersAndPunctuation, inputField.keyboardType);
205 Assert.AreEqual(InputField.CharacterValidation.Decimal, inputField.characterValidation);
206
207 inputField.contentType = InputField.ContentType.Alphanumeric;
208 Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
209 Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
210 Assert.AreEqual(TouchScreenKeyboardType.ASCIICapable, inputField.keyboardType);
211 Assert.AreEqual(InputField.CharacterValidation.Alphanumeric, inputField.characterValidation);
212
213 inputField.contentType = InputField.ContentType.Name;
214 Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
215 Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
216 Assert.AreEqual(TouchScreenKeyboardType.NamePhonePad, inputField.keyboardType);
217 Assert.AreEqual(InputField.CharacterValidation.Name, inputField.characterValidation);
218
219 inputField.contentType = InputField.ContentType.EmailAddress;
220 Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
221 Assert.AreEqual(InputField.InputType.Standard, inputField.inputType);
222 Assert.AreEqual(TouchScreenKeyboardType.EmailAddress, inputField.keyboardType);
223 Assert.AreEqual(InputField.CharacterValidation.EmailAddress, inputField.characterValidation);
224
225 inputField.contentType = InputField.ContentType.Password;
226 Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
227 Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
228 Assert.AreEqual(TouchScreenKeyboardType.Default, inputField.keyboardType);
229 Assert.AreEqual(InputField.CharacterValidation.None, inputField.characterValidation);
230
231 inputField.contentType = InputField.ContentType.Pin;
232 Assert.AreEqual(InputField.LineType.SingleLine, inputField.lineType);
233 Assert.AreEqual(InputField.InputType.Password, inputField.inputType);
234 Assert.AreEqual(TouchScreenKeyboardType.NumberPad, inputField.keyboardType);
235 Assert.AreEqual(InputField.CharacterValidation.Integer, inputField.characterValidation);
236 }
237
238 [Test]
239 public void SettingLineTypeDoesNotChangesContentTypeToCustom([Values(InputField.ContentType.Standard, InputField.ContentType.Autocorrected)] InputField.ContentType type)
240 {
241 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
242 inputField.contentType = type;
243
244 inputField.lineType = InputField.LineType.MultiLineNewline;
245
246 Assert.AreEqual(type, inputField.contentType);
247 }
248
249 [Test]
250 public void SettingLineTypeChangesContentTypeToCustom()
251 {
252 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
253 inputField.contentType = InputField.ContentType.Name;
254
255 inputField.lineType = InputField.LineType.MultiLineNewline;
256
257 Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
258 }
259
260 [Test]
261 public void SettingInputChangesContentTypeToCustom()
262 {
263 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
264 inputField.contentType = InputField.ContentType.Name;
265
266 inputField.inputType = InputField.InputType.Password;
267
268 Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
269 }
270
271 [Test]
272 public void SettingCharacterValidationChangesContentTypeToCustom()
273 {
274 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
275 inputField.contentType = InputField.ContentType.Name;
276
277 inputField.characterValidation = InputField.CharacterValidation.None;
278
279 Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
280 }
281
282 [Test]
283 public void SettingKeyboardTypeChangesContentTypeToCustom()
284 {
285 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
286 inputField.contentType = InputField.ContentType.Name;
287
288 inputField.keyboardType = TouchScreenKeyboardType.ASCIICapable;
289
290 Assert.AreEqual(InputField.ContentType.Custom, inputField.contentType);
291 }
292
293 [UnityTest]
294 public IEnumerator CaretRectSameSizeAsTextRect()
295 {
296 InputField inputfield = m_PrefabRoot.GetComponentInChildren<InputField>();
297 HorizontalLayoutGroup lg = inputfield.gameObject.AddComponent<HorizontalLayoutGroup>();
298 lg.childControlWidth = true;
299 lg.childControlHeight = false;
300 lg.childForceExpandWidth = true;
301 lg.childForceExpandHeight = true;
302 ContentSizeFitter csf = inputfield.gameObject.AddComponent<ContentSizeFitter>();
303 csf.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
304 csf.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
305 inputfield.text = "Hello World!";
306
307 yield return new WaitForSeconds(1.0f);
308
309 Rect prevTextRect = inputfield.textComponent.rectTransform.rect;
310 Rect prevCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
311 inputfield.text = "Hello World!Hello World!Hello World!";
312
313 LayoutRebuilder.MarkLayoutForRebuild(inputfield.transform as RectTransform);
314
315 yield return new WaitForSeconds(1.0f);
316
317 Rect newTextRect = inputfield.textComponent.rectTransform.rect;
318 Rect newCaretRect = (inputfield.textComponent.transform.parent.GetChild(0) as RectTransform).rect;
319
320 Assert.IsFalse(prevTextRect == newTextRect);
321 Assert.IsTrue(prevTextRect == prevCaretRect);
322 Assert.IsFalse(prevCaretRect == newCaretRect);
323 Assert.IsTrue(newTextRect == newCaretRect);
324 }
325 }
326}