Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JInternalFrame/8146321/JInternalFrameIconTest.java
41153 views
1
/*
2
* Copyright (c) 2016, 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
/*
25
* @test
26
* @key headful
27
* @bug 8146321 8151282
28
* @summary verifies JInternalFrame Icon and ImageIcon
29
* @library ../../regtesthelpers
30
* @build Util
31
* @run main JInternalFrameIconTest
32
*/
33
import java.io.File;
34
import java.awt.BorderLayout;
35
import java.awt.Component;
36
import java.awt.Graphics;
37
import java.awt.Point;
38
import java.awt.Rectangle;
39
import java.awt.Robot;
40
import java.awt.image.BufferedImage;
41
import javax.imageio.ImageIO;
42
import javax.swing.Icon;
43
import javax.swing.ImageIcon;
44
import javax.swing.JDesktopPane;
45
import javax.swing.JFrame;
46
import javax.swing.JInternalFrame;
47
import javax.swing.SwingUtilities;
48
import javax.swing.UIManager;
49
import javax.swing.UnsupportedLookAndFeelException;
50
51
public class JInternalFrameIconTest {
52
53
private static JFrame frame;
54
private static JDesktopPane desktopPane;
55
private static JInternalFrame internalFrame;
56
private static ImageIcon titleImageIcon;
57
private static Icon titleIcon;
58
private static BufferedImage imageIconImage;
59
private static BufferedImage iconImage;
60
private static Robot robot;
61
private static volatile String errorString = "";
62
63
64
public static void main(String[] args) throws Exception {
65
robot = new Robot();
66
UIManager.LookAndFeelInfo[] lookAndFeelArray
67
= UIManager.getInstalledLookAndFeels();
68
for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
69
executeCase(lookAndFeelItem.getClassName());
70
}
71
if (!"".equals(errorString)) {
72
throw new RuntimeException("Error Log:\n" + errorString);
73
}
74
75
}
76
77
private static void executeCase(String lookAndFeelString) throws Exception {
78
if (tryLookAndFeel(lookAndFeelString)) {
79
createImageIconUI(lookAndFeelString);
80
robot.waitForIdle();
81
robot.delay(1000);
82
getImageIconBufferedImage();
83
robot.waitForIdle();
84
robot.delay(1000);
85
cleanUp();
86
robot.waitForIdle();
87
robot.delay(1000);
88
89
createIconUI(lookAndFeelString);
90
robot.waitForIdle();
91
robot.delay(1000);
92
getIconBufferedImage();
93
robot.waitForIdle();
94
robot.delay(1000);
95
cleanUp();
96
robot.waitForIdle();
97
robot.delay(1000);
98
99
testIfSame(lookAndFeelString);
100
robot.waitForIdle();
101
robot.delay(1000);
102
}
103
104
}
105
106
private static void createImageIconUI(final String lookAndFeelString)
107
throws Exception {
108
SwingUtilities.invokeAndWait(new Runnable() {
109
@Override
110
public void run() {
111
desktopPane = new JDesktopPane();
112
internalFrame = new JInternalFrame();
113
frame = new JFrame();
114
internalFrame.setTitle(lookAndFeelString);
115
titleImageIcon = new ImageIcon() {
116
@Override
117
public int getIconWidth() {
118
return 16;
119
}
120
121
@Override
122
public int getIconHeight() {
123
return 16;
124
}
125
126
@Override
127
public void paintIcon(
128
Component c, Graphics g, int x, int y) {
129
g.setColor(java.awt.Color.black);
130
g.fillRect(x, y, 16, 16);
131
}
132
};
133
internalFrame.setFrameIcon(titleImageIcon);
134
internalFrame.setSize(500, 200);
135
internalFrame.setVisible(true);
136
desktopPane.add(internalFrame);
137
138
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
139
frame.getContentPane().setLayout(new BorderLayout());
140
frame.getContentPane().add(desktopPane, "Center");
141
frame.setSize(500, 500);
142
frame.setLocationRelativeTo(null);
143
frame.setVisible(true);
144
frame.toFront();
145
}
146
});
147
}
148
149
private static void createIconUI(final String lookAndFeelString)
150
throws Exception {
151
SwingUtilities.invokeAndWait(new Runnable() {
152
@Override
153
public void run() {
154
desktopPane = new JDesktopPane();
155
internalFrame = new JInternalFrame();
156
frame = new JFrame();
157
internalFrame.setTitle(lookAndFeelString);
158
titleIcon = new Icon() {
159
@Override
160
public int getIconWidth() {
161
return 16;
162
}
163
164
@Override
165
public int getIconHeight() {
166
return 16;
167
}
168
169
@Override
170
public void paintIcon(
171
Component c, Graphics g, int x, int y) {
172
g.setColor(java.awt.Color.black);
173
g.fillRect(x, y, 16, 16);
174
}
175
};
176
internalFrame.setFrameIcon(titleIcon);
177
internalFrame.setSize(500, 200);
178
internalFrame.setVisible(true);
179
desktopPane.add(internalFrame);
180
181
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
182
frame.getContentPane().setLayout(new BorderLayout());
183
frame.getContentPane().add(desktopPane, "Center");
184
frame.setSize(500, 500);
185
frame.setLocationRelativeTo(null);
186
frame.setVisible(true);
187
frame.toFront();
188
}
189
});
190
}
191
192
private static void getImageIconBufferedImage() throws Exception {
193
Point point = internalFrame.getLocationOnScreen();
194
Rectangle rect = internalFrame.getBounds();
195
Rectangle captureRect = new Rectangle(
196
point.x + internalFrame.getInsets().left,
197
point.y + internalFrame.getInsets().top,
198
titleImageIcon.getIconWidth(),
199
titleImageIcon.getIconHeight());
200
201
System.out.println("imageicon captureRect " + captureRect);
202
imageIconImage
203
= robot.createScreenCapture(captureRect);
204
}
205
206
private static void getIconBufferedImage() throws Exception {
207
Point point = internalFrame.getLocationOnScreen();
208
Rectangle rect = internalFrame.getBounds();
209
Rectangle captureRect = new Rectangle(
210
point.x + internalFrame.getInsets().left,
211
point.y + internalFrame.getInsets().top,
212
titleIcon.getIconWidth(),
213
titleIcon.getIconHeight());
214
215
System.out.println("icon captureRect " + captureRect);
216
iconImage
217
= robot.createScreenCapture(captureRect);
218
}
219
220
private static void testIfSame(final String lookAndFeelString)
221
throws Exception {
222
if (!bufferedImagesEqual(imageIconImage, iconImage)) {
223
ImageIO.write(imageIconImage, "png", new File("imageicon-fail.png"));
224
ImageIO.write(iconImage, "png", new File("iconImage-fail.png"));
225
String error ="[" + lookAndFeelString
226
+ "] : ERROR: icon and imageIcon not same.";
227
errorString += error;
228
System.err.println(error);
229
} else {
230
System.out.println("[" + lookAndFeelString
231
+ "] : SUCCESS: icon and imageIcon same.");
232
}
233
}
234
235
private static boolean bufferedImagesEqual(
236
BufferedImage bufferedImage1, BufferedImage bufferedImage2) {
237
boolean flag = true;
238
239
if (bufferedImage1.getWidth() == bufferedImage2.getWidth()
240
&& bufferedImage1.getHeight() == bufferedImage2.getHeight()) {
241
final int colorTolerance = 25;
242
final int mismatchTolerance = (int) (0.1
243
* bufferedImage1.getWidth() * bufferedImage1.getHeight());
244
int mismatchCounter = 0;
245
for (int x = 0; x < bufferedImage1.getWidth(); x++) {
246
for (int y = 0; y < bufferedImage1.getHeight(); y++) {
247
248
int color1 = bufferedImage1.getRGB(x, y);
249
int red1 = (color1 >> 16) & 0x000000FF;
250
int green1 = (color1 >> 8) & 0x000000FF;
251
int blue1 = (color1) & 0x000000FF;
252
253
int color2 = bufferedImage2.getRGB(x, y);
254
int red2 = (color2 >> 16) & 0x000000FF;
255
int green2 = (color2 >> 8) & 0x000000FF;
256
int blue2 = (color2) & 0x000000FF;
257
if (red1 != red2 || green1 != green2 || blue1 != blue2) {
258
++mismatchCounter;
259
if ((Math.abs(red1 - red2) > colorTolerance)
260
|| (Math.abs(green1 - green2) > colorTolerance)
261
|| (Math.abs(blue1 - blue2) > colorTolerance)) {
262
263
flag = false;
264
}
265
}
266
}
267
}
268
if (mismatchCounter > mismatchTolerance) {
269
flag = false;
270
}
271
} else {
272
System.err.println("ERROR: size is different");
273
flag = false;
274
}
275
return flag;
276
}
277
278
private static void cleanUp() throws Exception {
279
SwingUtilities.invokeAndWait(new Runnable() {
280
@Override
281
public void run() {
282
frame.dispose();
283
}
284
});
285
}
286
287
private static boolean tryLookAndFeel(String lookAndFeelString)
288
throws Exception {
289
//This test case is not applicable for Motif and gtk LAFs
290
if(lookAndFeelString.contains("motif")
291
|| lookAndFeelString.contains("gtk")) {
292
return false;
293
}
294
try {
295
UIManager.setLookAndFeel(
296
lookAndFeelString);
297
298
} catch (UnsupportedLookAndFeelException
299
| ClassNotFoundException
300
| InstantiationException
301
| IllegalAccessException e) {
302
return false;
303
}
304
return true;
305
}
306
}
307
308