|
25 | 25 |
|
26 | 26 | package test.javafx.scene.control;
|
27 | 27 |
|
| 28 | +import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.*; |
28 | 29 | import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
|
29 | 30 | import test.com.sun.javafx.scene.control.infrastructure.KeyModifier;
|
30 | 31 | import test.com.sun.javafx.scene.control.infrastructure.StageLoader;
|
|
34 | 35 | import javafx.scene.input.KeyCombination;
|
35 | 36 | import org.junit.After;
|
36 | 37 | import org.junit.Before;
|
| 38 | +import org.junit.Ignore; |
37 | 39 | import org.junit.Test;
|
38 | 40 | import org.junit.runner.RunWith;
|
39 | 41 | import org.junit.runners.Parameterized;
|
40 | 42 |
|
41 | 43 | import java.util.Arrays;
|
42 | 44 | import java.util.Collection;
|
| 45 | +import javafx.scene.Group; |
43 | 46 | import javafx.scene.control.Button;
|
44 | 47 | import javafx.scene.control.ContextMenu;
|
45 | 48 | import javafx.scene.control.Menu;
|
@@ -297,4 +300,77 @@ private void assertSceneDoesNotContainKeyCombination(KeyCombination keyCombinati
|
297 | 300 | keyboard.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT);
|
298 | 301 | assertEquals(0, eventCounter);
|
299 | 302 | }
|
| 303 | + |
| 304 | + @Test public void testAcceleratorShouldNotGetFiredWhenMenuItemRemovedFromScene() { |
| 305 | + KeyEventFirer kb = new KeyEventFirer(item1, scene); |
| 306 | + |
| 307 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 308 | + assertEquals(1, eventCounter); |
| 309 | + assertEquals(1, getListenerCount(item1.acceleratorProperty())); |
| 310 | + |
| 311 | + // Remove all children from the scene |
| 312 | + Group root = (Group)scene.getRoot(); |
| 313 | + root.getChildren().clear(); |
| 314 | + |
| 315 | + assertEquals(0, getListenerCount(item1.acceleratorProperty())); |
| 316 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 317 | + assertEquals(1, eventCounter); |
| 318 | + } |
| 319 | + |
| 320 | + @Test public void testAcceleratorShouldGetFiredWhenMenuItemRemovedAndAddedBackToScene() { |
| 321 | + KeyEventFirer kb = new KeyEventFirer(item1, scene); |
| 322 | + |
| 323 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 324 | + assertEquals(1, eventCounter); |
| 325 | + |
| 326 | + Group root = (Group)scene.getRoot(); |
| 327 | + scene.setRoot(new Group()); // Remove all children from the scene |
| 328 | + scene.setRoot(root); // Add the children to the same scene |
| 329 | + |
| 330 | + assertEquals(1, getListenerCount(item1.acceleratorProperty())); |
| 331 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 332 | + assertEquals(2, eventCounter); |
| 333 | + } |
| 334 | + |
| 335 | + @Test public void testAcceleratorShouldGetFiredWhenMenuItemRemovedAndAddedToDifferentScene() { |
| 336 | + KeyEventFirer kb = new KeyEventFirer(item1, scene); |
| 337 | + |
| 338 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 339 | + assertEquals(1, eventCounter); |
| 340 | + |
| 341 | + Group root = (Group)scene.getRoot(); |
| 342 | + scene.setRoot(new Group()); // Remove all children from the scene |
| 343 | + Scene diffScene = new Scene(root); // Add the children to a different scene |
| 344 | + sl.getStage().setScene(diffScene); |
| 345 | + kb = new KeyEventFirer(item1, diffScene); |
| 346 | + |
| 347 | + assertEquals(1, getListenerCount(item1.acceleratorProperty())); |
| 348 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 349 | + assertEquals(2, eventCounter); |
| 350 | + } |
| 351 | + |
| 352 | + @Ignore("JDK-8268374") |
| 353 | + @Test public void testAcceleratorShouldNotGetFiredWhenControlsIsRemovedFromSceneThenContextMenuIsSetToNullAndControlIsAddedBackToScene() { |
| 354 | + KeyEventFirer kb = new KeyEventFirer(item1, scene); |
| 355 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 356 | + assertEquals(1, eventCounter); |
| 357 | + |
| 358 | + Group root = (Group)scene.getRoot(); |
| 359 | + scene.setRoot(new Group()); // Remove all children from the scene |
| 360 | + |
| 361 | + if (testClass == Button.class) { |
| 362 | + btn.setContextMenu(null); |
| 363 | + } else if (testClass == Tab.class) { |
| 364 | + tab.setContextMenu(null); |
| 365 | + } else if (testClass == TableColumn.class) { |
| 366 | + tableColumn.setContextMenu(null); |
| 367 | + } else if (testClass == TreeTableColumn.class) { |
| 368 | + treeTableColumn.setContextMenu(null); |
| 369 | + } |
| 370 | + scene.setRoot(root); // Add the children to a different scene |
| 371 | + |
| 372 | + assertEquals(0, getListenerCount(item1.acceleratorProperty())); |
| 373 | + kb.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 374 | + assertEquals(1, eventCounter); |
| 375 | + } |
300 | 376 | }
|
0 commit comments