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