using NUnit.Framework; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.TestTools; /// /// Test the SystemController class /// [TestFixture] public class SystemControllerTests { /// /// Wait time between scene transitions /// private const float WAIT_TIME = 0.2f; /// /// Reference to the SystemController instance /// private SystemController controller; /// /// Setting up the tests /// [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("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake(); controller = SystemController.GetInstance(); controller.LoadNextScene("Common/Scenes/MainMenuScreen"); yield return new WaitForSeconds(WAIT_TIME); } /// /// Cleaning up the tests /// [TearDown] public void TearDown_SystemController() { controller.BackToScene(SceneManager.sceneCountInBuildSettings); } /// /// Test whether all scenes are correctly inserted to the build path /// [Test] public void Test_GetSceneIndex_ValidScene() { List scenes = new List() { "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])); } /// /// Test whether a next scene can be loaded /// [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()); } /// /// Test whether a next scene can be loaded /// [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()); } /// /// Test whether a scene can be swapped with the current one /// [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()); } /// /// Test whether a scene can be swapped with the current one /// [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()); } /// /// Test whether the previous scene can be loaded /// [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()); } /// /// Test whether when requesting to go back on the ast scene, will close the application /// [UnityTest] public IEnumerator Test_BackToPreviousScene_QuitApplication() { int stackSize = controller.GetSceneStackSize(); controller.BackToPreviousScene(); yield return new WaitForSeconds(WAIT_TIME); Assert.AreEqual(stackSize - 1, controller.GetSceneStackSize()); } /// /// Test whether a previous scene can be loaded /// [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()); } /// /// Test whether when requesting to go back to scene that is no longer loaded, will close the application /// [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()); } /// /// Test whether a previous scene can be loaded /// [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()); } /// /// Test whether when requesting to go back to scene that is no longer loaded, will close the application /// [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()); } }