Skip to content

Commit eef5e8d

Browse files
feat: test utilities script including Playmode test helpers (#484)
1 parent 9f183e9 commit eef5e8d

14 files changed

+214
-53
lines changed
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
using UnityEngine;
2-
using UnityEditor;
32
using UnityEngine.TestTools;
43
using NUnit.Framework;
54
using System.Collections;
65

7-
class EditorExampleTest
6+
namespace Unity.Multiplayer.Samples.Utilities.Tests.Editor
87
{
9-
10-
[Test]
11-
public void EditorSampleTestSimplePasses()
8+
class EditorExampleTest
129
{
13-
// Use the Assert class to test conditions.
14-
}
10+
[Test]
11+
public void EditorSampleTestSimplePasses()
12+
{
13+
// Use the Assert class to test conditions.
14+
}
1515

16-
// A UnityTest behaves like a coroutine in PlayMode
17-
// and allows you to yield null to skip a frame in EditMode
18-
[UnityTest]
19-
public IEnumerator EditorSampleTestWithEnumeratorPasses()
20-
{
21-
// Use the Assert class to test conditions.
22-
// yield to skip a frame
23-
yield return null;
16+
// A UnityTest behaves like a coroutine in PlayMode
17+
// and allows you to yield null to skip a frame in EditMode
18+
[UnityTest]
19+
public IEnumerator EditorSampleTestWithEnumeratorPasses()
20+
{
21+
// Use the Assert class to test conditions.
22+
// yield to skip a frame
23+
yield return null;
24+
}
2425
}
2526
}

Packages/com.unity.multiplayer.samples.coop/Tests/Editor/EditorTests.asmdef

Lines changed: 0 additions & 12 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "Unity.Multiplayer.Samples.Utilities.Tests.Editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"UnityEngine.TestRunner",
6+
"UnityEditor.TestRunner"
7+
],
8+
"includePlatforms": [
9+
"Editor"
10+
],
11+
"excludePlatforms": [],
12+
"allowUnsafeCode": false,
13+
"overrideReferences": true,
14+
"precompiledReferences": [
15+
"nunit.framework.dll"
16+
],
17+
"autoReferenced": false,
18+
"defineConstraints": [
19+
"UNITY_INCLUDE_TESTS"
20+
],
21+
"versionDefines": [],
22+
"noEngineReferences": false
23+
}

Packages/com.unity.multiplayer.samples.coop/Tests/Runtime/RuntimeExampleTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using NUnit.Framework;
44
using System.Collections;
55

6-
namespace Tests
6+
namespace Unity.Multiplayer.Samples.Utilities.Tests.Runtime
77
{
88
public class RuntimeExampleTest
99
{

Packages/com.unity.multiplayer.samples.coop/Tests/Runtime/RuntimeTests.asmdef

Lines changed: 0 additions & 6 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "Unity.Multiplayer.Samples.Utilities.Tests.Runtime",
3+
"rootNamespace": "",
4+
"references": [
5+
"UnityEngine.TestRunner",
6+
"UnityEditor.TestRunner"
7+
],
8+
"includePlatforms": [],
9+
"excludePlatforms": [],
10+
"allowUnsafeCode": false,
11+
"overrideReferences": true,
12+
"precompiledReferences": [
13+
"nunit.framework.dll"
14+
],
15+
"autoReferenced": false,
16+
"defineConstraints": [
17+
"UNITY_INCLUDE_TESTS"
18+
],
19+
"versionDefines": [],
20+
"noEngineReferences": false
21+
}

Packages/com.unity.multiplayer.samples.coop/Utilities/Test.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

Packages/com.unity.multiplayer.samples.coop/Utilities/Testing.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using NUnit.Framework;
6+
using Unity.Netcode;
7+
using UnityEngine.SceneManagement;
8+
9+
namespace Unity.Multiplayer.Samples.Utilities
10+
{
11+
public abstract class TestUtilities
12+
{
13+
const float k_MaxSceneLoadDuration = 10f;
14+
15+
/// <summary>
16+
/// Helper wrapper method for asserting the completion of a network scene load to be used inside Playmode tests.
17+
/// A scene is either loaded successfully, or the loading process has timed out and will throw an exception.
18+
/// </summary>
19+
/// <param name="sceneName"> Name of scene </param>
20+
/// <param name="networkSceneManager"> NetworkSceneManager instance </param>
21+
/// <returns> IEnumerator to track scene load process </returns>
22+
public static IEnumerator AssertIsNetworkSceneLoaded(string sceneName, NetworkSceneManager networkSceneManager)
23+
{
24+
Assert.That(networkSceneManager != null, "NetworkSceneManager instance is null!");
25+
26+
yield return new WaitForNetworkSceneLoad(sceneName, networkSceneManager);
27+
}
28+
29+
/// <summary>
30+
/// Custom IEnumerator class to validate the loading of a Scene by name. If a scene load lasts longer than
31+
/// k_MaxSceneLoadDuration it is considered a timeout.
32+
/// </summary>
33+
public class WaitForSceneLoad : CustomYieldInstruction
34+
{
35+
string m_SceneName;
36+
37+
float m_LoadSceneStart;
38+
39+
float m_MaxLoadDuration;
40+
41+
public override bool keepWaiting
42+
{
43+
get
44+
{
45+
var scene = SceneManager.GetSceneByName(m_SceneName);
46+
47+
var isSceneLoaded = scene.IsValid() && scene.isLoaded;
48+
49+
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
50+
{
51+
throw new Exception($"Timeout for scene load for scene name {m_SceneName}");
52+
}
53+
54+
return !isSceneLoaded;
55+
}
56+
}
57+
58+
public WaitForSceneLoad(string sceneName, float maxLoadDuration = k_MaxSceneLoadDuration)
59+
{
60+
m_LoadSceneStart = Time.time;
61+
m_SceneName = sceneName;
62+
m_MaxLoadDuration = maxLoadDuration;
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Custom IEnumerator class to validate the loading of a Scene through Netcode for GameObjects by name.
68+
/// If a scene load lasts longer than k_MaxSceneLoadDuration it is considered a timeout.
69+
/// </summary>
70+
class WaitForNetworkSceneLoad : CustomYieldInstruction
71+
{
72+
string m_SceneName;
73+
74+
float m_LoadSceneStart;
75+
76+
float m_MaxLoadDuration;
77+
78+
bool m_IsNetworkSceneLoaded;
79+
80+
NetworkSceneManager m_NetworkSceneManager;
81+
82+
public override bool keepWaiting
83+
{
84+
get
85+
{
86+
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
87+
{
88+
m_NetworkSceneManager.OnLoadEventCompleted -= ConfirmSceneLoad;
89+
90+
throw new Exception($"Timeout for network scene load for scene name {m_SceneName}");
91+
}
92+
93+
return !m_IsNetworkSceneLoaded;
94+
}
95+
}
96+
97+
public WaitForNetworkSceneLoad(string sceneName, NetworkSceneManager networkSceneManager, float maxLoadDuration = k_MaxSceneLoadDuration)
98+
{
99+
m_LoadSceneStart = Time.time;
100+
m_SceneName = sceneName;
101+
m_MaxLoadDuration = maxLoadDuration;
102+
103+
m_NetworkSceneManager = networkSceneManager;
104+
105+
m_NetworkSceneManager.OnLoadEventCompleted += ConfirmSceneLoad;
106+
}
107+
108+
void ConfirmSceneLoad(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
109+
{
110+
if (sceneName == m_SceneName)
111+
{
112+
m_IsNetworkSceneLoaded = true;
113+
114+
m_NetworkSceneManager.OnLoadEventCompleted -= ConfirmSceneLoad;
115+
}
116+
}
117+
}
118+
}
119+
}

Packages/com.unity.multiplayer.samples.coop/Utilities/Test.cs.meta renamed to Packages/com.unity.multiplayer.samples.coop/Utilities/Testing/TestUtilities.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "Unity.Multiplayer.Samples.Utilities.Testing",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:1491147abca9d7d4bb7105af628b223e"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": true,
11+
"precompiledReferences": [
12+
"nunit.framework.dll"
13+
],
14+
"autoReferenced": true,
15+
"defineConstraints": [],
16+
"versionDefines": [],
17+
"noEngineReferences": false
18+
}

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

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)