Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JPopupMenu/7156657/bug7156657.java
41155 views
1
/*
2
* Copyright (c) 2012, 2021, 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
import java.awt.AlphaComposite;
25
import java.awt.Color;
26
import java.awt.Graphics;
27
import java.awt.Graphics2D;
28
import java.awt.GraphicsConfiguration;
29
import java.awt.GraphicsDevice;
30
import java.awt.GraphicsEnvironment;
31
import java.awt.Point;
32
import java.awt.Rectangle;
33
import java.awt.Robot;
34
import java.awt.Window;
35
import java.awt.image.BufferedImage;
36
import java.io.File;
37
import java.util.concurrent.Callable;
38
39
import javax.imageio.ImageIO;
40
import javax.swing.JFrame;
41
import javax.swing.JMenuItem;
42
import javax.swing.JPanel;
43
import javax.swing.JPopupMenu;
44
import javax.swing.SwingUtilities;
45
46
/*
47
@test
48
@key headful
49
@bug 7156657 8186617
50
@summary Version 7 doesn't support translucent popup menus against a
51
translucent window
52
@library ../../regtesthelpers
53
*/
54
public class bug7156657 {
55
private static JFrame lowerFrame;
56
57
private static JFrame frame;
58
59
private static JPopupMenu popupMenu;
60
61
public static void main(String[] args) throws Exception {
62
final Robot robot = new Robot();
63
64
Boolean skipTest = Util.invokeOnEDT(new Callable<Boolean>() {
65
@Override
66
public Boolean call() throws Exception {
67
frame = createFrame();
68
if (!frame.getGraphicsConfiguration().isTranslucencyCapable()) {
69
System.out.println("Translucency is not supported, the test skipped");
70
71
return true;
72
}
73
74
lowerFrame = createFrame();
75
lowerFrame.getContentPane().setBackground(Color.RED);
76
lowerFrame.setVisible(true);
77
78
popupMenu = new JPopupMenu();
79
popupMenu.setOpaque(false);
80
popupMenu.add(new TransparentMenuItem("1111"));
81
popupMenu.add(new TransparentMenuItem("2222"));
82
popupMenu.add(new TransparentMenuItem("3333"));
83
84
setOpaque(frame, false);
85
JPanel pnContent = new JPanel();
86
pnContent.setBackground(new Color(255, 255, 255, 128));
87
frame.add(pnContent);
88
frame.setVisible(true);
89
90
return false;
91
}
92
});
93
94
if (skipTest) {
95
return;
96
}
97
98
robot.waitForIdle();
99
100
SwingUtilities.invokeAndWait(new Runnable() {
101
@Override
102
public void run() {
103
popupMenu.show(frame, 0, 0);
104
}
105
});
106
107
robot.waitForIdle();
108
109
Rectangle popupRectangle = Util.invokeOnEDT(new Callable<Rectangle>() {
110
@Override
111
public Rectangle call() throws Exception {
112
return new Rectangle(popupMenu.getLocationOnScreen(),
113
popupMenu.getSize());
114
}
115
});
116
117
BufferedImage redBackgroundCapture = robot.createScreenCapture(popupRectangle);
118
BufferedImage redFrame = robot.createScreenCapture(frame.getBounds());
119
120
SwingUtilities.invokeAndWait(new Runnable() {
121
@Override
122
public void run() {
123
lowerFrame.getContentPane().setBackground(Color.GREEN);
124
lowerFrame.invalidate();
125
}
126
});
127
128
robot.waitForIdle();
129
robot.delay(1000); // Give frame time to repaint
130
131
BufferedImage greenBackgroundCapture = robot.createScreenCapture(popupRectangle);
132
BufferedImage greenFrame = robot.createScreenCapture(frame.getBounds());
133
134
if (Util.compareBufferedImages(redBackgroundCapture, greenBackgroundCapture)) {
135
try {
136
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
137
for (int i = 0; i < devices.length; i++) {
138
GraphicsConfiguration[] screens = devices[i].getConfigurations();
139
for (int j = 0; j < screens.length; j++) {
140
BufferedImage fullScreen = robot.createScreenCapture(screens[j].getBounds());
141
if (screens[j].getBounds().intersects(popupRectangle)) {
142
Graphics g = fullScreen.getGraphics();
143
g.setColor(Color.CYAN);
144
g.drawRect(popupRectangle.x - 1, popupRectangle.y - 1,
145
popupRectangle.width + 2, popupRectangle.height + 2);
146
g.dispose();
147
}
148
ImageIO.write(fullScreen, "png", new File("dev" + i + "scr" + j + ".png"));
149
}
150
}
151
ImageIO.write(redFrame, "png", new File("redframe.png"));
152
ImageIO.write(redBackgroundCapture, "png", new File("redbg.png"));
153
ImageIO.write(greenFrame, "png", new File("greenframe.png"));
154
ImageIO.write(greenBackgroundCapture, "png", new File("greenbg.png"));
155
} finally {
156
SwingUtilities.invokeAndWait(() -> {
157
frame.dispose();
158
lowerFrame.dispose();
159
});
160
}
161
robot.waitForIdle();
162
throw new RuntimeException("The test failed");
163
}
164
165
SwingUtilities.invokeAndWait(new Runnable() {
166
@Override
167
public void run() {
168
popupMenu.setVisible(false);
169
frame.dispose();
170
lowerFrame.dispose();
171
}
172
});
173
174
System.out.println("The test passed");
175
}
176
177
public static void setOpaque(Window window, boolean opaque) {
178
Color bg = window.getBackground();
179
if (bg == null) {
180
bg = new Color(0, 0, 0, 0);
181
}
182
window.setBackground(new Color(bg.getRed(), bg.getGreen(), bg.getBlue(),
183
opaque ? 255 : 0));
184
}
185
186
private static JFrame createFrame() {
187
JFrame result = new JFrame();
188
189
result.setSize(400, 300);
190
result.setLocationRelativeTo(null);
191
result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
192
result.setUndecorated(true);
193
194
return result;
195
}
196
197
private static class TransparentMenuItem extends JMenuItem {
198
public TransparentMenuItem(String text) {
199
super(text);
200
setOpaque(false);
201
}
202
203
@Override
204
public void paint(Graphics g) {
205
Graphics2D g2 = (Graphics2D) g.create();
206
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
207
super.paint(g2);
208
g2.dispose();
209
}
210
}
211
}
212
213