game-of-prod/src/test/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMoveTest.java
2020-06-10 14:27:49 +02:00

328 lines
11 KiB
Java

package ruscz.game.gameOfProd.Windows;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import ruscz.game.gameOfProd.Cells.Person;
import ruscz.game.gameOfProd.Cells.TriggerCell;
import ruscz.game.gameOfProd.Game;
import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Save.SaveManager;
import javax.swing.*;
import java.util.stream.Stream;
import static ruscz.game.gameOfProd.Windows.WindowOfFreeMove.sleep;
public class WindowOfFreeMoveTest {
public static final int KEY_LEFT = 37;
public static final int KEY_UP = 38;
public static final int KEY_RIGHT = 39;
public static final int KEY_DOWN = 40;
public static final int KEY_EVENT = 69;
private Timer timer;
private Game game;
private WindowOfFreeMove freeMove;
private Map testMap;
private Person person;
@BeforeEach
public void setUp() {
game = new Game();
freeMove = new WindowOfFreeMove(game);
game.add(freeMove);
WindowOfFreeMove.setCHAPTER("999");
freeMove.start(true);
testMap = Map.findMapByName("m6",freeMove);
freeMove.setMapManager(testMap);
person = freeMove.getMapManager().getActualMap().getPerson();
}
@AfterEach
public void cleanUp() {
game.dispose();
}
public static Stream<Arguments> argumentsStream() {
return Stream.of(
Arguments.of(new int[]{KEY_UP}, 0, -1),
Arguments.of(new int[]{KEY_DOWN}, 0, 1),
Arguments.of(new int[]{KEY_LEFT}, -1, 0),
Arguments.of(new int[]{KEY_RIGHT}, 1, 0),
Arguments.of(new int[]{KEY_RIGHT, KEY_UP}, 1, 0),
Arguments.of(new int[]{KEY_RIGHT, KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_LEFT, KEY_DOWN}, 0, 1),
Arguments.of(new int[]{KEY_UP, KEY_UP, KEY_RIGHT, KEY_LEFT, KEY_RIGHT, KEY_RIGHT, KEY_UP}, 0, -3),
Arguments.of(new int[]{KEY_LEFT, KEY_UP, KEY_RIGHT, KEY_DOWN, KEY_UP, KEY_RIGHT, KEY_RIGHT}, 2, 0),
Arguments.of(new int[]{KEY_LEFT, KEY_LEFT, KEY_LEFT, KEY_LEFT}, -4, 0),
Arguments.of(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 0, 0)
);
}
/**
* @param directions is field of inputs
* @param xPath is expected distance walked on X axis
* @param yPath is expected distance walked on Y axis
* Process test that simulates hero's movement on a map, using input from player
*/
@ParameterizedTest
@MethodSource("argumentsStream")
public void move_moveCharacterOnMap_CharacterChangesPositionWithCollisionDetection(int[] directions, int xPath, int yPath) {
//Arrage
freeMove.revalidate();
//Act
final int[] i = {0};
timer = new Timer(10, actionEvent -> {
if (!person.getIsMoving()) {
if (i[0] == directions.length) {
stopTimer();
}
else {
person.setDirectionMove(directions[i[0]]);
i[0]++;
}
}
});
timer.start();
while(timer.isRunning()) {
continue;
}
//Assert
Assertions.assertEquals(testMap.getSpawnPoint()[0] + xPath, person.getX());
Assertions.assertEquals(testMap.getSpawnPoint()[1] + yPath, person.getY());
}
/**
* Helper method that stops timer
*/
private void stopTimer() {
timer.stop();
}
/**
* Process test that simulates hero's transition to a different map using MOVETO cell
*/
@Test
public void moveToMap_CharacterMovesToDifferentMap_CharacterIsOnCorrectMap() {
//Arrange
int[] directions = {KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN};
//Act
final int[] i = {0};
timer = new Timer(10, actionEvent -> {
if (!person.getIsMoving()) {
if (i[0] == directions.length) {
stopTimer();
}
else {
person.setDirectionMove(directions[i[0]]);
i[0]++;
}
}
});
timer.start();
while(timer.isRunning() || person.getIsMoving()) {
continue;
}
sleep(50);
//Assert
Assertions.assertEquals("m1", freeMove.getMapManager().getActualMap().getName());
//Act
final boolean[] flag = {false};
timer = new Timer(10, actionEvent -> {
if (!freeMove.getMapManager().getActualMap().getPerson().getIsMoving() && WindowOfFreeMove.canPersonMove()) {
if (!flag[0]) {
freeMove.getMapManager().getActualMap().getPerson().setDirectionMove(KEY_DOWN);
flag[0] = true;
}
else {
stopTimer();
}
}
});
timer.start();
while(timer.isRunning() || freeMove.getMapManager().getActualMap().getPerson().getIsMoving()) {
continue;
}
//Assert
Assertions.assertEquals("m6", freeMove.getMapManager().getActualMap().getName());
}
/**
* Process test that simulates hero's interaction with a TRIGGER cell and passage through the running event
*/
@Test
public void startEvent_CharacterEntersEventThenGoesThroughEventThenExitsEvent_CorrectCharacterResponses() {
//Arrange
int[] directions = {KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP, KEY_UP};
//Act
final int[] i = {0};
timer = new Timer(10, actionEvent -> {
if (!person.getIsMoving()) {
if (i[0] == directions.length) {
stopTimer();
}
else {
person.setDirectionMove(directions[i[0]]);
i[0]++;
}
}
});
timer.start();
while(timer.isRunning() || person.getIsMoving()) {
continue;
}
sleep(50);
TriggerCell triggerCell = testMap.findTriggerByXY(person.getX(), person.getY());
//Assert
Assertions.assertTrue(person.isInEvent());
Assertions.assertFalse(person.getIsMoving());
Assertions.assertFalse(triggerCell == null);
//Act
person.setDirectionMove(KEY_LEFT);
//Assert
Assertions.assertTrue(person.isInEvent());
Assertions.assertEquals(triggerCell.getX(), person.getX());
Assertions.assertEquals(triggerCell.getY(), person.getY());
//Act
timer = new Timer(100, actionEvent -> {
if (!person.isInEvent()) {
stopTimer();
}
else {
if (!triggerCell.getDialog().getTimerIsRunning()) {
person.setDirectionMove(KEY_EVENT);
}
}
});
timer.start();
while(timer.isRunning() || person.isInEvent()) {
continue;
}
final boolean[] flag = {false};
timer = new Timer(10, actionEvent -> {
if (!person.getIsMoving()) {
if (!flag[0]) {
person.setDirectionMove(KEY_LEFT);
flag[0] = true;
}
else {
stopTimer();
}
}
});
timer.start();
while(timer.isRunning() || person.getIsMoving()) {
continue;
}
//Assert
Assertions.assertEquals(triggerCell.getX() - 1, person.getX());
Assertions.assertEquals(triggerCell.getY(), person.getY());
}
/**
* Process test that simulates heros's interaction with a TRIGGER cell, transition to a different map and saving progress in game
*/
@Test
public void saveGame_CharacterInteractsWithTriggerCellThenMovesToDifferentMapThenGameIsSaved_CorrectlySavedProgress() {
//Assert
SaveManager saveManager;
//Act
startEvent_CharacterEntersEventThenGoesThroughEventThenExitsEvent_CorrectCharacterResponses();
int[] directions = {KEY_RIGHT, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN, KEY_DOWN};
final int[] i = {0};
timer = new Timer(10, actionEvent -> {
if (!person.getIsMoving()) {
if (i[0] == directions.length) {
stopTimer();
}
else {
person.setDirectionMove(directions[i[0]]);
i[0]++;
}
}
});
timer.start();
while(timer.isRunning() || person.getIsMoving()) {
continue;
}
moveToMap_CharacterMovesToDifferentMap_CharacterIsOnCorrectMap();
final boolean[] flag = {false};
timer = new Timer(10, actionEvent -> {
if (!freeMove.getMapManager().getActualMap().getPerson().getIsMoving() && WindowOfFreeMove.canPersonMove()) {
if (!flag[0]) {
freeMove.getMapManager().getActualMap().getPerson().setDirectionMove(KEY_DOWN);
flag[0] = true;
}
else {
stopTimer();
}
}
});
timer.start();
while(timer.isRunning() || freeMove.getMapManager().getActualMap().getPerson().getIsMoving()) {
continue;
}
//Assert
Assertions.assertEquals("m1", freeMove.getMapManager().getActualMap().getName());
//Act
person = freeMove.getMapManager().getActualMap().getPerson();
String[] personCoords = new String[]{freeMove.getMapManager().getActualMap().getName(), String.valueOf(person.getX()),
String.valueOf(person.getY()), String.valueOf(person.getDirectionSight())};
boolean triggerIsActive = testMap.findTriggerByXY(11, 11).getActive();
saveManager = new SaveManager(freeMove.getMapManager());
saveManager.saveGame();
game.dispose();
game = new Game();
freeMove = new WindowOfFreeMove(game);
game.add(freeMove);
WindowOfFreeMove.setCHAPTER("999");
WindowOfFreeMove.setCanvasSize(new int[]{50, 50});
freeMove.start(false);
sleep(1000);
//Assert
Assertions.assertEquals(personCoords[0], freeMove.getMapManager().getActualMap().getName());
Assertions.assertEquals(personCoords[1], String.valueOf(freeMove.getMapManager().getActualMap().getPerson().getX()));
Assertions.assertEquals(personCoords[2], String.valueOf(freeMove.getMapManager().getActualMap().getPerson().getY()));
Assertions.assertEquals(personCoords[3], String.valueOf(freeMove.getMapManager().getActualMap().getPerson().getDirectionSight()));
Assertions.assertEquals(triggerIsActive, Map.findMapByName("m6", freeMove).findTriggerByXY(11, 11).getActive());
}
}