Skip to content

Commit 7d5e5f7

Browse files
author
Arun Prasaad
committed
Verify moving to level 2 after the end of level 1
1 parent e37e2a5 commit 7d5e5f7

File tree

4 files changed

+77
-21
lines changed

4 files changed

+77
-21
lines changed

.cascade

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ avoid_unnecessary_getters = true
1010
# Method ordering: helper methods after their callers
1111
# Test names should be propositional (e.g., "returns_true_when_condition_met")
1212
propositional_test_names = true
13-
# Test descriptions should be propositional (e.g., "returns_initial_position_after_undo")
14-
propositional_test_descriptions = true
1513
helper_methods_after_caller = true
1614

1715
[editing]
@@ -25,6 +23,13 @@ align_related_assignments = true
2523
# Avoid comments that state the obvious or repeat the code
2624
avoid_superfluous_comments = true
2725

26+
[testing]
27+
# Assertion messages should be propositional statements
28+
# - Should be clear, testable statements of fact
29+
# - Present tense is preferred (e.g., "is" instead of "should be")
30+
# - Should describe the expected state/behavior
31+
propositional_assertion_messages = true
32+
2833
[communication]
2934
# Be direct and to the point
3035
# Don't suggest alternatives unless asked

src/main/java/ihm/SokobanWindow.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.awt.event.KeyEvent;
66
import java.awt.event.KeyListener;
7-
import java.awt.image.BufferedImage;
87
import java.util.ArrayList;
98
import java.util.List;
109
import javax.swing.*;
@@ -19,7 +18,8 @@
1918
public class SokobanWindow extends JFrame implements KeyListener{
2019

2120
public static final int IMAGE_SIZE = 32;
22-
private final JButton okButton;
21+
private final JButton okButton = new JButton("OK");
22+
private final JButton nextLevelButton = new JButton("Next level");
2323

2424
public Controller getController() {
2525
return controller;
@@ -49,7 +49,6 @@ public SokobanWindow(Controller controller) {
4949
this.pack();
5050
this.setLocationRelativeTo( null );
5151
this.setVisible( true );
52-
okButton = new JButton("OK");
5352
}
5453

5554
@Override
@@ -129,24 +128,36 @@ void clickOkButton() {
129128
okButton.doClick();
130129
}
131130

131+
@VisibleForTesting
132+
void clickNextLevelButton() {
133+
nextLevelButton.doClick();
134+
}
135+
132136
private void handleNextLevel() {
133-
Object[] options = {"Next level", "Quit"};
134-
Image image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
135-
int result = JOptionPane.showOptionDialog(this,
136-
"You won!",
137-
"End of level",
138-
JOptionPane.YES_NO_OPTION,
139-
JOptionPane.QUESTION_MESSAGE,
140-
new ImageIcon(image),
141-
options,
142-
options[0]);
143-
if (result == JOptionPane.YES_OPTION) {
137+
JDialog dialog = new JDialog(this, "Level Complete!", true);
138+
139+
nextLevelButton.addActionListener(_ -> {
140+
dialog.dispose();
144141
this.dispose();
145142
new SokobanWindow(this.controller.nextLevel());
146-
}
147-
else if (result == JOptionPane.NO_OPTION) {
143+
});
144+
145+
JButton quitButton = new JButton("Quit");
146+
quitButton.addActionListener(_ -> {
147+
dialog.dispose();
148148
exitGame();
149-
}
149+
});
150+
151+
JOptionPane.showOptionDialog(
152+
dialog,
153+
"You won!",
154+
"Level Complete!",
155+
JOptionPane.DEFAULT_OPTION,
156+
JOptionPane.INFORMATION_MESSAGE,
157+
null,
158+
new Object[]{nextLevelButton, quitButton},
159+
nextLevelButton
160+
);
150161
}
151162

152163
@Override

src/test/java/ihm/SokobanWindowTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ void completing_a_custom_level_disposes_window_and_shows_home_window() {
335335
timer.setRepeats(false);
336336
SwingUtilities.invokeLater(timer::start);
337337

338-
// When:
339-
// Single move to push the box to the target
338+
// When: Perform a single move to push the box onto the target and end the level
340339
KeyEvent rightKey = new KeyEvent(window,
341340
KeyEvent.KEY_PRESSED,
342341
System.currentTimeMillis(),
@@ -353,4 +352,38 @@ void completing_a_custom_level_disposes_window_and_shows_home_window() {
353352
.as("Contains a visible HomeWindow")
354353
.anyMatch(w -> w instanceof HomeWindow && w.isShowing());
355354
}
355+
356+
@Test
357+
void completing_level_1_loads_next_level() {
358+
// given - create a test window with the simplified test level
359+
window = createTestWindow("src/test/resources/levels/level1.txt");
360+
361+
// Simulate the user clicking Next Level to proceed to the next level
362+
// This has to be done before the move to push the box to the target
363+
// because the dialog will wait for user input
364+
Timer timer = new Timer(10, _ -> window.clickNextLevelButton());
365+
timer.setRepeats(false);
366+
SwingUtilities.invokeLater(timer::start);
367+
368+
// When: Perform a single move to push the box onto the target and end the level
369+
pressKey(KeyEvent.VK_RIGHT);
370+
371+
then(window.isShowing())
372+
.as("Current window is disposed after level completion")
373+
.isFalse();
374+
375+
then(Window.getWindows())
376+
.as("A new window with title 'Level 2' is created")
377+
.anyMatch(w -> w instanceof SokobanWindow sw && sw.getTitle().equals("Level 2"));
378+
}
379+
380+
private void pressKey(int keyCode) {
381+
KeyEvent keyEvent = new KeyEvent(window,
382+
KeyEvent.KEY_PRESSED,
383+
System.currentTimeMillis(),
384+
0,
385+
keyCode,
386+
KeyEvent.CHAR_UNDEFINED);
387+
window.keyPressed(keyEvent);
388+
}
356389
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MMMMMMM
2+
M#####M
3+
M#####M
4+
M#GCT#M
5+
M#####M
6+
M#####M
7+
MMMMMMM

0 commit comments

Comments
 (0)