ARSessionTestFixture.cs
5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.Management;
namespace UnityEngine.XR.ARFoundation
{
[TestFixture]
public class ARSessionTestFixture
{
enum SupportsInstall
{
No,
Yes
}
class MockProvider : XRSessionSubsystem.Provider
{
public static SessionAvailability availability;
public override Promise<SessionAvailability> GetAvailabilityAsync()
{
return Promise<SessionAvailability>.CreateResolvedPromise(availability);
}
}
class MockSessionSubsystem : XRSessionSubsystem
{
#if !UNITY_2020_2_OR_NEWER
protected override Provider CreateProvider() => new MockProvider();
#endif
}
class SubsystemThatSupportsInstall : MockSessionSubsystem { }
class SubsystemThatDoesNotSupportInstall : MockSessionSubsystem { }
static string GetSubsystemName(SupportsInstall supportsInstall) => $"SessionThatSupportsInstall{supportsInstall.ToString()}";
static XRSessionSubsystemDescriptor.Cinfo GetDescriptorCinfo(SupportsInstall supportsInstall)
{
var canBeInstalled = supportsInstall == SupportsInstall.Yes;
var type = canBeInstalled
? typeof(SubsystemThatSupportsInstall)
: typeof(SubsystemThatDoesNotSupportInstall);
return new XRSessionSubsystemDescriptor.Cinfo
{
id = GetSubsystemName(supportsInstall),
#if UNITY_2020_2_OR_NEWER
providerType = typeof(MockProvider),
subsystemTypeOverride = type,
#else
subsystemImplementationType = type,
#endif
supportsInstall = canBeInstalled,
};
}
[OneTimeSetUp]
public void RegisterTestDescriptor()
{
XRSessionSubsystemDescriptor.RegisterDescriptor(GetDescriptorCinfo(SupportsInstall.No));
XRSessionSubsystemDescriptor.RegisterDescriptor(GetDescriptorCinfo(SupportsInstall.Yes));
}
class MockLoader : XRLoaderHelper
{
public static SupportsInstall supportsInstall;
static List<XRSessionSubsystemDescriptor> s_SessionSubsystemDescriptors = new List<XRSessionSubsystemDescriptor>();
XRSessionSubsystem sessionSubsystem => GetLoadedSubsystem<XRSessionSubsystem>();
public override bool Initialize()
{
CreateSubsystem<XRSessionSubsystemDescriptor, XRSessionSubsystem>(s_SessionSubsystemDescriptors, GetSubsystemName(supportsInstall));
return sessionSubsystem != null;
}
}
static void CheckAvailabilitySync()
{
var enumerator = ARSession.CheckAvailability();
while (enumerator.MoveNext()) { }
}
static void InitMock(SupportsInstall supportsInstall, SessionAvailability availability)
{
MockLoader.supportsInstall = supportsInstall;
MockProvider.availability = availability;
var xrManager = ScriptableObject.CreateInstance<XRManagerSettings>();
xrManager.loaders.Clear();
xrManager.loaders.Add(ScriptableObject.CreateInstance<MockLoader>());
xrManager.InitializeLoaderSync();
XRGeneralSettings.Instance = ScriptableObject.CreateInstance<XRGeneralSettings>();
XRGeneralSettings.Instance.Manager = xrManager;
}
static void DeinitMock()
{
var xrManager = XRGeneralSettings.Instance.Manager;
xrManager.DeinitializeLoader();
xrManager.loaders.Clear();
ScriptableObject.Destroy(xrManager);
ScriptableObject.Destroy(XRGeneralSettings.Instance);
XRGeneralSettings.Instance = null;
}
static void RunAvailabilityCheck(SupportsInstall supportsInstall, SessionAvailability availability)
{
ARSession.s_State = ARSessionState.None;
InitMock(supportsInstall, availability);
CheckAvailabilitySync();
DeinitMock();
}
[Test]
public void DoesNotThrowWhenNoSubsystemPresent()
{
CheckAvailabilitySync();
}
[Test]
public void ReportsReady()
{
RunAvailabilityCheck(SupportsInstall.No, SessionAvailability.Supported | SessionAvailability.Installed);
Assert.AreEqual(ARSessionState.Ready, ARSession.state);
}
[Test]
public void ReportsUnsupported()
{
RunAvailabilityCheck(SupportsInstall.No, SessionAvailability.None);
Assert.AreEqual(ARSessionState.Unsupported, ARSession.state);
}
[Test]
public void ReportsUnsupportedWhenSupportedButNotInstalledAndInstallNotPossible()
{
RunAvailabilityCheck(SupportsInstall.No, SessionAvailability.Supported);
Assert.AreEqual(ARSessionState.Unsupported, ARSession.state);
}
[Test]
public void ReportsNeedsInstallWhenSupportedButNotInstalledAndInstallIsPossible()
{
RunAvailabilityCheck(SupportsInstall.Yes, SessionAvailability.Supported);
Assert.AreEqual(ARSessionState.NeedsInstall, ARSession.state);
}
}
}