Skip to content

Commit

Permalink
Update modals and fix headless issues (#99)
Browse files Browse the repository at this point in the history
* fix(framework): Tweaks modals for better compatibility

* test(framework): Adjust tests

* docs(testing): Add more information about headless tests
  • Loading branch information
LeStegii authored May 16, 2024
1 parent 2901ba6 commit a14b9bf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
1 change: 1 addition & 0 deletions docs/testing/1-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ test {
```

Whenever the tests are ran with `CI=true`, headless mode will be enabled allowing for testing in CI environments like GitHub Actions.
In order to fix some issues with tests running in headless mode, make sure to set the stage's coordinates to a value like (0, 0) because otherwise some calculations can fail due to the position being NaN.

## Mockito

Expand Down
2 changes: 2 additions & 0 deletions docs/testing/2-controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class ControllerTest extends ApplicationTest {
public void start(Stage stage) throws Exception {
super.start(stage);
this.stage = stage;
stage.setX(0); // This fixes potential issues in headless environments
stage.setY(0);
app.start(stage);
stage.requestFocus(); // Make the test use the correct stage
}
Expand Down
28 changes: 22 additions & 6 deletions framework/src/main/java/org/fulib/fx/controller/Modals.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import org.fulib.fx.FulibFxApp;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -89,10 +91,10 @@ public static <Display extends Parent> void showModal(FulibFxApp app, Stage curr
* When closing the window, the controller will be destroyed. If the {@link Stage#setOnCloseRequest(EventHandler)}
* method is overridden, the controller will not be destroyed automatically.
*
* @param app the app instance
* @param component the class of the controller to show
* @param params the parameters to pass to the controller
* @param <Display> the type of the controller
* @param app the app instance
* @param component the class of the controller to show
* @param params the parameters to pass to the controller
* @param <Display> the type of the controller
*/
public static <Display extends Parent> void showModal(FulibFxApp app, Display component, Map<String, Object> params) {
showModal(app, app.stage(), component, (stage, controller) -> {
Expand Down Expand Up @@ -195,9 +197,10 @@ public static <Display extends Node> void showModal(FulibFxApp app, Stage curren
modalStage.setScene(scene);
modalStage.initStyle(StageStyle.TRANSPARENT);
modalStage.initOwner(currentStage);
modalStage.initModality(Modality.WINDOW_MODAL);
modalStage.show();
modalStage.initModality(Modality.APPLICATION_MODAL);
modalStage.requestFocus();
modalStage.show();
modalStage.setAlwaysOnTop(true);
initializer.accept(modalStage, component);
});
}
Expand Down Expand Up @@ -226,4 +229,17 @@ public void hide() {

}

/**
* Returns a list of all visible modal stages
*
* @return A list of all visible modal stages
*/
public static List<ModalStage> getModalStages() {
return Window.getWindows()
.stream()
.filter(window -> window instanceof Modals.ModalStage)
.map(window -> (ModalStage) window)
.toList();
}

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

import javafx.stage.Stage;
import org.fulib.fx.controller.Subscriber;
import org.mockito.Spy;
import org.testfx.framework.junit5.ApplicationTest;

import java.util.Locale;
import java.util.ResourceBundle;

public class ControllerTest extends ApplicationTest {

@Spy
protected App app = new App();

@Spy
Subscriber subscriber;

@Spy
ResourceBundle bundle = ResourceBundle.getBundle(
"de/uniks/ludo/lang/lang",
Locale.ENGLISH
);

protected Stage stage;

@Override
public void start(Stage stage) throws Exception {
super.start(stage);
this.stage = stage;
stage.setX(0);
stage.setY(0);
stage.requestFocus();
app.start(stage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.testfx.api.FxAssert.verifyThat;

@ExtendWith(MockitoExtension.class)
public class GameOverControllerTest extends ControllerTest {
@Spy
ResourceBundle bundle = ResourceBundle.getBundle(
"de/uniks/ludo/lang/lang",
Locale.ENGLISH
);

@InjectMocks
GameOverController gameOverController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,28 @@
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import org.fulib.fx.controller.Subscriber;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.ResourceBundle;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.when;
import static org.testfx.api.FxAssert.verifyThat;
import static org.testfx.util.NodeQueryUtils.hasText;

@ExtendWith(MockitoExtension.class)
public class IngameControllerTest extends ControllerTest {

@Spy
ResourceBundle bundle = ResourceBundle.getBundle("de/uniks/ludo/lang/lang", Locale.ENGLISH);
@Spy
GameService gameService;
@Spy
Subscriber subscriber;

@InjectMocks
DiceSubComponent diceSubComponent;

@InjectMocks
IngameController ingameController;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

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

@ExtendWith(MockitoExtension.class)
public class SetupControllerTest extends ControllerTest {
@Spy
ResourceBundle bundle = ResourceBundle.getBundle(
"de/uniks/ludo/lang/lang",
Locale.ENGLISH
);

@InjectMocks
SetupController setupController;
Expand Down

0 comments on commit a14b9bf

Please sign in to comment.