using NUnit.Framework; using System.Collections; using System.IO; using UnityEditor; using UnityEngine; using UnityEngine.TestTools; using UnityEngine.UI; /// /// Test the PanelMinigameProgress class /// [TestFixture] public class PanelMinigameProgressTests { /// /// Wait time between scene transitions /// private const float WAIT_TIME = 0.2f; /// /// Setup the PanelMinigameProgress tests /// private IEnumerator Setup_PanelMinigameProgress() { string path = $"{Application.persistentDataPath}/wesign_unit_test.json"; string minigame = "{\"entries\":[],\"minigameIndex\":1,\"latestScores\":[{\"scoreValue\":70,\"time\":\"19/04/2023 22:32:39\"},{\"scoreValue\":55,\"time\":\"20/04/2023 11:50:10\"},{\"scoreValue\":55,\"time\":\"20/04/2023 13:27:15\"}],\"highestScores\":[{\"scoreValue\":70,\"time\":\"19/04/2023 22:32:39\"},{\"scoreValue\":55,\"time\":\"20/04/2023 11:50:10\"},{\"scoreValue\":55,\"time\":\"20/04/2023 13:27:15\"}]}"; string oneUser = $"{{\"version\":1027,\"users\":[{{\"entries\":[],\"username\":\"Tester0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[{minigame}],\"courses\":[]}}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}"; File.WriteAllText(path, oneUser); PersistentDataController.PATH = path; PersistentDataController.GetInstance().Load(); AssetDatabase.LoadAssetAtPath("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake(); SystemController.GetInstance().SwapScene("Accounts/Scenes/UserProgressScreen"); yield return new WaitForSeconds(WAIT_TIME); GameObject.FindObjectOfType().DisplayMinigames(); yield return new WaitForSeconds(WAIT_TIME); } /// /// Setup the PanelMinigameProgress tests /// private IEnumerator Setup_PanelMinigameProgress_Empty() { string path = $"{Application.persistentDataPath}/wesign_unit_test.json"; string oneUser = $"{{\"version\":1027,\"users\":[{{\"entries\":[],\"username\":\"Tester0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}"; File.WriteAllText(path, oneUser); PersistentDataController.PATH = path; PersistentDataController.GetInstance().Load(); AssetDatabase.LoadAssetAtPath("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake(); SystemController.GetInstance().SwapScene("Accounts/Scenes/UserProgressScreen"); yield return new WaitForSeconds(WAIT_TIME); GameObject.FindObjectOfType().DisplayMinigames(); yield return new WaitForSeconds(WAIT_TIME); } /// /// Setup the PanelMinigameProgress tests /// private IEnumerator Setup_PanelMinigameProgress_NoScorePresent() { string path = $"{Application.persistentDataPath}/wesign_unit_test.json"; string minigame = "{\"entries\":[],\"minigameIndex\":1,\"latestScores\":[],\"highestScores\":[]}"; string oneUser = $"{{\"version\":1027,\"users\":[{{\"entries\":[],\"username\":\"Tester0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[{minigame}],\"courses\":[]}}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}"; File.WriteAllText(path, oneUser); PersistentDataController.PATH = path; PersistentDataController.GetInstance().Load(); AssetDatabase.LoadAssetAtPath("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake(); SystemController.GetInstance().SwapScene("Accounts/Scenes/UserProgressScreen"); yield return new WaitForSeconds(WAIT_TIME); GameObject.FindObjectOfType().DisplayMinigames(); yield return new WaitForSeconds(WAIT_TIME); } /// /// Setup the PanelMinigameProgress tests /// private IEnumerator Setup_PanelMinigameProgress_Multiple() { string path = $"{Application.persistentDataPath}/wesign_unit_test.json"; string minigame = "[{\"entries\":[],\"minigameIndex\":1,\"latestScores\":[{\"scoreValue\":70,\"time\":\"20/04/2023 16:08:43\"},{\"scoreValue\":20,\"time\":\"20/04/2023 16:11:53\"}],\"highestScores\":[{\"scoreValue\":70,\"time\":\"20/04/2023 16:08:43\"},{\"scoreValue\":20,\"time\":\"20/04/2023 16:11:53\"}]},{\"entries\":[],\"minigameIndex\":2,\"latestScores\":[{\"scoreValue\":194,\"time\":\"20/04/2023 15:27:41\"},{\"scoreValue\":155,\"time\":\"20/04/2023 15:28:56\"},{\"scoreValue\":84,\"time\":\"20/04/2023 15:42:10\"},{\"scoreValue\":465,\"time\":\"20/04/2023 16:14:22\"}],\"highestScores\":[{\"scoreValue\":465,\"time\":\"20/04/2023 16:14:22\"},{\"scoreValue\":194,\"time\":\"20/04/2023 15:27:41\"},{\"scoreValue\":155,\"time\":\"20/04/2023 15:28:56\"},{\"scoreValue\":84,\"time\":\"20/04/2023 15:42:10\"}]},{\"entries\":[],\"minigameIndex\":0,\"latestScores\":[{\"scoreValue\":1090,\"time\":\"20/04/2023 16:02:14\"},{\"scoreValue\":1180,\"time\":\"20/04/2023 16:07:02\"}],\"highestScores\":[{\"scoreValue\":1180,\"time\":\"20/04/2023 16:07:02\"},{\"scoreValue\":1090,\"time\":\"20/04/2023 16:02:14\"}]}]"; string oneUser = $"{{\"version\":1027,\"users\":[{{\"entries\":[],\"username\":\"Tester0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":{minigame},\"courses\":[]}}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}"; File.WriteAllText(path, oneUser); PersistentDataController.PATH = path; PersistentDataController.GetInstance().Load(); AssetDatabase.LoadAssetAtPath("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake(); SystemController.GetInstance().SwapScene("Accounts/Scenes/UserProgressScreen"); yield return new WaitForSeconds(WAIT_TIME); GameObject.FindObjectOfType().DisplayMinigames(); yield return new WaitForSeconds(WAIT_TIME); } /// /// Cleanup after testing /// [TearDown] public void TearDown_PanelMinigameProgress() { PersistentDataController.PATH = null; } /// /// Test whether every item that needs to be assign in the editor, is assigned /// [UnityTest] public IEnumerator Test_EditorAssignments() { yield return Setup_PanelMinigameProgress(); var panel = GameObject.FindObjectOfType(); Assert.IsNotNull(panel); Assert.IsNotNull(panel.minigameList); Assert.IsNotNull(panel.minigameCardPrefab); Assert.IsNotNull(panel.minigamesContainer); Assert.IsNotNull(panel.minigameInfo); Assert.IsNotNull(panel.emptyMinigames); Assert.IsNotNull(panel.minigameTitle); Assert.IsNotNull(panel.progressGraph); Assert.IsNotNull(panel.emptyHighscore); } /// /// Test whether the panel is correctly initialized /// [UnityTest] public IEnumerator Test_Start() { yield return Setup_PanelMinigameProgress(); var panel = GameObject.FindObjectOfType(); Assert.IsTrue(panel.minigameInfo.gameObject.activeSelf); Assert.IsFalse(panel.emptyMinigames.gameObject.activeSelf); Assert.IsFalse(panel.emptyHighscore.gameObject.activeSelf); Assert.IsTrue(panel.progressGraph.gameObject.activeSelf); var minigame = panel.minigameList.GetMinigameByIndex(MinigameIndex.HANGMAN); Assert.AreEqual(minigame.title, panel.minigameTitle.text); } /// /// Test whether the panel is correctly initialized when there is no minigame progress /// [UnityTest] public IEnumerator Test_Start_Empty() { yield return Setup_PanelMinigameProgress_Empty(); var panel = GameObject.FindObjectOfType(); Assert.IsFalse(panel.minigameInfo.gameObject.activeSelf); Assert.IsTrue(panel.emptyMinigames.gameObject.activeSelf); } /// /// Test whether the panel is correctly initialized when there are minigames, but these don't have any score /// [UnityTest] public IEnumerator Test_Start_NoScorePresent() { yield return Setup_PanelMinigameProgress_NoScorePresent(); var panel = GameObject.FindObjectOfType(); Assert.IsTrue(panel.minigameInfo.gameObject.activeSelf); Assert.IsFalse(panel.emptyMinigames.gameObject.activeSelf); Assert.IsTrue(panel.emptyHighscore.gameObject.activeSelf); Assert.IsFalse(panel.progressGraph.gameObject.activeSelf); } /// /// Test whether we can select a minigame /// [UnityTest] public IEnumerator Test_UpdateSelection() { yield return Setup_PanelMinigameProgress_Multiple(); var panel = GameObject.FindObjectOfType(); var minigames = GameObject.FindObjectsOfType(); var currentMinigame = minigames[0]; currentMinigame.selectActivity.Invoke(); yield return new WaitForSeconds(WAIT_TIME); Assert.AreEqual(new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f), currentMinigame.GetComponent().color); for (int i = 1; i < minigames.Length; i++) Assert.AreEqual(Color.gray, minigames[i].GetComponent().color); Assert.AreEqual(currentMinigame.title.text, panel.minigameTitle.text); } }