Resolve WES-187 "Unit tests justsign and spellingbee"

This commit is contained in:
Helena Van Breugel
2023-05-14 11:58:09 +00:00
committed by Dries Van Schuylenbergh
parent 1e09f09dae
commit 53fc361af4
11 changed files with 655 additions and 80 deletions

View File

@@ -276,6 +276,7 @@ public class JustSignController : AbstractMinigameController
DestroySymbolAt(0);
incorrectSigns++;
timingFeedback.text = $"Te laat! \n {offscreenScore}";
timingFeedback.color = new Color(0x10 / 255.0f, 0x10 / 255.0f, 0x10 / 255.0f);
imageFeedback.sprite = tooLateSprite;
}
}
@@ -360,25 +361,36 @@ public class JustSignController : AbstractMinigameController
activeSymbols.Add(newSymbolObject);
}
/// <summary>
/// Get the threshold for a given sign
/// </summary>
/// <param name="sign"></param>
/// <returns></returns>
public float GetThreshold(string sign)
{
Learnable predSign = currentTheme.learnables.Find(l => l.name.ToUpper().Replace(" ", "-") == sign);
return predSign.thresholdDistance;
}
/// <summary>
/// The logic to process the signs sent by the signPredictor
/// </summary>
/// <param name="accuracy">The accuracy of the passed sign</param>
/// <param name="predictedSign">The name of the passed sign</param>
protected override void ProcessMostProbableSign(float distance, string predictedSign)
public override void ProcessMostProbableSign(float distance, string predictedSign)
{
Learnable predSign = currentTheme.learnables.Find(l => l.name.ToUpper().Replace(" ", "-") == predictedSign);
//Learnable predSign = currentTheme.learnables.Find(l => l.name == predictedSign);
float threshold = GetThreshold(predictedSign);
// If there is a feedback-object, we wil change its appearance
if (feedbackText != null && feedbackProgressImage != null)
{
Color col;
if (distance < predSign.thresholdDistance)
if (distance < threshold)
{
feedbackText.text = $"Herkent '{predictedSign}'";
col = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (distance < 1.5 * predSign.thresholdDistance)
else if (distance < 1.5 * threshold)
{
feedbackText.text = $"Lijkt op '{predictedSign}'";
col = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
@@ -394,7 +406,7 @@ public class JustSignController : AbstractMinigameController
float oldValue = feedbackProgress.value;
// use an exponential scale
float newValue = 1 - Mathf.Clamp(distance - predSign.thresholdDistance, 0.0f, 3.0f) / 3;
float newValue = 1 - Mathf.Clamp(distance - threshold, 0.0f, 3.0f) / 3;
feedbackProgress.gameObject.Tween("FeedbackUpdate", oldValue, newValue, 0.2f, TweenScaleFunctions.CubicEaseInOut, (t) =>
{
if (feedbackProgress != null)
@@ -405,59 +417,68 @@ public class JustSignController : AbstractMinigameController
}
// The logic for the internal workings of the game
if (distance < predSign.thresholdDistance)
if (distance < threshold)
{
int matchedSymbolIndex = activeWords.IndexOf(predictedSign.ToUpper());
// Destroy the oldest symbol if the current input matches it
if (0 <= matchedSymbolIndex)
{
float x = activeSymbols[matchedSymbolIndex].transform.localPosition.x;
// parameters to define the Perfect hit zone
float perfectRange = hitZonePerfect.sizeDelta.x;
float perfectCenter = hitZonePerfect.localPosition.x;
// parameters to define the Good hit zone
float goodRange = hitZoneGood.sizeDelta.x;
float goodCenter = hitZoneGood.localPosition.x;
// parameters to define the Meh hit zone
float mehRange = hitZoneMeh.sizeDelta.x;
float mehCenter = hitZoneMeh.localPosition.x;
if (perfectCenter - perfectRange / 2 <= x && x <= perfectCenter + perfectRange / 2)
{
timingFeedback.text = $"Perfect! \n +{perfectScore}";
imageFeedback.sprite = perfectSprite;
perfectSigns++;
timingFeedback.color = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (goodCenter - goodRange / 2 <= x && x <= goodCenter + goodRange / 2)
{
timingFeedback.text = $"Goed \n +{goodScore}";
imageFeedback.sprite = goodSprite;
goodSigns++;
timingFeedback.color = new Color(0xf7 / 255.0f, 0xad / 255.0f, 0x19 / 255.0f);
}
else if (mehCenter - mehRange / 2 <= x && x <= mehCenter + mehRange / 2)
{
timingFeedback.text = $"Bijna... \n +{mehScore}";
imageFeedback.sprite = mehSprite;
mehSigns++;
timingFeedback.color = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else
{
timingFeedback.text = $"Te vroeg! \n {terribleScore}";
imageFeedback.sprite = terribleSprite;
terribleSigns++;
timingFeedback.color = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
DestroySymbolAt(matchedSymbolIndex);
MatchedSymbol(matchedSymbolIndex);
}
}
}
/// <summary>
/// The logic to process a correct sign within the zones in the game
/// </summary>
/// <param name="index"></param>
void MatchedSymbol(int index)
{
float x = activeSymbols[index].transform.localPosition.x;
// parameters to define the Perfect hit zone
float perfectRange = hitZonePerfect.sizeDelta.x;
float perfectCenter = hitZonePerfect.localPosition.x;
// parameters to define the Good hit zone
float goodRange = hitZoneGood.sizeDelta.x;
float goodCenter = hitZoneGood.localPosition.x;
// parameters to define the Meh hit zone
float mehRange = hitZoneMeh.sizeDelta.x;
float mehCenter = hitZoneMeh.localPosition.x;
if (perfectCenter - perfectRange / 2 <= x && x <= perfectCenter + perfectRange / 2)
{
timingFeedback.text = $"Perfect! \n +{perfectScore}";
imageFeedback.sprite = perfectSprite;
perfectSigns++;
timingFeedback.color = new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f);
}
else if (goodCenter - goodRange / 2 <= x && x <= goodCenter + goodRange / 2)
{
timingFeedback.text = $"Goed \n +{goodScore}";
imageFeedback.sprite = goodSprite;
goodSigns++;
timingFeedback.color = new Color(0xf7 / 255.0f, 0xad / 255.0f, 0x19 / 255.0f);
}
else if (mehCenter - mehRange / 2 <= x && x <= mehCenter + mehRange / 2)
{
timingFeedback.text = $"Bijna... \n +{mehScore}";
imageFeedback.sprite = mehSprite;
mehSigns++;
timingFeedback.color = new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f);
}
else
{
timingFeedback.text = $"Te vroeg! \n {terribleScore}";
imageFeedback.sprite = terribleSprite;
terribleSigns++;
timingFeedback.color = new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f);
}
DestroySymbolAt(index);
}
/// <summary>
/// The logic to set the scoreboard of justsign
/// </summary>
@@ -517,4 +538,18 @@ public class JustSignController : AbstractMinigameController
DestroySymbolAt(0);
}
}
/// <summary>
/// Get sign for the first active symbol
/// </summary>
/// <returns></returns>
public string GetFirstSign()
{
if (activeSymbols.Count > 0)
{
TMP_Text text = activeSymbols[0].GetComponentInChildren<TMP_Text>();
return text.text;
}
return null;
}
}

View File

@@ -0,0 +1,342 @@
using NUnit.Framework;
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
public class JustSignControllerTests
{
/// <summary>
/// Setup for testing Just Sign
/// </summary>
/// <returns></returns>
[UnitySetUp]
public IEnumerator SetupFunction()
{
string path = $"{Application.persistentDataPath}/wesign_unit_test.json";
string oneUser = $"{{\"version\":1027,\"users\":[{{\"entries\":[],\"username\":\"TEST\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[],\"courses\":[]}}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
File.WriteAllText(path, oneUser);
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.JUST_SIGN);
SystemController.GetInstance().LoadNextScene("JustSign/Scenes/JustSignGame");
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 a sign that is done too quickly, i.e., feedback during game and scoreboard after
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator EarlyScoreTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(5.0f);
string currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
yield return new WaitForSeconds(0.2f);
Assert.AreEqual("Te vroeg! \n -3", justSignController.timingFeedback.text);
Assert.AreEqual(-3, justSignController.CalculateScore());
Assert.AreEqual("Score: -3", justSignController.scoreDisplay.text);
justSignController.ActivateEnd(true);
yield return new WaitForSeconds(0.2f);
JustSignGameEndedPanel justSignGameEndedPanel = GameObject.FindObjectOfType<JustSignGameEndedPanel>();
Assert.NotNull(justSignGameEndedPanel);
Assert.AreEqual("0", justSignGameEndedPanel.perfectSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.goodSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.mehSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.terribleSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.notFoundSignsText.text);
Assert.AreEqual("Score: -3", justSignGameEndedPanel.scoreText.text);
string gpmText = (60f / 64).ToString("#") + " GPM";
Assert.AreEqual(gpmText, justSignGameEndedPanel.gpmText.text);
}
/// <summary>
/// Tests a sign that is done too late, i.e., feedback during game and scoreboard after
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator TooLateScoreTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(23.0f);
Assert.AreEqual("Te laat! \n -5", justSignController.timingFeedback.text);
Assert.AreEqual(-5, justSignController.CalculateScore());
Assert.AreEqual("Score: -5", justSignController.scoreDisplay.text);
justSignController.ActivateEnd(true);
yield return new WaitForSeconds(0.2f);
JustSignGameEndedPanel justSignGameEndedPanel = GameObject.FindObjectOfType<JustSignGameEndedPanel>();
Assert.NotNull(justSignGameEndedPanel);
Assert.AreEqual("0", justSignGameEndedPanel.perfectSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.goodSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.mehSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.terribleSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.notFoundSignsText.text);
Assert.AreEqual("Score: -5", justSignGameEndedPanel.scoreText.text);
string gpmText = (0).ToString("#") + " GPM";
Assert.AreEqual(gpmText, justSignGameEndedPanel.gpmText.text);
}
/// <summary>
/// Tests a sign that is done in the meh-zone, i.e., feedback during game and scoreboard after
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator MehScoreTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(14.5f);
string currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
yield return new WaitForSeconds(0.2f);
Assert.AreEqual("Bijna... \n +10", justSignController.timingFeedback.text);
Assert.AreEqual(10, justSignController.CalculateScore());
Assert.AreEqual("Score: 10", justSignController.scoreDisplay.text);
justSignController.ActivateEnd(true);
yield return new WaitForSeconds(0.2f);
JustSignGameEndedPanel justSignGameEndedPanel = GameObject.FindObjectOfType<JustSignGameEndedPanel>();
Assert.NotNull(justSignGameEndedPanel);
Assert.AreEqual("0", justSignGameEndedPanel.perfectSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.goodSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.mehSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.terribleSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.notFoundSignsText.text);
Assert.AreEqual("Score: 10", justSignGameEndedPanel.scoreText.text);
string gpmText = (60f / 64).ToString("#") + " GPM";
Assert.AreEqual(gpmText, justSignGameEndedPanel.gpmText.text);
}
/// <summary>
/// Tests a sign that is done in the good-zone, i.e., feedback during game and scoreboard after
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator GoodScoreTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(17.5f);
string currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
yield return new WaitForSeconds(0.2f);
Assert.AreEqual("Goed \n +20", justSignController.timingFeedback.text);
Assert.AreEqual(20, justSignController.CalculateScore());
Assert.AreEqual("Score: 20", justSignController.scoreDisplay.text);
justSignController.ActivateEnd(true);
yield return new WaitForSeconds(0.2f);
JustSignGameEndedPanel justSignGameEndedPanel = GameObject.FindObjectOfType<JustSignGameEndedPanel>();
Assert.NotNull(justSignGameEndedPanel);
Assert.AreEqual("0", justSignGameEndedPanel.perfectSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.goodSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.mehSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.terribleSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.notFoundSignsText.text);
Assert.AreEqual("Score: 20", justSignGameEndedPanel.scoreText.text);
string gpmText = (60f / 64).ToString("#") + " GPM";
Assert.AreEqual(gpmText, justSignGameEndedPanel.gpmText.text);
}
/// <summary>
/// Tests a sign that is done in the perfect-zone, i.e., feedback during game and scoreboard after
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator PerfectScoreTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(19.0f);
string currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
yield return new WaitForSeconds(0.2f);
Assert.AreEqual("Perfect! \n +50", justSignController.timingFeedback.text);
Assert.AreEqual(50, justSignController.CalculateScore());
Assert.AreEqual("Score: 50", justSignController.scoreDisplay.text);
justSignController.ActivateEnd(true);
yield return new WaitForSeconds(0.2f);
JustSignGameEndedPanel justSignGameEndedPanel = GameObject.FindObjectOfType<JustSignGameEndedPanel>();
Assert.NotNull(justSignGameEndedPanel);
Assert.AreEqual("1", justSignGameEndedPanel.perfectSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.goodSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.mehSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.terribleSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.notFoundSignsText.text);
Assert.AreEqual("Score: 50", justSignGameEndedPanel.scoreText.text);
string gpmText = (60f / 64).ToString("#") + " GPM";
Assert.AreEqual(gpmText, justSignGameEndedPanel.gpmText.text);
}
/// <summary>
/// Tests the entire game, i.e., feedback during game and scoreboard after
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator WalkthroughTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(10.0f);
string currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
Assert.AreEqual(justSignController.CalculateScore(), -3);
yield return new WaitForSeconds(10.0f);
currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
Assert.AreEqual(justSignController.CalculateScore(), 7);
yield return new WaitForSeconds(6.0f);
currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
Assert.AreEqual(justSignController.CalculateScore(), 27);
yield return new WaitForSeconds(6.0f);
currentSign = justSignController.GetFirstSign();
justSignController.ProcessMostProbableSign(1.0f, currentSign);
Assert.AreEqual(justSignController.CalculateScore(), 77);
yield return new WaitForSeconds(10.0f);
Assert.AreEqual(justSignController.CalculateScore(), 72);
justSignController.ActivateEnd(true);
yield return new WaitForSeconds(0.2f);
JustSignGameEndedPanel justSignGameEndedPanel = GameObject.FindObjectOfType<JustSignGameEndedPanel>();
Assert.NotNull(justSignGameEndedPanel);
Assert.AreEqual("1", justSignGameEndedPanel.perfectSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.goodSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.mehSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.terribleSignsText.text);
Assert.AreEqual("1", justSignGameEndedPanel.notFoundSignsText.text);
Assert.AreEqual("Score: 72", justSignGameEndedPanel.scoreText.text);
string gpmText = (60f * 4/ 64).ToString("#") + " GPM";
Assert.AreEqual(gpmText, justSignGameEndedPanel.gpmText.text);
}
/// <summary>
/// Tests if the game ends when the song does
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator SongEndTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(0.2f);
Song song = justSignController.songList.songs[justSignController.songList.currentSongIndex];
song.duration = 1;
yield return new WaitForSeconds(1.2f);
JustSignGameEndedPanel justSignGameEndedPanel = GameObject.FindObjectOfType<JustSignGameEndedPanel>();
Assert.NotNull(justSignGameEndedPanel);
Assert.AreEqual("0", justSignGameEndedPanel.perfectSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.goodSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.mehSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.terribleSignsText.text);
Assert.AreEqual("0", justSignGameEndedPanel.notFoundSignsText.text);
Assert.AreEqual("Score: 0", justSignGameEndedPanel.scoreText.text);
string gpmText = (0).ToString("#") + " GPM";
Assert.AreEqual(gpmText, justSignGameEndedPanel.gpmText.text);
}
/// <summary>
/// Tests the feedback in the game
/// </summary>
/// <returns></returns>
[UnityTest]
public IEnumerator FeedbackTest()
{
JustSignController justSignController = GameObject.FindObjectOfType<JustSignController>();
yield return new WaitForSeconds(5.0f);
string currentSign = justSignController.GetFirstSign();
float threshold = justSignController.GetThreshold(currentSign);
justSignController.ProcessMostProbableSign(0.9f * threshold, currentSign);
yield return new WaitForSeconds(0.2f);
Assert.Null(justSignController.GetFirstSign());
Assert.AreEqual($"Herkent '{currentSign}'", justSignController.feedbackText.text);
Assert.AreEqual(new Color(0x8b / 255.0f, 0xd4 / 255.0f, 0x5e / 255.0f), justSignController.feedbackText.color);
yield return new WaitForSeconds(5.0f);
currentSign = justSignController.GetFirstSign();
threshold = justSignController.GetThreshold(currentSign);
justSignController.ProcessMostProbableSign(1.2f * threshold, currentSign);
yield return new WaitForSeconds(0.2f);
Assert.AreEqual($"Lijkt op '{currentSign}'", justSignController.feedbackText.text);
Assert.AreEqual(new Color(0xf2 / 255.0f, 0x7f / 255.0f, 0x0c / 255.0f), justSignController.feedbackText.color);
justSignController.ProcessMostProbableSign(3.0f * threshold, currentSign);
yield return new WaitForSeconds(0.2f);
Assert.AreEqual("Detecteren...", justSignController.feedbackText.text);
Assert.AreEqual(new Color(0xf5 / 255.0f, 0x49 / 255.0f, 0x3d / 255.0f), justSignController.feedbackText.color);
}
/// <summary>
/// Cleanup after testing
/// </summary>
[TearDown]
public void TearDown_JustSignTests()
{
PersistentDataController.PATH = null;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3d310f7a3879a54fa36ab0d2e981f79
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,7 +4,14 @@
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"JustSignScripts"
"AccountsScripts",
"JustSignScripts",
"SignPredictor",
"MinigameScripts",
"Unity.TextMeshPro",
"CommonScripts",
"InterfacesScripts",
"ArchitectureScripts"
],
"includePlatforms": [],
"excludePlatforms": [],