using NUnit.Framework; using System.Collections; using System.IO; using UnityEditor; using UnityEngine; using UnityEngine.TestTools; /// /// Test the UserButton class /// public class UserButtonTests { /// /// Setup the environment before each test /// [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("Assets/Accounts/ScriptableObjects/UserAvatarList.asset").Awake(); SystemController.GetInstance().LoadNextScene("Common/Scenes/CoursesMenuScreen"); yield return new WaitForSeconds(0.2f); } /// /// Cleanup after testing /// [TearDown] public void TearDown_UserButtonTests() { PersistentDataController.PATH = null; } /// /// Test the ChangeUserCallback on the UserButton /// [UnityTest] public IEnumerator ChangeUserCallbackTest() { var userButton = GameObject.FindObjectOfType(); userButton.ChangeUserCallback(); yield return new WaitForSeconds(0.2f); var changeUserScreen = GameObject.FindObjectOfType(); Assert.IsNotNull(changeUserScreen); yield return null; } /// /// Test if the username and avatar are correctly loaded /// [UnityTest] public IEnumerator CorrectUsernameAndAvatarTest() { var userButton = GameObject.FindObjectOfType(); Assert.AreEqual("TEST", userButton.username.text, "Username is not correctly displayed."); Assert.IsNotNull(userButton.avatar.sprite, "Avatar sprite is null."); yield return null; } /// /// Test the transition to UserProgressScreen /// [UnityTest] public IEnumerator ChangeSceneToUserProgressScreenTest() { var userButton = GameObject.FindObjectOfType(); userButton.OpenProgressCallback(); yield return new WaitForSeconds(0.2f); var userProgressScreen = GameObject.FindObjectOfType(); Assert.IsNotNull(userProgressScreen, "Scene was not correctly changed to UserProgressScreen."); } /// /// Test is the user dropdown menu works correctly /// [UnityTest] public IEnumerator ToggleDropdownTest() { var userButton = GameObject.FindObjectOfType(); bool initialActiveState = userButton.dropdownBox.activeSelf; userButton.ToggleDropdown(); Assert.AreNotEqual(initialActiveState, userButton.dropdownBox.activeSelf, "Dropdown box active state was not toggled."); yield return null; } }