From 7cd16fbf3347367115634d1616cf1e48a6655f6c Mon Sep 17 00:00:00 2001 From: 3013016 Date: Mon, 6 Jan 2025 23:42:41 +0100 Subject: [PATCH] Game UI test --- src/test/java/GameUIControllerTest.java | 157 ++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 src/test/java/GameUIControllerTest.java diff --git a/src/test/java/GameUIControllerTest.java b/src/test/java/GameUIControllerTest.java new file mode 100644 index 0000000..88774f2 --- /dev/null +++ b/src/test/java/GameUIControllerTest.java @@ -0,0 +1,157 @@ +import GUI.GameUIController; +import GUI.HitoriDialogManager; +import javafx.stage.Stage; +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.testfx.framework.junit5.ApplicationExtension; +import org.testfx.framework.junit5.Start; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; +import java.util.Optional; + +@ExtendWith(ApplicationExtension.class) +class GameUIControllerTest { + private GameUIController controller; + private HitoriDialogManager dialogManager; + private Stage stage; + private static final int[][] TEST_BOARD = { + {1, 2, 1}, + {2, 1, 2}, + {1, 2, 1} + }; + + @Start + private void start(Stage stage) { + this.stage = stage; + } + + @BeforeEach + void setUp() { + dialogManager = mock(HitoriDialogManager.class); + controller = new GameUIController(TEST_BOARD, dialogManager); + } + + @Test + void testInitialization() { + assertNotNull(controller); + assertNotNull(controller.getBoardPanel()); + assertNotNull(controller.getControlPanel()); + assertNotNull(controller.getScorePanel()); + assertFalse(controller.isPaused()); + } + + @Test + void testHandleLeftClick() { + controller.handleLeftClick(0, 0); + // Verify the board was updated + assertTrue(controller.getBoardPanel().getChildren().size() > 0); + } + + @Test + void testHandleRightClick() { + controller.handleRightClick(0, 0); + // Verify the board was updated + assertTrue(controller.getBoardPanel().getChildren().size() > 0); + } + + @Test + void testTogglePause() { + assertFalse(controller.isPaused()); + controller.togglePause(); + assertTrue(controller.isPaused()); + controller.togglePause(); + assertFalse(controller.isPaused()); + } + + @Test + void testResetGame() { + // Make some moves first + controller.handleLeftClick(0, 0); + controller.handleRightClick(1, 1); + + controller.resetGame(); + // Verify the board is reset + var boardPanel = controller.getBoardPanel(); + assertEquals(9, boardPanel.getChildren().size()); // 3x3 board + } + + @Test + void testUndoRedo() { + controller.handleLeftClick(0, 0); + controller.undo(); + controller.redo(); + // Verify the board state + assertTrue(controller.getBoardPanel().getChildren().size() > 0); + } + + @Test + void testShowErrors() { + // Create some errors first + controller.handleLeftClick(0, 0); + controller.handleLeftClick(0, 1); + + controller.showErrors(); + verify(dialogManager, times(1)).showAlert(anyString(), anyString()); + } + + @Test + void testSaveGame() { + controller.saveGame(); + verify(dialogManager).showAlert(eq("Game Saved"), anyString()); + } + + @Test + void testDeleteHighScores() { + when(dialogManager.confirmDeleteHighScores()).thenReturn(true); + controller.deleteHighScores(); + verify(dialogManager).showAlert(eq("High Scores Deleted"), anyString()); + } + + @Test + void testHandleWin() { + // Setup mock responses + when(dialogManager.askForPlayerName()).thenReturn(Optional.of("TestPlayer")); + when(dialogManager.confirmNewGame()).thenReturn(false); + + // Make winning moves (this is simplified) + makeWinningMoves(); + + // Verify win handling + verify(dialogManager, times(1)).showAlert(eq("Congratulations!"), anyString()); + } + + @Test + void testConvertErrorsToSet() { + List errors = List.of( + new int[]{0, 0}, + new int[]{1, 1} + ); + var errorSet = controller.convertErrorsToSet(errors); + assertTrue(errorSet.contains("0,0")); + assertTrue(errorSet.contains("1,1")); + } + + @Test + void testCleanup() { + controller.cleanup(); + assertFalse(controller.isPaused()); + } + + // Helper method to simulate winning moves + private void makeWinningMoves() { + // This would need to be implemented according to your winning condition + // For test purposes, we'll just make some moves + controller.handleLeftClick(0, 2); + controller.handleRightClick(0, 0); + controller.handleRightClick(0, 1); + controller.handleRightClick(1, 0); + controller.handleRightClick(1, 1); + controller.handleRightClick(1, 2); + controller.handleRightClick(2, 0); + controller.handleRightClick(2, 1); + controller.handleLeftClick(2, 2); + } +} -- 2.51.0