Skip to content

Commit fb2f9c9

Browse files
author
Arun Prasaad
committed
Add a test for the back button
1 parent de41053 commit fb2f9c9

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

src/test/java/ihm/EditorTest.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.jspecify.annotations.NullMarked;
88

99
import javax.swing.*;
10+
import java.awt.*;
1011
import java.awt.event.MouseListener;
1112
import java.awt.event.MouseMotionListener;
1213
import java.io.IOException;
@@ -32,15 +33,17 @@ class EditorTest {
3233

3334
@BeforeEach
3435
void setUp() throws Exception {
35-
// Create a new Editor instance for testing
3636
editor = new Editor(TEST_ROWS, TEST_COLUMNS, TEST_LEVEL_NAME);
3737
deleteIfExists(TEST_LEVEL_PATH);
3838
}
3939

4040
@AfterEach
4141
void tearDown() throws IOException {
42-
// Clean up the editor window after each test
43-
editor.dispose();
42+
// Clean up windows
43+
Arrays.stream(java.awt.Window.getWindows())
44+
.filter(java.awt.Window::isDisplayable)
45+
.forEach(java.awt.Window::dispose);
46+
4447
deleteIfExists(TEST_LEVEL_PATH);
4548
}
4649

@@ -174,6 +177,44 @@ void empty_button_sets_content_to_floor() {
174177
then(editor.getContent()).isEqualTo(TileType.FLOOR);
175178
}
176179

180+
@Test
181+
void back_button_deletes_level_file() throws IOException {
182+
// Given
183+
Files.createDirectories(TEST_LEVEL_PATH.getParent());
184+
Files.createFile(TEST_LEVEL_PATH);
185+
then(Files.exists(TEST_LEVEL_PATH)).isTrue();
186+
187+
JButton backButton = findComponentByNameAsType(editor, Editor.Component.BACK_BUTTON.name(), JButton.class);
188+
189+
// When
190+
backButton.doClick();
191+
192+
// Then
193+
then(Files.exists(TEST_LEVEL_PATH))
194+
.as("The level file no longer exists")
195+
.isFalse();
196+
}
197+
198+
@Test
199+
void back_button_closes_editor_and_shows_home_window() {
200+
// Given
201+
JButton backButton = findComponentByNameAsType(editor, Editor.Component.BACK_BUTTON.name(), JButton.class);
202+
203+
// When
204+
backButton.doClick();
205+
206+
// Then
207+
then(editor.isDisplayable())
208+
.as("The editor window is no longer displayed")
209+
.isFalse();
210+
211+
then(Arrays.stream(Window.getWindows())
212+
.filter(Window::isVisible)
213+
.toList())
214+
.as("Exactly one HomeWindow is visible")
215+
.hasExactlyElementsOfTypes(HomeWindow.class);
216+
}
217+
177218
@Test
178219
void quit_button_triggers_exit_handler_and_deletes_file() throws IOException {
179220
// Given

0 commit comments

Comments
 (0)