|
| 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 | +#if !WINDOWS_UWP |
| 5 | +// When the .NET scripting backend is enabled and C# projects are built |
| 6 | +// The assembly that this file is part of is still built for the player, |
| 7 | +// even though the assembly itself is marked as a test assembly (this is not |
| 8 | +// expected because test assemblies should not be included in player builds). |
| 9 | +// Because the .NET backend is deprecated in 2018 and removed in 2019 and this |
| 10 | +// issue will likely persist for 2018, this issue is worked around by wrapping all |
| 11 | +// play mode tests in this check. |
| 12 | + |
| 13 | +using Microsoft.MixedReality.Toolkit.SpatialAwareness; |
| 14 | +using Microsoft.MixedReality.Toolkit.Utilities; |
| 15 | +using NUnit.Framework; |
| 16 | +using System.Collections; |
| 17 | +using UnityEditor; |
| 18 | +using UnityEngine; |
| 19 | +using UnityEngine.TestTools; |
| 20 | + |
| 21 | +namespace Microsoft.MixedReality.Toolkit.Tests |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Test class that validates observers start and stop based on right configuration in editor. |
| 25 | + /// </summary> |
| 26 | + public class SpatialObserverTests |
| 27 | + { |
| 28 | + private const string TestSpatialAwarenessSysteProfilePath = "Assets/MixedRealityToolkit.Tests/PlayModeTests/TestProfiles/TestMixedRealitySpatialAwarenessSystemProfile.asset"; |
| 29 | + private const string TestSpatialAwarenessSysteProfilePath_ManualStart = "Assets/MixedRealityToolkit.Tests/PlayModeTests/TestProfiles/TestMixedRealitySpatialAwarenessSystemProfile_ManualStart.asset"; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Test default case of Auto-Start SpatialObjectMeshObserver observer type and SpatialAwarenessSystem in editor |
| 33 | + /// Validates that system resumes (i.e auto-starts again) when disabled/enabled |
| 34 | + /// </summary> |
| 35 | + [UnityTest] |
| 36 | + public IEnumerator TestAutoStartObserver() |
| 37 | + { |
| 38 | + var mrtkProfile = CreateMRTKTestProfile(TestSpatialAwarenessSysteProfilePath); |
| 39 | + TestUtilities.InitializeMixedRealityToolkit(mrtkProfile); |
| 40 | + yield return PlayModeTestUtilities.WaitForInputSystemUpdate(); |
| 41 | + |
| 42 | + var spatialAwarenesssDataProvider = CoreServices.SpatialAwarenessSystem as IMixedRealityDataProviderAccess; |
| 43 | + var spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 44 | + Assert.IsNotNull(spatialObserver, "No SpatialObjectMeshObserver data provider created or found"); |
| 45 | + Assert.IsTrue(spatialObserver.IsRunning); |
| 46 | + Assert.IsNotEmpty(spatialObserver.Meshes); |
| 47 | + |
| 48 | + CoreServices.SpatialAwarenessSystem.Disable(); |
| 49 | + spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 50 | + Assert.IsNull(spatialObserver); |
| 51 | + |
| 52 | + CoreServices.SpatialAwarenessSystem.Enable(); |
| 53 | + spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 54 | + Assert.IsNotNull(spatialObserver, "No SpatialObjectMeshObserver data provider created or found"); |
| 55 | + Assert.IsTrue(spatialObserver.IsRunning); |
| 56 | + yield return PlayModeTestUtilities.WaitForInputSystemUpdate(); |
| 57 | + Assert.IsNotEmpty(spatialObserver.Meshes); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Test case of Manual-Start SpatialObjectMeshObserver observer type and SpatialAwarenessSystem in editor |
| 62 | + /// Validates that system should not resume observers unless directly told by test |
| 63 | + /// </summary> |
| 64 | + [UnityTest] |
| 65 | + public IEnumerator TestManualStartObserver() |
| 66 | + { |
| 67 | + var mrtkProfile = CreateMRTKTestProfile(TestSpatialAwarenessSysteProfilePath_ManualStart); |
| 68 | + TestUtilities.InitializeMixedRealityToolkit(mrtkProfile); |
| 69 | + yield return PlayModeTestUtilities.WaitForInputSystemUpdate(); |
| 70 | + |
| 71 | + var spatialAwarenesssDataProvider = CoreServices.SpatialAwarenessSystem as IMixedRealityDataProviderAccess; |
| 72 | + var spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 73 | + Assert.IsNotNull(spatialObserver, "No SpatialObjectMeshObserver data provider created or found"); |
| 74 | + Assert.IsFalse(spatialObserver.IsRunning); |
| 75 | + Assert.IsEmpty(spatialObserver.Meshes); |
| 76 | + |
| 77 | + CoreServices.SpatialAwarenessSystem.Disable(); |
| 78 | + spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 79 | + Assert.IsNull(spatialObserver); |
| 80 | + |
| 81 | + CoreServices.SpatialAwarenessSystem.Enable(); |
| 82 | + spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 83 | + Assert.IsNotNull(spatialObserver, "No SpatialObjectMeshObserver data provider created or found"); |
| 84 | + Assert.IsFalse(spatialObserver.IsRunning); |
| 85 | + Assert.IsEmpty(spatialObserver.Meshes); |
| 86 | + |
| 87 | + CoreServices.SpatialAwarenessSystem.ResumeObservers(); |
| 88 | + Assert.IsTrue(spatialObserver.IsRunning); |
| 89 | + yield return PlayModeTestUtilities.WaitForInputSystemUpdate(); |
| 90 | + Assert.IsNotEmpty(spatialObserver.Meshes); |
| 91 | + |
| 92 | + CoreServices.SpatialAwarenessSystem.Disable(); |
| 93 | + spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 94 | + Assert.IsNull(spatialObserver); |
| 95 | + |
| 96 | + CoreServices.SpatialAwarenessSystem.Enable(); |
| 97 | + spatialObserver = spatialAwarenesssDataProvider.GetDataProvider<SpatialObjectMeshObserver.SpatialObjectMeshObserver>(); |
| 98 | + Assert.IsNotNull(spatialObserver, "No SpatialObjectMeshObserver data provider created or found"); |
| 99 | + Assert.IsFalse(spatialObserver.IsRunning); |
| 100 | + Assert.IsEmpty(spatialObserver.Meshes); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Test that when the SpatialAwarenessSystem is disabled in the MRTK profile, no service is created |
| 105 | + /// </summary> |
| 106 | + [UnityTest] |
| 107 | + public IEnumerator TestDisableSpatialAwarenessSystem() |
| 108 | + { |
| 109 | + var mrtkProfile = CreateMRTKTestProfile(TestSpatialAwarenessSysteProfilePath); |
| 110 | + |
| 111 | + mrtkProfile.IsSpatialAwarenessSystemEnabled = false; |
| 112 | + |
| 113 | + TestUtilities.InitializeMixedRealityToolkit(mrtkProfile); |
| 114 | + yield return PlayModeTestUtilities.WaitForInputSystemUpdate(); |
| 115 | + |
| 116 | + Assert.IsNull(CoreServices.SpatialAwarenessSystem); |
| 117 | + } |
| 118 | + |
| 119 | + private static MixedRealityToolkitConfigurationProfile CreateMRTKTestProfile(string spatialAwarenessSystemPath) |
| 120 | + { |
| 121 | + var mrtkProfile = ScriptableObject.CreateInstance<MixedRealityToolkitConfigurationProfile>(); |
| 122 | + |
| 123 | + mrtkProfile.SpatialAwarenessSystemSystemType = new SystemType(typeof(MixedRealitySpatialAwarenessSystem)); |
| 124 | + mrtkProfile.IsSpatialAwarenessSystemEnabled = true; |
| 125 | + mrtkProfile.SpatialAwarenessSystemProfile = AssetDatabase.LoadAssetAtPath<MixedRealitySpatialAwarenessSystemProfile>(spatialAwarenessSystemPath); |
| 126 | + |
| 127 | + return mrtkProfile; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +#endif |
0 commit comments