380 lines
14 KiB
C#
380 lines
14 KiB
C#
using NUnit.Framework;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.TestTools;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// Test the ChangeUserScreen class
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class ChangeUserScreenTests
|
|
{
|
|
/// <summary>
|
|
/// Wait time between scene transitions
|
|
/// </summary>
|
|
private const float WAIT_TIME = 0.2f;
|
|
|
|
/// <summary>
|
|
/// The default current user when dealing with multiple users
|
|
/// </summary>
|
|
private const int currentUser = 2;
|
|
|
|
/// <summary>
|
|
/// Setup the tests for a single user
|
|
/// </summary>
|
|
private IEnumerator Setup_ChangeUserScreen_SingleUser(string startScreen = "Accounts/Scenes/ChangeUserScreen")
|
|
{
|
|
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<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake();
|
|
|
|
SystemController.GetInstance().SwapScene(startScreen);
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setup the tests for a multiple users
|
|
/// </summary>
|
|
private IEnumerator Setup_ChangeUserScreen_MultipleUsers(string startScreen = "Accounts/Scenes/ChangeUserScreen")
|
|
{
|
|
string path = $"{Application.persistentDataPath}/wesign_unit_test.json";
|
|
string users = "";
|
|
for (int i = 0; i < 5; i++)
|
|
{
|
|
users += $"{{\"entries\":[],\"username\":\"Tester{i}\",\"avatarIndex\":{i},\"playtime\":0.0,\"minigames\":[],\"courses\":[]}}";
|
|
if (i < 4) users += ",";
|
|
}
|
|
const int currentUser = 2;
|
|
string fiveUsers = $"{{\"version\":1027,\"users\":[{users}],\"currentUser\":{currentUser},\"currentMinigame\":0,\"currentCourse\":0,\"currentTheme\":0}}";
|
|
|
|
File.WriteAllText(path, fiveUsers);
|
|
PersistentDataController.PATH = path;
|
|
PersistentDataController.GetInstance().Load();
|
|
AssetDatabase.LoadAssetAtPath<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake();
|
|
|
|
SystemController.GetInstance().SwapScene(startScreen);
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cleanup after testing
|
|
/// </summary>
|
|
[TearDown]
|
|
public void TearDown_ChangeUserScreen()
|
|
{
|
|
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_ChangeUserScreen_SingleUser();
|
|
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
Assert.IsNotNull(changeUserController);
|
|
Assert.IsNotNull(changeUserController.userPrefab);
|
|
Assert.IsNotNull(changeUserController.usersContainer);
|
|
Assert.IsNotNull(changeUserController.error);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether the screen is correctly initialized (one user)
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_Start_SingleUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_SingleUser();
|
|
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
Assert.IsFalse(changeUserController.error.activeSelf);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether the users is correctly displayed (one user)
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_DisplayUsers_SingleUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_SingleUser();
|
|
|
|
// The content of the usercard is tested in the `UserCardTests`-script
|
|
// Here, we only check the amount of users
|
|
Assert.AreEqual(1, UserList.GetUsers().Count);
|
|
var cards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
Assert.AreEqual(1, cards.Count);
|
|
Assert.AreEqual(new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f), cards[0].gameObject.GetComponent<Image>().color);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether the users are correctly displayed (mulitple users)
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_DisplayUsers_MultipleUsers()
|
|
{
|
|
yield return Setup_ChangeUserScreen_MultipleUsers();
|
|
|
|
// The content of the usercard is tested in the `UserCardTests`-script
|
|
// Here, we only check the amount of users and whether the correct one is selected
|
|
Assert.AreEqual(UserList.GetCurrentUser().GetUsername(), UserList.GetUserByIndex(currentUser).GetUsername());
|
|
var cards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
Assert.AreEqual(UserList.GetUsers().Count, cards.Count);
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, cards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether selecting a new user before the current one updates the display correctly
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_UpdateSelection_BeforeCurrentUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_MultipleUsers();
|
|
|
|
var cards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
|
|
// Before update
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, cards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
|
|
// Update
|
|
const int newUser = 1;
|
|
cards[newUser].selectUser.Invoke();
|
|
|
|
// After update
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
Color expected = i == newUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, cards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether selecting the current user for update the display will change nothing
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_UpdateSelection_CurrentUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_MultipleUsers();
|
|
|
|
var cards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
|
|
// Before update
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, cards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
|
|
// Update
|
|
cards[currentUser].selectUser.Invoke();
|
|
|
|
// After update
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, cards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether selecting a new user after the current one updates the display correctly
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_UpdateSelection_AfterCurrentUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_MultipleUsers();
|
|
|
|
var cards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
|
|
// Before update
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, cards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
|
|
// Update
|
|
const int newUser = 3;
|
|
cards[newUser].selectUser.Invoke();
|
|
|
|
// After update
|
|
for (int i = 0; i < cards.Count; i++)
|
|
{
|
|
Color expected = i == newUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, cards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test IChooseYou callback
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_IChooseYou()
|
|
{
|
|
yield return Setup_ChangeUserScreen_SingleUser("Common/Scenes/MainMenuScreen");
|
|
|
|
SystemController.GetInstance().LoadNextScene("Accounts/Scenes/ChangeUserScreen");
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
changeUserController.IChooseYou();
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
Assert.AreEqual(SystemController.GetSceneIndex("Common/Scenes/MainMenuScreen"), SystemController.GetInstance().currentScene);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the GotoUserCreation callback
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_GotoUserCreation()
|
|
{
|
|
yield return Setup_ChangeUserScreen_SingleUser();
|
|
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
changeUserController.GotoUserCreation();
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
Assert.IsNotNull(userCreationController);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the user cards DeleteUser callback
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_UserCardDeleteUser_BeforeCurrentUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_MultipleUsers();
|
|
|
|
// Before update
|
|
var oldCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
for (int i = 0; i < oldCards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, oldCards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
|
|
// Update
|
|
oldCards[1].DeleteUser();
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
Assert.IsFalse(changeUserController.error.activeSelf);
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
// After update
|
|
var newCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
Assert.AreEqual(oldCards.Count - 1, newCards.Count);
|
|
for (int i = 0; i < newCards.Count; i++)
|
|
{
|
|
Color expected = i == (currentUser - 1) ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, newCards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the user cards DeleteUser callback
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_UserCardDeleteUser_CurrentUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_MultipleUsers();
|
|
|
|
// Before update
|
|
var oldCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
for (int i = 0; i < oldCards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, oldCards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
|
|
// Update
|
|
oldCards[currentUser].DeleteUser();
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
Assert.IsFalse(changeUserController.error.activeSelf);
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
// After update
|
|
var newCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
Assert.AreEqual(oldCards.Count - 1, newCards.Count);
|
|
for (int i = 0; i < newCards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, newCards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the user cards DeleteUser callback
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_UserCardDeleteUser_AfterCurrentUser()
|
|
{
|
|
yield return Setup_ChangeUserScreen_MultipleUsers();
|
|
|
|
// Before update
|
|
var oldCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
for (int i = 0; i < oldCards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, oldCards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
|
|
// Update
|
|
oldCards[3].DeleteUser();
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
Assert.IsFalse(changeUserController.error.activeSelf);
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
// After update
|
|
var newCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
Assert.AreEqual(oldCards.Count - 1, newCards.Count);
|
|
for (int i = 0; i < newCards.Count; i++)
|
|
{
|
|
Color expected = i == currentUser ? new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f) : Color.gray;
|
|
Assert.AreEqual(expected, newCards[i].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test the user cards DeleteUser callback
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_UserCardDeleteUser_Invalid()
|
|
{
|
|
yield return Setup_ChangeUserScreen_SingleUser();
|
|
|
|
// Before update
|
|
var oldCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
Assert.AreEqual(1, oldCards.Count);
|
|
Assert.AreEqual(new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f), oldCards[0].gameObject.GetComponent<Image>().color);
|
|
|
|
// Update
|
|
oldCards[0].DeleteUser();
|
|
var changeUserController = GameObject.FindObjectOfType<ChangeUserScreen>();
|
|
Assert.IsTrue(changeUserController.error.activeSelf);
|
|
|
|
// After update
|
|
var newCards = GameObject.FindObjectsOfType<UserCard>().ToList();
|
|
Assert.AreEqual(1, newCards.Count);
|
|
Assert.AreEqual(new Color(66 / 255f, 158 / 255f, 189 / 255f, 1f), newCards[0].gameObject.GetComponent<Image>().color);
|
|
}
|
|
}
|