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  /*
25  * @test
26  * @key headful
27  * @bug 8162959
28  * @summary Validate output of createMultiResolutionScreenCapture
29  *          new API which returns MultiResolutionImage.
30  * @run main/othervm -Dsun.java2d.uiScale=1 ScreenCaptureTest
31  * @run main/othervm -Dsun.java2d.uiScale=2 ScreenCaptureTest
32  */
33 import java.awt.Dimension;
34 import java.awt.GraphicsEnvironment;
35 import java.awt.Image;
36 import java.awt.Rectangle;
37 import java.awt.Toolkit;
38 import java.awt.geom.AffineTransform;
39 import java.awt.image.BufferedImage;
40 import java.awt.image.MultiResolutionImage;
41 import java.awt.BorderLayout;
42 import java.awt.Canvas;
43 import java.awt.Color;
44 import java.awt.Frame;
45 import java.awt.Graphics;
46 import java.awt.Panel;
47 import java.awt.Robot;
48 import java.util.List;
49 
50 public class ScreenCaptureTest {
51 
52     private static Robot robot;
53     private static Frame frame;
54     private static boolean isHiDPI = true;
55     private static final Color[] COLORS = {
56         Color.GREEN, Color.BLUE, Color.ORANGE, Color.RED};
57 
main(String[] args)58     public static void main(String[] args) throws Exception {
59 
60         frame = new Frame();
61         frame.setBounds(0, 0, 400, 400);
62         frame.setUndecorated(true);
63         robot = new Robot();
64         Panel panel = new Panel(new BorderLayout());
65         Canvas canvas = new Canvas() {
66             public void paint(Graphics g) {
67                 super.paint(g);
68                 int w = getWidth();
69                 int h = getHeight();
70                 g.setColor(COLORS[0]);
71                 g.fillRect(0, 0, w / 2, h / 2);
72                 g.setColor(COLORS[1]);
73                 g.fillRect(w / 2, 0, w / 2, h / 2);
74                 g.setColor(COLORS[2]);
75                 g.fillRect(0, h / 2, w / 2, h / 2);
76                 g.setColor(COLORS[3]);
77                 g.fillRect(w / 2, h / 2, w / 2, h / 2);
78             }
79         };
80 
81         panel.add(canvas);
82         frame.add(panel);
83         frame.setVisible(true);
84         robot.delay(500);
85         robot.waitForIdle();
86 
87         int w = frame.getWidth();
88         int h = frame.getHeight();
89 
90         // getPixelColor Test
91         // Check pixel color in first quardant GREEN; x=100, y=100
92         if (!robot.getPixelColor(w / 4, h / 4).equals(COLORS[0])) {
93             throw new RuntimeException("Wrong Pixel Color! Expected GREEN");
94         }
95         // Check pixel color in second quardant; BLUE, x=300, y=100
96         if (!robot.getPixelColor(3 * w / 4, h / 4).equals(COLORS[1])) {
97             throw new RuntimeException("Wrong Pixel Color! Expected BLUE");
98         }
99         // Check pixel color in third quardant; ORANGE, x=100, y=300
100         if (!robot.getPixelColor(w / 4, 3 * h / 4).equals(COLORS[2])) {
101             throw new RuntimeException("Wrong Pixel Color! Expected ORANGE");
102         }
103         // Check pixel color in fourth quardant; RED, x=300, y=300
104         if (!robot.getPixelColor(3 * w / 4, 3 * h / 4).equals(COLORS[3])) {
105             throw new RuntimeException("Wrong Pixel Color! Expected RED");
106         }
107 
108         // createScreenCaptureTest
109         AffineTransform tx = GraphicsEnvironment.getLocalGraphicsEnvironment()
110                 .getDefaultScreenDevice().getDefaultConfiguration()
111                 .getDefaultTransform();
112 
113         if (tx.getScaleX() == 1 && tx.getScaleY() == 1) {
114             isHiDPI = false;
115         }
116 
117         MultiResolutionImage image
118                 = robot.createMultiResolutionScreenCapture(frame.getBounds());
119         List<Image> imageList = image.getResolutionVariants();
120         int size = imageList.size();
121         BufferedImage lowResImage;
122         BufferedImage highResImage;
123 
124         if (!isHiDPI) {
125             // Check if output is MultiResolutionImage with one variant
126             if (size != 1) {
127                 throw new RuntimeException(" Invalid variant size");
128             }
129 
130             lowResImage = (BufferedImage) imageList.get(0);
131             System.out.println(frame.getBounds());
132             System.out.println(lowResImage.getWidth()+" "+lowResImage.getHeight());
133             if (frame.getWidth() != lowResImage.getWidth()
134                         || frame.getHeight() != lowResImage.getHeight()) {
135                 throw new RuntimeException(" Invalid Image size");
136             }
137 
138         } else {
139             // Check if output contains two variants.
140             if (size != 2) {
141                 throw new RuntimeException(" Invalid variant size");
142             }
143 
144             // Check if hight resolution image size is scale times low resolution image.
145             lowResImage = (BufferedImage) imageList.get(0);
146             highResImage = (BufferedImage) imageList.get(1);
147 
148             int lW = (int) lowResImage.getWidth();
149             int lH = (int) lowResImage.getHeight();
150             int hW = (int) highResImage.getWidth();
151             int hH = (int) highResImage.getHeight();
152 
153             if ( hW != (tx.getScaleX() * lW) || hH != (tx.getScaleY() * lH)) {
154                 throw new RuntimeException(" Invalid Resolution Variants");
155             }
156 
157             // Check if both image colors are same at some location.
158             if (lowResImage.getRGB(lW / 4, lH / 4)
159                     != highResImage.getRGB(hW / 4, hH / 4)) {
160                 throw new RuntimeException("Wrong image color!");
161             }
162 
163             if (lowResImage.getRGB(3 * lW / 4, lH / 4)
164                     != highResImage.getRGB(3 * hW / 4, hH / 4)) {
165                 throw new RuntimeException("Wrong image color!");
166             }
167 
168             if (lowResImage.getRGB(lW / 4, 3 * lH / 4)
169                     != highResImage.getRGB(hW / 4, 3 * hH / 4)) {
170                 throw new RuntimeException("Wrong image color!");
171             }
172 
173             if (lowResImage.getRGB(3 * lW / 4, 3 * lH / 4)
174                     != highResImage.getRGB(3 * hW / 4, 3 * hH / 4)) {
175                 throw new RuntimeException("Wrong image color!");
176             }
177 
178         }
179 
180         frame.dispose();
181     }
182 
183 }
184