public virtual void Setup()
        {
            try
            {
                // Apparently, NUnit is reusing instances :(
                m_KeyInfos = default;

                // Disable input debugger so we don't waste time responding to all the
                // input system activity from the tests.
                #if UNITY_EDITOR
                InputDebuggerWindow.Disable();
                #endif

                runtime = new InputTestRuntime();

                // Push current input system state on stack.
                InputSystem.SaveAndReset(enableRemoting: false, runtime: runtime);

                #if UNITY_EDITOR
                // Make sure we're not affected by the user giving focus away from the
                // game view.
                InputEditorUserSettings.lockInputToGameView = true;
                #endif

                // We use native collections in a couple places. We when leak them, we want to know where exactly
                // the allocation came from so enable full leak detection in tests.
                NativeLeakDetection.Mode = NativeLeakDetectionMode.EnabledWithStackTrace;

                // For [UnityTest]s, we need to process input in sync with the player loop. However, InputTestRuntime
                // is divorced from the player loop by virtue of not being tied into NativeInputSystem. Listen
                // for NativeInputSystem.Update here and trigger input processing in our isolated InputSystem.
                // This is irrelevant for normal [Test]s but for [UnityTest]s that run over several frames, it's crucial.
                // NOTE: We're severing the tie the previous InputManager had to NativeInputRuntime here. This means that
                //       device removal events that happen to occur while tests are running will get lost.
                NativeInputRuntime.instance.onUpdate =
                    (InputUpdateType updateType, ref InputEventBuffer buffer) =>
                {
                    if (InputSystem.s_Manager.ShouldRunUpdate(updateType))
                    {
                        InputSystem.Update(updateType);
                    }
                    // We ignore any input coming from native.
                    buffer.Reset();
                };
                NativeInputRuntime.instance.onShouldRunUpdate =
                    updateType => true;
            }
            catch (Exception exception)
            {
                Debug.LogError("Failed to set up input system for test " + TestContext.CurrentContext.Test.Name);
                Debug.LogException(exception);
                throw;
            }

            if (InputSystem.devices.Count > 0)
            {
                Assert.Fail("Input system should not have devices after reset");
            }
        }
示例#2
0
        public virtual void Setup()
        {
            try
            {
                // Disable input debugger so we don't waste time responding to all the
                // input system activity from the tests.
                #if UNITY_EDITOR
                InputDebuggerWindow.Disable();
                #endif

                runtime = new InputTestRuntime();

                // Push current input system state on stack.
                InputSystem.SaveAndReset(enableRemoting: false, runtime: runtime);

                #if UNITY_EDITOR
                // Make sure we're not affected by the user giving focus away from the
                // game view.
                InputEditorUserSettings.lockInputToGameView = true;
                #endif

                var testProperties = TestContext.CurrentContext.Test.Properties;
                if (testProperties.ContainsKey("TimesliceEvents") && testProperties["TimesliceEvents"][0].Equals("Off"))
                {
                    InputSystem.settings.timesliceEvents = false;
                }

                // We use native collections in a couple places. We when leak them, we want to know where exactly
                // the allocation came from so enable full leak detection in tests.
                NativeLeakDetection.Mode = NativeLeakDetectionMode.EnabledWithStackTrace;
            }
            catch (Exception exception)
            {
                Debug.LogError("Failed to set up input system for test " + TestContext.CurrentContext.Test.Name);
                Debug.LogException(exception);
                throw exception;
            }

            if (InputSystem.devices.Count > 0)
            {
                Assert.Fail("Input system should not have devices after reset");
            }
        }
示例#3
0
        public virtual void Setup()
        {
            try
            {
                // Apparently, NUnit is reusing instances :(
                m_KeyInfos = default;

                // Disable input debugger so we don't waste time responding to all the
                // input system activity from the tests.
                #if UNITY_EDITOR
                InputDebuggerWindow.Disable();
                #endif

                runtime = new InputTestRuntime();

                // Push current input system state on stack.
                InputSystem.SaveAndReset(enableRemoting: false, runtime: runtime);

                // Override the editor messing with logic like canRunInBackground and focus and
                // make it behave like in the player.
                #if UNITY_EDITOR
                InputSystem.settings.editorInputBehaviorInPlayMode = InputSettings.EditorInputBehaviorInPlayMode.AllDeviceInputAlwaysGoesToGameView;
                #endif

                // For a [UnityTest] play mode test, we don't want editor updates interfering with the test,
                // so turn them off.
                #if UNITY_EDITOR
                if (Application.isPlaying && IsUnityTest())
                {
                    InputSystem.s_Manager.m_UpdateMask &= ~InputUpdateType.Editor;
                }
                #endif

                // We use native collections in a couple places. We when leak them, we want to know where exactly
                // the allocation came from so enable full leak detection in tests.
                NativeLeakDetection.Mode = NativeLeakDetectionMode.EnabledWithStackTrace;

                // For [UnityTest]s, we need to process input in sync with the player loop. However, InputTestRuntime
                // is divorced from the player loop by virtue of not being tied into NativeInputSystem. Listen
                // for NativeInputSystem.Update here and trigger input processing in our isolated InputSystem.
                // This is irrelevant for normal [Test]s but for [UnityTest]s that run over several frames, it's crucial.
                // NOTE: We're severing the tie the previous InputManager had to NativeInputRuntime here. This means that
                //       device removal events that happen to occur while tests are running will get lost.
                NativeInputRuntime.instance.onUpdate =
                    (InputUpdateType updateType, ref InputEventBuffer buffer) =>
                {
                    if (InputSystem.s_Manager.ShouldRunUpdate(updateType))
                    {
                        InputSystem.Update(updateType);
                    }
                    // We ignore any input coming from native.
                    buffer.Reset();
                };
                NativeInputRuntime.instance.onShouldRunUpdate =
                    updateType => true;

                #if UNITY_EDITOR
                m_OnPlayModeStateChange = OnPlayModeStateChange;
                EditorApplication.playModeStateChanged += m_OnPlayModeStateChange;
                #endif

                // Always want to merge by default
                InputSystem.settings.disableRedundantEventsMerging = false;
            }
            catch (Exception exception)
            {
                Debug.LogError("Failed to set up input system for test " + TestContext.CurrentContext.Test.Name);
                Debug.LogException(exception);
                throw;
            }

            m_Initialized = true;

            if (InputSystem.devices.Count > 0)
            {
                Assert.Fail("Input system should not have devices after reset");
            }
        }