|
30 | 30 | import test.com.sun.javafx.scene.control.infrastructure.StageLoader;
|
31 | 31 | import javafx.event.ActionEvent;
|
32 | 32 | import javafx.event.EventHandler;
|
33 |
| - |
| 33 | +import javafx.geometry.Bounds; |
| 34 | +import javafx.geometry.NodeOrientation; |
34 | 35 | import javafx.geometry.Side;
|
35 | 36 | import javafx.scene.control.Button;
|
36 | 37 | import javafx.scene.control.ContextMenu;
|
|
44 | 45 |
|
45 | 46 | import static org.junit.Assert.*;
|
46 | 47 | import static com.sun.javafx.scene.control.ContextMenuContentShim.*;
|
| 48 | + |
47 | 49 | import java.util.Optional;
|
48 | 50 | import javafx.scene.Node;
|
49 | 51 | import javafx.scene.input.KeyCode;
|
@@ -636,4 +638,121 @@ private ContextMenu createContextMenuAndShowSubMenu() {
|
636 | 638 | assertEquals("Expected " + item1.getText() + ", found " + focusedItem.getText(),
|
637 | 639 | item1, focusedItem);
|
638 | 640 | }
|
| 641 | + |
| 642 | + @Test public void test_position_showOnScreen() { |
| 643 | + ContextMenu cm = createContextMenu(false); |
| 644 | + cm.show(anchorBtn, 100, 100); |
| 645 | + |
| 646 | + assertEquals(100, cm.getAnchorX(), 0.0); |
| 647 | + assertEquals(100, cm.getAnchorY(), 0.0); |
| 648 | + } |
| 649 | + |
| 650 | + @Test public void test_position_showOnTop() throws InterruptedException { |
| 651 | + ContextMenu cm = createContextMenu(false); |
| 652 | + cm.show(anchorBtn, Side.TOP, 0, 0); |
| 653 | + |
| 654 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 655 | + Node cmNode = cm.getScene().getRoot(); |
| 656 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 657 | + |
| 658 | + assertEquals(anchorBounds.getMinX(), cmBounds.getMinX(), 0.0); |
| 659 | + assertEquals(anchorBounds.getMinY(), cmBounds.getMaxY(), 0.0); |
| 660 | + } |
| 661 | + |
| 662 | + @Test public void test_position_showOnTopOffset() throws InterruptedException { |
| 663 | + ContextMenu cm = createContextMenu(false); |
| 664 | + cm.show(anchorBtn, Side.TOP, 3, 5); |
| 665 | + |
| 666 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 667 | + Node cmNode = cm.getScene().getRoot(); |
| 668 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 669 | + |
| 670 | + assertEquals(anchorBounds.getMinX() + 3, cmBounds.getMinX(), 0.0); |
| 671 | + assertEquals(anchorBounds.getMinY() + 5, cmBounds.getMaxY(), 0.0); |
| 672 | + } |
| 673 | + |
| 674 | + @Test public void test_position_withOrientationTop() throws InterruptedException { |
| 675 | + ContextMenu cm = createContextMenu(false); |
| 676 | + anchorBtn.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT); |
| 677 | + cm.show(anchorBtn, Side.TOP, 0, 0); |
| 678 | + |
| 679 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 680 | + Node cmNode = cm.getScene().getRoot(); |
| 681 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 682 | + |
| 683 | + assertEquals(anchorBounds.getMaxX(), cmBounds.getMaxX(), 0.0); |
| 684 | + assertEquals(anchorBounds.getMinY(), cmBounds.getMaxY(), 0.0); |
| 685 | + } |
| 686 | + |
| 687 | + @Test public void test_position_withOrientationLeft() throws InterruptedException { |
| 688 | + ContextMenu cm = createContextMenu(false); |
| 689 | + anchorBtn.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT); |
| 690 | + cm.show(anchorBtn, Side.LEFT, 0, 0); |
| 691 | + |
| 692 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 693 | + Node cmNode = cm.getScene().getRoot(); |
| 694 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 695 | + |
| 696 | + assertEquals(anchorBounds.getMaxX(), cmBounds.getMinX(), 0.0); |
| 697 | + assertEquals(anchorBounds.getMinY(), cmBounds.getMinY(), 0.0); |
| 698 | + } |
| 699 | + |
| 700 | + |
| 701 | + @Test public void test_position_withCSS() throws InterruptedException { |
| 702 | + anchorBtn.getScene().getStylesheets().add( |
| 703 | + getClass().getResource("test_position_showOnTopWithCSS.css").toExternalForm() |
| 704 | + ); |
| 705 | + test_position_showOnTop(); |
| 706 | + test_position_showOnRight(); |
| 707 | + test_position_showOnLeft(); |
| 708 | + test_position_showOnBottom(); |
| 709 | + } |
| 710 | + |
| 711 | + @Test public void test_position_showOnRight() { |
| 712 | + ContextMenu cm = createContextMenu(false); |
| 713 | + cm.show(anchorBtn, Side.RIGHT, 0, 0); |
| 714 | + |
| 715 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 716 | + Node cmNode = cm.getScene().getRoot(); |
| 717 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 718 | + |
| 719 | + assertEquals(anchorBounds.getMaxX(), cmBounds.getMinX(), 0.0); |
| 720 | + assertEquals(anchorBounds.getMinY(), cmBounds.getMinY(), 0.0); |
| 721 | + } |
| 722 | + |
| 723 | + @Test public void test_position_showOnRightOffset() { |
| 724 | + ContextMenu cm = createContextMenu(false); |
| 725 | + cm.show(anchorBtn, Side.RIGHT, 3, 5); |
| 726 | + |
| 727 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 728 | + Node cmNode = cm.getScene().getRoot(); |
| 729 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 730 | + |
| 731 | + assertEquals(anchorBounds.getMaxX() + 3, cmBounds.getMinX(), 0.0); |
| 732 | + assertEquals(anchorBounds.getMinY() + 5, cmBounds.getMinY(), 0.0); |
| 733 | + } |
| 734 | + |
| 735 | + @Test public void test_position_showOnBottom() { |
| 736 | + ContextMenu cm = createContextMenu(false); |
| 737 | + cm.show(anchorBtn, Side.BOTTOM, 0, 0); |
| 738 | + |
| 739 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 740 | + Node cmNode = cm.getScene().getRoot(); |
| 741 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 742 | + |
| 743 | + assertEquals(anchorBounds.getMinX(), cmBounds.getMinX(), 0.0); |
| 744 | + assertEquals(anchorBounds.getMaxY(), cmBounds.getMinY(), 0.0); |
| 745 | + } |
| 746 | + |
| 747 | + @Test public void test_position_showOnLeft() { |
| 748 | + ContextMenu cm = createContextMenu(false); |
| 749 | + cm.show(anchorBtn, Side.LEFT, 0, 0); |
| 750 | + |
| 751 | + Bounds anchorBounds = anchorBtn.localToScreen(anchorBtn.getLayoutBounds()); |
| 752 | + Node cmNode = cm.getScene().getRoot(); |
| 753 | + Bounds cmBounds = cm.getScene().getRoot().localToScreen(cmNode.getLayoutBounds()); |
| 754 | + |
| 755 | + assertEquals(anchorBounds.getMinX(), cmBounds.getMaxX(), 0.0); |
| 756 | + assertEquals(anchorBounds.getMinY(), cmBounds.getMinY(), 0.0); |
| 757 | + } |
639 | 758 | }
|
0 commit comments