Skip to content

Commit

Permalink
test(ludo): Create app test
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStegii committed Feb 27, 2024
1 parent e0c0a03 commit 7838d94
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 9 deletions.
3 changes: 0 additions & 3 deletions framework/src/main/java/org/fulib/fx/FulibFxApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ protected void display(@NotNull Parent parent) {

// Internal helper method
protected void cleanup() {
System.out.println("cleanup");
System.out.println(this);
System.out.println(frameworkComponent);
this.frameworkComponent.controllerManager().cleanup();
}

Expand Down
16 changes: 15 additions & 1 deletion ludo/src/main/java/de/uniks/ludo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import javafx.stage.Stage;
import org.fulib.fx.FulibFxApp;

import javax.inject.Singleton;
import java.nio.file.Path;
import java.util.logging.Level;

import static javafx.scene.input.KeyEvent.KEY_PRESSED;

@Singleton
public class App extends FulibFxApp {


private final MainComponent component;
private MainComponent component;

public App() {
super();
Expand Down Expand Up @@ -78,4 +80,16 @@ public void stop() {
public MainComponent component() {
return component;
}

/**
* Overrides the dagger component of the application.
*
* @param component The new dagger component
* @return The application itself
*/
public App setComponent(MainComponent component) {
this.component = component;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ private void setupListenerForPiece(Circle circle, Piece piece, String color) {
// Creates a circle for a piece and sets the position to the piece's current field
private Circle createPieceCircle(Piece piece) {
Circle circle = new Circle(16);
circle.setId("piece-" + piece.getOwner().getId() + "-" + piece.getOwner().getPieces().indexOf(piece));
Player player = piece.getOwner();
AnchorPane.setTopAnchor(circle, (double) piece.getOn().getY() * 50 + 4);
AnchorPane.setLeftAnchor(circle, (double) piece.getOn().getX() * 50 + 4);
Expand All @@ -210,6 +211,7 @@ private Circle createPieceCircle(Piece piece) {
// Creates a circle for a field and sets the position to the field's coordinates
private Circle createFieldCircle(Field field) {
Circle circle = new Circle(20);
circle.setId("field-" + field.getX() + "-" + field.getY());
AnchorPane.setTopAnchor(circle, (double) field.getY() * 50);
AnchorPane.setLeftAnchor(circle, (double) field.getX() * 50);
String color = field instanceof HomeField ? "gray" : "white";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.uniks.ludo.controller.sub;

import de.uniks.ludo.service.GameService;
import io.reactivex.rxjava3.core.Observable;
import javafx.animation.Timeline;
import javafx.beans.property.BooleanProperty;
Expand All @@ -20,6 +21,9 @@ public class DiceSubComponent extends VBox {
@FXML
public Label eyesLabel;

@Inject
GameService gameService;

private final BooleanProperty enabled = new SimpleBooleanProperty(true);

private CompletableFuture<Integer> rollFuture;
Expand All @@ -36,7 +40,7 @@ public void onRender() {
this.rollAnimation = new Timeline();
this.rollAnimation.setCycleCount(10);
rollAnimation.getKeyFrames().add(new javafx.animation.KeyFrame(javafx.util.Duration.millis(100), event -> {
int random = (int) (Math.random() * 6 + 1);
int random = gameService.rollRandom();
eyesLabel.setText(String.valueOf(random));
}));
this.rollAnimation.setOnFinished(event -> this.rollFuture.complete(Integer.parseInt(eyesLabel.getText())));
Expand Down
12 changes: 12 additions & 0 deletions ludo/src/main/java/de/uniks/ludo/service/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Optional;
import java.util.Random;

@Singleton
public class GameService {

private final Random random;

@Inject
public GameService() {
this.random = new Random();
}

public GameService(Random random) {
this.random = random;
}

/**
Expand Down Expand Up @@ -165,4 +173,8 @@ public boolean movePiece(Piece piece, Field targetField) {
piece.setOn(targetField);
return isDone(piece.getOwner());
}

public int rollRandom() {
return random.nextInt(6) + 1;
}
}
233 changes: 229 additions & 4 deletions ludo/src/test/java/de/uniks/ludo/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package de.uniks.ludo;


import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.testfx.api.FxAssert;
import org.testfx.framework.junit5.ApplicationTest;

@ExtendWith(MockitoExtension.class)
import static org.testfx.util.WaitForAsyncUtils.waitForFxEvents;

public class AppTest extends ApplicationTest {

@Spy
Expand All @@ -17,13 +19,236 @@ public class AppTest extends ApplicationTest {
@Override
public void start(Stage stage) throws Exception {
super.start(stage);
app.setComponent(DaggerTestComponent.builder().mainApp(app).build());
app.start(stage);
stage.requestFocus();
}

@Test
public void test() {
moveTo("2");;
moveBy(0, -20);
press(MouseButton.PRIMARY);
release(MouseButton.PRIMARY);
clickOn("#startButton");

waitForFxEvents();

FxAssert.verifyThat("#playerLabel", (Label label) -> label.getText().contains("1"));

roll();
clickOn("#piece-1-3");
roll();
clickOn("#piece-1-3");
roll();
clickOn("#piece-1-3");

FxAssert.verifyThat("#playerLabel", (Label label) -> label.getText().contains("2"));

roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-2");
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-3");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
clickOn("#piece-1-2");
roll();
roll();
roll();
roll();
roll();
roll();
roll();
roll();
roll();
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-1-1");
roll();
roll();
clickOn("#piece-1-1");
roll();
roll();
clickOn("#piece-1-1");
roll();
roll();
clickOn("#piece-1-1");
roll();
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-1-1");
roll();
roll();
clickOn("#piece-1-1");
roll();
roll();
clickOn("#piece-1-0");
roll();
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-1-1");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-2");
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-1-0");
roll();
clickOn("#piece-2-0");
roll();
roll();
clickOn("#piece-2-0");
roll();
roll();
clickOn("#piece-2-0");
roll();
roll();
clickOn("#piece-2-0");
roll();
roll();
clickOn("#piece-2-0");
roll();
clickOn("#piece-1-0");

waitForFxEvents();

FxAssert.verifyThat("#playerWonLabel", Node::isVisible);

}

private void roll() {
clickOn("#eyesLabel");;
sleep(1100);
}

}
17 changes: 17 additions & 0 deletions ludo/src/test/java/de/uniks/ludo/TestComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.uniks.ludo;

import dagger.Component;
import de.uniks.ludo.dagger.MainComponent;
import de.uniks.ludo.dagger.MainModule;

import javax.inject.Singleton;

@Component(modules = {MainModule.class, TestModule.class})
@Singleton
public interface TestComponent extends MainComponent {

@Component.Builder
interface Builder extends MainComponent.Builder {
TestComponent build();
}
}
Loading

0 comments on commit 7838d94

Please sign in to comment.