Files
unity-application/Assets/Accounts/Tests/PlayMode/UserProgressScreenTests.cs
2023-05-04 09:09:49 +00:00

132 lines
7.2 KiB
C#

using NUnit.Framework;
using System.Collections;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
/// <summary>
/// Test the UserProgressScreen class
/// </summary>
[TestFixture]
public class UserProgressScreenTests
{
/// <summary>
/// Wait time between scene transitions
/// </summary>
private const float WAIT_TIME = 0.2f;
/// <summary>
/// Setup the UserProgressScreen tests
/// </summary>
private IEnumerator Setup_UserProgressScreen(bool progress = true)
{
string path = $"{Application.persistentDataPath}/wesign_unit_test.json";
string course = "{\"entries\":[],\"courseIndex\":0,\"progress\":0.03846153989434242,\"completedLearnables\":1,\"inUseLearnables\":7,\"totalLearnables\":26,\"learnables\":[{\"entries\":[],\"index\":0,\"inUse\":true,\"name\":\"A\",\"progress\":3.0},{\"entries\":[],\"index\":1,\"inUse\":true,\"name\":\"B\",\"progress\":4.5},{\"entries\":[],\"index\":2,\"inUse\":true,\"name\":\"C\",\"progress\":1.5},{\"entries\":[],\"index\":3,\"inUse\":true,\"name\":\"D\",\"progress\":1.5},{\"entries\":[],\"index\":4,\"inUse\":true,\"name\":\"E\",\"progress\":1.5},{\"entries\":[],\"index\":5,\"inUse\":true,\"name\":\"F\",\"progress\":1.5},{\"entries\":[],\"index\":6,\"inUse\":true,\"name\":\"G\",\"progress\":0.0},{\"entries\":[],\"index\":7,\"inUse\":false,\"name\":\"H\",\"progress\":0.0},{\"entries\":[],\"index\":8,\"inUse\":false,\"name\":\"I\",\"progress\":0.0},{\"entries\":[],\"index\":9,\"inUse\":false,\"name\":\"J\",\"progress\":0.0},{\"entries\":[],\"index\":10,\"inUse\":false,\"name\":\"K\",\"progress\":0.0},{\"entries\":[],\"index\":11,\"inUse\":false,\"name\":\"L\",\"progress\":0.0},{\"entries\":[],\"index\":12,\"inUse\":false,\"name\":\"M\",\"progress\":0.0},{\"entries\":[],\"index\":13,\"inUse\":false,\"name\":\"N\",\"progress\":0.0},{\"entries\":[],\"index\":14,\"inUse\":false,\"name\":\"O\",\"progress\":0.0},{\"entries\":[],\"index\":15,\"inUse\":false,\"name\":\"P\",\"progress\":0.0},{\"entries\":[],\"index\":16,\"inUse\":false,\"name\":\"Q\",\"progress\":0.0},{\"entries\":[],\"index\":17,\"inUse\":false,\"name\":\"R\",\"progress\":0.0},{\"entries\":[],\"index\":18,\"inUse\":false,\"name\":\"S\",\"progress\":0.0},{\"entries\":[],\"index\":19,\"inUse\":false,\"name\":\"T\",\"progress\":0.0},{\"entries\":[],\"index\":20,\"inUse\":false,\"name\":\"U\",\"progress\":0.0},{\"entries\":[],\"index\":21,\"inUse\":false,\"name\":\"V\",\"progress\":0.0},{\"entries\":[],\"index\":22,\"inUse\":false,\"name\":\"W\",\"progress\":0.0},{\"entries\":[],\"index\":23,\"inUse\":false,\"name\":\"X\",\"progress\":0.0},{\"entries\":[],\"index\":24,\"inUse\":false,\"name\":\"Y\",\"progress\":0.0},{\"entries\":[],\"index\":25,\"inUse\":false,\"name\":\"Z\",\"progress\":0.0}]}";
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\"}]}";
if (!progress) course = minigame = "";
string oneUser = $"{{\"version\":1027,\"users\":[{{\"entries\":[],\"username\":\"Tester0\",\"avatarIndex\":0,\"playtime\":0.0,\"minigames\":[{minigame}],\"courses\":[{course}]}}],\"currentUser\":0,\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
File.WriteAllText(path, oneUser);
PersistentDataController.PATH = path;
PersistentDataController.GetInstance().Load();
AssetDatabase.LoadAssetAtPath<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake();
SystemController.GetInstance().SwapScene("Accounts/Scenes/UserProgressScreen");
yield return new WaitForSeconds(WAIT_TIME);
}
/// <summary>
/// Cleanup after testing
/// </summary>
[TearDown]
public void TearDown_UserProgressScreen()
{
PersistentDataController.PATH = null;
}
/// <summary>
/// Test whether every item that needs to be assign in the editor, is assigned
/// </summary>
[UnityTest]
public IEnumerator Test_EditorAssignments()
{
yield return Setup_UserProgressScreen();
var userProgressController = GameObject.FindObjectOfType<UserProgressScreen>();
Assert.IsNotNull(userProgressController.username);
Assert.IsNotNull(userProgressController.avatar);
Assert.IsNotNull(userProgressController.coursesPanel);
Assert.IsNotNull(userProgressController.coursesTabButton);
Assert.IsNotNull(userProgressController.minigamesPanel);
Assert.IsNotNull(userProgressController.minigamesTabButton);
}
/// <summary>
/// Test whether the screen is correctly initialized
/// </summary>
[UnityTest]
public IEnumerator Test_Start_NoProgress()
{
yield return Setup_UserProgressScreen(false);
var userProgressController = GameObject.FindObjectOfType<UserProgressScreen>();
var user = UserList.GetCurrentUser();
Assert.AreEqual(user.GetUsername(), userProgressController.username.text);
Assert.AreEqual(user.GetAvatar(), userProgressController.avatar.sprite);
Assert.IsTrue(userProgressController.coursesPanel.activeSelf);
Assert.Zero(GameObject.FindObjectsOfType<CourseProgressCard>().Length);
Assert.IsFalse(userProgressController.minigamesPanel.activeSelf);
}
/// <summary>
/// Test whether the screen is correctly initialized
/// </summary>
[UnityTest]
public IEnumerator Test_Start_WithProgress()
{
yield return Setup_UserProgressScreen();
var userProgressController = GameObject.FindObjectOfType<UserProgressScreen>();
var user = UserList.GetCurrentUser();
Assert.AreEqual(user.GetUsername(), userProgressController.username.text);
Assert.AreEqual(user.GetAvatar(), userProgressController.avatar.sprite);
Assert.IsTrue(userProgressController.coursesPanel.activeSelf);
var courses = GameObject.FindObjectsOfType<CourseProgressCard>().ToList();
Assert.AreEqual(user.GetCourses().Count, courses.Count);
Assert.IsFalse(userProgressController.minigamesPanel.activeSelf);
}
[UnityTest]
public IEnumerator Test_DisplayCourses()
{
yield return Setup_UserProgressScreen();
var userProgressController = GameObject.FindObjectOfType<UserProgressScreen>();
userProgressController.DisplayCourses();
yield return new WaitForSeconds(WAIT_TIME);
Assert.IsTrue(userProgressController.coursesPanel.activeSelf);
Assert.IsFalse(userProgressController.minigamesPanel.activeSelf);
}
[UnityTest]
public IEnumerator Test_DisplayMinigames()
{
yield return Setup_UserProgressScreen();
var userProgressController = GameObject.FindObjectOfType<UserProgressScreen>();
userProgressController.DisplayMinigames();
yield return new WaitForSeconds(WAIT_TIME);
Assert.IsFalse(userProgressController.coursesPanel.activeSelf);
Assert.IsTrue(userProgressController.minigamesPanel.activeSelf);
}
}