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 DesktopInputFieldTests : BaseInputFieldTests, IPrebuildSetup
16 {
17 protected const string kPrefabPath = "Assets/Resources/DesktopInputFieldPrefab.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("DesktopInputFieldPrefab")) as GameObject;
30
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
35 [TearDown]
36 public virtual void TearDown()
37 {
38 GUIUtility.systemCopyBuffer = null;
39 FontUpdateTracker.UntrackText(m_PrefabRoot.GetComponentInChildren<Text>());
40 GameObject.DestroyImmediate(m_PrefabRoot);
41 }
42
43 [OneTimeTearDown]
44 public void OnetimeTearDown()
45 {
46#if UNITY_EDITOR
47 AssetDatabase.DeleteAsset(kPrefabPath);
48#endif
49 }
50
51 [UnityTest]
52 [UnityPlatform(exclude = new[] { RuntimePlatform.Switch })] // Currently InputField.ActivateInputFieldInternal calls Switch SoftwareKeyboard screen ; without user input or a command to close the SoftwareKeyboard this blocks the tests suite
53 public IEnumerator FocusOnPointerClickWithLeftButton()
54 {
55 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
56 PointerEventData data = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
57 data.button = PointerEventData.InputButton.Left;
58 inputField.OnPointerClick(data);
59
60 MethodInfo lateUpdate = typeof(InputField).GetMethod("LateUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
61 lateUpdate.Invoke(inputField, null);
62
63#if UNITY_GAMECORE && !UNITY_EDITOR
64 if (TouchScreenKeyboard.isSupported)
65 {
66 // On Xbox, the onScreenKeyboard is going to constrain the application and make it go out of focus.
67 // We need to wait for the application to go out of focus before we can close the onScreenKeyboard.
68 while (Application.isFocused)
69 {
70 yield return null;
71 }
72 }
73#endif
74
75 Assert.IsTrue(inputField.isFocused);
76
77#if UNITY_GAMECORE && !UNITY_EDITOR
78 // On Xbox, we then need to close onScreenKeyboard and wait for the application to be focused again.
79 // 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.
80 if (!TouchScreenKeyboard.isSupported || !TouchScreenKeyboard.visible)
81 {
82 yield break;
83 }
84
85 while (!Application.isFocused)
86 {
87 if (inputField.touchScreenKeyboard != null)
88 {
89 inputField.touchScreenKeyboard.active = false;
90 }
91 yield return null;
92 }
93#else
94 yield break;
95#endif
96 }
97
98 [UnityTest]
99 public IEnumerator DoesNotFocusOnPointerClickWithRightOrMiddleButton()
100 {
101 InputField inputField = m_PrefabRoot.GetComponentInChildren<InputField>();
102 PointerEventData data = new PointerEventData(m_PrefabRoot.GetComponentInChildren<EventSystem>());
103 data.button = PointerEventData.InputButton.Middle;
104 inputField.OnPointerClick(data);
105 yield return null;
106
107 data.button = PointerEventData.InputButton.Right;
108 inputField.OnPointerClick(data);
109 yield return null;
110
111 Assert.IsFalse(inputField.isFocused);
112 }
113 }
114}