1 /*
2 * Copyright (c) 2018, 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.Color;
25 import java.awt.Frame;
26 import java.awt.Point;
27 import java.awt.Robot;
28 import java.util.List;
29
30 /**
31 * @test
32 * @key headful
33 * @bug 8215105
34 * @summary tests that Robot can capture the common colors without artifacts
35 */
36 public final class CheckCommonColors {
37
38 private static final Frame frame = new Frame();
39 private static Robot robot;
40
41 public static void main(final String[] args) throws Exception {
42 robot = new Robot();
43 try {
44 test();
45 } finally {
46 frame.dispose();
47 }
48 }
49
50 private static void test() {
51 frame.setSize(400, 400);
52 frame.setLocationRelativeTo(null);
53 frame.setUndecorated(true);
54 for (final Color color : List.of(Color.WHITE, Color.LIGHT_GRAY,
55 Color.GRAY, Color.DARK_GRAY,
56 Color.BLACK, Color.RED, Color.PINK,
57 Color.ORANGE, Color.YELLOW,
58 Color.GREEN, Color.MAGENTA, Color.CYAN,
59 Color.BLUE)) {
60 frame.dispose();
61 frame.setBackground(color);
62 frame.setVisible(true);
63 checkPixels(color);
64 }
65 }
66
67 private static void checkPixels(final Color color) {
68 int attempt = 0;
69 while (true) {
70 Point p = frame.getLocationOnScreen();
71 Color pixel = robot.getPixelColor(p.x + frame.getWidth() / 2,
72 p.y + frame.getHeight() / 2);
73 if (color.equals(pixel)) {
74 return;
75 }
76 if (attempt > 10) {
77 System.err.println("Expected: " + color);
78 System.err.println("Actual: " + pixel);
79 throw new RuntimeException("Too many attempts: " + attempt);
80 }
81 // skip Robot.waitForIdle to speedup the common case, but also take
82 // care about slow systems
83 robot.delay((int) Math.pow(2.2, attempt++));
84 }
85 }
86 }
|
1 /*
2 * Copyright (c) 2018, 2020, 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.Color;
25 import java.awt.Frame;
26 import java.awt.GraphicsDevice;
27 import java.awt.GraphicsEnvironment;
28 import java.awt.Point;
29 import java.awt.Rectangle;
30 import java.awt.Robot;
31 import java.awt.image.BufferedImage;
32 import java.util.List;
33
34 /**
35 * @test
36 * @key headful
37 * @bug 8215105 8211999
38 * @summary tests that Robot can capture the common colors without artifacts
39 */
40 public final class CheckCommonColors {
41
42 private static final Frame frame = new Frame();
43 private static Robot robot;
44
45 public static void main(final String[] args) throws Exception {
46 robot = new Robot();
47 var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
48 for (GraphicsDevice gd : ge.getScreenDevices()) {
49 try {
50 test(gd.getDefaultConfiguration().getBounds());
51 } finally {
52 frame.dispose();
53 }
54 }
55 }
56
57 private static void test(Rectangle screen) {
58 frame.setSize(400, 400);
59 frame.setLocation((int)screen.getCenterX() - 200,
60 (int)screen.getCenterY() - 200);
61 frame.setUndecorated(true);
62 for (final Color color : List.of(Color.WHITE, Color.LIGHT_GRAY,
63 Color.GRAY, Color.DARK_GRAY,
64 Color.BLACK, Color.RED, Color.PINK,
65 Color.ORANGE, Color.YELLOW,
66 Color.GREEN, Color.MAGENTA, Color.CYAN,
67 Color.BLUE)) {
68 frame.dispose();
69 frame.setBackground(color);
70 frame.setVisible(true);
71 checkPixels(color, true);
72 checkPixels(color, false);
73 }
74 }
75
76 private static void checkPixels(final Color color, boolean useRect) {
77 System.out.println("color = " + color + ", useRect = " + useRect);
78 int attempt = 0;
79 while (true) {
80 Point p = frame.getLocationOnScreen();
81 Color pixel;
82 Rectangle rect = new Rectangle(p.x + frame.getWidth() / 2,
83 p.y + frame.getHeight() / 2, 1, 1);
84 if (useRect) {
85 BufferedImage bi = robot.createScreenCapture(rect);
86 pixel = new Color(bi.getRGB(0, 0));
87 } else {
88 pixel = robot.getPixelColor(rect.x, rect.y);
89 }
90 if (color.equals(pixel)) {
91 return;
92 }
93 if (attempt > 10) {
94 System.err.println("Expected: " + color);
95 System.err.println("Actual: " + pixel);
96 throw new RuntimeException("Too many attempts: " + attempt);
97 }
98 // skip Robot.waitForIdle to speedup the common case, but also take
99 // care about slow systems
100 robot.delay((int) Math.pow(2.2, attempt++));
101 }
102 }
103 }
|