using NUnit.Framework; using System.Collections; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; using UnityEngine.TestTools; /// /// Test the SpellingBeeController class /// public class SpellingBeeControllerTests { /// /// Setup for testing Spelling Bee /// [UnitySetUp] public IEnumerator SetupFunction() { string path = $"{Application.persistentDataPath}/wesign_unit_test.json"; string oneUser = $"{{\"version\":1537,\"users\":[{{\"entries\":[],\"username\":\"TEST\",\"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(); // Go to the minigame-selection scene to make sure that the minigameIndex is set correctly SystemController.GetInstance().LoadNextScene("Common/Scenes/ListMinigamesScreen"); yield return new WaitForSeconds(0.2f); ListMinigamesScreen minigameScreen = GameObject.FindObjectOfType(); minigameScreen.minigameList.SetCurrentMinigame(MinigameIndex.SPELLING_BEE); SystemController.GetInstance().LoadNextScene("SpellingBee/Scenes/SpellingBeeGame"); yield return new WaitForSeconds(0.2f); // Fetch the SignPredictor and deactivate it, this stops the basic game-loop from happening var signPredictor = GameObject.FindObjectOfType(); GameObject signPredictorController = signPredictor.gameObject; signPredictorController.SetActive(false); yield return new WaitForSeconds(0.2f); } /// /// Tests the calculations of the score /// [UnityTest] public IEnumerator CheckScoreTest() { SpellingBeeController spellingBeeController = GameObject.FindObjectOfType(); yield return new WaitForSeconds(0.2f); Assert.AreEqual(0, spellingBeeController.CalculateScore()); spellingBeeController.NextWord(); Assert.AreEqual(0, spellingBeeController.CalculateScore()); spellingBeeController.NextLetter(true); Assert.AreEqual(10, spellingBeeController.CalculateScore()); } /// /// Tests the ending panel /// [UnityTest] public IEnumerator ActivateGameOverTest() { SpellingBeeController spellingBeeController = GameObject.FindObjectOfType(); spellingBeeController.ActivateEnd(false); yield return new WaitForSeconds(0.2f); SpellingBeeGameEndedPanel SpellingBeeGameEndedPanel = GameObject.FindObjectOfType(); Assert.NotNull(SpellingBeeGameEndedPanel); Assert.AreEqual("VERLOREN", SpellingBeeGameEndedPanel.endText.text); } /// /// Tests the scoreboard in case of completion /// [UnityTest] public IEnumerator ActivateWinTests() { SpellingBeeController spellingBeeController = GameObject.FindObjectOfType(); spellingBeeController.ActivateEnd(true); yield return new WaitForSeconds(0.2f); SpellingBeeGameEndedPanel SpellingBeeGameEndedPanel = GameObject.FindObjectOfType(); Assert.NotNull(SpellingBeeGameEndedPanel); Assert.AreEqual("GEWONNEN", SpellingBeeGameEndedPanel.endText.text); } /// /// Tests the scoreboard when timer goes off /// [UnityTest] public IEnumerator CheckGameOverTest() { SpellingBeeController spellingBeeController = GameObject.FindObjectOfType(); spellingBeeController.AddSeconds(-60); yield return new WaitForSeconds(0.1f); SpellingBeeGameEndedPanel SpellingBeeGameEndedPanel = GameObject.FindObjectOfType(); Assert.NotNull(SpellingBeeGameEndedPanel); Assert.AreEqual("VERLOREN", SpellingBeeGameEndedPanel.endText.text); } /// /// Tests the completion from a word and from the entire theme + scoreboard /// [UnityTest] public IEnumerator CheckGoToNextWord() { SpellingBeeController spellingBeeController = GameObject.FindObjectOfType(); var letters = spellingBeeController.SkipToEnd().ToList().Where((c) => c != ' '); spellingBeeController.PredictSign(spellingBeeController.GetSign()); yield return new WaitForSeconds(0.2f); Assert.IsTrue(spellingBeeController.bonusTimeText.activeSelf); yield return new WaitForSeconds(1.0f); Assert.IsFalse(spellingBeeController.bonusTimeText.activeSelf); foreach (var _ in letters) { spellingBeeController.NextLetter(false); } spellingBeeController.NextWord(); yield return new WaitForSeconds(0.2f); SpellingBeeGameEndedPanel SpellingBeeGameEndedPanel = GameObject.FindObjectOfType(); Assert.NotNull(SpellingBeeGameEndedPanel); Assert.AreEqual("GEWONNEN", SpellingBeeGameEndedPanel.endText.text); } /// /// Tests the feedback in the game /// [UnityTest] public IEnumerator FeedbackTest() { SpellingBeeController spellingBeeController = GameObject.FindObjectOfType(); string sign = spellingBeeController.GetSign(); float threshold = spellingBeeController.GetThreshold(sign); spellingBeeController.ProcessCurrentAndPredicted(0.9f * threshold, sign, 0.9f * threshold, sign); yield return new WaitForSeconds(0.2f); Assert.AreEqual("Goed", spellingBeeController.feedbackText.text); Assert.AreEqual(new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f), spellingBeeController.feedbackText.color); Assert.AreEqual(new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f), spellingBeeController.scoreBonus.color); yield return new WaitForSeconds(0.2f); if (sign != spellingBeeController.GetSign()) { string expected = spellingBeeController.GetSign(); float distance = 3.0f * spellingBeeController.GetThreshold(expected); spellingBeeController.ProcessCurrentAndPredicted(0.9f * threshold, sign, distance, expected); yield return new WaitForSeconds(2.5f); Assert.AreEqual($"Verkeerde gebaar: '{sign}'", spellingBeeController.feedbackText.text); Assert.AreEqual(new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f), spellingBeeController.feedbackText.color); spellingBeeController.ProcessCurrentAndPredicted(0.9f * threshold, sign, distance, expected); Assert.AreEqual(new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f), spellingBeeController.scoreBonus.color); } yield return new WaitForSeconds(0.2f); sign = spellingBeeController.GetSign(); threshold = spellingBeeController.GetThreshold(sign); spellingBeeController.ProcessCurrentAndPredicted(1.2f * threshold, sign, 1.2f * threshold, sign); yield return new WaitForSeconds(0.2f); Assert.AreEqual("Bijna...", spellingBeeController.feedbackText.text); Assert.AreEqual(new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f), spellingBeeController.feedbackText.color); spellingBeeController.ProcessCurrentAndPredicted(3.0f * threshold, sign, 3.0f * threshold, sign); yield return new WaitForSeconds(0.2f); Assert.AreEqual("Detecteren...", spellingBeeController.feedbackText.text); Assert.AreEqual(new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f), spellingBeeController.feedbackText.color); if (sign != spellingBeeController.GetSign()) { string expected = spellingBeeController.GetSign(); float distance = 3.0f * spellingBeeController.GetThreshold(expected); spellingBeeController.ProcessCurrentAndPredicted(0.9f * threshold, sign, distance, expected); yield return new WaitForSeconds(2.5f); Assert.AreEqual($"Verkeerde gebaar: '{sign}'", spellingBeeController.feedbackText.text); Assert.AreEqual(new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f), spellingBeeController.feedbackText.color); spellingBeeController.ProcessCurrentAndPredicted(0.9f * threshold, sign, distance, expected); Assert.AreEqual(new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f), spellingBeeController.scoreBonus.color); } } /// /// Cleanup after testing /// [TearDown] public void TearDown_SpellingBeeTests() { PersistentDataController.PATH = null; } }