|
| 1 | +package ihm; |
| 2 | + |
| 3 | +import logic.Controller; |
| 4 | +import logic.Warehouse; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import javax.swing.*; |
| 9 | +import java.awt.event.KeyEvent; |
| 10 | +import java.nio.file.Paths; |
| 11 | + |
| 12 | +import static org.assertj.core.api.BDDAssertions.then; |
| 13 | + |
| 14 | +class SokobanWindowTest { |
| 15 | + |
| 16 | + private SokobanWindow window; |
| 17 | + private Controller controller; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + void setUp() { |
| 21 | + // Use a test level file |
| 22 | + String testLevelPath = Paths.get("src/test/resources/levels/test_level.txt").toAbsolutePath().toString(); |
| 23 | + controller = new Controller(testLevelPath); |
| 24 | + window = new SokobanWindow(controller); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void window_has_correct_initial_state() { |
| 29 | + // then |
| 30 | + then(window.isVisible()).isTrue(); |
| 31 | + then(window.isResizable()).isFalse(); |
| 32 | + then(window.getDefaultCloseOperation()).isEqualTo(JFrame.EXIT_ON_CLOSE); |
| 33 | + |
| 34 | + // Verify the window size is set based on the warehouse |
| 35 | + Warehouse warehouse = controller.warehouse; |
| 36 | + int columns = warehouse.getColumns(); |
| 37 | + int lines = warehouse.getLines(); |
| 38 | + int imageWidth = columns * SokobanWindow.IMAGE_SIZE; |
| 39 | + int imageHeight = lines * SokobanWindow.IMAGE_SIZE; |
| 40 | + |
| 41 | + // Verify window is at least as large as the game content |
| 42 | + then(window.getSize().width).isGreaterThanOrEqualTo(imageWidth); |
| 43 | + then(window.getSize().height).isGreaterThanOrEqualTo(imageHeight); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void handles_right_direction_key_press() { |
| 48 | + // given - initial positions |
| 49 | + int initialLine = controller.worker.getLine(); |
| 50 | + int initialColumn = controller.worker.getColumn(); |
| 51 | + |
| 52 | + // Move right - should push the box |
| 53 | + KeyEvent rightKey = new KeyEvent(window, |
| 54 | + KeyEvent.KEY_PRESSED, |
| 55 | + System.currentTimeMillis(), |
| 56 | + 0, |
| 57 | + KeyEvent.VK_RIGHT, |
| 58 | + KeyEvent.CHAR_UNDEFINED); |
| 59 | + |
| 60 | + // when |
| 61 | + window.keyPressed(rightKey); |
| 62 | + |
| 63 | + // then - verify worker moved right (pushing the box) |
| 64 | + then(controller.worker.getLine()).isEqualTo(initialLine); |
| 65 | + then(controller.worker.getColumn()).isEqualTo(initialColumn + 1); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void handles_left_direction_key_press() { |
| 70 | + // given - initial positions |
| 71 | + int initialLine = controller.worker.getLine(); |
| 72 | + int initialColumn = controller.worker.getColumn(); |
| 73 | + |
| 74 | + // Move left |
| 75 | + KeyEvent leftKey = new KeyEvent(window, |
| 76 | + KeyEvent.KEY_PRESSED, |
| 77 | + System.currentTimeMillis(), |
| 78 | + 0, |
| 79 | + KeyEvent.VK_LEFT, |
| 80 | + KeyEvent.CHAR_UNDEFINED); |
| 81 | + |
| 82 | + // when |
| 83 | + window.keyPressed(leftKey); |
| 84 | + |
| 85 | + // then - verify worker moved left |
| 86 | + then(controller.worker.getLine()).isEqualTo(initialLine); |
| 87 | + then(controller.worker.getColumn()).isEqualTo(initialColumn - 1); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void handles_up_direction_key_press() { |
| 92 | + // given - initial positions |
| 93 | + int initialLine = controller.worker.getLine(); |
| 94 | + int initialColumn = controller.worker.getColumn(); |
| 95 | + |
| 96 | + // Move up |
| 97 | + KeyEvent upKey = new KeyEvent(window, |
| 98 | + KeyEvent.KEY_PRESSED, |
| 99 | + System.currentTimeMillis(), |
| 100 | + 0, |
| 101 | + KeyEvent.VK_UP, |
| 102 | + KeyEvent.CHAR_UNDEFINED); |
| 103 | + |
| 104 | + // when |
| 105 | + window.keyPressed(upKey); |
| 106 | + |
| 107 | + // then - verify worker moved up |
| 108 | + then(controller.worker.getLine()).isEqualTo(initialLine - 1); |
| 109 | + then(controller.worker.getColumn()).isEqualTo(initialColumn); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void handles_down_direction_key_press() { |
| 114 | + // given - initial positions |
| 115 | + int initialLine = controller.worker.getLine(); |
| 116 | + int initialColumn = controller.worker.getColumn(); |
| 117 | + |
| 118 | + // Move down |
| 119 | + KeyEvent downKey = new KeyEvent(window, |
| 120 | + KeyEvent.KEY_PRESSED, |
| 121 | + System.currentTimeMillis(), |
| 122 | + 0, |
| 123 | + KeyEvent.VK_DOWN, |
| 124 | + KeyEvent.CHAR_UNDEFINED); |
| 125 | + |
| 126 | + // when |
| 127 | + window.keyPressed(downKey); |
| 128 | + |
| 129 | + // then - verify worker moved down |
| 130 | + then(controller.worker.getLine()).isEqualTo(initialLine + 1); |
| 131 | + then(controller.worker.getColumn()).isEqualTo(initialColumn); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void handles_restart_action() { |
| 136 | + // given |
| 137 | + int initialColumn = controller.worker.getColumn(); |
| 138 | + KeyEvent rightKey = new KeyEvent(window, |
| 139 | + KeyEvent.KEY_PRESSED, |
| 140 | + System.currentTimeMillis(), |
| 141 | + 0, |
| 142 | + KeyEvent.VK_RIGHT, |
| 143 | + KeyEvent.CHAR_UNDEFINED); |
| 144 | + window.keyPressed(rightKey); |
| 145 | + int columnAfterMove = controller.worker.getColumn(); |
| 146 | + |
| 147 | + // when - press backspace to restart |
| 148 | + KeyEvent restartKey = new KeyEvent(window, |
| 149 | + KeyEvent.KEY_PRESSED, |
| 150 | + System.currentTimeMillis(), |
| 151 | + 0, |
| 152 | + KeyEvent.VK_BACK_SPACE, |
| 153 | + KeyEvent.CHAR_UNDEFINED); |
| 154 | + window.keyPressed(restartKey); |
| 155 | + |
| 156 | + // then - worker should be back at initial position |
| 157 | + then(controller.worker.getColumn()).isNotEqualTo(columnAfterMove); |
| 158 | + then(controller.worker.getColumn()).isEqualTo(initialColumn); |
| 159 | + } |
| 160 | +} |
0 commit comments