Files
unity-application/Assets/Architecture/Tests/PlayMode/SystemControllerTests.cs
2023-05-17 17:36:27 +00:00

305 lines
12 KiB
C#

using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
/// <summary>
/// Test the SystemController class
/// </summary>
[TestFixture]
public class SystemControllerTests
{
/// <summary>
/// Wait time between scene transitions
/// </summary>
private const float WAIT_TIME = 0.2f;
/// <summary>
/// Reference to the SystemController instance
/// </summary>
private SystemController controller;
/// <summary>
/// Setting up the tests
/// </summary>
[UnitySetUp]
public IEnumerator Setup_SystemController()
{
string path = $"{Application.persistentDataPath}/wesign_unit_test.json";
string oneUser = "{\"version\":1537,\"users\":[{\"entries\":[],\"username\":\"Tester0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0,\"useGPU\":false}";
File.WriteAllText(path, oneUser);
PersistentDataController.PATH = path;
PersistentDataController.GetInstance().Load();
AssetDatabase.LoadAssetAtPath<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake();
controller = SystemController.GetInstance();
controller.LoadNextScene("Common/Scenes/MainMenuScreen");
yield return new WaitForSeconds(WAIT_TIME);
}
/// <summary>
/// Cleaning up the tests
/// </summary>
[TearDown]
public void TearDown_SystemController()
{
controller.BackToScene(SceneManager.sceneCountInBuildSettings);
}
/// <summary>
/// Test whether all scenes are correctly inserted to the build path
/// </summary>
[Test]
public void Test_GetSceneIndex_ValidScene()
{
List<string> scenes = new List<string>()
{
"Common/Scenes/Boot",
"Common/Scenes/MainMenuScreen",
"Common/Scenes/CoursesMenuScreen",
"Common/Scenes/ListCoursesScreen",
"Common/Scenes/ListMinigamesScreen",
"Common/Scenes/CourseActivityScreen",
"Common/Scenes/MinigameActivityScreen",
"Common/Scenes/ThemeSelectionScreen",
"Common/Scenes/SettingsScreen",
"Accounts/Scenes/UserCreationScreen",
"Accounts/Scenes/ChangeUserScreen",
"Accounts/Scenes/UserProgressScreen",
"Courses/Scenes/CourseScreen",
"SpellingBee/Scenes/SpellingBeeGame",
"Hangman/Scenes/HangmanGame",
"JustSign/Scenes/JustSignGame",
};
for (var i = 0; i < scenes.Count; i++)
Assert.AreEqual(i, SystemController.GetSceneIndex(scenes[i]));
}
/// <summary>
/// Test whether a next scene can be loaded
/// </summary>
[UnityTest]
public IEnumerator Test_LoadNextScene_String()
{
int stackSize = controller.GetSceneStackSize();
string previous = "Common/Scenes/MainMenuScreen";
string next = "Common/Scenes/CoursesMenuScreen";
Assert.AreEqual(SystemController.GetSceneIndex(previous), controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
controller.LoadNextScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(SystemController.GetSceneIndex(previous), controller.previousScene);
Assert.AreEqual(SystemController.GetSceneIndex(next), controller.currentScene);
Assert.AreEqual(stackSize + 1, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether a next scene can be loaded
/// </summary>
[UnityTest]
public IEnumerator Test_LoadNextScene_Int()
{
int stackSize = controller.GetSceneStackSize();
int previous = 1;
int next = 2;
Assert.AreEqual(previous, controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
controller.LoadNextScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(previous, controller.previousScene);
Assert.AreEqual(next, controller.currentScene);
Assert.AreEqual(stackSize + 1, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether a scene can be swapped with the current one
/// </summary>
[UnityTest]
public IEnumerator Test_SwapScene_String()
{
int stackSize = controller.GetSceneStackSize();
string previous = "Common/Scenes/MainMenuScreen";
string next = "Common/Scenes/CoursesMenuScreen";
Assert.AreEqual(SystemController.GetSceneIndex(previous), controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
controller.SwapScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(SystemController.GetSceneIndex(previous), controller.previousScene);
Assert.AreEqual(SystemController.GetSceneIndex(next), controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether a scene can be swapped with the current one
/// </summary>
[UnityTest]
public IEnumerator Test_SwapScene_Int()
{
int stackSize = controller.GetSceneStackSize();
int previous = 1;
int next = 2;
Assert.AreEqual(previous, controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
controller.SwapScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(previous, controller.previousScene);
Assert.AreEqual(next, controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether the previous scene can be loaded
/// </summary>
[UnityTest]
public IEnumerator Test_BackToPreviousScene_LoadScene()
{
int stackSize = controller.GetSceneStackSize();
string previous = "Common/Scenes/MainMenuScreen";
string current = "Common/Scenes/CoursesMenuScreen";
string next = "Common/Scenes/ListCoursesScreen";
controller.LoadNextScene(current);
yield return new WaitForSeconds(WAIT_TIME);
controller.LoadNextScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(SystemController.GetSceneIndex(current), controller.previousScene);
Assert.AreEqual(SystemController.GetSceneIndex(next), controller.currentScene);
Assert.AreEqual(stackSize + 2, controller.GetSceneStackSize());
controller.BackToPreviousScene();
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(stackSize + 1, controller.GetSceneStackSize());
controller.BackToPreviousScene();
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(SystemController.GetSceneIndex(current), controller.previousScene);
Assert.AreEqual(SystemController.GetSceneIndex(previous), controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether when requesting to go back on the ast scene, will close the application
/// </summary>
[UnityTest]
public IEnumerator Test_BackToPreviousScene_QuitApplication()
{
int stackSize = controller.GetSceneStackSize();
controller.BackToPreviousScene();
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(stackSize - 1, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether a previous scene can be loaded
/// </summary>
[UnityTest]
public IEnumerator Test_BackToScene_String_LoadScene()
{
int stackSize = controller.GetSceneStackSize();
string previous = "Common/Scenes/MainMenuScreen";
string current = "Common/Scenes/CoursesMenuScreen";
string next = "Common/Scenes/ListCoursesScreen";
controller.LoadNextScene(current);
yield return new WaitForSeconds(WAIT_TIME);
controller.LoadNextScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(SystemController.GetSceneIndex(current), controller.previousScene);
Assert.AreEqual(SystemController.GetSceneIndex(next), controller.currentScene);
Assert.AreEqual(stackSize + 2, controller.GetSceneStackSize());
controller.BackToScene(previous);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(SystemController.GetSceneIndex(next), controller.previousScene);
Assert.AreEqual(SystemController.GetSceneIndex(previous), controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether when requesting to go back to scene that is no longer loaded, will close the application
/// </summary>
[UnityTest]
public IEnumerator Test_BackToScene_String_QuitApplication()
{
int stackSize = controller.GetSceneStackSize();
string previous = "Common/Scenes/MainMenuScreen";
string current = "Common/Scenes/CoursesMenuScreen";
string next = "Common/Scenes/ListCoursesScreen";
controller.SwapScene(current);
yield return new WaitForSeconds(WAIT_TIME);
controller.LoadNextScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(SystemController.GetSceneIndex(current), controller.previousScene);
Assert.AreEqual(SystemController.GetSceneIndex(next), controller.currentScene);
Assert.AreEqual(stackSize + 1, controller.GetSceneStackSize());
controller.BackToScene(previous);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(stackSize - 1, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether a previous scene can be loaded
/// </summary>
[UnityTest]
public IEnumerator Test_BackToScene_Int_LoadScene()
{
int stackSize = controller.GetSceneStackSize();
int previous = 1;
int current = 2;
int next = 3;
controller.LoadNextScene(current);
yield return new WaitForSeconds(WAIT_TIME);
controller.LoadNextScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(current, controller.previousScene);
Assert.AreEqual(next, controller.currentScene);
Assert.AreEqual(stackSize + 2, controller.GetSceneStackSize());
controller.BackToScene(previous);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(next, controller.previousScene);
Assert.AreEqual(previous, controller.currentScene);
Assert.AreEqual(stackSize, controller.GetSceneStackSize());
}
/// <summary>
/// Test whether when requesting to go back to scene that is no longer loaded, will close the application
/// </summary>
[UnityTest]
public IEnumerator Test_BackToScene_Int_QuitApplication()
{
int stackSize = controller.GetSceneStackSize();
int previous = 1;
int current = 2;
int next = 3;
controller.SwapScene(current);
yield return new WaitForSeconds(WAIT_TIME);
controller.LoadNextScene(next);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(current, controller.previousScene);
Assert.AreEqual(next, controller.currentScene);
Assert.AreEqual(stackSize + 1, controller.GetSceneStackSize());
controller.BackToScene(previous);
yield return new WaitForSeconds(WAIT_TIME);
Assert.AreEqual(stackSize - 1, controller.GetSceneStackSize());
}
}