-
Notifications
You must be signed in to change notification settings - Fork 8
/
FileChooserControllerTest.java
350 lines (253 loc) · 9.34 KB
/
FileChooserControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*-
* #%L
* FXFileChooser
* %%
* Copyright (C) 2017 - 2021 Oliver Loeffler, Raumzeitfalle.net
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package net.raumzeitfalle.fx.filechooser;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.function.Consumer;
import javax.imageio.ImageIO;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import javafx.beans.property.ObjectProperty;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXMLLoader;
import javafx.geometry.VerticalDirection;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.MenuButton;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import net.raumzeitfalle.fx.filechooser.locations.Locations;
class FileChooserControllerTest extends FxTestTemplate {
private FileChooserModel model;
private FileChooserController controller;
private TestDirChooser dirChooser;
protected PathFilter[] getPathFilter() {
return new PathFilter[] {
PathFilter.create("XML", p->p.getFileName().toString().toLowerCase().endsWith(".xml")),
PathFilter.acceptAllFiles("all files")
};
}
protected Skin getSkin() { return Skin.DARK; }
@Override
public void start(Stage stage) {
primaryStage = stage;
model = FileChooserModel.startingIn(Paths.get("./"), getPathFilter());
model.addLocation(Locations.withName("TEST", Paths.get("TestData/SomeFiles")));
dirChooser = new TestDirChooser(model.currentSearchPath());
controller = new FileChooserController(model, dirChooser, ()->stage.close(), FileChooserViewOption.STAGE);
Class<?> thisClass = getClass();
String fileName = "FileChooserView.fxml";
URL resource = thisClass.getResource(fileName);
FXMLLoader loader = new FXMLLoader(resource);
loader.setController(controller);
Parent view = null;
try {
view = loader.load();
} catch (IOException e) {
Label errorLabel = new Label("Could not load FileChooserView.");
errorLabel.setTextFill(Color.WHITE);
StackPane pane = new StackPane();
Rectangle rect = new Rectangle();
rect.setFill(Color.RED);
rect.widthProperty().bind(pane.widthProperty());
rect.heightProperty().bind(pane.heightProperty());
pane.getChildren().add(rect);
pane.getChildren().add(errorLabel);
view = pane;
}
Scene scene = new Scene(view, 700, 500);
stage.setScene(scene);
stage.show();
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void clickOnCancelClosesWindow() {
clickOn("#cancelButton", MouseButton.PRIMARY);
assertFalse(primaryStage.isShowing());
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void that_dialog_is_initialized_properly() {
ListView<?> list = lookup("#listOfFiles").query();
Button okay = lookup("#okButton").query();
Button cancel = lookup("#cancelButton").query();
sleep(400);
assertTrue(okay.isDisabled());
assertFalse(cancel.isDisabled());
assertFalse(list.getItems().isEmpty());
assertNull(model.getSelectedFile());
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void that_parent_of_filepath_is_used_for_dirchange_in_textbox_while_filename_is_used_for_filter() {
clickOn("#fileNameFilter");
write(Paths.get("TestData/SomeFiles/TestFile1.txt").toString());
press(KeyCode.ENTER);
sleep(200);
ListView<?> list = lookup("#listOfFiles").query();
// Keeps the file name as the file actually exists, hence its only 1 item
assertEquals(1, list.getItems().size());
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void that_parent_of_filepath_is_used_for_dirchange_in_textbox() {
clickOn("#fileNameFilter");
write(Paths.get("TestData/SomeFiles/").toString());
press(KeyCode.ENTER);
sleep(200);
ListView<?> list = lookup("#listOfFiles").query();
assertEquals(11, list.getItems().size());
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void that_list_is_updated_after_clicking_refresh(@TempDir Path directory) throws IOException {
ListView<?> list = lookup("#listOfFiles").query();
ObservableList<?> items = list.getItems();
dirChooser.setDirectory(directory);
clickOn("#chooser");
clickOn("#refreshButton");
sleep(200);
assertTrue(items.isEmpty());
Path source = Paths.get("TestData/SomeFiles/TestFile1.txt");
Path target = directory.resolve(source.getFileName());
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
clickOn("#refreshButton");
sleep(200);
assertEquals(1, items.size());
}
@Disabled("Sort functionality")
@Test
void that_list_is_sorted_after_clicking_on_sortmenu() {
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void that_selection_is_accepted_with_doubleclick() {
dirChooser.setDirectory(Paths.get("TestData/SomeFiles"));
clickOn("#chooser");
sleep(200);
ListView<?> list = lookup("#listOfFiles").query();
assertEquals(11, list.getItems().size());
doubleClickOn("#listOfFiles");
sleep(200);
assertFalse(primaryStage.isShowing());
Path selection = model.getSelectedFile();
assertNotNull(selection);
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void that_list_content_is_reduced_by_entering_filtertext() {
dirChooser.setDirectory(Paths.get("TestData/SomeFiles"));
clickOn("#chooser");
sleep(200);
ListView<?> list = lookup("#listOfFiles").query();
assertEquals(11, list.getItems().size());
clickOn("#fileNameFilter");
write("doc");
assertEquals(2, list.getItems().size(), "there are only 2 files which match the filter 'doc'");
write("xml");
assertEquals(0, list.getItems().size(), "there is NO file which matches the filter 'docxml'");
eraseText("docxml".length());
write("xml");
assertEquals(1, list.getItems().size(), "there is 1 file which matches the filter 'xml'");
}
@Test
@EnabledOnOs({OS.WINDOWS, OS.MAC})
void that_pathfilters_from_file_type_menu_are_applied() {
dirChooser.setDirectory(Paths.get("./TestData/SomeFiles"));
clickOn("#chooser");
ListView<?> list = lookup("#listOfFiles").query();
MenuButton filterMenu = lookup("#fileExtensionFilter").query();
clickOn(filterMenu);
clickOn("XML");
sleep(200);
assertEquals(1, list.getItems().size(), "there is 1 file which matches the filter 'xml'");
clickOn(filterMenu);
clickOn("all files");
sleep(200);
assertEquals(11, list.getItems().size(), "for filter 'all files' 11 files are expected.");
}
@EnabledOnOs({OS.WINDOWS, OS.LINUX, OS.MAC})
void that_selection_is_accepted_with_okay_after_dirchange_in_textbox() {
Button okay = lookup("#okButton").query();
assertTrue(okay.isDisabled());
clickOn("#fileNameFilter");
write("./TestData/SomeFiles/");
sleep(200);
ListView<?> list = lookup("#listOfFiles").query();
assertTrue(list.getItems().isEmpty());
clickOn("#fileNameFilter");
press(KeyCode.ENTER);
sleep(200);
assertEquals(11, list.getItems().size());
clickOn("#listOfFiles");
scroll(2, VerticalDirection.DOWN);
sleep(400); // must not appear like a double click
clickOn("#listOfFiles");
assertTrue(primaryStage.isShowing());
clickOn(okay);
sleep(200);
assertFalse(primaryStage.isShowing());
Path selection = model.getSelectedFile();
assertNotNull(selection);
}
protected void captureImage(Parent put, String filename) {
BufferedImage bImage = SwingFXUtils.fromFXImage(capture(put).getImage(), null);
try {
ImageIO.write(bImage, "png", new File(filename));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static class TestDirChooser implements PathSupplier {
private Path directory = null;
private TestDirChooser(ObjectProperty<Path> startLocation) {
directory = startLocation.get();
}
@Override
public void getUpdate(Consumer<Path> update) {
update.accept(directory);
}
public void setDirectory(Path dir) {
this.directory = dir;
}
}
}