1
1
using System . Collections ;
2
2
using UnityEngine ;
3
3
using NUnit . Framework ;
4
+ using Unity . Netcode ;
4
5
using UnityEngine . EventSystems ;
5
6
using UnityEngine . SceneManagement ;
6
7
using UnityEngine . UI ;
@@ -9,6 +10,8 @@ namespace Unity.Multiplayer.Samples.Utilities
9
10
{
10
11
public abstract class TestUtilities
11
12
{
13
+ const float k_MaxSceneLoadDuration = 10f ;
14
+
12
15
/// <summary>
13
16
/// Finds an active Button GameObject by name. If Button component is present on GameObject, it will be clicked.
14
17
/// </summary>
@@ -34,7 +37,7 @@ public static void ClickButtonByName(string name)
34
37
/// is either loaded successfully, or the loading process has timed out.
35
38
/// </summary>
36
39
/// <param name="sceneName"> Name of scene </param>
37
- /// <returns></returns>
40
+ /// <returns> IEnumerator to track scene load process </returns>
38
41
public static IEnumerator AssertIsSceneLoaded ( string sceneName )
39
42
{
40
43
var waitUntilSceneLoaded = new WaitForSceneLoad ( sceneName ) ;
@@ -43,46 +46,118 @@ public static IEnumerator AssertIsSceneLoaded(string sceneName)
43
46
44
47
Assert . That ( ! waitUntilSceneLoaded . timedOut ) ;
45
48
}
46
- }
47
49
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!" ) ;
57
60
58
- float m_LoadSceneStart ;
61
+ var waitForNetworkSceneLoad = new WaitForNetworkSceneLoad ( sceneName , networkSceneManager ) ;
59
62
60
- float m_MaxLoadDuration ;
63
+ yield return waitForNetworkSceneLoad ;
61
64
62
- public bool timedOut { get ; private set ; }
65
+ Assert . That ( ! waitForNetworkSceneLoad . timedOut ) ;
66
+ }
63
67
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
65
73
{
66
- get
67
- {
68
- var scene = SceneManager . GetSceneByName ( m_SceneName ) ;
74
+ string m_SceneName ;
69
75
70
- var isSceneLoaded = scene . IsValid ( ) && scene . isLoaded ;
76
+ float m_LoadSceneStart ;
71
77
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
73
85
{
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 ;
75
96
}
97
+ }
76
98
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 ;
78
104
}
79
105
}
80
106
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
82
112
{
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
+ }
86
161
}
87
162
}
88
163
}
0 commit comments