47163
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
/*
|
|
24 |
* @test
|
|
25 |
* @key headful
|
|
26 |
* @bug 8173739
|
|
27 |
* @summary Verifies if JPopupMenu disappears on KeyEvent
|
|
28 |
* @run main TestPopupMenu
|
|
29 |
*/
|
|
30 |
import java.awt.Color;
|
|
31 |
import java.awt.Dimension;
|
|
32 |
import java.awt.GridBagConstraints;
|
|
33 |
import java.awt.GridBagLayout;
|
|
34 |
import java.awt.Insets;
|
|
35 |
import java.awt.Point;
|
|
36 |
import java.awt.Robot;
|
|
37 |
import java.awt.event.InputEvent;
|
|
38 |
import java.awt.event.KeyEvent;
|
|
39 |
import java.beans.PropertyVetoException;
|
|
40 |
import javax.swing.JComponent;
|
|
41 |
import javax.swing.JDesktopPane;
|
|
42 |
import javax.swing.JFrame;
|
|
43 |
import javax.swing.JInternalFrame;
|
|
44 |
import javax.swing.JLabel;
|
|
45 |
import javax.swing.JMenuItem;
|
|
46 |
import javax.swing.JPanel;
|
|
47 |
import javax.swing.JPopupMenu;
|
|
48 |
import javax.swing.JScrollPane;
|
|
49 |
import javax.swing.KeyStroke;
|
|
50 |
import javax.swing.SwingUtilities;
|
|
51 |
|
|
52 |
public class TestPopupMenu {
|
|
53 |
private JFrame frame;
|
|
54 |
private JLabel label;
|
|
55 |
private volatile Point p = null;
|
|
56 |
private volatile Dimension d = null;
|
|
57 |
|
|
58 |
public static void main(String[] args) throws Exception {
|
|
59 |
new TestPopupMenu();
|
|
60 |
}
|
|
61 |
|
|
62 |
void blockTillDisplayed(JComponent comp) throws Exception {
|
|
63 |
while (p == null) {
|
|
64 |
try {
|
|
65 |
SwingUtilities.invokeAndWait(() -> {
|
|
66 |
p = comp.getLocationOnScreen();
|
|
67 |
d = comp.getSize();
|
|
68 |
});
|
|
69 |
} catch (IllegalStateException e) {
|
|
70 |
try {
|
|
71 |
Thread.sleep(1000);
|
|
72 |
} catch (InterruptedException ie) {
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
public TestPopupMenu() throws Exception {
|
|
79 |
Robot robot = new Robot();
|
|
80 |
robot.setAutoDelay(200);
|
|
81 |
try {
|
|
82 |
SwingUtilities.invokeAndWait(() -> {
|
|
83 |
try {
|
|
84 |
createAndShowUI();
|
|
85 |
} catch (Exception ex) {
|
|
86 |
throw new RuntimeException(ex);
|
|
87 |
}
|
|
88 |
});
|
|
89 |
blockTillDisplayed(label);
|
|
90 |
robot.waitForIdle();
|
|
91 |
robot.mouseMove(p.x + d.width/2, p.y + d.height/2);
|
|
92 |
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
|
|
93 |
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
|
|
94 |
robot.waitForIdle();
|
|
95 |
robot.keyPress(KeyEvent.VK_CONTROL);
|
|
96 |
robot.keyPress(KeyEvent.VK_U);
|
|
97 |
robot.keyRelease(KeyEvent.VK_U);
|
|
98 |
robot.keyRelease(KeyEvent.VK_CONTROL);
|
|
99 |
robot.waitForIdle();
|
|
100 |
JPopupMenu popup = label.getComponentPopupMenu();
|
|
101 |
if (popup != null && popup.isVisible()) {
|
|
102 |
throw new RuntimeException("Popup is visible in wrong internal frame");
|
|
103 |
}
|
|
104 |
} finally {
|
|
105 |
SwingUtilities.invokeAndWait(()->frame.dispose());
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
private void createAndShowUI() throws Exception {
|
|
110 |
frame = new JFrame();
|
|
111 |
frame.setTitle("Test Frame");
|
|
112 |
frame.setSize(800, 600);
|
|
113 |
|
|
114 |
JDesktopPane pane = new JDesktopPane();
|
|
115 |
TestInternalFrameWPopup testInternalFrame1 = new TestInternalFrameWPopup();
|
|
116 |
pane.add(testInternalFrame1);
|
|
117 |
|
|
118 |
testInternalFrame1.setVisible(true);
|
|
119 |
JScrollPane scrollPane = new JScrollPane(pane);
|
|
120 |
frame.getContentPane().add(scrollPane);
|
|
121 |
testInternalFrame1.setMaximum(true);
|
|
122 |
frame.getRootPane().registerKeyboardAction(e -> {
|
|
123 |
TestInternalFrame testInternalFrame2 = new TestInternalFrame();
|
|
124 |
pane.add(testInternalFrame2);
|
|
125 |
try {
|
|
126 |
testInternalFrame2.setMaximum(true);
|
|
127 |
} catch (PropertyVetoException ex) {
|
|
128 |
throw new RuntimeException(ex);
|
|
129 |
}
|
|
130 |
testInternalFrame2.setVisible(true);
|
|
131 |
}, KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_MASK),
|
|
132 |
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
|
133 |
|
|
134 |
frame.setVisible(true);
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* Background color Cyan
|
|
139 |
*/
|
|
140 |
class TestInternalFrameWPopup extends JInternalFrame {
|
|
141 |
|
|
142 |
TestInternalFrameWPopup() {
|
|
143 |
jbInit();
|
|
144 |
}
|
|
145 |
|
|
146 |
private void jbInit() {
|
|
147 |
setTitle("Test Internal Frame With Popup");
|
|
148 |
setContentPane(getContainerPanel());
|
|
149 |
setMaximizable(true);
|
|
150 |
setClosable(true);
|
|
151 |
setMinimumSize(new Dimension(500, 300));
|
|
152 |
setSize(500, 300);
|
|
153 |
}
|
|
154 |
|
|
155 |
private JPanel getContainerPanel() {
|
|
156 |
JPanel panel = new JPanel();
|
|
157 |
panel.setLayout(new GridBagLayout());
|
|
158 |
label = new JLabel("Test Label");
|
|
159 |
JPopupMenu popup = new JPopupMenu();
|
|
160 |
JMenuItem menuItem1 = new JMenuItem("Item 1");
|
|
161 |
JMenuItem menuItem2 = new JMenuItem("Item 2");
|
|
162 |
JMenuItem menuItem3 = new JMenuItem("Item 3");
|
|
163 |
JMenuItem menuItem4 = new JMenuItem("Item 4");
|
|
164 |
JMenuItem menuItem5 = new JMenuItem("Item 5");
|
|
165 |
menuItem1.setOpaque(false);
|
|
166 |
menuItem2.setOpaque(false);
|
|
167 |
menuItem3.setOpaque(false);
|
|
168 |
menuItem4.setOpaque(false);
|
|
169 |
menuItem5.setOpaque(false);
|
|
170 |
popup.add(menuItem1);
|
|
171 |
popup.add(menuItem2);
|
|
172 |
popup.add(menuItem3);
|
|
173 |
popup.add(menuItem4);
|
|
174 |
popup.add(menuItem5);
|
|
175 |
label.setComponentPopupMenu(popup);
|
|
176 |
popup.setBackground(Color.CYAN);
|
|
177 |
panel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
|
|
178 |
GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
|
|
179 |
panel.setBackground(Color.CYAN);
|
|
180 |
return panel;
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* Background color Gray
|
|
186 |
*
|
|
187 |
*/
|
|
188 |
class TestInternalFrame extends JInternalFrame {
|
|
189 |
public TestInternalFrame() {
|
|
190 |
jbInit();
|
|
191 |
}
|
|
192 |
|
|
193 |
private void jbInit() {
|
|
194 |
setTitle("Test Internal Frame");
|
|
195 |
getContentPane().setBackground(Color.GRAY);
|
|
196 |
setMaximizable(true);
|
|
197 |
setClosable(true);
|
|
198 |
setMinimumSize(new Dimension(500, 300));
|
|
199 |
setSize(500, 300);
|
|
200 |
}
|
|
201 |
}
|
|
202 |
}
|