1010import java .awt .event .MouseListener ;
1111import java .awt .event .MouseMotionListener ;
1212import java .io .IOException ;
13+ import java .nio .file .Files ;
1314import java .nio .file .Path ;
1415import java .util .Arrays ;
1516
1617import static java .nio .file .Files .deleteIfExists ;
1718import static org .assertj .core .api .BDDAssertions .then ;
19+ import static org .mockito .Mockito .mock ;
20+ import static org .mockito .Mockito .verify ;
1821import static swing .ComponentFinder .findComponentByNameAsType ;
1922
2023@ NullMarked
@@ -31,6 +34,7 @@ class EditorTest {
3134 void setUp () throws Exception {
3235 // Create a new Editor instance for testing
3336 editor = new Editor (TEST_ROWS , TEST_COLUMNS , TEST_LEVEL_NAME );
37+ deleteIfExists (TEST_LEVEL_PATH );
3438 }
3539
3640 @ AfterEach
@@ -162,10 +166,38 @@ void target_button_sets_content_to_storage_area() {
162166 void empty_button_sets_content_to_floor () {
163167 // Given
164168 JButton button = findComponentByNameAsType (editor , Editor .Component .EMPTY_BUTTON .name (), JButton .class );
165-
166- // When - Simulate button click
169+
170+ // When
167171 button .doClick ();
168-
172+
173+ // Then
169174 then (editor .getContent ()).isEqualTo (TileType .FLOOR );
170175 }
176+
177+ @ Test
178+ void quit_button_triggers_exit_handler_and_deletes_file () throws IOException {
179+ // Given
180+ ExitHandler mockExitHandler = mock (ExitHandler .class );
181+
182+ // Create a test level file that should be deleted
183+ Files .createDirectories (TEST_LEVEL_PATH .getParent ());
184+ Files .createFile (TEST_LEVEL_PATH );
185+ then (Files .exists (TEST_LEVEL_PATH )).isTrue ();
186+
187+ editor = new Editor (TEST_ROWS , TEST_COLUMNS , TEST_LEVEL_NAME ) {
188+ @ Override
189+ ExitHandler defaultExitHandler () {
190+ return mockExitHandler ;
191+ }
192+ };
193+
194+ JButton quitButton = findComponentByNameAsType (editor , Editor .Component .QUIT_BUTTON .name (), JButton .class );
195+
196+ // When
197+ quitButton .doClick ();
198+
199+ // Then
200+ then (Files .exists (TEST_LEVEL_PATH )).isFalse ();
201+ verify (mockExitHandler ).exit (ExitHandler .SUCCESS );
202+ }
171203}
0 commit comments