-
Notifications
You must be signed in to change notification settings - Fork 557
feat: test utilities script including Playmode test helpers #484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9206577
bc95fe5
82f0dd6
40808d2
bce3f8a
07cfb79
019f19c
488db79
cb5c780
148821d
c4d0580
4509df0
aafded5
31d7ca0
f3c963e
115b538
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using UnityEngine.TestTools; | ||
using NUnit.Framework; | ||
using System.Collections; | ||
|
||
class EditorExampleTest | ||
namespace Unity.Multiplayer.Samples.Utilities.Tests.Editor | ||
{ | ||
|
||
[Test] | ||
public void EditorSampleTestSimplePasses() | ||
class EditorExampleTest | ||
{ | ||
// Use the Assert class to test conditions. | ||
} | ||
[Test] | ||
public void EditorSampleTestSimplePasses() | ||
{ | ||
// Use the Assert class to test conditions. | ||
} | ||
|
||
// A UnityTest behaves like a coroutine in PlayMode | ||
// and allows you to yield null to skip a frame in EditMode | ||
[UnityTest] | ||
public IEnumerator EditorSampleTestWithEnumeratorPasses() | ||
{ | ||
// Use the Assert class to test conditions. | ||
// yield to skip a frame | ||
yield return null; | ||
// A UnityTest behaves like a coroutine in PlayMode | ||
// and allows you to yield null to skip a frame in EditMode | ||
[UnityTest] | ||
public IEnumerator EditorSampleTestWithEnumeratorPasses() | ||
{ | ||
// Use the Assert class to test conditions. | ||
// yield to skip a frame | ||
yield return null; | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "Unity.Multiplayer.Samples.Utilities.Tests.Editor", | ||
"rootNamespace": "", | ||
"references": [ | ||
"UnityEngine.TestRunner", | ||
"UnityEditor.TestRunner" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": true, | ||
"precompiledReferences": [ | ||
"nunit.framework.dll" | ||
], | ||
"autoReferenced": false, | ||
"defineConstraints": [ | ||
"UNITY_INCLUDE_TESTS" | ||
], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "Unity.Multiplayer.Samples.Utilities.Tests.Runtime", | ||
"rootNamespace": "", | ||
"references": [ | ||
"UnityEngine.TestRunner", | ||
"UnityEditor.TestRunner" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": true, | ||
"precompiledReferences": [ | ||
"nunit.framework.dll" | ||
], | ||
"autoReferenced": false, | ||
"defineConstraints": [ | ||
"UNITY_INCLUDE_TESTS" | ||
], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
This file was deleted.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using NUnit.Framework; | ||
using Unity.Netcode; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Unity.Multiplayer.Samples.Utilities | ||
{ | ||
public abstract class TestUtilities | ||
{ | ||
const float k_MaxSceneLoadDuration = 10f; | ||
|
||
/// <summary> | ||
/// Helper wrapper method for asserting the completion of a network scene load to be used inside Playmode tests. | ||
/// A scene is either loaded successfully, or the loading process has timed out and will throw an exception. | ||
/// </summary> | ||
/// <param name="sceneName"> Name of scene </param> | ||
/// <param name="networkSceneManager"> NetworkSceneManager instance </param> | ||
/// <returns> IEnumerator to track scene load process </returns> | ||
public static IEnumerator AssertIsNetworkSceneLoaded(string sceneName, NetworkSceneManager networkSceneManager) | ||
{ | ||
Assert.That(networkSceneManager != null, "NetworkSceneManager instance is null!"); | ||
|
||
yield return new WaitForNetworkSceneLoad(sceneName, networkSceneManager); | ||
} | ||
|
||
/// <summary> | ||
/// Custom IEnumerator class to validate the loading of a Scene by name. If a scene load lasts longer than | ||
/// k_MaxSceneLoadDuration it is considered a timeout. | ||
/// </summary> | ||
public class WaitForSceneLoad : CustomYieldInstruction | ||
{ | ||
string m_SceneName; | ||
|
||
float m_LoadSceneStart; | ||
|
||
float m_MaxLoadDuration; | ||
|
||
public override bool keepWaiting | ||
{ | ||
get | ||
{ | ||
var scene = SceneManager.GetSceneByName(m_SceneName); | ||
|
||
var isSceneLoaded = scene.IsValid() && scene.isLoaded; | ||
|
||
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration) | ||
{ | ||
throw new Exception($"Timeout for scene load for scene name {m_SceneName}"); | ||
} | ||
|
||
return !isSceneLoaded; | ||
} | ||
} | ||
|
||
public WaitForSceneLoad(string sceneName, float maxLoadDuration = k_MaxSceneLoadDuration) | ||
{ | ||
m_LoadSceneStart = Time.time; | ||
m_SceneName = sceneName; | ||
m_MaxLoadDuration = maxLoadDuration; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Custom IEnumerator class to validate the loading of a Scene through Netcode for GameObjects by name. | ||
/// If a scene load lasts longer than k_MaxSceneLoadDuration it is considered a timeout. | ||
/// </summary> | ||
class WaitForNetworkSceneLoad : CustomYieldInstruction | ||
{ | ||
string m_SceneName; | ||
|
||
float m_LoadSceneStart; | ||
|
||
float m_MaxLoadDuration; | ||
|
||
bool m_IsNetworkSceneLoaded; | ||
|
||
NetworkSceneManager m_NetworkSceneManager; | ||
|
||
public override bool keepWaiting | ||
{ | ||
get | ||
{ | ||
if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration) | ||
{ | ||
m_NetworkSceneManager.OnLoadEventCompleted -= ConfirmSceneLoad; | ||
|
||
throw new Exception($"Timeout for network scene load for scene name {m_SceneName}"); | ||
} | ||
|
||
return !m_IsNetworkSceneLoaded; | ||
} | ||
} | ||
|
||
public WaitForNetworkSceneLoad(string sceneName, NetworkSceneManager networkSceneManager, float maxLoadDuration = k_MaxSceneLoadDuration) | ||
{ | ||
m_LoadSceneStart = Time.time; | ||
m_SceneName = sceneName; | ||
m_MaxLoadDuration = maxLoadDuration; | ||
|
||
m_NetworkSceneManager = networkSceneManager; | ||
|
||
m_NetworkSceneManager.OnLoadEventCompleted += ConfirmSceneLoad; | ||
} | ||
|
||
void ConfirmSceneLoad(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut) | ||
{ | ||
if (sceneName == m_SceneName) | ||
{ | ||
m_IsNetworkSceneLoaded = true; | ||
|
||
m_NetworkSceneManager.OnLoadEventCompleted -= ConfirmSceneLoad; | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "Unity.Multiplayer.Samples.Utilities.Testing", | ||
"rootNamespace": "", | ||
"references": [ | ||
"GUID:1491147abca9d7d4bb7105af628b223e" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": true, | ||
"precompiledReferences": [ | ||
"nunit.framework.dll" | ||
], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.