174 lines
6.8 KiB
C#
174 lines
6.8 KiB
C#
using NUnit.Framework;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.TestTools;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// Test the UserCreationScreen class
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class UserCreationScreenTests
|
|
{
|
|
/// <summary>
|
|
/// Wait time between scene transitions
|
|
/// </summary>
|
|
private const float WAIT_TIME = 0.2f;
|
|
|
|
/// <summary>
|
|
/// Setup the UserCreationScreen tests
|
|
/// </summary>
|
|
private IEnumerator Setup_UserCreationScreen(string startScreen = "Accounts/Scenes/UserCreationScreen")
|
|
{
|
|
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>
|
|
/// Cleanup after testing
|
|
/// </summary>
|
|
[TearDown]
|
|
public void TearDown_UserCreationScreen()
|
|
{
|
|
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_UserCreationScreen();
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
Assert.IsNotNull(userCreationController);
|
|
Assert.IsNotNull(userCreationController.errorMessage);
|
|
Assert.IsNotNull(userCreationController.inputName);
|
|
Assert.IsNotNull(userCreationController.avatarsContainer);
|
|
Assert.IsNotNull(userCreationController.avatarPrefab);
|
|
Assert.IsNotNull(userCreationController.backButton);
|
|
Assert.IsTrue(UserCreationScreen.canGoBack);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether the screen is correctly initialized
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_Start()
|
|
{
|
|
yield return Setup_UserCreationScreen();
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
Assert.IsFalse(userCreationController.errorMessage.activeSelf);
|
|
Assert.IsTrue(userCreationController.backButton.activeSelf);
|
|
|
|
var avatars = userCreationController.avatarsContainer.GetComponentsInChildren<Button>().ToList()
|
|
.ConvertAll((b) => b.GetComponentsInChildren<Image>().Except(b.GetComponents<Image>()).First());
|
|
|
|
Assert.AreEqual(UserList.AVATARS.Count, avatars.Count);
|
|
for (int i = 0; i < avatars.Count; i++)
|
|
Assert.AreEqual(UserList.AVATARS[i], avatars[i].sprite);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether the screen is correctly initialized (from invalid savefile)
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_Start_InvalidSavefile()
|
|
{
|
|
string path = $"{Application.persistentDataPath}/wesign_unit_test.json";
|
|
File.Delete(path);
|
|
PersistentDataController.PATH = path;
|
|
PersistentDataController.GetInstance().Load();
|
|
AssetDatabase.LoadAssetAtPath<UserAvatarList>("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake();
|
|
|
|
SystemController.GetInstance().SwapScene("Common/Scenes/Boot");
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
Assert.IsNotNull(userCreationController);
|
|
Assert.IsFalse(userCreationController.backButton.activeSelf);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether a new user can be created
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_CreateUser_Valid()
|
|
{
|
|
yield return Setup_UserCreationScreen("Common/Scenes/MainMenuScreen");
|
|
|
|
SystemController.GetInstance().LoadNextScene("Accounts/Scenes/UserCreationScreen");
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
userCreationController.inputName.text = "newTester";
|
|
userCreationController.CreateUser();
|
|
Assert.IsFalse(userCreationController.errorMessage.activeSelf);
|
|
yield return new WaitForSeconds(WAIT_TIME);
|
|
|
|
Assert.AreEqual(SystemController.GetSceneIndex("Common/Scenes/MainMenuScreen"), SystemController.GetInstance().currentScene);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test whether no new user is created when the username is too long
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_CreateUser_UsernameTooLong()
|
|
{
|
|
yield return Setup_UserCreationScreen();
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
userCreationController.inputName.text = "aninvalidsuperduperlongusername";
|
|
userCreationController.CreateUser();
|
|
Assert.IsTrue(userCreationController.errorMessage.activeSelf);
|
|
Assert.AreEqual("Je gebruikersnaam moet bestaan uit minimum 1 en maximum 12 letters (a-z en A-Z) of cijfers (0-9).", userCreationController.errorMessage.GetComponent<TMP_Text>().text);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Test whether no new user is created when the username contains invalid characters
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_CreateUser_UsernameInvalidChars()
|
|
{
|
|
yield return Setup_UserCreationScreen();
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
userCreationController.inputName.text = "! an invalid username !";
|
|
userCreationController.CreateUser();
|
|
Assert.IsTrue(userCreationController.errorMessage.activeSelf);
|
|
Assert.AreEqual("Je gebruikersnaam moet bestaan uit minimum 1 en maximum 12 letters (a-z en A-Z) of cijfers (0-9).", userCreationController.errorMessage.GetComponent<TMP_Text>().text);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Test whether no new user is created when the username already exists
|
|
/// </summary>
|
|
[UnityTest]
|
|
public IEnumerator Test_CreateUser_UsernameAlreadyExists()
|
|
{
|
|
yield return Setup_UserCreationScreen();
|
|
|
|
var userCreationController = GameObject.FindObjectOfType<UserCreationScreen>();
|
|
userCreationController.inputName.text = "Tester0";
|
|
userCreationController.CreateUser();
|
|
Assert.IsTrue(userCreationController.errorMessage.activeSelf);
|
|
Assert.AreEqual("Deze gebruikersnaam bestaat al! Kies een andere.", userCreationController.errorMessage.GetComponent<TMP_Text>().text);
|
|
}
|
|
}
|
|
|