Skip to content

Commit

Permalink
Implemented junit tests for PauseMenuFactory and PauseMenuTimeStopCom…
Browse files Browse the repository at this point in the history
…ponent.
  • Loading branch information
AlasdairS4698737 committed Oct 2, 2023
1 parent bf377e4 commit c58dc5c
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c58dc5c

Please sign in to comment.