|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
25 | 25 |
|
26 | 26 | package test.javafx.scene.control;
|
27 | 27 |
|
| 28 | +import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer; |
| 29 | +import test.com.sun.javafx.scene.control.infrastructure.KeyModifier; |
| 30 | +import test.com.sun.javafx.scene.control.infrastructure.StageLoader; |
28 | 31 | import javafx.beans.property.ObjectProperty;
|
29 | 32 | import javafx.beans.property.SimpleBooleanProperty;
|
30 | 33 | import javafx.beans.property.SimpleObjectProperty;
|
|
33 | 36 | import javafx.event.Event;
|
34 | 37 | import javafx.event.EventHandler;
|
35 | 38 | import javafx.event.EventType;
|
| 39 | +import javafx.scene.Group; |
36 | 40 | import javafx.scene.Node;
|
| 41 | +import javafx.scene.Scene; |
37 | 42 | import javafx.scene.control.Menu;
|
| 43 | +import javafx.scene.control.MenuButton; |
38 | 44 | import javafx.scene.control.MenuItem;
|
39 | 45 | import javafx.scene.input.KeyCharacterCombination;
|
| 46 | +import javafx.scene.input.KeyCode; |
40 | 47 | import javafx.scene.input.KeyCodeCombination;
|
41 | 48 | import javafx.scene.input.KeyCombination;
|
42 | 49 | import javafx.scene.input.KeyCombination.Modifier;
|
@@ -581,4 +588,37 @@ public static final class NewEventHandlerStub implements EventHandler<Event> {
|
581 | 588 | menuItem.getProperties().put(null, null);
|
582 | 589 | assertTrue(menuItem.getProperties().size() > 0);
|
583 | 590 | }
|
| 591 | + |
| 592 | + private int eventCounter = 0; |
| 593 | + @Test public void testAcceleratorIsNotFiredWhenMenuItemRemovedFromScene() { |
| 594 | + MenuItem item = new MenuItem("Item 1"); |
| 595 | + item.setOnAction(e -> eventCounter++); |
| 596 | + item.setAccelerator(KeyCombination.valueOf("alt+1")); |
| 597 | + |
| 598 | + MenuButton menuButton = new MenuButton(); |
| 599 | + menuButton.getItems().add(item); |
| 600 | + |
| 601 | + StageLoader s = new StageLoader(menuButton); |
| 602 | + Scene scene = s.getStage().getScene(); |
| 603 | + KeyEventFirer keyboard = new KeyEventFirer(item, scene); |
| 604 | + |
| 605 | + // Invoke MenuItem's action listener twice by using accelerator KeyCombination |
| 606 | + keyboard.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 607 | + assertEquals(1, eventCounter); |
| 608 | + |
| 609 | + keyboard.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 610 | + assertEquals(2, eventCounter); |
| 611 | + |
| 612 | + // Remove all children from the scene |
| 613 | + Group root = (Group)scene.getRoot(); |
| 614 | + root.getChildren().clear(); |
| 615 | + |
| 616 | + // Assert that the MenuItem's action listener is not invoked |
| 617 | + // after MenuItem has been removed from the scene |
| 618 | + keyboard.doKeyPress(KeyCode.DIGIT1, KeyModifier.ALT); |
| 619 | + assertEquals(2, eventCounter); |
| 620 | + |
| 621 | + // Assert that key combination does not remain in the scene's list of accelerators |
| 622 | + assertFalse(scene.getAccelerators().containsKey(KeyCombination.keyCombination("alt+1"))); |
| 623 | + } |
584 | 624 | }
|
0 commit comments