diff --git a/src/test/java/module-info.java b/src/test/java/module-info.java index 30c743b6..97e916d8 100644 --- a/src/test/java/module-info.java +++ b/src/test/java/module-info.java @@ -6,4 +6,6 @@ opens unittest.testcases.graphics.game to org.junit.platform.commons; opens unittest.testcases.math to org.junit.platform.commons; + + opens unittest.testcases.systems.control to org.junit.platform.commons; } diff --git a/src/test/java/unittest/HeadlessHelper.java b/src/test/java/unittest/HeadlessHelper.java index 0fcd128b..e6a5f726 100644 --- a/src/test/java/unittest/HeadlessHelper.java +++ b/src/test/java/unittest/HeadlessHelper.java @@ -4,7 +4,7 @@ import java.awt.GraphicsEnvironment; -import unittest.mock.MockManager; +import unittest.mock.systems.control.MockRunnableSimpleManager; public class HeadlessHelper { @@ -25,7 +25,7 @@ private static boolean headlessTest() { } public static void runFastJWith(Runnable runnable) { - FastJEngine.init("For those sweet, sweet testing purposes", new MockManager(runnable)); + FastJEngine.init("For those sweet, sweet testing purposes", new MockRunnableSimpleManager(runnable)); try { FastJEngine.run(); } catch (NullPointerException ignored) { diff --git a/src/test/java/unittest/mock/MockBehavior.java b/src/test/java/unittest/mock/systems/behaviors/MockBehavior.java similarity index 93% rename from src/test/java/unittest/mock/MockBehavior.java rename to src/test/java/unittest/mock/systems/behaviors/MockBehavior.java index 755d8100..0ee4d047 100644 --- a/src/test/java/unittest/mock/MockBehavior.java +++ b/src/test/java/unittest/mock/systems/behaviors/MockBehavior.java @@ -1,4 +1,4 @@ -package unittest.mock; +package unittest.mock.systems.behaviors; import io.github.lucasstarsz.fastj.math.Pointf; import io.github.lucasstarsz.fastj.graphics.game.GameObject; diff --git a/src/test/java/unittest/mock/systems/control/MockEmptyScene.java b/src/test/java/unittest/mock/systems/control/MockEmptyScene.java new file mode 100644 index 00000000..baf2ab1b --- /dev/null +++ b/src/test/java/unittest/mock/systems/control/MockEmptyScene.java @@ -0,0 +1,26 @@ +package unittest.mock.systems.control; + +import io.github.lucasstarsz.fastj.graphics.Display; + +import io.github.lucasstarsz.fastj.systems.control.Scene; + +import java.util.UUID; + +public class MockEmptyScene extends Scene { + + public MockEmptyScene() { + super(UUID.randomUUID().toString()); + } + + @Override + public void load(Display display) { + } + + @Override + public void unload(Display display) { + } + + @Override + public void update(Display display) { + } +} diff --git a/src/test/java/unittest/mock/systems/control/MockEmptySimpleManager.java b/src/test/java/unittest/mock/systems/control/MockEmptySimpleManager.java new file mode 100644 index 00000000..f841f367 --- /dev/null +++ b/src/test/java/unittest/mock/systems/control/MockEmptySimpleManager.java @@ -0,0 +1,15 @@ +package unittest.mock.systems.control; + +import io.github.lucasstarsz.fastj.graphics.Display; + +import io.github.lucasstarsz.fastj.systems.control.SimpleManager; + +public class MockEmptySimpleManager extends SimpleManager { + @Override + public void init(Display display) { + } + + @Override + public void update(Display display) { + } +} diff --git a/src/test/java/unittest/mock/MockScene.java b/src/test/java/unittest/mock/systems/control/MockNameSettingScene.java similarity index 65% rename from src/test/java/unittest/mock/MockScene.java rename to src/test/java/unittest/mock/systems/control/MockNameSettingScene.java index cdef28f8..f825b810 100644 --- a/src/test/java/unittest/mock/MockScene.java +++ b/src/test/java/unittest/mock/systems/control/MockNameSettingScene.java @@ -1,13 +1,13 @@ -package unittest.mock; +package unittest.mock.systems.control; import io.github.lucasstarsz.fastj.graphics.Display; import io.github.lucasstarsz.fastj.systems.control.Scene; -public class MockScene extends Scene { +public class MockNameSettingScene extends Scene { - public MockScene() { - super(""); + public MockNameSettingScene(String setName) { + super(setName); } @Override diff --git a/src/test/java/unittest/mock/MockManager.java b/src/test/java/unittest/mock/systems/control/MockRunnableSimpleManager.java similarity index 73% rename from src/test/java/unittest/mock/MockManager.java rename to src/test/java/unittest/mock/systems/control/MockRunnableSimpleManager.java index 1f48de14..8adb5b33 100644 --- a/src/test/java/unittest/mock/MockManager.java +++ b/src/test/java/unittest/mock/systems/control/MockRunnableSimpleManager.java @@ -1,15 +1,15 @@ -package unittest.mock; +package unittest.mock.systems.control; import io.github.lucasstarsz.fastj.engine.FastJEngine; import io.github.lucasstarsz.fastj.graphics.Display; import io.github.lucasstarsz.fastj.systems.control.SimpleManager; -public class MockManager extends SimpleManager { +public class MockRunnableSimpleManager extends SimpleManager { private final Runnable runnable; - public MockManager(Runnable runnable) { + public MockRunnableSimpleManager(Runnable runnable) { this.runnable = runnable; } diff --git a/src/test/java/unittest/mock/systems/control/MockSceneManager.java b/src/test/java/unittest/mock/systems/control/MockSceneManager.java new file mode 100644 index 00000000..0cfb4937 --- /dev/null +++ b/src/test/java/unittest/mock/systems/control/MockSceneManager.java @@ -0,0 +1,11 @@ +package unittest.mock.systems.control; + +import io.github.lucasstarsz.fastj.graphics.Display; + +import io.github.lucasstarsz.fastj.systems.control.SceneManager; + +public class MockSceneManager extends SceneManager { + @Override + public void init(Display display) { + } +} diff --git a/src/test/java/unittest/mock/systems/control/MockUpdatableScene.java b/src/test/java/unittest/mock/systems/control/MockUpdatableScene.java new file mode 100644 index 00000000..cacc50dd --- /dev/null +++ b/src/test/java/unittest/mock/systems/control/MockUpdatableScene.java @@ -0,0 +1,27 @@ +package unittest.mock.systems.control; + +import io.github.lucasstarsz.fastj.graphics.Display; + +import io.github.lucasstarsz.fastj.systems.control.Scene; + +import java.util.UUID; + +public abstract class MockUpdatableScene extends Scene { + + public MockUpdatableScene() { + super(UUID.randomUUID().toString()); + } + + @Override + public void load(Display display) { + + } + + @Override + public void unload(Display display) { + + } + + @Override + public abstract void update(Display display); +} diff --git a/src/test/java/unittest/mock/systems/tags/MockTaggableEntity.java b/src/test/java/unittest/mock/systems/tags/MockTaggableEntity.java new file mode 100644 index 00000000..dbee37b3 --- /dev/null +++ b/src/test/java/unittest/mock/systems/tags/MockTaggableEntity.java @@ -0,0 +1,11 @@ +package unittest.mock.systems.tags; + +import io.github.lucasstarsz.fastj.graphics.Drawable; + +import io.github.lucasstarsz.fastj.systems.control.Scene; + +public class MockTaggableEntity extends Drawable { + @Override + public void destroy(Scene originScene) { + } +} diff --git a/src/test/java/unittest/testcases/graphics/game/GameObjectTests.java b/src/test/java/unittest/testcases/graphics/game/GameObjectTests.java index 3d555377..3d93bd6b 100644 --- a/src/test/java/unittest/testcases/graphics/game/GameObjectTests.java +++ b/src/test/java/unittest/testcases/graphics/game/GameObjectTests.java @@ -7,8 +7,8 @@ import io.github.lucasstarsz.fastj.systems.control.Scene; import org.junit.jupiter.api.Test; -import unittest.mock.MockBehavior; -import unittest.mock.MockScene; +import unittest.mock.systems.behaviors.MockBehavior; +import unittest.mock.systems.control.MockEmptyScene; import unittest.mock.graphics.MockGameObject; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,7 +31,7 @@ void checkAddBehaviorToGameObject_shouldAllowMultiple() { GameObject gameObject = new MockGameObject(); Behavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); int behaviorCount = 255; for (int i = 0; i < behaviorCount; i++) { @@ -46,7 +46,7 @@ void checkClearBehaviorsFromGameObject_shouldRemoveAll() { GameObject gameObject = new MockGameObject(); Behavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); int behaviorCount = 255; for (int i = 0; i < behaviorCount; i++) { @@ -63,7 +63,7 @@ void checkRemoveBehaviorFromGameObject() { GameObject gameObject = new MockGameObject(); Behavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); int behaviorCount = 255; for (int i = 0; i < behaviorCount; i++) { @@ -81,7 +81,7 @@ void checkRemoveBehaviorFromGameObject() { @Test void checkAddAndRemoveBehaviorsFromGameObject_usingMethodChaining() { Behavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); GameObject gameObject = new MockGameObject() .addBehavior(mockBehavior, mockScene) // 1 @@ -100,7 +100,7 @@ void checkAddAndRemoveBehaviorsFromGameObject_usingMethodChaining() { void checkInitBehaviors_shouldInitializePointf() { GameObject gameObject = new MockGameObject(); MockBehavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); gameObject.addBehavior(mockBehavior, mockScene); gameObject.initBehaviors(); @@ -112,7 +112,7 @@ void checkInitBehaviors_shouldInitializePointf() { void checkUpdateBehaviors_shouldIncrementPointf() { GameObject gameObject = new MockGameObject(); MockBehavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); gameObject.addBehavior(mockBehavior, mockScene); gameObject.initBehaviors(); @@ -130,7 +130,7 @@ void checkUpdateBehaviors_shouldIncrementPointf() { void checkDestroyBehaviors_shouldMakePointfNull() { GameObject gameObject = new MockGameObject(); MockBehavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); gameObject.addBehavior(mockBehavior, mockScene); gameObject.initBehaviors(); // pointf is not null here @@ -144,7 +144,7 @@ void checkDestroyBehaviors_shouldMakePointfNull() { void tryUpdateBehaviorWithoutInitializing_shouldThrowNullPointerException() { GameObject gameObject = new MockGameObject(); MockBehavior mockBehavior = new MockBehavior(); - Scene mockScene = new MockScene(); + Scene mockScene = new MockEmptyScene(); gameObject.addBehavior(mockBehavior, mockScene); diff --git a/src/test/java/unittest/testcases/systems/control/SceneManagerTests.java b/src/test/java/unittest/testcases/systems/control/SceneManagerTests.java new file mode 100644 index 00000000..5241f305 --- /dev/null +++ b/src/test/java/unittest/testcases/systems/control/SceneManagerTests.java @@ -0,0 +1,141 @@ +package unittest.testcases.systems.control; + +import io.github.lucasstarsz.fastj.systems.control.Scene; +import io.github.lucasstarsz.fastj.systems.control.SceneManager; + +import org.junit.jupiter.api.Test; +import unittest.mock.systems.control.MockNameSettingScene; +import unittest.mock.systems.control.MockEmptyScene; +import unittest.mock.systems.control.MockSceneManager; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class SceneManagerTests { + + @Test + void checkSceneManagerCreation() { + SceneManager sceneManager = new MockSceneManager(); + + assertEquals(0, sceneManager.getScenes().size(), "After creating the manager, it should not contain any scenes."); + assertNull(sceneManager.getCurrentScene(), "After creating the manager, the current scene should not yet have been set."); + assertFalse(sceneManager.isSwitchingScenes(), "After creating the manager, it should not be switching scenes."); + } + + @Test + void checkSceneManagerSceneAdding() { + SceneManager sceneManager = new MockSceneManager(); + + Scene scene = new MockEmptyScene(); + sceneManager.addScene(scene); + + assertEquals(1, sceneManager.getScenes().size(), "After adding a scene to the manager, it should contain one scene."); + } + + @Test + void trySceneManagerSceneAdding_withNameThatAlreadyExists() { + SceneManager sceneManager = new MockSceneManager(); + + String sceneName = "having two scenes with the same name should throw an exception"; + Scene nameSettingScene1 = new MockNameSettingScene(sceneName); + Scene nameSettingScene2 = new MockNameSettingScene(sceneName); + + sceneManager.addScene(nameSettingScene1); + + Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.addScene(nameSettingScene2)); + + String expectedExceptionMessage = "The scene name \"" + sceneName + "\" is already in use." + + System.lineSeparator() + + "Scenes added: [" + sceneName + "]"; + assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message."); + } + + @Test + void checkSceneManagerSceneGetting_bySceneName() { + SceneManager sceneManager = new MockSceneManager(); + + Scene scene = new MockEmptyScene(); + sceneManager.addScene(scene); + + assertEquals(scene, sceneManager.getScene(scene.getSceneName()), "After adding a scene to the manager, getting that scene by its name should return the original scene."); + } + + @Test + void trySceneManagerGetScene_withSceneNameThatDoesNotExist() { + SceneManager sceneManager = new MockSceneManager(); + + String sceneName = "trying to get a scene with a scene name that doesn't exist should throw an exception"; + Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.getScene(sceneName)); + + String expectedExceptionMessage = "A scene with the name: \"" + sceneName + "\" hasn't been added!"; + assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message."); + } + + @Test + void checkSceneManagerSceneRemoving_bySceneObject() { + SceneManager sceneManager = new MockSceneManager(); + + Scene scene = new MockEmptyScene(); + sceneManager.addScene(scene); + sceneManager.removeScene(scene); + + assertEquals(0, sceneManager.getScenes().size(), "After removing the scene from the manager, it should not contain any scenes."); + } + + @Test + void checkSceneManagerSceneRemoving_bySceneName() { + SceneManager sceneManager = new MockSceneManager(); + + Scene scene = new MockEmptyScene(); + sceneManager.addScene(scene); + sceneManager.removeScene(scene.getSceneName()); + + assertEquals(0, sceneManager.getScenes().size(), "After removing the scene from the manager, it should not contain any scenes."); + } + + @Test + void trySceneManagerSceneRemoving_bySceneName_withSceneNameThatDoesNotExist() { + SceneManager sceneManager = new MockSceneManager(); + + String sceneName = "trying to remove a scene with a scene name that doesn't exist should throw an exception"; + Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.removeScene(sceneName)); + + String expectedExceptionMessage = "A scene with the name: \"" + sceneName + "\" hasn't been added!"; + assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message."); + } + + @Test + void checkSceneManagerCurrentSceneSetting_bySceneObject() { + SceneManager sceneManager = new MockSceneManager(); + + Scene scene = new MockEmptyScene(); + sceneManager.addScene(scene); + sceneManager.setCurrentScene(scene); + + assertEquals(scene, sceneManager.getCurrentScene(), "After setting the manager's current scene, getting the current scene should return the same scene."); + } + + @Test + void checkSceneManagerCurrentSceneSetting_bySceneName() { + SceneManager sceneManager = new MockSceneManager(); + + Scene scene = new MockEmptyScene(); + sceneManager.addScene(scene); + sceneManager.setCurrentScene(scene.getSceneName()); + + assertEquals(scene, sceneManager.getCurrentScene(), "After setting the manager's current scene, getting the current scene should return the same scene."); + } + + @Test + void trySceneManagerSetCurrentScene_bySceneName_withSceneNameThatDoesNotExist() { + SceneManager sceneManager = new MockSceneManager(); + + String sceneName = "trying to set the current scene with a scene name that doesn't exist should throw an exception"; + Throwable exception = assertThrows(IllegalStateException.class, () -> sceneManager.setCurrentScene(sceneName)); + + String expectedExceptionMessage = "A scene with the name: \"" + sceneName + "\" hasn't been added!"; + assertEquals(expectedExceptionMessage, exception.getCause().getMessage(), "The exception message should match the expected exception message."); + } +} diff --git a/src/test/java/unittest/testcases/systems/control/SceneTests.java b/src/test/java/unittest/testcases/systems/control/SceneTests.java new file mode 100644 index 00000000..bdd2c24b --- /dev/null +++ b/src/test/java/unittest/testcases/systems/control/SceneTests.java @@ -0,0 +1,57 @@ +package unittest.testcases.systems.control; + +import io.github.lucasstarsz.fastj.graphics.Camera; +import io.github.lucasstarsz.fastj.graphics.Drawable; +import io.github.lucasstarsz.fastj.graphics.game.GameObject; + +import io.github.lucasstarsz.fastj.systems.behaviors.BehaviorManager; +import io.github.lucasstarsz.fastj.systems.control.Scene; +import io.github.lucasstarsz.fastj.systems.tags.TagManager; + +import org.junit.jupiter.api.Test; +import unittest.mock.graphics.MockDrawable; +import unittest.mock.graphics.MockGameObject; +import unittest.mock.systems.control.MockEmptyScene; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SceneTests { + + @Test + void checkSceneCreation() { + Scene scene = new MockEmptyScene(); + + assertEquals(Camera.Default, scene.getCamera(), "The created scene's Camera should match the default camera."); + assertFalse(scene.isInitialized(), "Upon creation, the scene should not be initialized."); + assertNotNull(TagManager.getEntityList(scene), "Upon creation, the scene should be added to the tag manager's map of taggable entity lists."); + assertNotNull(BehaviorManager.getList(scene), "Upon creation, the scene should be added to the behavior manager's map of taggable entity lists."); + } + + @Test + void checkSceneInitialization() { + Scene scene = new MockEmptyScene(); + scene.setInitialized(true); + assertTrue(scene.isInitialized(), "After initializing the scene, the scene should be initialized."); + } + + @Test + void checkSceneResetting() { + Scene scene = new MockEmptyScene(); + + scene.getCamera().rotate(10f); + Drawable taggableEntity = new MockDrawable(); + scene.addTaggableEntity(taggableEntity); + GameObject gameObject = new MockGameObject(); + scene.addBehaviorListener(gameObject); + + scene.reset(); + + assertEquals(Camera.Default, scene.getCamera(), "After resetting the scene, its Camera should match the default camera."); + assertFalse(scene.isInitialized(), "After resetting the scene, the scene should not be initialized."); + assertEquals(0, TagManager.getEntityList(scene).size(), "After resetting the scene, the tag manager should contain no taggable entities for it."); + assertEquals(0, BehaviorManager.getList(scene).size(), "After resetting the scene, the behavior manager should contain no behavior listeners for it."); + } +} diff --git a/src/test/java/unittest/testcases/systems/control/SimpleManagerTests.java b/src/test/java/unittest/testcases/systems/control/SimpleManagerTests.java new file mode 100644 index 00000000..33a2a352 --- /dev/null +++ b/src/test/java/unittest/testcases/systems/control/SimpleManagerTests.java @@ -0,0 +1,46 @@ +package unittest.testcases.systems.control; + +import io.github.lucasstarsz.fastj.graphics.Camera; +import io.github.lucasstarsz.fastj.graphics.Drawable; +import io.github.lucasstarsz.fastj.graphics.game.GameObject; + +import io.github.lucasstarsz.fastj.systems.behaviors.BehaviorManager; +import io.github.lucasstarsz.fastj.systems.control.SimpleManager; +import io.github.lucasstarsz.fastj.systems.tags.TagManager; + +import org.junit.jupiter.api.Test; +import unittest.mock.graphics.MockDrawable; +import unittest.mock.graphics.MockGameObject; +import unittest.mock.systems.control.MockEmptySimpleManager; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class SimpleManagerTests { + + @Test + void checkSimpleManagerCreation() { + SimpleManager simpleManager = new MockEmptySimpleManager(); + + assertEquals(Camera.Default, simpleManager.getCamera(), "The created manager's Camera should match the default camera."); + assertNotNull(TagManager.getEntityList(simpleManager), "Upon creation, the manager should be added to the tag manager's map of taggable entity lists."); + assertNotNull(BehaviorManager.getList(simpleManager), "Upon creation, the manager should be added to the behavior manager's map of taggable entity lists."); + } + + @Test + void checkSimpleManagerResetting() { + SimpleManager simpleManager = new MockEmptySimpleManager(); + + simpleManager.getCamera().rotate(10f); + Drawable taggableEntity = new MockDrawable(); + simpleManager.addTaggableEntity(taggableEntity); + GameObject gameObject = new MockGameObject(); + simpleManager.addBehaviorListener(gameObject); + + simpleManager.reset(); + + assertEquals(Camera.Default, simpleManager.getCamera(), "After resetting the manager, its Camera should match the default camera."); + assertEquals(0, TagManager.getEntityList(simpleManager).size(), "After resetting the manager, the tag manager should contain no taggable entities for it."); + assertEquals(0, BehaviorManager.getList(simpleManager).size(), "After resetting the manager, the behavior manager should contain no behavior listeners for it."); + } +}