Skip to content

Commit 1d1eeae

Browse files
author
Arun Prasaad
committed
Add test for end of level 10
1 parent 7d5e5f7 commit 1d1eeae

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/main/java/ihm/SokobanWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ else if (input == GameAction.STOP) {
9999
}
100100

101101
private void handleEndOfLevel() {
102-
if (controller.isOnCustomLevel()) {
102+
if (controller.isOnCustomLevel() || controller.getLevel() == 10) {
103103

104104
JDialog dialog = new JDialog(this, "Level Complete!", true);
105105
okButton.addActionListener(_ -> dialog.dispose());

src/test/java/ihm/SokobanWindowTest.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.nio.file.Paths;
1818

1919
import static org.assertj.core.api.BDDAssertions.then;
20+
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
2021

2122
import java.util.Arrays;
2223

@@ -361,7 +362,7 @@ void completing_level_1_loads_next_level() {
361362
// Simulate the user clicking Next Level to proceed to the next level
362363
// This has to be done before the move to push the box to the target
363364
// because the dialog will wait for user input
364-
Timer timer = new Timer(10, _ -> window.clickNextLevelButton());
365+
Timer timer = new Timer(100, _ -> window.clickNextLevelButton());
365366
timer.setRepeats(false);
366367
SwingUtilities.invokeLater(timer::start);
367368

@@ -377,6 +378,37 @@ void completing_level_1_loads_next_level() {
377378
.anyMatch(w -> w instanceof SokobanWindow sw && sw.getTitle().equals("Level 2"));
378379
}
379380

381+
@Test
382+
void no_more_levels_after_completing_level_10() {
383+
// given - create a test window with the simplified test level
384+
window.dispose();
385+
window = createTestWindow("src/test/resources/levels/level10.txt");
386+
387+
// Simulate the user clicking Next Level to proceed to the next level
388+
// This has to be done before the move to push the box to the target
389+
// because the dialog will wait for user input
390+
Timer timer = new Timer(100, _ -> window.clickOkButton());
391+
timer.setRepeats(false);
392+
SwingUtilities.invokeLater(timer::start);
393+
394+
// When:
395+
// Perform a single move to push the box onto the target and end the level
396+
397+
assertTimeoutPreemptively(
398+
java.time.Duration.ofSeconds(2),
399+
() -> pressKey(KeyEvent.VK_RIGHT),
400+
"Too long to complete the level"
401+
);
402+
403+
then(window.isShowing())
404+
.as("Current window is disposed after level completion")
405+
.isFalse();
406+
407+
then(Window.getWindows())
408+
.as("Contains a visible HomeWindow")
409+
.anyMatch(w -> w instanceof HomeWindow && w.isShowing());
410+
}
411+
380412
private void pressKey(int keyCode) {
381413
KeyEvent keyEvent = new KeyEvent(window,
382414
KeyEvent.KEY_PRESSED,

0 commit comments

Comments
 (0)