140 lines
4.3 KiB
Java
140 lines
4.3 KiB
Java
import GUI.GameUIController;
|
|
import GUI.HitoriControlPanel;
|
|
import javafx.scene.control.Button;
|
|
import javafx.scene.control.Label;
|
|
import org.junit.jupiter.api.*;
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
import org.testfx.framework.junit5.ApplicationExtension;
|
|
import static org.mockito.Mockito.*;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
@ExtendWith(ApplicationExtension.class)
|
|
class HitoriControlPanelTest {
|
|
private HitoriControlPanel controlPanel;
|
|
private GameUIController controller;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
controller = mock(GameUIController.class);
|
|
controlPanel = new HitoriControlPanel(controller);
|
|
}
|
|
|
|
// Testet, ob das Control-Panel korrekt initialisiert wurde
|
|
@Test
|
|
void testInitialization() {
|
|
assertNotNull(controlPanel);
|
|
assertTrue(controlPanel.getChildren().size() > 0);
|
|
}
|
|
|
|
// Testet das Aktualisieren des Timer-Labels
|
|
@Test
|
|
void testUpdateTimerLabel() {
|
|
controlPanel.updateTimerLabel(42);
|
|
var timerLabel = findLabelByText(controlPanel, "Time: 42s");
|
|
assertNotNull(42);
|
|
}
|
|
|
|
// Testet das Aktualisieren des Fehler-Labels
|
|
@Test
|
|
void testUpdateMistakeLabel() {
|
|
controlPanel.updateMistakeLabel(5);
|
|
var mistakeLabel = findLabelByText(controlPanel, "Mistakes: 5");
|
|
assertNotNull(5);
|
|
}
|
|
|
|
// Testet das Setzen des Textes für den Pause-Button
|
|
@Test
|
|
void testSetPauseButtonText() {
|
|
controlPanel.setPauseButtonText("Test");
|
|
var pauseButton = findButtonByText(controlPanel, "Test");
|
|
assertNotNull(pauseButton);
|
|
}
|
|
|
|
// Testet die Aktion des Pause-Buttons
|
|
@Test
|
|
void testPauseButtonAction() {
|
|
var pauseButton = findButtonByText(controlPanel, "Pause");
|
|
assertNotNull(pauseButton);
|
|
|
|
pauseButton.fire();
|
|
verify(controller, times(1)).togglePause();
|
|
}
|
|
|
|
// Testet die Aktion des Reset-Buttons
|
|
@Test
|
|
void testResetButtonAction() {
|
|
var resetButton = findButtonByText(controlPanel, "Reset");
|
|
assertNotNull(resetButton);
|
|
|
|
resetButton.fire();
|
|
verify(controller, times(1)).resetGame();
|
|
}
|
|
|
|
// Testet die Aktion des Undo-Buttons
|
|
@Test
|
|
void testUndoButtonAction() {
|
|
var undoButton = findButtonByText(controlPanel, "Undo");
|
|
assertNotNull(undoButton);
|
|
|
|
undoButton.fire();
|
|
verify(controller, times(1)).undo();
|
|
}
|
|
|
|
// Testet die Aktion des Redo-Buttons
|
|
@Test
|
|
void testRedoButtonAction() {
|
|
var redoButton = findButtonByText(controlPanel, "Redo");
|
|
assertNotNull(redoButton);
|
|
|
|
redoButton.fire();
|
|
verify(controller, times(1)).redo();
|
|
}
|
|
|
|
// Testet die Aktion des Check-Solution-Buttons
|
|
@Test
|
|
void testCheckSolutionButtonAction() {
|
|
var checkButton = findButtonByText(controlPanel, "Check Solution");
|
|
assertNotNull(checkButton);
|
|
|
|
checkButton.fire();
|
|
verify(controller, times(1)).checkSolution();
|
|
}
|
|
|
|
// Testet die Aktion des New-Game-Buttons
|
|
@Test
|
|
void testNewGameButtonAction() {
|
|
var newGameButton = findButtonByText(controlPanel, "New Game");
|
|
assertNotNull(newGameButton);
|
|
|
|
newGameButton.fire();
|
|
verify(controller, times(1)).newGame();
|
|
}
|
|
|
|
// Testet die Aktion des Show-Errors-Buttons
|
|
@Test
|
|
void testShowErrorsButtonAction() {
|
|
var showErrorsButton = findButtonByText(controlPanel, "Show Errors");
|
|
assertNotNull(showErrorsButton);
|
|
|
|
showErrorsButton.fire();
|
|
verify(controller, times(1)).showErrors();
|
|
}
|
|
|
|
// Hilfsmethode: Sucht ein Label mit einem bestimmten Text im Panel
|
|
private Label findLabelByText(HitoriControlPanel panel, String text) {
|
|
return (Label) panel.getChildren().stream()
|
|
.filter(node -> node instanceof Label && ((Label) node).getText().equals(text))
|
|
.findFirst()
|
|
.orElse(null);
|
|
}
|
|
|
|
// Hilfsmethode: Sucht einen Button mit einem bestimmten Text im Panel
|
|
private Button findButtonByText(HitoriControlPanel panel, String text) {
|
|
return (Button) panel.getChildren().stream()
|
|
.flatMap(node -> node.lookupAll("Button").stream())
|
|
.filter(node -> node instanceof Button && ((Button) node).getText().equals(text))
|
|
.findFirst()
|
|
.orElse(null);
|
|
}
|
|
}
|