Skip to content

Commit bce3f8a

Browse files
adding an assertion helper for a network scene load
1 parent 40808d2 commit bce3f8a

File tree

2 files changed

+104
-27
lines changed

2 files changed

+104
-27
lines changed
Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using UnityEngine;
33
using NUnit.Framework;
4+
using Unity.Netcode;
45
using UnityEngine.EventSystems;
56
using UnityEngine.SceneManagement;
67
using UnityEngine.UI;
@@ -9,6 +10,8 @@ namespace Unity.Multiplayer.Samples.Utilities
910
{
1011
public abstract class TestUtilities
1112
{
13+
const float k_MaxSceneLoadDuration = 10f;
14+
1215
/// <summary>
1316
/// Finds an active Button GameObject by name. If Button component is present on GameObject, it will be clicked.
1417
/// </summary>
@@ -34,7 +37,7 @@ public static void ClickButtonByName(string name)
3437
/// is either loaded successfully, or the loading process has timed out.
3538
/// </summary>
3639
/// <param name="sceneName"> Name of scene </param>
37-
/// <returns></returns>
40+
/// <returns> IEnumerator to track scene load process </returns>
3841
public static IEnumerator AssertIsSceneLoaded(string sceneName)
3942
{
4043
var waitUntilSceneLoaded = new WaitForSceneLoad(sceneName);
@@ -43,46 +46,118 @@ public static IEnumerator AssertIsSceneLoaded(string sceneName)
4346

4447
Assert.That(!waitUntilSceneLoaded.timedOut);
4548
}
46-
}
4749

48-
/// <summary>
49-
/// Custom IEnumerator class to validate the loading of a Scene by name. If a scene load lasts longer than
50-
/// k_MaxSceneLoadDuration it is considered a timeout.
51-
/// </summary>
52-
public class WaitForSceneLoad : CustomYieldInstruction
53-
{
54-
const float k_MaxSceneLoadDuration = 10f;
55-
56-
string m_SceneName;
50+
/// <summary>
51+
/// Helper wrapper method for asserting the completion of a scene load to be used inside Playmode tests. A scene
52+
/// is either loaded successfully, or the loading process has timed out.
53+
/// </summary>
54+
/// <param name="sceneName"> Name of scene </param>
55+
/// <param name="networkSceneManager"> NetworkSceneManager instance </param>
56+
/// <returns> IEnumerator to track scene load process </returns>
57+
public static IEnumerator AssertIsNetworkSceneLoaded(string sceneName, NetworkSceneManager networkSceneManager)
58+
{
59+
Assert.That(networkSceneManager != null, "NetworkSceneManager instance is null!");
5760

58-
float m_LoadSceneStart;
61+
var waitForNetworkSceneLoad = new WaitForNetworkSceneLoad(sceneName, networkSceneManager);
5962

60-
float m_MaxLoadDuration;
63+
yield return waitForNetworkSceneLoad;
6164

62-
public bool timedOut { get; private set; }
65+
Assert.That(!waitForNetworkSceneLoad.timedOut);
66+
}
6367

64-
public override bool keepWaiting
68+
/// <summary>
69+
/// Custom IEnumerator class to validate the loading of a Scene by name. If a scene load lasts longer than
70+
/// k_MaxSceneLoadDuration it is considered a timeout.
71+
/// </summary>
72+
class WaitForSceneLoad : CustomYieldInstruction
6573
{
66-
get
67-
{
68-
var scene = SceneManager.GetSceneByName(m_SceneName);
74+
string m_SceneName;
6975

70-
var isSceneLoaded = scene.IsValid() && scene.isLoaded;
76+
float m_LoadSceneStart;
7177

72-
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
78+
float m_MaxLoadDuration;
79+
80+
public bool timedOut { get; private set; }
81+
82+
public override bool keepWaiting
83+
{
84+
get
7385
{
74-
timedOut = true;
86+
var scene = SceneManager.GetSceneByName(m_SceneName);
87+
88+
var isSceneLoaded = scene.IsValid() && scene.isLoaded;
89+
90+
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
91+
{
92+
timedOut = true;
93+
}
94+
95+
return !isSceneLoaded && !timedOut;
7596
}
97+
}
7698

77-
return !isSceneLoaded && !timedOut;
99+
public WaitForSceneLoad(string sceneName, float maxLoadDuration = k_MaxSceneLoadDuration)
100+
{
101+
m_LoadSceneStart = Time.time;
102+
m_SceneName = sceneName;
103+
m_MaxLoadDuration = maxLoadDuration;
78104
}
79105
}
80106

81-
public WaitForSceneLoad(string sceneName, float maxLoadDuration = k_MaxSceneLoadDuration)
107+
/// <summary>
108+
/// Custom IEnumerator class to validate the loading of a Scene by name. If a scene load lasts longer than
109+
/// k_MaxSceneLoadDuration it is considered a timeout.
110+
/// </summary>
111+
class WaitForNetworkSceneLoad : CustomYieldInstruction
82112
{
83-
m_LoadSceneStart = Time.time;
84-
m_SceneName = sceneName;
85-
m_MaxLoadDuration = maxLoadDuration;
113+
string m_SceneName;
114+
115+
float m_LoadSceneStart;
116+
117+
float m_MaxLoadDuration;
118+
119+
bool m_IsNetworkSceneLoaded;
120+
121+
NetworkSceneManager m_NetworkSceneManager;
122+
123+
public bool timedOut { get; private set; }
124+
125+
public override bool keepWaiting
126+
{
127+
get
128+
{
129+
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
130+
{
131+
timedOut = true;
132+
133+
m_NetworkSceneManager.OnSceneEvent -= ConfirmSceneLoad;
134+
}
135+
136+
return !m_IsNetworkSceneLoaded && !timedOut;
137+
}
138+
}
139+
140+
public WaitForNetworkSceneLoad(string sceneName, NetworkSceneManager networkSceneManager, float maxLoadDuration = k_MaxSceneLoadDuration)
141+
{
142+
m_LoadSceneStart = Time.time;
143+
m_SceneName = sceneName;
144+
m_MaxLoadDuration = maxLoadDuration;
145+
146+
m_NetworkSceneManager = networkSceneManager;
147+
148+
m_NetworkSceneManager.OnSceneEvent += ConfirmSceneLoad;
149+
}
150+
151+
void ConfirmSceneLoad(SceneEvent sceneEvent)
152+
{
153+
if (sceneEvent.SceneName == m_SceneName &&
154+
sceneEvent.SceneEventType == SceneEventType.LoadEventCompleted)
155+
{
156+
m_IsNetworkSceneLoaded = true;
157+
158+
m_NetworkSceneManager.OnSceneEvent -= ConfirmSceneLoad;
159+
}
160+
}
86161
}
87162
}
88163
}

Packages/com.unity.multiplayer.samples.coop/Utilities/Testing/com.unity.multiplayer.samples.utilities.testing.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"name": "Unity.Multiplayer.Samples.Utilities.Testing",
33
"rootNamespace": "",
4-
"references": [],
4+
"references": [
5+
"GUID:1491147abca9d7d4bb7105af628b223e"
6+
],
57
"includePlatforms": [],
68
"excludePlatforms": [],
79
"allowUnsafeCode": false,

0 commit comments

Comments
 (0)