|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using HoloToolkit.Unity.InputModule; |
| 6 | +using NUnit.Framework; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.EventSystems; |
| 9 | + |
| 10 | +namespace HoloToolkit.Unity.Tests |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class InputManagerTests |
| 14 | + { |
| 15 | + private List<GameObject> receivedEventSources; |
| 16 | + private BaseEventData eventData; |
| 17 | + |
| 18 | + [SetUp] |
| 19 | + public void SetUpTests() |
| 20 | + { |
| 21 | + TestUtils.ClearScene(); |
| 22 | + receivedEventSources = new List<GameObject>(); |
| 23 | + //Create a main camera and add input manager, event system and gaze manager to it |
| 24 | + var inputManagerContainer = TestUtils.CreateMainCamera().gameObject; |
| 25 | + inputManagerContainer.AddComponent<InputManager>(); |
| 26 | + inputManagerContainer.AddComponent<GazeManager>(); |
| 27 | + var eventSystem = inputManagerContainer.AddComponent<EventSystem>(); |
| 28 | + eventData = new BaseEventData(eventSystem); |
| 29 | + |
| 30 | + inputManagerContainer.transform.position = inputManagerContainer.transform.forward * -5; |
| 31 | + //call awake and start |
| 32 | + inputManagerContainer.CallAwake().CallStart(); |
| 33 | + } |
| 34 | + |
| 35 | + [Test] |
| 36 | + public void DisableInputManagerRefCountNoEventCaught() |
| 37 | + { |
| 38 | + CreateGlobalTestHandler().CallAwake().CallStart(); |
| 39 | + |
| 40 | + InputManager.Instance.PushInputDisable(); |
| 41 | + FireTestEvent(); |
| 42 | + InputManager.Instance.PopInputDisable(); |
| 43 | + |
| 44 | + Assert.That(receivedEventSources, Is.Empty); |
| 45 | + } |
| 46 | + |
| 47 | + [Test] |
| 48 | + public void DisableInputManagerScriptNoEventCaught() |
| 49 | + { |
| 50 | + CreateGlobalTestHandler().CallAwake().CallStart(); |
| 51 | + |
| 52 | + InputManager.Instance.enabled = false; |
| 53 | + FireTestEvent(); |
| 54 | + InputManager.Instance.enabled = true; |
| 55 | + |
| 56 | + Assert.That(receivedEventSources, Is.Empty); |
| 57 | + } |
| 58 | + |
| 59 | + [Test] |
| 60 | + public void CatchSingleGlobalEvent() |
| 61 | + { |
| 62 | + CreateGlobalTestHandler().CallAwake().CallStart(); |
| 63 | + |
| 64 | + FireTestEvent(); |
| 65 | + |
| 66 | + Assert.That(receivedEventSources, Is.Not.Empty); |
| 67 | + } |
| 68 | + |
| 69 | + [Test] |
| 70 | + public void CatchSingleGlobalEventCheckSource() |
| 71 | + { |
| 72 | + var globalHandler = CreateGlobalTestHandler().CallAwake().CallStart(); |
| 73 | + |
| 74 | + FireTestEvent(); |
| 75 | + |
| 76 | + Assert.That(receivedEventSources[0], Is.EqualTo(globalHandler)); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public void CatchDoubleGlobalEvent() |
| 81 | + { |
| 82 | + CreateGlobalTestHandler().CallAwake().CallStart(); |
| 83 | + CreateGlobalTestHandler().CallAwake().CallStart(); |
| 84 | + |
| 85 | + FireTestEvent(); |
| 86 | + |
| 87 | + Assert.That(receivedEventSources.Count, Is.EqualTo(2)); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void CatchDoubleGlobalEventCheckSource() |
| 92 | + { |
| 93 | + var globalHandler1 = CreateGlobalTestHandler().CallAwake().CallStart(); |
| 94 | + var globalHandler2 = CreateGlobalTestHandler().CallAwake().CallStart(); |
| 95 | + |
| 96 | + FireTestEvent(); |
| 97 | + |
| 98 | + Assert.That(receivedEventSources.Contains(globalHandler1), Is.True); |
| 99 | + Assert.That(receivedEventSources.Contains(globalHandler2), Is.True); |
| 100 | + } |
| 101 | + |
| 102 | + [Test] |
| 103 | + public void CatchFocusedEvent() |
| 104 | + { |
| 105 | + var focusedHandler = CreateTestHandler().CallAwake().CallStart(); |
| 106 | + |
| 107 | + InputManager.Instance.OverrideFocusedObject = focusedHandler; |
| 108 | + FireTestEvent(); |
| 109 | + |
| 110 | + Assert.That(receivedEventSources, Is.Not.Empty); |
| 111 | + } |
| 112 | + |
| 113 | + [Test] |
| 114 | + public void CatchModalEvent() |
| 115 | + { |
| 116 | + var modalHandler = CreateTestHandler().CallAwake().CallStart(); |
| 117 | + |
| 118 | + InputManager.Instance.PushModalInputHandler(modalHandler); |
| 119 | + FireTestEvent(); |
| 120 | + InputManager.Instance.PopModalInputHandler(); |
| 121 | + |
| 122 | + Assert.That(receivedEventSources, Is.Not.Empty); |
| 123 | + } |
| 124 | + |
| 125 | + [Test] |
| 126 | + public void CatchOneModalEventAndCheckSource() |
| 127 | + { |
| 128 | + var modalHandler1 = CreateTestHandler().CallAwake().CallStart(); |
| 129 | + var modalHandler2 = CreateTestHandler().CallAwake().CallStart(); |
| 130 | + |
| 131 | + InputManager.Instance.PushModalInputHandler(modalHandler1); |
| 132 | + InputManager.Instance.PushModalInputHandler(modalHandler2); |
| 133 | + FireTestEvent(); |
| 134 | + InputManager.Instance.PopModalInputHandler(); |
| 135 | + InputManager.Instance.PopModalInputHandler(); |
| 136 | + |
| 137 | + Assert.That(receivedEventSources.Count, Is.EqualTo(1)); |
| 138 | + Assert.That(receivedEventSources[0], Is.EqualTo(modalHandler2)); |
| 139 | + } |
| 140 | + |
| 141 | + [Test] |
| 142 | + public void CatchFallbackEvent() |
| 143 | + { |
| 144 | + var fallbackHandler = CreateTestHandler().CallAwake().CallStart(); |
| 145 | + |
| 146 | + InputManager.Instance.PushFallbackInputHandler(fallbackHandler); |
| 147 | + FireTestEvent(); |
| 148 | + InputManager.Instance.PopFallbackInputHandler(); |
| 149 | + |
| 150 | + Assert.That(receivedEventSources, Is.Not.Empty); |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void CatchModalEventOverFocusedEvent() |
| 155 | + { |
| 156 | + var focusedHandler = CreateTestHandler().CallAwake().CallStart(); |
| 157 | + var modalHandler = CreateTestHandler().CallAwake().CallStart(); |
| 158 | + |
| 159 | + InputManager.Instance.OverrideFocusedObject = focusedHandler; |
| 160 | + InputManager.Instance.PushModalInputHandler(modalHandler); |
| 161 | + FireTestEvent(); |
| 162 | + InputManager.Instance.PopModalInputHandler(); |
| 163 | + |
| 164 | + Assert.That(receivedEventSources.Count, Is.EqualTo(1)); |
| 165 | + Assert.That(receivedEventSources[0], Is.EqualTo(modalHandler)); |
| 166 | + } |
| 167 | + |
| 168 | + [Test] |
| 169 | + public void CatchModalFocusedChildEvent() |
| 170 | + { |
| 171 | + var focusedHandler = CreateTestHandler().CallAwake().CallStart(); |
| 172 | + var modalHandler = CreateTestHandler().CallAwake().CallStart(); |
| 173 | + focusedHandler.transform.SetParent(modalHandler.transform); |
| 174 | + |
| 175 | + InputManager.Instance.OverrideFocusedObject = focusedHandler; |
| 176 | + InputManager.Instance.PushModalInputHandler(modalHandler); |
| 177 | + FireTestEvent(); |
| 178 | + InputManager.Instance.PopModalInputHandler(); |
| 179 | + |
| 180 | + Assert.That(receivedEventSources.Count, Is.EqualTo(1)); |
| 181 | + Assert.That(receivedEventSources[0], Is.EqualTo(focusedHandler)); |
| 182 | + } |
| 183 | + |
| 184 | + [Test] |
| 185 | + public void CatchMultipleEventSources() |
| 186 | + { |
| 187 | + var globalHandler1 = CreateGlobalTestHandler().CallAwake().CallStart(); |
| 188 | + var globalHandler2 = CreateGlobalTestHandler().CallAwake().CallStart(); |
| 189 | + var focusedHandler = CreateTestHandler().CallAwake().CallStart(); |
| 190 | + var modalHandler = CreateTestHandler().CallAwake().CallStart(); |
| 191 | + var fallbackHandler = CreateTestHandler().CallAwake().CallStart(); |
| 192 | + |
| 193 | + InputManager.Instance.OverrideFocusedObject = focusedHandler; |
| 194 | + InputManager.Instance.PushFallbackInputHandler(fallbackHandler); |
| 195 | + |
| 196 | + FireTestEvent(); |
| 197 | + Assert.That(receivedEventSources, Is.EquivalentTo(new List<GameObject> { globalHandler1, globalHandler2, focusedHandler })); |
| 198 | + |
| 199 | + InputManager.Instance.PushModalInputHandler(modalHandler); |
| 200 | + FireTestEvent(); |
| 201 | + Assert.That(receivedEventSources, Is.EquivalentTo(new List<GameObject> { globalHandler1, globalHandler2, modalHandler })); |
| 202 | + |
| 203 | + modalHandler.SetActive(false); |
| 204 | + FireTestEvent(); |
| 205 | + Assert.That(receivedEventSources, Is.EquivalentTo(new List<GameObject> { globalHandler1, globalHandler2, focusedHandler })); |
| 206 | + |
| 207 | + focusedHandler.SetActive(false); |
| 208 | + FireTestEvent(); |
| 209 | + Assert.That(receivedEventSources, Is.EquivalentTo(new List<GameObject> { globalHandler1, globalHandler2, fallbackHandler })); |
| 210 | + |
| 211 | + InputManager.Instance.PopModalInputHandler(); |
| 212 | + InputManager.Instance.PopFallbackInputHandler(); |
| 213 | + } |
| 214 | + |
| 215 | + [Test] |
| 216 | + public void FocusChangeFullIntegration() |
| 217 | + { |
| 218 | + var handler = CreateCubeTestHandler().CallAwake().CallStart(); |
| 219 | + |
| 220 | + GazeManager.Instance.gameObject.CallUpdate(); |
| 221 | + |
| 222 | + Assert.That(receivedEventSources.Count, Is.EqualTo(1)); |
| 223 | + Assert.That(receivedEventSources[0], Is.EqualTo(handler)); |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + private GameObject CreateTestHandler() |
| 228 | + { |
| 229 | + return SetTestHandler(new GameObject()); |
| 230 | + } |
| 231 | + |
| 232 | + private GameObject CreateCubeTestHandler() |
| 233 | + { |
| 234 | + return SetTestHandler(GameObject.CreatePrimitive(PrimitiveType.Cube)); |
| 235 | + } |
| 236 | + |
| 237 | + private GameObject SetTestHandler(GameObject gameObject) |
| 238 | + { |
| 239 | + gameObject.AddComponent<TestEventHandler>().EventFiredCallback = OnEventFired; |
| 240 | + return gameObject; |
| 241 | + } |
| 242 | + |
| 243 | + |
| 244 | + private GameObject CreateGlobalTestHandler() |
| 245 | + { |
| 246 | + return CreateTestHandler().AddComponent<SetGlobalListener>().gameObject; |
| 247 | + } |
| 248 | + |
| 249 | + private void FireTestEvent() |
| 250 | + { |
| 251 | + receivedEventSources = new List<GameObject>(); |
| 252 | + InputManager.Instance.HandleEvent(eventData, TestEventHandler.OnTestHandler); |
| 253 | + } |
| 254 | + |
| 255 | + private void OnEventFired(GameObject source) |
| 256 | + { |
| 257 | + receivedEventSources.Add(source); |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments