From 2fd55e903ff65a03f11c93945b2e26d7c6e13089 Mon Sep 17 00:00:00 2001 From: 3013016 Date: Mon, 6 Jan 2025 23:44:23 +0100 Subject: [PATCH] Board Panel GUI Test --- src/test/java/HitoriBoardPanelTest.java | 114 ++++++++++++++++++++++++ src/test/java/HitoriGameScoresTest.java | 1 + 2 files changed, 115 insertions(+) create mode 100644 src/test/java/HitoriBoardPanelTest.java diff --git a/src/test/java/HitoriBoardPanelTest.java b/src/test/java/HitoriBoardPanelTest.java new file mode 100644 index 0000000..39124f8 --- /dev/null +++ b/src/test/java/HitoriBoardPanelTest.java @@ -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()); + } +} diff --git a/src/test/java/HitoriGameScoresTest.java b/src/test/java/HitoriGameScoresTest.java index 8b0d4bc..0e27e9c 100644 --- a/src/test/java/HitoriGameScoresTest.java +++ b/src/test/java/HitoriGameScoresTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.File; import java.util.List; + class HitoriGameScoresTest { private HitoriGameScores scores;