-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathOnSceneEventCallbackTests.cs
More file actions
281 lines (242 loc) · 11.7 KB
/
OnSceneEventCallbackTests.cs
File metadata and controls
281 lines (242 loc) · 11.7 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Unity.Netcode;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace TestProject.RuntimeTests
{
[TestFixture(HostOrServer.Host)]
[TestFixture(HostOrServer.Server)]
public class OnSceneEventCallbackTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;
private const string k_SceneToLoad = "EmptyScene";
private const string k_PathToLoad = "Assets/Scenes/EmptyScene.unity";
public OnSceneEventCallbackTests(HostOrServer hostOrServer) : base(hostOrServer)
{
}
private struct ExpectedEvent
{
public SceneEvent SceneEvent;
public string SceneName;
public string ScenePath;
}
private readonly Queue<ExpectedEvent> m_ExpectedEventQueue = new();
private static int s_NumEventsProcessed;
private void OnSceneEvent(SceneEvent sceneEvent)
{
VerboseDebug($"OnSceneEvent! Type: {sceneEvent.SceneEventType}. for client: {sceneEvent.ClientId}");
if (m_ExpectedEventQueue.Count > 0)
{
var expectedEvent = m_ExpectedEventQueue.Dequeue();
ValidateEventsAreEqual(expectedEvent.SceneEvent, sceneEvent);
// Only LoadComplete events have an attached scene
if (sceneEvent.SceneEventType == SceneEventType.LoadComplete)
{
ValidateReceivedScene(expectedEvent, sceneEvent.Scene);
}
}
else
{
Assert.Fail($"Received unexpected event at index {s_NumEventsProcessed}: {sceneEvent.SceneEventType}");
}
s_NumEventsProcessed++;
}
public enum ClientType
{
Authority,
NonAuthority,
}
public enum Action
{
Load,
Unload,
}
[UnityTest]
public IEnumerator LoadAndUnloadCallbacks([Values] ClientType clientType, [Values] Action action)
{
yield return RunSceneEventCallbackTest(clientType, action, k_SceneToLoad);
}
[UnityTest]
public IEnumerator LoadSceneFromPath([Values] ClientType clientType)
{
yield return RunSceneEventCallbackTest(clientType, Action.Load, k_PathToLoad);
}
private IEnumerator RunSceneEventCallbackTest(ClientType clientType, Action action, string loadCall)
{
m_EnableVerboseDebug = true;
var client = m_ClientNetworkManagers[0];
var managerToTest = clientType == ClientType.Authority ? m_ServerNetworkManager : client;
var expectedCompletedClients = new List<ulong> { client.LocalClientId };
// the authority ID is not inside ClientsThatCompleted when running as a server
if (m_UseHost)
{
expectedCompletedClients.Insert(0, m_ServerNetworkManager.LocalClientId);
}
Scene loadedScene = default;
if (action == Action.Unload)
{
// Load the scene initially
m_ServerNetworkManager.SceneManager.LoadScene(k_SceneToLoad, LoadSceneMode.Additive);
yield return WaitForConditionOrTimeOut(ValidateSceneIsLoaded);
AssertOnTimeout($"[Setup] Timed out waiting for client to load the scene {k_SceneToLoad}!");
// Wait for any pending messages to be processed
yield return s_DefaultWaitForTick;
// Get a reference to the scene to test
loadedScene = SceneManager.GetSceneByName(k_SceneToLoad);
}
s_NumEventsProcessed = 0;
m_ExpectedEventQueue.Clear();
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
SceneEvent = new SceneEvent()
{
SceneEventType = action == Action.Load ? SceneEventType.Load : SceneEventType.Unload,
LoadSceneMode = LoadSceneMode.Additive,
SceneName = k_SceneToLoad,
ScenePath = k_PathToLoad,
ClientId = managerToTest.LocalClientId,
},
});
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
SceneEvent = new SceneEvent()
{
SceneEventType = action == Action.Load ? SceneEventType.LoadComplete : SceneEventType.UnloadComplete,
LoadSceneMode = LoadSceneMode.Additive,
SceneName = k_SceneToLoad,
ScenePath = k_PathToLoad,
ClientId = managerToTest.LocalClientId,
},
SceneName = action == Action.Load ? k_SceneToLoad : null,
ScenePath = action == Action.Load ? k_PathToLoad : null
});
if (clientType == ClientType.Authority)
{
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
SceneEvent = new SceneEvent()
{
SceneEventType = action == Action.Load ? SceneEventType.LoadComplete : SceneEventType.UnloadComplete,
LoadSceneMode = LoadSceneMode.Additive,
SceneName = k_SceneToLoad,
ScenePath = k_PathToLoad,
ClientId = client.LocalClientId,
}
});
}
m_ExpectedEventQueue.Enqueue(new ExpectedEvent()
{
SceneEvent = new SceneEvent()
{
SceneEventType = action == Action.Load ? SceneEventType.LoadEventCompleted : SceneEventType.UnloadEventCompleted,
LoadSceneMode = LoadSceneMode.Additive,
SceneName = k_SceneToLoad,
ScenePath = k_PathToLoad,
ClientId = m_ServerNetworkManager.LocalClientId,
ClientsThatCompleted = expectedCompletedClients,
ClientsThatTimedOut = new List<ulong>()
}
});
//////////////////////////////////////////
// Testing event notifications
managerToTest.SceneManager.OnSceneEvent += OnSceneEvent;
if (action == Action.Load)
{
Assert.That(m_ServerNetworkManager.SceneManager.LoadScene(loadCall, LoadSceneMode.Additive) == SceneEventProgressStatus.Started);
yield return WaitForConditionOrTimeOut(ValidateSceneIsLoaded);
AssertOnTimeout($"[Test] Timed out waiting for client to load the scene {k_SceneToLoad}!");
}
else
{
Assert.That(loadedScene.name, Is.EqualTo(k_SceneToLoad), "scene was not loaded!");
Assert.That(m_ServerNetworkManager.SceneManager.UnloadScene(loadedScene) == SceneEventProgressStatus.Started);
yield return WaitForConditionOrTimeOut(ValidateSceneIsUnloaded);
AssertOnTimeout($"[Test] Timed out waiting for client to unload the scene {k_SceneToLoad}!");
}
// Wait for all messages to process
yield return s_DefaultWaitForTick;
if (m_ExpectedEventQueue.Count > 0)
{
Assert.Fail($"Failed to invoke all expected OnSceneEvent callbacks. {m_ExpectedEventQueue.Count} callbacks missing. First missing event is {m_ExpectedEventQueue.Dequeue().SceneEvent.SceneEventType}");
}
managerToTest.SceneManager.OnSceneEvent -= OnSceneEvent;
}
private bool ValidateSceneIsLoaded(StringBuilder errorBuilder)
{
var loadedScene = m_ServerNetworkManager.SceneManager.ScenesLoaded.Values.FirstOrDefault(scene => scene.name == k_SceneToLoad);
if (!loadedScene.isLoaded)
{
errorBuilder.AppendLine($"[ValidateIsLoaded] Scene {loadedScene.name} exists but is not loaded!");
return false;
}
if (m_ServerNetworkManager.SceneManager.SceneEventProgressTracking.Count > 0)
{
errorBuilder.AppendLine($"[ValidateIsLoaded] Server NetworkManager still has progress tracking events.");
return false;
}
foreach (var manager in m_ClientNetworkManagers)
{
// default will have isLoaded as false so we can get the scene or default and test on isLoaded
loadedScene = manager.SceneManager.ScenesLoaded.Values.FirstOrDefault(scene => scene.name == k_SceneToLoad);
if (!loadedScene.isLoaded)
{
errorBuilder.AppendLine($"[ValidateIsLoaded] Scene {loadedScene.name} exists but is not loaded!");
return false;
}
if (manager.SceneManager.SceneEventProgressTracking.Count > 0)
{
errorBuilder.AppendLine($"[ValidateIsLoaded] Client-{manager.name} still has progress tracking events.");
return false;
}
}
return true;
}
private bool ValidateSceneIsUnloaded()
{
if (m_ServerNetworkManager.SceneManager.ScenesLoaded.Values.Any(scene => scene.name == k_SceneToLoad))
{
return false;
}
if (m_ServerNetworkManager.SceneManager.SceneEventProgressTracking.Count > 0)
{
return false;
}
foreach (var manager in m_ClientNetworkManagers)
{
if (manager.SceneManager.ScenesLoaded.Values.Any(scene => scene.name == k_SceneToLoad))
{
return false;
}
if (manager.SceneManager.SceneEventProgressTracking.Count > 0)
{
return false;
}
}
return true;
}
private static void ValidateEventsAreEqual(SceneEvent expectedEvent, SceneEvent sceneEvent)
{
AssertField(expectedEvent.SceneEventType, sceneEvent.SceneEventType, nameof(sceneEvent.SceneEventType), sceneEvent.SceneEventType);
AssertField(expectedEvent.LoadSceneMode, sceneEvent.LoadSceneMode, nameof(sceneEvent.LoadSceneMode), sceneEvent.SceneEventType);
AssertField(expectedEvent.SceneName, sceneEvent.SceneName, nameof(sceneEvent.SceneName), sceneEvent.SceneEventType);
AssertField(expectedEvent.ClientId, sceneEvent.ClientId, nameof(sceneEvent.ClientId), sceneEvent.SceneEventType);
AssertField(expectedEvent.ClientsThatCompleted, sceneEvent.ClientsThatCompleted, nameof(sceneEvent.SceneEventType), sceneEvent.SceneEventType);
AssertField(expectedEvent.ClientsThatTimedOut, sceneEvent.ClientsThatTimedOut, nameof(sceneEvent.ClientsThatTimedOut), sceneEvent.SceneEventType);
}
// The LoadCompleted event includes the scene being loaded
private static void ValidateReceivedScene(ExpectedEvent expectedEvent, Scene scene)
{
AssertField(expectedEvent.SceneName, scene.name, "Scene.name", SceneEventType.LoadComplete);
AssertField(expectedEvent.ScenePath, scene.path, "Scene.path", SceneEventType.LoadComplete);
}
private static void AssertField<T>(T expected, T actual, string fieldName, SceneEventType type)
{
Assert.AreEqual(expected, actual, $"Failed on event {s_NumEventsProcessed} - {type}: Expected {fieldName} to be {expected}. Found {actual}");
}
}
}