Skip to content

Commit

Permalink
Merge pull request #55 from hdescottes/Improve_test_mock_graphics_com…
Browse files Browse the repository at this point in the history
…ponents

Improve test mock graphics components
  • Loading branch information
hdescottes authored Oct 22, 2023
2 parents eb7efd7 + 30b6e16 commit c5d23a9
Show file tree
Hide file tree
Showing 16 changed files with 473 additions and 303 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ allprojects {
dependencies {
testImplementation("org.junit.platform:junit-platform-launcher:$jUnitPlatformVersion")
testImplementation("org.junit.jupiter:junit-jupiter-api:$jUnitJupiterVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$jUnitJupiterVersion")
testImplementation("org.mockito:mockito-core:$mockitoVersion")
testImplementation("org.assertj:assertj-core:$assertJVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$jUnitJupiterVersion")
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/com/gdx/game/battle/BattleState.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void resumeOver() {
notify(currentOpponent, BattleObserver.BattleEvent.RESUME_OVER);
}

private Timer.Task getTurnTimer() {
protected Timer.Task getTurnTimer() {
return new Timer.Task() {
@Override
public void run() {
Expand Down Expand Up @@ -152,7 +152,7 @@ public void run() {
};
}

private Timer.Task getPlayerAttackCalculationTimer() {
protected Timer.Task getPlayerAttackCalculationTimer() {
return new Timer.Task() {
@Override
public void run() {
Expand Down Expand Up @@ -199,7 +199,7 @@ private void calculateDrops() {
}
}

private Timer.Task getOpponentAttackCalculationTimer() {
protected Timer.Task getOpponentAttackCalculationTimer() {
return new Timer.Task() {
@Override
public void run() {
Expand Down
33 changes: 16 additions & 17 deletions core/src/test/java/com/gdx/game/audio/AudioManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.gdx.game.GdxRunner;
import com.gdx.game.audio.AudioManager;
import com.gdx.game.audio.AudioObserver;
import com.gdx.game.manager.ResourceManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static com.gdx.game.audio.AudioObserver.AudioTypeEvent.MENU_THEME;
import static com.gdx.game.audio.AudioObserver.AudioTypeEvent.TOPPLE_THEME;
Expand All @@ -30,28 +33,24 @@ void testGetInstance_ShouldSucceed() {
assertThat(audioManager).isNotNull();
}

@Test
void testOnNotify_ShouldSucceedWithMusicPlayOnce() {
@ParameterizedTest
@MethodSource("loadMusic")
void testOnNotify(AudioObserver.AudioCommand command, AudioObserver.AudioTypeEvent event) {
new ResourceManager();
AudioManager audioManager = AudioManager.getInstance();
audioManager.setCurrentMusic(null);

audioManager.onNotify(AudioObserver.AudioCommand.MUSIC_LOAD, MENU_THEME);
audioManager.onNotify(AudioObserver.AudioCommand.MUSIC_PLAY_ONCE, MENU_THEME);
audioManager.onNotify(AudioObserver.AudioCommand.MUSIC_LOAD, event);
audioManager.onNotify(command, event);

assertThat(audioManager.getCurrentMusic()).isEqualTo(ResourceManager.getMusicAsset(MENU_THEME.getValue()));
assertThat(audioManager.getCurrentMusic()).isEqualTo(ResourceManager.getMusicAsset(event.getValue()));
}

@Test
void testOnNotify_ShouldSucceedWithMusicPlayLoop() {
new ResourceManager();
AudioManager audioManager = AudioManager.getInstance();
audioManager.setCurrentMusic(null);

audioManager.onNotify(AudioObserver.AudioCommand.MUSIC_LOAD, TOPPLE_THEME);
audioManager.onNotify(AudioObserver.AudioCommand.MUSIC_PLAY_LOOP, TOPPLE_THEME);

assertThat(audioManager.getCurrentMusic()).isEqualTo(ResourceManager.getMusicAsset(TOPPLE_THEME.getValue()));
private static Stream<Arguments> loadMusic() {
return Stream.of(
Arguments.of(AudioObserver.AudioCommand.MUSIC_PLAY_ONCE, MENU_THEME),
Arguments.of(AudioObserver.AudioCommand.MUSIC_PLAY_LOOP, TOPPLE_THEME)
);
}

@Test
Expand Down
162 changes: 162 additions & 0 deletions core/src/test/java/com/gdx/game/battle/BattleStateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.gdx.game.battle;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.gdx.game.GdxRunner;
import com.gdx.game.entities.Entity;
import com.gdx.game.entities.EntityConfig;
import com.gdx.game.entities.EntityFactory;
import com.gdx.game.entities.npc.NPCGraphicsComponent;
import com.gdx.game.entities.player.PlayerGraphicsComponent;
import com.gdx.game.inventory.InventoryObserver;
import com.gdx.game.profile.ProfileManager;
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 org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.MockedConstruction;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

@ExtendWith(GdxRunner.class)
public class BattleStateTest {

private MockedConstruction<PlayerGraphicsComponent> mockPlayerGraphics;

private MockedConstruction<NPCGraphicsComponent> mockNPCGraphics;

private final ProfileManager profileManager = ProfileManager.getInstance();

@BeforeEach
void init() {
Gdx.gl = mock(GL20.class);
Gdx.gl20 = mock(GL20.class);
mockPlayerGraphics = mockConstruction(PlayerGraphicsComponent.class);
mockNPCGraphics = mockConstruction(NPCGraphicsComponent.class);
profileManager.setProperty("currentPlayerAP", 5);
profileManager.setProperty("currentPlayerDP", 5);
profileManager.setProperty("currentPlayerMP", 5);
profileManager.setProperty("currentPlayerHP", 20);
}

@AfterEach
void end() {
mockPlayerGraphics.close();
mockNPCGraphics.close();
}

@Test
void playerAttack_doNotKill() {
BattleState battleState = spy(new BattleState());
Entity player = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.WARRIOR);
Entity enemy = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.ENEMY);
enemy.getEntityConfig().setPropertyValue(EntityConfig.EntityProperties.ENTITY_HEALTH_POINTS.toString(), "20");
enemy.getEntityConfig().setPropertyValue(EntityConfig.EntityProperties.ENTITY_PHYSICAL_DEFENSE_POINTS.toString(), "4");
battleState.setPlayer(player);
battleState.setCurrentOpponent(enemy);

battleState.getPlayerAttackCalculationTimer().run();

assertThat(enemy.getEntityConfig().getPropertyValue(EntityConfig.EntityProperties.ENTITY_HEALTH_POINTS.toString())).isLessThanOrEqualTo("19");
verify(battleState).notify(enemy, BattleObserver.BattleEvent.OPPONENT_HIT_DAMAGE);
verify(battleState).notify(enemy, BattleObserver.BattleEvent.PLAYER_TURN_DONE);
verify(battleState, never()).notify(enemy, BattleObserver.BattleEvent.OPPONENT_DEFEATED);
}

@Test
void playerAttack_killOpponent() {
String dropId = "1";
EntityConfig.Drop drop = new EntityConfig.Drop();
drop.setProbability(1);
drop.setItemTypeID(dropId);
BattleState battleState = spy(new BattleState());
Entity player = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.WARRIOR);
Entity enemy = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.ENEMY);
enemy.getEntityConfig().setPropertyValue(EntityConfig.EntityProperties.ENTITY_HEALTH_POINTS.toString(), "1");
enemy.getEntityConfig().setPropertyValue(EntityConfig.EntityProperties.ENTITY_PHYSICAL_DEFENSE_POINTS.toString(), "4");
enemy.getEntityConfig().addDrop(drop);
battleState.setPlayer(player);
battleState.setCurrentOpponent(enemy);

battleState.getPlayerAttackCalculationTimer().run();

assertThat(enemy.getEntityConfig().getPropertyValue(EntityConfig.EntityProperties.ENTITY_HEALTH_POINTS.toString())).isEqualTo("0");
verify(battleState).notify(enemy, BattleObserver.BattleEvent.OPPONENT_HIT_DAMAGE);
verify(battleState).notify(enemy, BattleObserver.BattleEvent.PLAYER_TURN_DONE);
verify(battleState).notify(enemy, BattleObserver.BattleEvent.OPPONENT_DEFEATED);
verify(battleState).notify(dropId, InventoryObserver.InventoryEvent.DROP_ITEM_ADDED);
}

@Test
void opponentAttack() {
BattleState battleState = spy(new BattleState());
Entity player = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.WARRIOR);
Entity enemy = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.ENEMY);
enemy.getEntityConfig().setPropertyValue(EntityConfig.EntityProperties.ENTITY_PHYSICAL_ATTACK_POINTS.toString(), "6");
battleState.setPlayer(player);
battleState.setCurrentOpponent(enemy);

battleState.getOpponentAttackCalculationTimer().run();

assertThat(player.getEntityConfig().getPropertyValue(EntityConfig.EntityProperties.ENTITY_HEALTH_POINTS.toString())).isLessThanOrEqualTo("19");
verify(battleState).notify(player, BattleObserver.BattleEvent.PLAYER_HIT_DAMAGE);
verify(battleState).notify(enemy, BattleObserver.BattleEvent.OPPONENT_TURN_DONE);
}

@ParameterizedTest
@MethodSource("determineRun")
void playerRuns(float speedRatio, BattleObserver.BattleEvent event) {
BattleState battleState = spy(new BattleState());
Entity enemy = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.ENEMY);
battleState.setCurrentOpponent(enemy);
battleState.setSpeedRatio(speedRatio);

battleState.playerRuns();

verify(battleState).notify(enemy, event);
}

private static Stream<Arguments> determineRun() {
return Stream.of(
Arguments.of(100f, BattleObserver.BattleEvent.PLAYER_RUNNING),
Arguments.of(-1.2f, BattleObserver.BattleEvent.PLAYER_TURN_DONE)
);
}

@ParameterizedTest
@MethodSource("determineTurn")
void determineTurn(float speedRatio, boolean expectPlayerTurn) {
BattleState battleState = spy(new BattleState());
Entity player = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.WARRIOR);
Entity enemy = EntityFactory.getInstance().getEntity(EntityFactory.EntityType.ENEMY);
enemy.getEntityConfig().setPropertyValue(EntityConfig.EntityProperties.ENTITY_HEALTH_POINTS.toString(), "5");
battleState.setPlayer(player);
battleState.setCurrentOpponent(enemy);
battleState.setSpeedRatio(speedRatio);

battleState.getTurnTimer().run();

if (expectPlayerTurn) {
verify(battleState).notify(player, BattleObserver.BattleEvent.PLAYER_TURN_START);
} else {
verify(battleState, never()).notify(player, BattleObserver.BattleEvent.PLAYER_TURN_START);
}
}

private static Stream<Arguments> determineTurn() {
return Stream.of(
Arguments.of(1.2f, true),
Arguments.of(0.8f, false)
);
}
}
29 changes: 15 additions & 14 deletions core/src/test/java/com/gdx/game/battle/BattleStatusUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import com.badlogic.gdx.graphics.GL20;
import com.gdx.game.GdxRunner;
import com.gdx.game.profile.ProfileManager;
import com.gdx.game.status.StatusUI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Arrays;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -41,23 +44,21 @@ public void testBattleStatusUI_ShouldSucceed() {
assertThat(Arrays.stream(battleStatusUI.getChildren().items).count()).isEqualTo(24);
}

@Test
public void testSetXpValue_ShouldSucceedWithLevelUp() {
@ParameterizedTest
@MethodSource("xpValue")
void testSetXpValue(int xp, int xpRemainder, int level) {
BattleStatusUI battleStatusUI = new BattleStatusUI();
battleStatusUI.setXPValue(210);
battleStatusUI.setXPValue(xp);

assertThat(battleStatusUI).isNotNull();
assertThat(battleStatusUI.getXPValue()).isEqualTo(10);
assertThat(battleStatusUI.getLevelValue()).isEqualTo(2);
assertThat(battleStatusUI.getXPValue()).isEqualTo(xpRemainder);
assertThat(battleStatusUI.getLevelValue()).isEqualTo(level);
}

@Test
public void testSetXpValue_ShouldSucceedWithTwoLevelUp() {
BattleStatusUI battleStatusUI = new BattleStatusUI();
battleStatusUI.setXPValue(650);

assertThat(battleStatusUI).isNotNull();
assertThat(battleStatusUI.getXPValue()).isEqualTo(50);
assertThat(battleStatusUI.getLevelValue()).isEqualTo(3);
private static Stream<Arguments> xpValue() {
return Stream.of(
Arguments.of(210, 10, 2),
Arguments.of(650, 50, 3)
);
}
}
45 changes: 18 additions & 27 deletions core/src/test/java/com/gdx/game/dialog/ConversationGraphTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import com.gdx.game.GdxRunner;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Hashtable;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -66,37 +70,24 @@ public void testIsValid_ShouldSucceedIsFalse() {
assertThat(isValid).isFalse();
}

@Test
public void testIsReachable_ShouldSucceedIsNotValid() {
Json json = new Json();
String fullFilenamePath = "conversations/conversation004.json";
ConversationGraph graph = json.fromJson(ConversationGraph.class, Gdx.files.internal(fullFilenamePath));

boolean isReachable = graph.isReachable("5", "1");

assertThat(isReachable).isFalse();
}

@Test
public void testIsReachable_ShouldSucceedReturnTrue() {
@ParameterizedTest
@MethodSource("loadConversation")
public void testIsReachable(String fullFilenamePath, String sourceNode, String destinationNode, boolean expectedResult) {
Json json = new Json();
String fullFilenamePath = "conversations/conversation004.json";
ConversationGraph graph = json.fromJson(ConversationGraph.class, Gdx.files.internal(fullFilenamePath));

boolean isReachable = graph.isReachable("1", "2");

assertThat(isReachable).isTrue();
boolean isReachable = graph.isReachable(sourceNode, destinationNode);
assertThat(isReachable).isEqualTo(expectedResult);
}

@Test
public void testIsReachable_ShouldSucceedReturnFalse() {
Json json = new Json();
String fullFilenamePath = "conversations/conversation004.json";
ConversationGraph graph = json.fromJson(ConversationGraph.class, Gdx.files.internal(fullFilenamePath));

boolean isReachable = graph.isReachable("1", "1");

assertThat(isReachable).isFalse();
static Stream<Arguments> loadConversation() {
return Stream.of(
// Test case 1: Source node is not valid
Arguments.of("conversations/conversation004.json", "5", "1", false),
// Test case 2: Valid path exists
Arguments.of("conversations/conversation004.json", "1", "2", true),
// Test case 3: No path exists between the same node
Arguments.of("conversations/conversation004.json", "1", "1", false)
);
}

@Test
Expand Down
Loading

0 comments on commit c5d23a9

Please sign in to comment.