Skip to content

Commit

Permalink
updated buildInputComponent test
Browse files Browse the repository at this point in the history
  • Loading branch information
The-AhmadAA committed Oct 10, 2023
1 parent c9ad35f commit 03c0905
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ private void addActors() {
}

// Update the centrally located towerTypes list -
logger.info("In UIElementsDisplay, the towers being sent to ServiceLocator are " + towers);
ServiceLocator.setTowerTypes(towers);

// Create the buttons - TODO This needs overhauling to pretty buttons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public boolean entitiesInTile(int x_coord, int y_coord) {
try {
mp = (TiledMapTileLayer)ServiceLocator.getMapService().getComponent().getMap().getLayers().get(0);
} catch (NullPointerException e) {
// MapService is not running
// MapService is not running - consider this occupied (invalid tile)
return true;
}
if (mp.getCell(x_coord, y_coord) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) {
buildTower((int)cursorPosition.x, (int)cursorPosition.y);
logger.debug("spawning a tower at {}, {}", cursorPosition.x, cursorPosition.y);
return true;
} else {
// TODO: Create a tile indication of invalid placement here??
return false;
}
return false;
}

/**
* Triggers player events on specific keycodes.
* Configures shortcut keys for building towers. Pressing the shortcut key
* sets the 'tower to build' variable in CurrencyService
*
* @return whether the input was processed
* @see InputProcessor#keyDown(int)
Expand Down Expand Up @@ -141,6 +144,7 @@ public void buildTower(int x, int y) {
// build the selected tower
newTower.setPosition(x, y);
ServiceLocator.getEntityService().register(newTower);

// Decrement currency and show a popup that reflects the cost of the build
ServiceLocator.getCurrencyService().getScrap().modify(-cost);
ServiceLocator.getCurrencyService().getDisplay().updateScrapsStats();
Expand All @@ -154,7 +158,9 @@ public void buildTower(int x, int y) {
} else {
// play a sound to indicate an invalid action
long soundId = errorSound.play();
errorSound.setVolume(soundId, 0.5f);
errorSound.setVolume(soundId, 1f);
// TODO: add a visual indication of the build fail, through
// currency display flash
}
}
}
Expand Down
284 changes: 118 additions & 166 deletions source/core/src/test/com/csse3200/game/input/BuildInputComponentTest.java
Original file line number Diff line number Diff line change
@@ -1,166 +1,118 @@
//package com.csse3200.game.input;
//
//import com.badlogic.gdx.Gdx;
//import com.badlogic.gdx.Graphics;
//import com.badlogic.gdx.graphics.g2d.TextureAtlas;
//import com.badlogic.gdx.maps.tiled.TiledMap;
//import com.badlogic.gdx.math.Vector2;
//import com.csse3200.game.GdxGame;
//import com.csse3200.game.components.CameraComponent;
//import com.csse3200.game.currency.Currency;
//import com.csse3200.game.entities.Entity;
//import com.csse3200.game.entities.EntityService;
//import com.csse3200.game.entities.factories.TowerFactory;
//import com.csse3200.game.extensions.GameExtension;
//import com.csse3200.game.physics.PhysicsService;
//import com.csse3200.game.rendering.DebugRenderer;
//import com.csse3200.game.rendering.RenderService;
//import com.csse3200.game.services.*;
//import org.junit.jupiter.api.AfterEach;
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.Test;
//import org.junit.jupiter.api.extension.ExtendWith;
//
//import static org.junit.jupiter.api.Assertions.assertEquals;
//import static org.junit.jupiter.api.Assertions.assertFalse;
//import static org.mockito.Mockito.*;
//
//@ExtendWith(GameExtension.class)
//class BuildInputComponentTest {
//
// private BuildInputComponent buildInputComponent;
// private Entity baseTower;
// private Entity weaponTower;
// private Entity wallTower;
// private Entity stunTower;
// private Entity fireTower;
// private Entity tntTower;
// private Entity droidTower;
// private String[] texture = {
// "images/towers/turret_deployed.png",
// "images/towers/turret01.png",
// "images/towers/wall_tower.png",
// "images/towers/fire_tower_atlas.png",
// "images/towers/stun_tower.png",
// "images/towers/DroidTower.png",
// "images/towers/TNTTower.png"
// };
// private String[] atlas = {
// "images/towers/turret01.atlas",
// "images/towers/stun_tower.atlas",
// "images/towers/fire_tower_atlas.atlas",
// "images/towers/DroidTower.atlas",
// "images/towers/TNTTower.atlas",
// "images/towers/barrier.atlas"
// };
// private static final String[] sounds = {
// "sounds/towers/gun_shot_trimmed.mp3",
// "sounds/towers/deploy.mp3",
// "sounds/towers/stow.mp3"
// };
//
// @BeforeEach
// void setup() {
// Gdx.graphics = mock(Graphics.class);
// when(Gdx.graphics.getDeltaTime()).thenReturn(10f);
//
// GameTime gameTime = mock(GameTime.class);
// CameraComponent camera = mock(CameraComponent.class);
// when(gameTime.getDeltaTime()).thenReturn(0.02f);
// ServiceLocator.registerTimeSource(gameTime);
// ServiceLocator.registerPhysicsService(new PhysicsService());
// RenderService render = new RenderService();
// render.setDebug(mock(DebugRenderer.class));
// ServiceLocator.registerRenderService(render);
//
// CurrencyService currencyService = new CurrencyService();
// ResourceService resourceService = new ResourceService();
// MapService mapService = mock(MapService.class);
// when(mapService.getComponent().tileToWorldPosition(0,0)).thenReturn(new Vector2(0,0));
// EntityService entityService = new EntityService();
//
// ServiceLocator.registerResourceService(resourceService);
// ServiceLocator.registerCurrencyService(currencyService);
// ServiceLocator.registerMapService(mapService);
// ServiceLocator.registerEntityService(entityService);
//
// resourceService.loadTextures(texture);
// resourceService.loadTextureAtlases(atlas);
// resourceService.loadSounds(sounds);
// resourceService.loadAll();
//
// ServiceLocator.getResourceService()
// .getAsset("images/towers/turret01.atlas", TextureAtlas.class);
// baseTower = TowerFactory.createBaseTower();
// weaponTower = TowerFactory.createWeaponTower();
// wallTower = TowerFactory.createWallTower();
// fireTower = TowerFactory.createFireTower();
// stunTower = TowerFactory.createFireTower();
// tntTower = TowerFactory.createTNTTower();
// droidTower = TowerFactory.createDroidTower();
//
// buildInputComponent = new BuildInputComponent(camera.getCamera());
// }
//
// @Test
// void shouldUpdatePriority() {
// int newPriority = 100;
// InputComponent inputComponent = spy(InputComponent.class);
//
// inputComponent.setPriority(newPriority);
// verify(inputComponent).setPriority(newPriority);
//
// int priority = inputComponent.getPriority();
// verify(inputComponent).getPriority();
//
// assertEquals(newPriority, priority);
// }
//
// @Test
// void shouldRegisterOnCreate() {
// InputService inputService = spy(InputService.class);
// ServiceLocator.registerInputService(inputService);
//
// InputComponent inputComponent = spy(InputComponent.class);
// inputComponent.create();
// verify(inputService).register(inputComponent);
// }
//
// @Test
// void shouldHandleTouchDown() {
// BuildInputComponent inputComponent = spy(BuildInputComponent.class);
// assertFalse(inputComponent.touchDown( 5, 6, 7, 8));
// }
//
// @Test
// void shouldRejectOccupiedTile() {
// Vector2 tile = ServiceLocator.getMapService().getComponent().tileToWorldPosition(0, 0);
// tntTower.setPosition(0,0);
// assertFalse(buildInputComponent.touchDown(0,0, 7,8));
// }
//
// @Test
// void shouldRejectInvalidTile() {
//
// }
//
// @Test
// void shouldHandleMissingMapService() {
//
// }
//
// @Test
// void shouldHandleMissingCurrencyService() {
//
// }
//
// @Test
// void shouldHandleInvalidTower() {
//
// }
//
// @Test
// void shouldHandleMissingEntityService() {
//
// }
//}
package com.csse3200.game.input;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Graphics;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.areas.terrain.TerrainComponent;
import com.csse3200.game.components.CameraComponent;
import com.csse3200.game.entities.Entity;
import com.csse3200.game.entities.EntityService;
import com.csse3200.game.entities.factories.TowerFactory;
import com.csse3200.game.extensions.GameExtension;
import com.csse3200.game.physics.PhysicsService;
import com.csse3200.game.rendering.DebugRenderer;
import com.csse3200.game.rendering.RenderService;
import com.csse3200.game.services.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.*;

@ExtendWith(GameExtension.class)
class BuildInputComponentTest {

private BuildInputComponent buildInputComponent;
EntityService entityService;

@BeforeEach
void setup() {
Gdx.graphics = mock(Graphics.class);
when(Gdx.graphics.getDeltaTime()).thenReturn(10f);

GameTime gameTime = mock(GameTime.class);
CameraComponent camera = mock(CameraComponent.class);
when(camera.getCamera()).thenReturn(mock(Camera.class));
when(gameTime.getDeltaTime()).thenReturn(0.02f);
ServiceLocator.registerTimeSource(gameTime);
ServiceLocator.registerPhysicsService(new PhysicsService());
RenderService render = new RenderService();
render.setDebug(mock(DebugRenderer.class));
ServiceLocator.registerRenderService(render);

CurrencyService currencyService = new CurrencyService();
ResourceService resourceService = new ResourceService();
MapService mapService = mock(MapService.class);
when(mapService.getComponent()).thenReturn(mock(TerrainComponent.class));
entityService = mock(EntityService.class);

ServiceLocator.registerResourceService(resourceService);
ServiceLocator.registerCurrencyService(currencyService);
ServiceLocator.registerMapService(mapService);
ServiceLocator.registerEntityService(entityService);

buildInputComponent = new BuildInputComponent(camera.getCamera());
}

@Test
void shouldUpdatePriority() {
int newPriority = 100;
InputComponent inputComponent = spy(InputComponent.class);

inputComponent.setPriority(newPriority);
verify(inputComponent).setPriority(newPriority);

int priority = inputComponent.getPriority();
verify(inputComponent).getPriority();

assertEquals(newPriority, priority);
}

@Test
void shouldRegisterOnCreate() {
InputService inputService = spy(InputService.class);
ServiceLocator.registerInputService(inputService);

InputComponent inputComponent = spy(InputComponent.class);
inputComponent.create();
verify(inputService).register(inputComponent);
}

@Test
void shouldHandleTouchDown() {
when(entityService.entitiesInTile(5, 5)).thenReturn(false);
assert(buildInputComponent.touchDown( 5, 5, 7, 8));
}

@Test
void shouldRejectOccupiedOrInvalidTile() {
// entitiesInTile checks for out of bounds condition as well
when(entityService.entitiesInTile(5, 5)).thenReturn(true);
assertFalse(buildInputComponent.touchDown(5,5, 7,8),
"Attempting to build on an existing tower should return False");
}

@Test
void shouldHandleMissingMapService() {

}

@Test
void shouldHandleMissingCurrencyService() {

}

@Test
void shouldHandleInvalidTower() {

}

@Test
void shouldHandleMissingEntityService() {

}
}

0 comments on commit 03c0905

Please sign in to comment.