-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
OpenButtonTest.java
185 lines (167 loc) · 7.51 KB
/
OpenButtonTest.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
/*
* This file is part of the PDF Split And Merge source code
* Created on 25/lug/2014
* Copyright 2017 by Sober Lemur S.r.l. (info@pdfsam.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.pdfsam.ui.components.tool;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.kordamp.ikonli.unicons.UniconsLine;
import org.pdfsam.model.io.NativeOpenFileRequest;
import org.pdfsam.model.pdf.PdfLoadRequest;
import org.pdfsam.model.tool.ClearToolRequest;
import org.pdfsam.model.tool.Tool;
import org.pdfsam.model.tool.ToolInputOutputType;
import org.pdfsam.model.ui.SetActiveContentItemRequest;
import org.pdfsam.test.ClearEventStudioExtension;
import org.pdfsam.test.DefaultPriorityTestTool;
import org.pdfsam.test.HitTestListener;
import org.sejda.model.notification.event.TaskExecutionCompletedEvent;
import org.sejda.model.output.DirectoryTaskOutput;
import org.sejda.model.output.FileTaskOutput;
import org.sejda.model.task.NotifiableTaskMetadata;
import org.sejda.model.task.Task;
import org.testfx.api.FxRobot;
import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.Start;
import org.testfx.util.WaitForAsyncUtils;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.pdfsam.eventstudio.StaticStudio.eventStudio;
/**
* @author Andrea Vacondio
*/
@ExtendWith(ApplicationExtension.class)
public class OpenButtonTest {
@RegisterExtension
static ClearEventStudioExtension staticExtension = new ClearEventStudioExtension("moduleId");
private final Tool tool = new DefaultPriorityTestTool();
private OpenButton victim;
private FxRobot robot;
@Start
public void start(Stage stage) {
victim = new OpenButton("moduleId", ToolInputOutputType.SINGLE_PDF, Collections.singletonList(tool));
victim.setMaxHeight(30);
victim.setVisible(true);
Scene scene = new Scene(new HBox(victim));
stage.setScene(scene);
stage.show();
}
@Test
public void openFileClick(@TempDir Path folder) throws Exception {
File file = Files.createTempFile(folder, null, null).toFile();
FileTaskOutput output = new FileTaskOutput(file);
TestListener listener = new TestListener(file);
eventStudio().add(listener);
WaitForAsyncUtils.waitForAsyncFx(2000, () -> victim.dispatch(output));
robot.clickOn(victim);
assertTrue(listener.isHit());
assertTrue(listener.equal);
}
@Test
public void openDirectoryClick(@TempDir Path folder) throws Exception {
File dir = Files.createTempDirectory(folder, null).toFile();
DirectoryTaskOutput output = new DirectoryTaskOutput(dir);
TestListener listener = new TestListener(dir);
eventStudio().add(listener);
WaitForAsyncUtils.waitForAsyncFx(2000, () -> victim.dispatch(output));
robot.clickOn(victim);
assertTrue(listener.isHit());
assertTrue(listener.equal);
}
@Test
public void openSingleFileDirectoryDestinationClick(@TempDir Path folder) throws Exception {
File file = Files.createTempFile(folder, null, null).toFile();
DirectoryTaskOutput output = new DirectoryTaskOutput(file.getParentFile());
TestListener listener = new TestListener(file);
eventStudio().add(listener);
NotifiableTaskMetadata taskMetadata = new NotifiableTaskMetadata(mock(Task.class));
taskMetadata.addTaskOutput(file);
eventStudio().broadcast(new TaskExecutionCompletedEvent(12, taskMetadata), "moduleId");
WaitForAsyncUtils.waitForAsyncFx(2000, () -> victim.dispatch(output));
robot.clickOn(victim);
assertTrue(listener.isHit());
assertTrue(listener.equal);
}
@Test
public void fileDestination() {
FileTaskOutput output = mock(FileTaskOutput.class);
WaitForAsyncUtils.waitForAsyncFx(2000, () -> victim.dispatch(output));
verify(output).getDestination();
Text icon = robot.lookup(".ikonli-font-icon").queryAs(Text.class);
assertEquals(String.valueOf((char) UniconsLine.FILE_ALT.getCode()), icon.getText());
}
@Test
public void directoryDestination() {
DirectoryTaskOutput output = mock(DirectoryTaskOutput.class);
WaitForAsyncUtils.waitForAsyncFx(2000, () -> victim.dispatch(output));
verify(output).getDestination();
Text icon = robot.lookup(".ikonli-font-icon").queryAs(Text.class);
assertEquals(String.valueOf((char) UniconsLine.FOLDER_OPEN.getCode()), icon.getText());
}
@Test
public void fileOrDirDestination() {
DirectoryTaskOutput output = mock(DirectoryTaskOutput.class);
WaitForAsyncUtils.waitForAsyncFx(2000, () -> victim.dispatch(output));
verify(output).getDestination();
Text icon = robot.lookup(".ikonli-font-icon").queryAs(Text.class);
assertEquals(String.valueOf((char) UniconsLine.FOLDER_OPEN.getCode()), icon.getText());
}
@Test
@Tag("NoHeadless")
public void sendToModuleClick(@TempDir Path folder) throws Exception {
File file = Files.createTempFile(folder, null, null).toFile();
NotifiableTaskMetadata taskMetadata = new NotifiableTaskMetadata(mock(Task.class));
taskMetadata.addTaskOutput(file);
TaskExecutionCompletedEvent event = new TaskExecutionCompletedEvent(10, taskMetadata);
HitTestListener<ClearToolRequest> clear = new HitTestListener<>();
eventStudio().add(ClearToolRequest.class, clear, tool.id());
HitTestListener<PdfLoadRequest> load = new HitTestListener<>();
eventStudio().add(PdfLoadRequest.class, load, tool.id());
HitTestListener<SetActiveContentItemRequest> active = new HitTestListener<>();
eventStudio().add(SetActiveContentItemRequest.class, active);
eventStudio().broadcast(event, "moduleId");
robot.clickOn(".arrow-button").clickOn(tool.descriptor().name());
assertTrue(clear.isHit());
assertTrue(active.isHit());
assertTrue(load.isHit());
}
private static class TestListener extends HitTestListener<NativeOpenFileRequest> {
private final File destination;
boolean equal = false;
private TestListener(File destination) {
this.destination = destination;
}
@Override
public void onEvent(NativeOpenFileRequest event) {
super.onEvent(event);
equal = destination.equals(event.file());
}
}
}