| | 1 | | using NUnit.Framework; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.TestTools; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEngine.SceneManagement; |
| | 6 | | using System.IO; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | public class ConfigTest : TestBase { |
| | 10 | | [OneTimeSetUp] |
| 0 | 11 | | public void LoadScene() { |
| 0 | 12 | | SceneManager.LoadScene("Scenes/MainScene"); |
| 0 | 13 | | } |
| | 14 | |
|
| | 15 | | [UnityTest] |
| 0 | 16 | | public IEnumerator TestAllConfigFilesLoad() { |
| 0 | 17 | | string configPath = Path.Combine(Application.streamingAssetsPath, "Configs"); |
| 0 | 18 | | string[] jsonFiles = Directory.GetFiles(configPath, "*.json"); |
| | 19 | |
|
| 0 | 20 | | Assert.IsTrue(jsonFiles.Length > 0, "No JSON files found in the Configs directory"); |
| | 21 | |
|
| 0 | 22 | | bool isPaused = false; |
| 0 | 23 | | double epsilon = 0.0002; |
| 0 | 24 | | for (int i = 0; i < jsonFiles.Length; i++) { |
| 0 | 25 | | if (i % 2 == 1) { |
| 0 | 26 | | SimManager.Instance.PauseSimulation(); |
| 0 | 27 | | isPaused = true; |
| 0 | 28 | | } |
| 0 | 29 | | yield return new WaitForSecondsRealtime(0.1f); |
| 0 | 30 | | SimManager.Instance.LoadNewConfig(jsonFiles[i]); |
| 0 | 31 | | yield return new WaitForSecondsRealtime(0.1f); |
| 0 | 32 | | double elapsedTime = SimManager.Instance.GetElapsedSimulationTime(); |
| 0 | 33 | | if (isPaused) { |
| 0 | 34 | | List<Agent> agents = SimManager.Instance.GetActiveAgents(); |
| 0 | 35 | | foreach (Agent agent in agents) { |
| 0 | 36 | | if (agent is Interceptor) { |
| | 37 | | // All interceptors start in INITIALIZED phase |
| 0 | 38 | | Assert.AreEqual( |
| | 39 | | Agent.FlightPhase.INITIALIZED, agent.GetFlightPhase(), |
| | 40 | | "All INTERCEPTOR agents should be in the INITIALIZED flight phase after loading while paused"); |
| | 41 | |
|
| 0 | 42 | | } else if (agent is Threat) { |
| | 43 | | // All threats start in MIDCOURSE phase |
| 0 | 44 | | Assert.AreEqual( |
| | 45 | | Agent.FlightPhase.MIDCOURSE, agent.GetFlightPhase(), |
| | 46 | | "All THREAT agents should be in the MIDCOURSE flight phase after loading while paused"); |
| 0 | 47 | | } |
| 0 | 48 | | } |
| 0 | 49 | | Assert.LessOrEqual(Mathf.Abs(Time.fixedDeltaTime), epsilon, |
| | 50 | | "Fixed delta time should be approximately 0 after loading while paused"); |
| 0 | 51 | | Assert.LessOrEqual(Mathf.Abs(Time.timeScale), epsilon, |
| | 52 | | "Time scale should be approximately 0 after loading while paused"); |
| 0 | 53 | | Assert.IsFalse(elapsedTime > 0 + epsilon, |
| | 54 | | "Simulation time should not have advanced after loading while paused"); |
| 0 | 55 | | } else { |
| 0 | 56 | | Assert.IsTrue(elapsedTime > 0 + epsilon, |
| | 57 | | "Simulation time should have advanced after loading while not paused"); |
| 0 | 58 | | Assert.LessOrEqual( |
| | 59 | | Mathf.Abs(Time.fixedDeltaTime - |
| | 60 | | (1.0f / SimManager.Instance.simulatorConfig.physicsUpdateRate)), |
| | 61 | | epsilon, "Physics update rate should be 1 / simulatorConfig.physicsUpdateRate"); |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | if (isPaused) { |
| 0 | 65 | | SimManager.Instance.ResumeSimulation(); |
| 0 | 66 | | isPaused = false; |
| 0 | 67 | | yield return new WaitForSecondsRealtime(0.1f); |
| 0 | 68 | | Assert.IsTrue(SimManager.Instance.GetElapsedSimulationTime() > 0 + epsilon, |
| | 69 | | "Simulation time should have advanced after resuming"); |
| 0 | 70 | | } |
| 0 | 71 | | } |
| 0 | 72 | | } |
| | 73 | | } |