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

217 lines
8.9 KiB
C#

using NUnit.Framework;
using System.Collections;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
/// <summary>
/// Test the SpellingBeeController class
/// </summary>
public class SpellingBeeControllerTests
{
/// <summary>
/// Setup for testing Spelling Bee
/// </summary>
[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<UserAvatarList>("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<ListMinigamesScreen>();
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<SignPredictor>();
GameObject signPredictorController = signPredictor.gameObject;
signPredictorController.SetActive(false);
yield return new WaitForSeconds(0.2f);
}
/// <summary>
/// Tests the calculations of the score
/// </summary>
[UnityTest]
public IEnumerator CheckScoreTest()
{
SpellingBeeController spellingBeeController = GameObject.FindObjectOfType<SpellingBeeController>();
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());
}
/// <summary>
/// Tests the ending panel
/// </summary>
[UnityTest]
public IEnumerator ActivateGameOverTest()
{
SpellingBeeController spellingBeeController = GameObject.FindObjectOfType<SpellingBeeController>();
spellingBeeController.ActivateEnd(false);
yield return new WaitForSeconds(0.2f);
SpellingBeeGameEndedPanel SpellingBeeGameEndedPanel = GameObject.FindObjectOfType<SpellingBeeGameEndedPanel>();
Assert.NotNull(SpellingBeeGameEndedPanel);
Assert.AreEqual("VERLOREN", SpellingBeeGameEndedPanel.endText.text);
}
/// <summary>
/// Tests the scoreboard in case of completion
/// </summary>
[UnityTest]
public IEnumerator ActivateWinTests()
{
SpellingBeeController spellingBeeController = GameObject.FindObjectOfType<SpellingBeeController>();
spellingBeeController.ActivateEnd(true);
yield return new WaitForSeconds(0.2f);
SpellingBeeGameEndedPanel SpellingBeeGameEndedPanel = GameObject.FindObjectOfType<SpellingBeeGameEndedPanel>();
Assert.NotNull(SpellingBeeGameEndedPanel);
Assert.AreEqual("GEWONNEN", SpellingBeeGameEndedPanel.endText.text);
}
/// <summary>
/// Tests the scoreboard when timer goes off
/// </summary>
[UnityTest]
public IEnumerator CheckGameOverTest()
{
SpellingBeeController spellingBeeController = GameObject.FindObjectOfType<SpellingBeeController>();
spellingBeeController.AddSeconds(-60);
yield return new WaitForSeconds(0.1f);
SpellingBeeGameEndedPanel SpellingBeeGameEndedPanel = GameObject.FindObjectOfType<SpellingBeeGameEndedPanel>();
Assert.NotNull(SpellingBeeGameEndedPanel);
Assert.AreEqual("VERLOREN", SpellingBeeGameEndedPanel.endText.text);
}
/// <summary>
/// Tests the completion from a word and from the entire theme + scoreboard
/// </summary>
[UnityTest]
public IEnumerator CheckGoToNextWord()
{
SpellingBeeController spellingBeeController = GameObject.FindObjectOfType<SpellingBeeController>();
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<SpellingBeeGameEndedPanel>();
Assert.NotNull(SpellingBeeGameEndedPanel);
Assert.AreEqual("GEWONNEN", SpellingBeeGameEndedPanel.endText.text);
}
/// <summary>
/// Tests the feedback in the game
/// </summary>
[UnityTest]
public IEnumerator FeedbackTest()
{
SpellingBeeController spellingBeeController = GameObject.FindObjectOfType<SpellingBeeController>();
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);
}
}
/// <summary>
/// Cleanup after testing
/// </summary>
[TearDown]
public void TearDown_SpellingBeeTests()
{
PersistentDataController.PATH = null;
}
}