diff --git a/source/core/src/test/com/csse3200/game/components/pausemenu/PauseMenuTimeStopComponentTest.java b/source/core/src/test/com/csse3200/game/components/pausemenu/PauseMenuTimeStopComponentTest.java new file mode 100644 index 000000000..65cf8cb8b --- /dev/null +++ b/source/core/src/test/com/csse3200/game/components/pausemenu/PauseMenuTimeStopComponentTest.java @@ -0,0 +1,49 @@ +package com.csse3200.game.components.pausemenu; + +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.EntityService; +import com.csse3200.game.extensions.GameExtension; +import com.csse3200.game.services.ServiceLocator; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import static org.mockito.Mockito.*; + +@ExtendWith(GameExtension.class) +public class PauseMenuTimeStopComponentTest { + Entity pauseMenu; + Entity entity; + @BeforeEach + void beforeEach() { + EntityService entityService = new EntityService(); + ServiceLocator.registerEntityService(entityService); + entity = mock(Entity.class); + when(entity.getId()).thenReturn(-1); //Ensure it does not coincide with the pause menu's ID + entityService.register(entity); + PauseMenuTimeStopComponent timeStopComponent = new PauseMenuTimeStopComponent(); + pauseMenu = new Entity(); + pauseMenu.addComponent(timeStopComponent); + } + + @Test + void pausesEntities() { + ServiceLocator.getEntityService().register(pauseMenu); + verify(entity).setEnabled(false); + } + + @Test + void unpausesEntitiesWhenDisposed() { + ServiceLocator.getEntityService().register(pauseMenu); + pauseMenu.dispose(); + verify(entity).setEnabled(true); + } + + @Test + void doesNotPauseNewEntities() { + ServiceLocator.getEntityService().register(pauseMenu); + Entity lateEntity = mock(Entity.class); + when(entity.getId()).thenReturn(-1); //Ensure it does not coincide with the pause menu's ID + ServiceLocator.getEntityService().register(lateEntity); + verify(lateEntity, times(0)).setEnabled(false); + } +} diff --git a/source/core/src/test/com/csse3200/game/entities/factories/PauseMenuFactoryTest.java b/source/core/src/test/com/csse3200/game/entities/factories/PauseMenuFactoryTest.java new file mode 100644 index 000000000..347868521 --- /dev/null +++ b/source/core/src/test/com/csse3200/game/entities/factories/PauseMenuFactoryTest.java @@ -0,0 +1,70 @@ +package com.csse3200.game.entities.factories; + +import com.badlogic.gdx.scenes.scene2d.Stage; +import com.csse3200.game.GdxGame; +import com.csse3200.game.components.pausemenu.*; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.EntityService; +import com.csse3200.game.extensions.GameExtension; +import com.csse3200.game.rendering.RenderService; +import com.csse3200.game.rendering.TextureRenderComponent; +import com.csse3200.game.services.ResourceService; +import com.csse3200.game.services.ServiceLocator; +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + + +@ExtendWith(GameExtension.class) +public class PauseMenuFactoryTest { + Entity entity; + GdxGame game; + + String[] texture = { + "images/ui/Sprites/UI_Glass_Toggle_Bar_01a.png" + }; + @BeforeEach + void beforeEach() { + EntityService entityService = new EntityService(); + ServiceLocator.registerEntityService(entityService); + RenderService renderService = mock(RenderService.class); + when(renderService.getStage()).thenReturn(mock(Stage.class)); + ServiceLocator.registerRenderService(renderService); + ResourceService resourceService = new ResourceService(); + ServiceLocator.registerResourceService(resourceService); + resourceService.loadTextures(texture); + resourceService.loadAll(); + + game = mock(GdxGame.class); + entity = PauseMenuFactory.createPauseMenu(game); + } + + @Test + void createsEntity() { + assertNotNull(entity); + } + + @Test + void entityHasRequiredComponents() { + assertNotNull(entity.getComponent(PauseMenuTimeStopComponent.class)); + assertNotNull(entity.getComponent(PauseMenuContinueButton.class)); + assertNotNull(entity.getComponent(PauseMenuSettingsButton.class)); + assertNotNull(entity.getComponent(PauseMenuPlanetSelectButton.class)); + assertNotNull(entity.getComponent(PauseMenuMainMenuButton.class)); + assertNotNull(entity.getComponent(TextureRenderComponent.class)); + } + + @Test + void duplicateNotCreated() { + Entity secondEntity = PauseMenuFactory.createPauseMenu(game); + assertNull(secondEntity); + } + + @Test + void createsSecondEntityAfterFirstIsDisposed() { + entity.dispose(); + Entity secondEntity = PauseMenuFactory.createPauseMenu(game); + assertNotNull(secondEntity); + } +}