Board Panel GUI Test
parent
7cd16fbf33
commit
2fd55e903f
|
|
@ -0,0 +1,114 @@
|
||||||
|
import domain.GameSolver;
|
||||||
|
import domain.HitoriGameMoves;
|
||||||
|
import GUI.GameUIController;
|
||||||
|
import GUI.HitoriBoardPanel;
|
||||||
|
import javafx.scene.control.Button;
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
import javafx.scene.input.MouseButton;
|
||||||
|
import javafx.scene.input.MouseEvent;
|
||||||
|
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 HitoriBoardPanelTest {
|
||||||
|
private HitoriBoardPanel boardPanel;
|
||||||
|
private HitoriGameMoves gameMoves;
|
||||||
|
private GameSolver gameSolver;
|
||||||
|
private GameUIController controller;
|
||||||
|
private static final int[][] TEST_BOARD = {
|
||||||
|
{1, 2, 1},
|
||||||
|
{2, 1, 2},
|
||||||
|
{1, 2, 1}
|
||||||
|
};
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
gameMoves = new HitoriGameMoves(TEST_BOARD);
|
||||||
|
gameSolver = new GameSolver(TEST_BOARD);
|
||||||
|
controller = mock(GameUIController.class);
|
||||||
|
boardPanel = new HitoriBoardPanel(gameMoves, gameSolver, controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testInitialization() {
|
||||||
|
assertNotNull(boardPanel);
|
||||||
|
assertEquals(9, boardPanel.getChildren().size()); // 3x3 board = 9 buttons
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCellCreation() {
|
||||||
|
var children = boardPanel.getChildren();
|
||||||
|
var firstButton = (Button) children.get(0);
|
||||||
|
assertEquals("1", firstButton.getText());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testButtonStyling() {
|
||||||
|
var children = boardPanel.getChildren();
|
||||||
|
var button = (Button) children.get(0);
|
||||||
|
assertEquals("-fx-background-color: lightgray; -fx-text-fill: black;", button.getStyle());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateBoard() {
|
||||||
|
boardPanel.updateBoard();
|
||||||
|
assertEquals(9, boardPanel.getChildren().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testShowErrors() {
|
||||||
|
// Mark some cells as black to create errors
|
||||||
|
gameMoves.markCellAsBlack(0, 0);
|
||||||
|
gameMoves.markCellAsBlack(0, 1);
|
||||||
|
|
||||||
|
boardPanel.showErrors();
|
||||||
|
|
||||||
|
// Verify that error cells are marked red
|
||||||
|
var children = boardPanel.getChildren();
|
||||||
|
for (var child : children) {
|
||||||
|
Button button = (Button) child;
|
||||||
|
if (button.getStyle().contains("red")) {
|
||||||
|
assertTrue(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLeftClickHandling() {
|
||||||
|
when(controller.isPaused()).thenReturn(false);
|
||||||
|
var button = (Button) boardPanel.getChildren().get(0);
|
||||||
|
|
||||||
|
MouseEvent event = mock(MouseEvent.class);
|
||||||
|
when(event.getButton()).thenReturn(MouseButton.PRIMARY);
|
||||||
|
button.fireEvent(event);
|
||||||
|
|
||||||
|
verify(controller, times(0)).handleLeftClick(anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRightClickHandling() {
|
||||||
|
when(controller.isPaused()).thenReturn(false);
|
||||||
|
var button = (Button) boardPanel.getChildren().get(0);
|
||||||
|
|
||||||
|
MouseEvent event = mock(MouseEvent.class);
|
||||||
|
when(event.getButton()).thenReturn(MouseButton.SECONDARY);
|
||||||
|
button.fireEvent(event);
|
||||||
|
|
||||||
|
verify(controller, times(0)).handleRightClick(anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPausedState() {
|
||||||
|
when(controller.isPaused()).thenReturn(true);
|
||||||
|
var button = (Button) boardPanel.getChildren().get(0);
|
||||||
|
|
||||||
|
MouseEvent event = mock(MouseEvent.class);
|
||||||
|
when(event.getButton()).thenReturn(MouseButton.PRIMARY);
|
||||||
|
button.fireEvent(event);
|
||||||
|
|
||||||
|
verify(controller, never()).handleLeftClick(anyInt(), anyInt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
class HitoriGameScoresTest {
|
class HitoriGameScoresTest {
|
||||||
private HitoriGameScores scores;
|
private HitoriGameScores scores;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue