forked from AY1920S1-CS2103T-T09-4/main
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuiRobot.java
119 lines (98 loc) · 3.86 KB
/
GuiRobot.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
package guitests;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import org.testfx.api.FxRobot;
import guitests.guihandles.exceptions.StageNotFoundException;
import javafx.stage.Stage;
/**
* Robot used to simulate user actions on the GUI.
* Extends {@link FxRobot} by adding some customized functionality and workarounds.
*/
public class GuiRobot extends FxRobot {
private static final int PAUSE_FOR_HUMAN_DELAY_MILLISECONDS = 50;
private static final int DEFAULT_WAIT_FOR_EVENT_TIMEOUT_MILLISECONDS = 5000;
private static final String PROPERTY_TESTFX_HEADLESS = "testfx.headless";
private final boolean isHeadlessMode;
public GuiRobot() {
String headlessPropertyValue = System.getProperty(PROPERTY_TESTFX_HEADLESS);
isHeadlessMode = headlessPropertyValue != null && headlessPropertyValue.equals("true");
}
/**
* Pauses execution for {@code PAUSE_FOR_HUMAN_DELAY_MILLISECONDS} milliseconds for a human to examine the
* effects of the test. This method will be disabled when the GUI tests are executed in headless mode to avoid
* unnecessary delays.
*/
public void pauseForHuman() {
if (isHeadlessMode) {
return;
}
sleep(PAUSE_FOR_HUMAN_DELAY_MILLISECONDS);
}
/**
* Returns true if tests are run in headless mode.
*/
public boolean isHeadlessMode() {
return isHeadlessMode;
}
/**
* Waits for {@code event} to be true by {@code DEFAULT_WAIT_FOR_EVENT_TIMEOUT_MILLISECONDS} milliseconds.
*
* @throws EventTimeoutException if the time taken exceeds {@code DEFAULT_WAIT_FOR_EVENT_TIMEOUT_MILLISECONDS}
* milliseconds.
*/
public void waitForEvent(BooleanSupplier event) {
waitForEvent(event, DEFAULT_WAIT_FOR_EVENT_TIMEOUT_MILLISECONDS);
}
/**
* Waits for {@code event} to be true.
*
* @param timeOut in milliseconds
* @throws EventTimeoutException if the time taken exceeds {@code timeOut}.
*/
public void waitForEvent(BooleanSupplier event, int timeOut) {
int timePassed = 0;
final int retryInterval = 50;
while (!event.getAsBoolean()) {
sleep(retryInterval);
timePassed += retryInterval;
if (timePassed >= timeOut) {
throw new EventTimeoutException();
}
}
pauseForHuman();
}
/**
* Returns true if the window with {@code stageTitle} is currently open.
*/
public boolean isWindowShown(String stageTitle) {
return getNumberOfWindowsShown(stageTitle) >= 1;
}
/**
* Returns the number of windows with {@code stageTitle} that are currently open.
*/
public int getNumberOfWindowsShown(String stageTitle) {
return (int) listTargetWindows().stream()
.filter(window -> window instanceof Stage && ((Stage) window).getTitle().equals(stageTitle))
.count();
}
/**
* Returns the first stage, ordered by proximity to the current target window, with the stage title.
* The order that the windows are searched are as follows (proximity): current target window,
* children of the target window, rest of the windows.
*
* @throws StageNotFoundException if the stage is not found.
*/
public Stage getStage(String stageTitle) {
Optional<Stage> targetStage = listTargetWindows().stream()
.filter(Stage.class::isInstance) // checks that the window is of type Stage
.map(Stage.class::cast)
.filter(stage -> stage.getTitle().equals(stageTitle))
.findFirst();
return targetStage.orElseThrow(StageNotFoundException::new);
}
/**
* Represents an error which occurs when a timeout occurs when waiting for an event.
*/
private class EventTimeoutException extends RuntimeException {
}
}