Skip to content

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

Merged
merged 16 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Assets/BossRoom/Scripts/Editor/SceneBootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Unity.Multiplayer.Samples.BossRoom.Editor
{
Expand Down Expand Up @@ -99,6 +100,15 @@ static void DisableDoNotLoadBootstrapSceneOnPlay()

static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange obj)
{
// detects whether playmode has been initiated by a TestRunner Playmode test; if so disable Boostrap loading
for (int i = 0; i < SceneManager.sceneCount; i++)
{
if (SceneManager.GetSceneAt(i).name.Contains("InitTestScene"))
{
return;
}
}

if (!LoadBootstrapScene)
{
return;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Unity.Multiplayer.Samples.Coop.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
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using NUnit.Framework;
using System.Collections;

namespace Tests
namespace Unity.Multiplayer.Samples.Coop.Tests.Runtime
{
public class RuntimeExampleTest
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Unity.Multiplayer.Samples.Coop.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
}
18 changes: 0 additions & 18 deletions Packages/com.unity.multiplayer.samples.coop/Utilities/Test.cs

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,88 @@
using System.Collections;
using UnityEngine;
using NUnit.Framework;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Unity.Multiplayer.Samples.Utilities
{
public abstract class TestUtils
{
/// <summary>
/// Finds an active Button GameObject by name. If Button component is present on GameObject, it will be clicked.
/// </summary>
/// <param name="name"> Name of Button GameObject </param>
public static void ClickButtonByName(string name)
{
var buttonGameObject = GameObject.Find(name);

Assert.IsNotNull(buttonGameObject,
$"Button GameObject with name {name} not found in scene!");

EventSystem.current.SetSelectedGameObject(buttonGameObject);

var buttonComponent = buttonGameObject.GetComponent<Button>();

Assert.IsNotNull(buttonComponent, $"Button component not found on {buttonGameObject.name}!");

buttonComponent.onClick.Invoke();
}

/// <summary>
/// Helper wrapper method for asserting the completion of a scene load to be used inside Playmode tests. A scene
/// is either loaded successfully, or the loading process has timed out.
/// </summary>
/// <param name="sceneName"> Name of scene </param>
/// <returns></returns>
public static IEnumerator AssertIsSceneLoaded(string sceneName)
{
var waitUntilSceneLoaded = new WaitForSceneLoad(sceneName);

yield return waitUntilSceneLoaded;

Assert.That(!waitUntilSceneLoaded.timedOut);
}
}

/// <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
{
const float k_MaxSceneLoadDuration = 10f;

string m_SceneName;

float m_LoadSceneStart;

float m_MaxLoadDuration;

public bool timedOut { get; private set; }

public override bool keepWaiting
{
get
{
var scene = SceneManager.GetSceneByName(m_SceneName);

var isSceneLoaded = scene.IsValid() && scene.isLoaded;

if (Time.time - m_LoadSceneStart >= m_MaxLoadDuration)
{
timedOut = true;
}

return !isSceneLoaded && !timedOut;
}
}

public WaitForSceneLoad(string sceneName, float maxLoadDuration = k_MaxSceneLoadDuration)
{
m_LoadSceneStart = Time.time;
m_SceneName = sceneName;
m_MaxLoadDuration = maxLoadDuration;
}
}
}

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,16 @@
{
"name": "Unity.Multiplayer.Samples.Utilities.Testing",
"rootNamespace": "",
"references": [],
"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.