SetEditorStateCommandTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using NUnit.Framework;
  2. using UnityEditor;
  3. using LLM.Editor.Commands;
  4. using LLM.Editor.Data;
  5. using LLM.Editor.Helper;
  6. using System.Collections;
  7. using UnityEngine.TestTools;
  8. using UnityEngine;
  9. namespace LLM.Editor.Tests.Unit
  10. {
  11. [TestFixture]
  12. public class SetEditorStateCommandTests
  13. {
  14. private CommandContext _context;
  15. [SetUp]
  16. public void SetUp()
  17. {
  18. _context = new CommandContext();
  19. // Ensure we are in Edit Mode before each test
  20. EditorApplication.ExitPlaymode();
  21. }
  22. [UnityTest]
  23. public IEnumerator Execute_WithPlayState_EntersPlayMode()
  24. {
  25. // Arrange
  26. var parameters = new SetEditorStateParams { state = EditorTargetState.Play };
  27. var command = new SetEditorStateCommand(parameters.ToJson());
  28. // Act
  29. var outcome = command.Execute(_context);
  30. // Assert
  31. Assert.AreEqual(CommandOutcome.Success, outcome);
  32. // Wait until the editor has entered play mode
  33. float timeout = 0f;
  34. while (!EditorApplication.isPlaying && timeout < 5f)
  35. {
  36. yield return null;
  37. timeout += Time.deltaTime;
  38. }
  39. Assert.IsTrue(EditorApplication.isPlaying, "Editor did not enter play mode within the timeout.");
  40. }
  41. [UnityTest]
  42. public IEnumerator Execute_WithStopState_ExitsPlayMode()
  43. {
  44. // Arrange
  45. // First, enter play mode to test exiting it
  46. EditorApplication.EnterPlaymode();
  47. float enterTimeout = 0f;
  48. while (!EditorApplication.isPlaying && enterTimeout < 5f)
  49. {
  50. yield return null;
  51. enterTimeout += Time.deltaTime;
  52. }
  53. Assert.IsTrue(EditorApplication.isPlaying, "Pre-condition failed: Editor did not enter play mode.");
  54. var parameters = new SetEditorStateParams { state = EditorTargetState.Stop };
  55. var command = new SetEditorStateCommand(parameters.ToJson());
  56. // Act
  57. var outcome = command.Execute(_context);
  58. // Assert
  59. Assert.AreEqual(CommandOutcome.Success, outcome);
  60. // Wait until the editor has exited play mode
  61. float exitTimeout = 0f;
  62. while (EditorApplication.isPlaying && exitTimeout < 5f)
  63. {
  64. yield return null;
  65. exitTimeout += Time.deltaTime;
  66. }
  67. Assert.IsFalse(EditorApplication.isPlaying, "Editor did not exit play mode within the timeout.");
  68. }
  69. [UnityTest]
  70. public IEnumerator Execute_WithPauseState_PausesPlayMode()
  71. {
  72. // Arrange
  73. EditorApplication.EnterPlaymode();
  74. float enterTimeout = 0f;
  75. while (!EditorApplication.isPlaying && enterTimeout < 5f)
  76. {
  77. yield return null;
  78. enterTimeout += Time.deltaTime;
  79. }
  80. Assert.IsTrue(EditorApplication.isPlaying, "Pre-condition failed: Editor did not enter play mode.");
  81. var parameters = new SetEditorStateParams { state = EditorTargetState.Pause };
  82. var command = new SetEditorStateCommand(parameters.ToJson());
  83. // Act
  84. var outcome = command.Execute(_context);
  85. // Assert
  86. Assert.AreEqual(CommandOutcome.Success, outcome);
  87. // The isPaused state should be set immediately, but we yield once to be safe
  88. yield return null;
  89. Assert.IsTrue(EditorApplication.isPaused);
  90. }
  91. }
  92. }