Skip to content

Commit 883277f

Browse files
committed
Updated robot test to run on other platforms
1 parent 868b263 commit 883277f

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

tests/system/src/test/java/test/robot/javafx/scene/MenuDoubleShortcutTest.java

+49-42
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@
4343

4444
import org.junit.jupiter.api.AfterAll;
4545
import org.junit.jupiter.api.Assertions;
46+
import org.junit.jupiter.api.Assumptions;
4647
import org.junit.jupiter.api.BeforeAll;
4748
import org.junit.jupiter.api.Test;
4849

4950
import com.sun.javafx.PlatformUtil;
5051

5152
import test.util.Util;
5253

53-
// When a key equivalent is sent it may be processed by
54-
// JavaFX and also trigger a menu item if the system menu
55-
// bar is in use.
5654
public class MenuDoubleShortcutTest {
5755

5856
static CountDownLatch startupLatch = new CountDownLatch(1);
@@ -62,13 +60,13 @@ public class MenuDoubleShortcutTest {
6260
static private final int delayMilliseconds = 100;
6361

6462
private enum TestResult {
63+
// We provide an explanation of what happened. Since we only see this
64+
// explanation on failure it is worded accordingly.
6565
IGNORED("Key press event triggered no actions"),
66-
FIREDTWICE("Key press event consumed by scene also fired menu bar item"),
67-
FIREDMENUITEM("Key press event fired menu bar item instead of scene"),
66+
FIREDTWICE("Key press event fired scene action and also a menu bar item"),
67+
FIREDMENUITEM("Key press event fired menu bar item instead of scene action"),
6868
FIREDSCENE("Key press event fired scene action instead of menu bar item");
6969

70-
// We provide an explanation of what happened. Since we only see this
71-
// explanation on failure it is worded accordingly.
7270
private String explanation;
7371
TestResult(String e) {
7472
explanation = e;
@@ -82,46 +80,59 @@ public String errorExplanation() {
8280
// KeyCode.A will be added to the menu bar and the scene
8381
// KeyCode.B will be added to the menu bar only
8482
// KeyCode.C will be added to the scene only.
85-
83+
private static final KeyCode menuBarAndSceneKeyCode = KeyCode.A;
84+
private static final KeyCode menuBarOnlyKeyCode = KeyCode.B;
85+
private static final KeyCode sceneOnlyKeyCode = KeyCode.C;
86+
private static final KeyCode noAcceleratorKeyCode = KeyCode.D;
87+
88+
private static final KeyCombination menuBarAndSceneAccelerator = new KeyCodeCombination(menuBarAndSceneKeyCode, KeyCombination.SHORTCUT_DOWN);
89+
private static final KeyCombination menuBarOnlyAccelerator = new KeyCodeCombination(menuBarOnlyKeyCode, KeyCombination.SHORTCUT_DOWN);
90+
private static final KeyCombination sceneOnlyAccelerator = new KeyCodeCombination(sceneOnlyKeyCode, KeyCombination.SHORTCUT_DOWN);
91+
92+
// On Mac the scene should process the event and it should
93+
// not trigger a system menu bar item.
94+
//
95+
// https://bugs.openjdk.org/browse/JDK-8087863
96+
// https://bugs.openjdk.org/browse/JDK-8088897
8697
@Test
87-
void sceneComesBeforeMenuBar() {
88-
// Assumptions.assumeTrue(PlatformUtil.isMac());
89-
90-
// KeyCode.A is in the menu bar and scene
91-
testApp.testKey(KeyCode.A);
98+
void macSceneComesBeforeMenuBar() {
99+
Assumptions.assumeTrue(PlatformUtil.isMac());
100+
testApp.testKey(menuBarAndSceneKeyCode);
92101
Util.sleep(delayMilliseconds);
93102
TestResult result = testApp.testResult();
94103
Assertions.assertEquals(TestResult.FIREDSCENE, result, result.errorExplanation());
95104
}
96105

106+
// On platforms other than Mac the menu bar should process the event
107+
// and the scene should not.
97108
@Test
98-
void acceleratorOnlyInMenuBar() {
99-
// Assumptions.assumeTrue(PlatformUtil.isMac());
109+
void nonMacMenuBarComesBeforeScene() {
110+
Assumptions.assumeFalse(PlatformUtil.isMac());
111+
testApp.testKey(menuBarAndSceneKeyCode);
112+
Util.sleep(delayMilliseconds);
113+
TestResult result = testApp.testResult();
114+
Assertions.assertEquals(TestResult.FIREDMENUITEM, result, result.errorExplanation());
115+
}
100116

101-
// KeyCode.B is only in the menu bar.
102-
testApp.testKey(KeyCode.B);
117+
@Test
118+
void acceleratorOnlyInMenuBar() {
119+
testApp.testKey(menuBarOnlyKeyCode);
103120
Util.sleep(delayMilliseconds);
104121
TestResult result = testApp.testResult();
105122
Assertions.assertEquals(TestResult.FIREDMENUITEM, result, result.errorExplanation());
106123
}
107124

108125
@Test
109126
void acceleratorOnlyInScene() {
110-
// Assumptions.assumeTrue(PlatformUtil.isMac());
111-
112-
// KeyCode.C is only in the scene.
113-
testApp.testKey(KeyCode.C);
127+
testApp.testKey(sceneOnlyKeyCode);
114128
Util.sleep(delayMilliseconds);
115129
TestResult result = testApp.testResult();
116130
Assertions.assertEquals(TestResult.FIREDSCENE, result, result.errorExplanation());
117131
}
118132

119133
@Test
120134
void acceleratorAbsent() {
121-
// Assumptions.assumeTrue(PlatformUtil.isMac());
122-
123-
// KeyCode.D is not registered as an accelerator
124-
testApp.testKey(KeyCode.D);
135+
testApp.testKey(noAcceleratorKeyCode);
125136
Util.sleep(delayMilliseconds);
126137
TestResult result = testApp.testResult();
127138
Assertions.assertEquals(TestResult.IGNORED, result, result.errorExplanation());
@@ -143,7 +154,7 @@ public static class TestApp extends Application {
143154
private boolean menuBarItemFired = false;
144155

145156
private MenuItem createMenuItem(KeyCombination accelerator) {
146-
MenuItem menuItem = new MenuItem("Cmd+A menu item");
157+
MenuItem menuItem = new MenuItem(accelerator.getName() + " menu item");
147158
menuItem.setAccelerator(accelerator);
148159
menuItem.setOnAction(e -> {
149160
menuBarItemFired = true;
@@ -163,42 +174,38 @@ public void start(Stage primaryStage) {
163174
MenuBar menuBar = new MenuBar();
164175
menuBar.setUseSystemMenuBar(true);
165176

166-
// KeyCode.A will be added to the menu bar and the scene
167-
// KeyCode.B will be added to the menu bar only
168-
// KeyCode.C will be added to the scene only.
169-
KeyCombination acceleratorA = new KeyCodeCombination(KeyCode.A, KeyCombination.SHORTCUT_DOWN);
170-
KeyCombination acceleratorB = new KeyCodeCombination(KeyCode.B, KeyCombination.SHORTCUT_DOWN);
171-
KeyCombination acceleratorC = new KeyCodeCombination(KeyCode.C, KeyCombination.SHORTCUT_DOWN);
172-
173177
Menu menu = new Menu("Top menu");
174-
menu.getItems().add(createMenuItem(acceleratorA));
175-
menu.getItems().add(createMenuItem(acceleratorB));
178+
menu.getItems().add(createMenuItem(menuBarAndSceneAccelerator));
179+
menu.getItems().add(createMenuItem(menuBarOnlyAccelerator));
176180
menuBar.getMenus().add(menu);
177181

178182
Scene scene = new Scene(new VBox(menuBar, label), 200, 200);
179-
scene.getAccelerators().put(acceleratorA, () -> {
183+
scene.getAccelerators().put(menuBarAndSceneAccelerator, () -> {
180184
sceneAcceleratorFired = true;
181185
});
182-
scene.getAccelerators().put(acceleratorC, () -> {
186+
scene.getAccelerators().put(sceneOnlyAccelerator, () -> {
183187
sceneAcceleratorFired = true;
184188
});
185189

186190
stage.setScene(scene);
187-
stage.setOnShown(e -> { startupLatch.countDown(); });
191+
stage.setOnShown(e -> {
192+
Platform.runLater(() -> {
193+
startupLatch.countDown();
194+
});
195+
});
188196
stage.show();
189197
}
190198

191199
public void testKey(KeyCode code) {
192200
sceneAcceleratorFired = false;
193201
menuBarItemFired = false;
194202
Platform.runLater(() -> {
195-
// Need to ensure Cmd is present so this is handled
196-
// as a key equivalent.
203+
KeyCode shortcutCode = (PlatformUtil.isMac() ? KeyCode.COMMAND : KeyCode.CONTROL);
197204
Robot robot = new Robot();
198-
robot.keyPress(KeyCode.COMMAND);
205+
robot.keyPress(shortcutCode);
199206
robot.keyPress(code);
200207
robot.keyRelease(code);
201-
robot.keyRelease(KeyCode.COMMAND);
208+
robot.keyRelease(shortcutCode);
202209
});
203210
}
204211

0 commit comments

Comments
 (0)