Skip to content

Commit

Permalink
Added Junit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad11Dab committed Oct 14, 2023
1 parent 43649e1 commit 919858f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 20 deletions.
13 changes: 13 additions & 0 deletions source/core/src/main/com/csse3200/game/rendering/CameraShaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,17 @@ private void checkParameters(float shakeRadius, float minimumShakeRadius, float
this.minimumShakeRadius = minimumShakeRadius;
this.radiusFallOffFactor = radiusFallOffFactor;
}

public float getShakeRadius() {
return shakeRadius;
}

public float getMinimumRadius() {
return minimumShakeRadius;
}

public float getFallOffFactor() {
return radiusFallOffFactor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,39 @@
import com.csse3200.game.services.CurrencyService;
import com.csse3200.game.services.ResourceService;
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;

@ExtendWith(GameExtension.class)
class TerrainComponentTest {

private TerrainComponent component;
private TiledMapTile mockTile;

@BeforeEach
public void setUp() {
component = makeComponent(TerrainOrientation.ORTHOGONAL, 1f);

// Mock Gdx input to return specific mouse position
Gdx.input = mock(Input.class);
when(Gdx.input.getX()).thenReturn(2);
when(Gdx.input.getY()).thenReturn(4);


MapLayers mockLayers = mock(MapLayers.class);
when(component.getMap().getLayers()).thenReturn(mockLayers);

TiledMapTileLayer mockTileLayer = mock(TiledMapTileLayer.class);
when(mockLayers.get(0)).thenReturn(mockTileLayer);

TiledMapTileLayer.Cell mockCell = mock(TiledMapTileLayer.Cell.class);
when(mockTileLayer.getCell(2, 4)).thenReturn(mockCell);

mockTile = mock(TiledMapTile.class);
when(mockCell.getTile()).thenReturn(mockTile);

}
@Test
void shouldConvertPositionOrthogonal() {
TerrainComponent component = makeComponent(TerrainOrientation.ORTHOGONAL, 3f);
Expand All @@ -51,42 +79,44 @@ void shouldConvertPositionHexagonal() {
}

@Test
void shouldHighlightTileOnHover1() {

TerrainComponent component = makeComponent(TerrainOrientation.ORTHOGONAL, 1f);

// Mock Gdx input to return specific mouse position
Gdx.input = mock(Input.class);
when(Gdx.input.getX()).thenReturn(2);
when(Gdx.input.getY()).thenReturn(4);
void shouldHighlightTileGreenOnHover() {

Texture mockTexture = mock(Texture.class);
ServiceLocator.registerResourceService(mock(ResourceService.class));

MapLayers mockLayers = mock(MapLayers.class);
when(component.getMap().getLayers()).thenReturn(mockLayers);
ServiceLocator.registerCurrencyService(mock(CurrencyService.class));
when(ServiceLocator.getCurrencyService().getTower()).thenReturn(TowerType.WEAPON);

TiledMapTileLayer mockTileLayer = mock(TiledMapTileLayer.class);
when(mockLayers.get(0)).thenReturn(mockTileLayer);
ServiceLocator.registerEntityService(mock(EntityService.class));
when(ServiceLocator.getEntityService().entitiesInTile(2,4)).thenReturn(Boolean.FALSE);

TiledMapTileLayer.Cell mockCell = mock(TiledMapTileLayer.Cell.class);
when(mockTileLayer.getCell(2, 4)).thenReturn(mockCell);
when(ServiceLocator.getCurrencyService().getScrap()).thenReturn(mock(Scrap.class));
when(ServiceLocator.getCurrencyService().getScrap().canBuy(Integer.parseInt(ServiceLocator.getCurrencyService().getTower().getPrice()))).thenReturn(Boolean.TRUE);
// The tile should turn green since it meets the requirement
when(ServiceLocator.getResourceService().getAsset("images/green_tile.png", Texture.class))
.thenReturn(mockTexture);

TiledMapTile mockTile = mock(TiledMapTile.class);
when(mockCell.getTile()).thenReturn(mockTile);
component.hoverHighlight();
// Verify that the tile's texture region was changed
verify(mockTile).setTextureRegion(any(TextureRegion.class));
}

@Test
void shouldHighlightTileRedOnHover() {

Texture mockTexture = mock(Texture.class);
ServiceLocator.registerResourceService(mock(ResourceService.class));

ServiceLocator.registerCurrencyService(mock(CurrencyService.class));
when(ServiceLocator.getCurrencyService().getTower()).thenReturn(TowerType.WEAPON);
when(ServiceLocator.getCurrencyService().getTower()).thenReturn(TowerType.TNT);

ServiceLocator.registerEntityService(mock(EntityService.class));
when(ServiceLocator.getEntityService().entitiesInTile(2,4)).thenReturn(Boolean.FALSE);

when(ServiceLocator.getCurrencyService().getScrap()).thenReturn(mock(Scrap.class));
when(ServiceLocator.getCurrencyService().getScrap().canBuy(Integer.parseInt(ServiceLocator.getCurrencyService().getTower().getPrice()))).thenReturn(Boolean.TRUE);
// The tile should turn green since it meets the requirement
when(ServiceLocator.getResourceService().getAsset("images/green_tile.png", Texture.class))
when(ServiceLocator.getCurrencyService().getScrap().canBuy(Integer.parseInt(ServiceLocator.getCurrencyService().getTower().getPrice()))).thenReturn(Boolean.FALSE);
// The tile should turn red since we can't afford the tower
when(ServiceLocator.getResourceService().getAsset("images/red_tile.png", Texture.class))
.thenReturn(mockTexture);

component.hoverHighlight();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.csse3200.game.rendering;

import com.badlogic.gdx.graphics.OrthographicCamera;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;


public class CameraShakerTest {

private CameraShaker cameraShaker;
private OrthographicCamera camera;

@BeforeEach
public void setUp() {
camera = new OrthographicCamera();
cameraShaker = new CameraShaker(camera);
}

@Test
public void testStartShaking() {
assertFalse(cameraShaker.isCameraShaking());
cameraShaker.startShaking();
assertTrue(cameraShaker.isCameraShaking());
}

@Test
public void testReset() {
cameraShaker.startShaking();
cameraShaker.reset();
assertFalse(cameraShaker.isCameraShaking());
assertEquals(cameraShaker.origPosition, camera.position);
}

@Test
public void testDefaultConstructorValues() {
assertEquals(0.05f, cameraShaker.getShakeRadius(), 0.001f);
assertEquals(0.001f, cameraShaker.getMinimumRadius(), 0.001f);
assertEquals(0.8f, cameraShaker.getFallOffFactor(), 0.001f);
}

}

0 comments on commit 919858f

Please sign in to comment.