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 Visually validate multiresolution screencapture.
  28  * @run main/othervm/manual -Dsun.java2d.uiScale=2 ScreenCaptureResolutionTest 
  29  */
  30 import java.awt.AWTException;
  31 import java.awt.Color;
  32 import java.awt.GridBagConstraints;
  33 import java.awt.GridBagLayout;
  34 import java.util.concurrent.CountDownLatch;
  35 import javax.swing.JPanel;
  36 import javax.swing.JTextArea;
  37 import javax.swing.SwingUtilities;
  38 import javax.swing.JButton;
  39 import javax.swing.JFrame;
  40 import java.awt.event.ActionEvent;
  41 import java.awt.event.ActionListener;
  42 import java.util.concurrent.TimeUnit;
  43 import javax.swing.ImageIcon;
  44 import javax.swing.JLabel;
  45 import java.awt.Robot;
  46 import java.awt.image.BufferedImage;
  47 import java.util.List;
  48 import java.awt.Image;
  49 import java.awt.image.MultiResolutionImage;
  50 
  51 public class ScreenCaptureResolutionTest {
  52 
  53     public static void main(String args[]) throws Exception {
  54 
  55         final CountDownLatch latch = new CountDownLatch(1);
  56         TestUI test = new TestUI(latch);
  57 
  58         SwingUtilities.invokeLater(new Runnable() {
  59             @Override
  60             public void run() {
  61                 try {
  62                     test.createUI();
  63                 } catch (Exception ex) {
  64                     throw new RuntimeException("Exception while creating UI");
  65                 }
  66             }
  67         });
  68         latch.await(3, TimeUnit.SECONDS);
  69         SwingUtilities.invokeLater(new Runnable() {
  70             @Override
  71             public void run() {
  72                 try {
  73                     test.validateScreenCapture();
  74                 } catch (Exception ex) {
  75                     throw new RuntimeException("Exception while"
  76                         + " validating ScreenCapture");
  77                 }
  78             }
  79         });
  80         boolean status = latch.await(5, TimeUnit.MINUTES);
  81         if (!status) {
  82             System.out.println("Test timed out.");
  83         }
  84 
  85         SwingUtilities.invokeAndWait(new Runnable() {
  86             @Override
  87             public void run() {
  88                 try {
  89                     test.disposeUI();
  90                 } catch (Exception ex) {
  91                     throw new RuntimeException("Exception while disposing UI");
  92                 }
  93             }
  94         });
  95 
  96         if (test.testResult == false) {
  97             throw new RuntimeException("Test Failed.");
  98         }
  99     }
 100 }
 101 
 102 class TestUI {
 103 
 104     private static JFrame mainFrame;
 105     private static JFrame outputImageFrame;
 106     private static JFrame inputImageFrame;
 107     private static JPanel outputControlPanel;
 108     private static JPanel mainControlPanel;
 109 
 110     private static JTextArea instructionTextArea;
 111 
 112     private static JPanel inputImagePanel;
 113     private static JPanel resultButtonPanel;
 114     private static JButton passButton;
 115     private static JButton failButton;
 116 
 117     private static GridBagLayout layout;
 118     private final CountDownLatch latch;
 119     public boolean testResult = false;
 120 
 121     public TestUI(CountDownLatch latch) throws Exception {
 122         this.latch = latch;
 123     }
 124 
 125     public void validateScreenCapture() throws AWTException {
 126         Robot robot = new Robot();
 127         outputControlPanel = new JPanel(layout);
 128         GridBagConstraints ogbc = new GridBagConstraints();
 129 
 130         MultiResolutionImage image
 131                 = robot.createMultiResolutionScreenCapture(inputImageFrame.getBounds());
 132         List<Image> imageList = image.getResolutionVariants();
 133         int size = imageList.size();
 134         BufferedImage lowResImage = (BufferedImage) imageList.get(0); 
 135         BufferedImage highResImage = (BufferedImage) imageList.get(1);
 136       
 137         outputImageFrame = new JFrame("Output");
 138         outputImageFrame.getContentPane().setLayout(new GridBagLayout());
 139         ogbc.gridx = 0;
 140         ogbc.gridy = 0;
 141         ogbc.fill = GridBagConstraints.HORIZONTAL;
 142         outputControlPanel.add(new JLabel(new ImageIcon(lowResImage)), ogbc);
 143         int width = lowResImage.getWidth();
 144         int height = lowResImage.getHeight();
 145         JLabel labelImg1 = new JLabel("LEFT:Width: " + width
 146                                       + " Height: " + height);
 147         ogbc.gridx = 0;
 148         ogbc.gridy = 1;
 149         outputControlPanel.add(labelImg1, ogbc);
 150         ogbc.gridx = 1;
 151         ogbc.gridy = 0;
 152         outputControlPanel.add(new JLabel(new ImageIcon(highResImage)), ogbc);
 153         width = highResImage.getWidth();
 154         height = highResImage.getHeight();
 155         JLabel labelImg2 = new JLabel("RIGHT:Width: " + width
 156                                       + " Height: " + height);
 157         ogbc.gridx = 1;
 158         ogbc.gridy = 1;
 159         outputControlPanel.add(labelImg2, ogbc);
 160         outputControlPanel.setBackground(Color.GRAY);
 161         outputImageFrame.add(outputControlPanel);
 162 
 163         outputImageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 164         outputImageFrame.setBounds(600, 0, 400, 300);
 165         outputImageFrame.setLocationRelativeTo(null);
 166         outputImageFrame.setVisible(true);
 167     }
 168 
 169     public final void createUI() throws Exception {
 170 
 171         mainFrame = new JFrame("ScreenCaptureResolutionTest");
 172 
 173         layout = new GridBagLayout();
 174         mainControlPanel = new JPanel(layout);
 175         resultButtonPanel = new JPanel(layout);
 176 
 177         GridBagConstraints gbc = new GridBagConstraints();
 178 
 179         // Create Test instructions
 180         String instructions
 181                 = "INSTRUCTIONS:"
 182                 + "\n Test to Visually validate MultiResolutionn Image. "
 183                 + "\n 1. Check if output window contains two screenshot "
 184                 + "\n    of input window. "
 185                 + "\n 2. Right image should be twice the size of Left. "
 186                 + "\n 3. Quality of Right image should be better than Left. "
 187                 + "\n If all the three conditons are satisfied, then "
 188                 + "\n click pass else fail.";
 189         instructionTextArea = new JTextArea();
 190         instructionTextArea.setText(instructions);
 191         instructionTextArea.setEnabled(false);
 192         instructionTextArea.setDisabledTextColor(Color.black);
 193         instructionTextArea.setBackground(Color.white);
 194 
 195         gbc.gridx = 0;
 196         gbc.gridy = 0;
 197         gbc.fill = GridBagConstraints.HORIZONTAL;
 198         mainControlPanel.add(instructionTextArea, gbc);
 199 
 200         gbc.gridx = 0;
 201         gbc.gridy = 1;
 202 
 203         inputImagePanel = new JPanel(layout);
 204         JLabel label = new JLabel("Resolution");
 205         inputImagePanel.add(label);
 206         inputImageFrame = new JFrame("Input");
 207         inputImageFrame.add(inputImagePanel);
 208    
 209         inputImageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 210         inputImageFrame.setBounds(100, 200, 100, 100);
 211         inputImageFrame.setVisible(true);
 212 
 213         passButton = new JButton("Pass");
 214         passButton.setActionCommand("Pass");
 215         passButton.addActionListener((ActionEvent e) -> {
 216             outputImageFrame.dispose();
 217             inputImageFrame.dispose();
 218             testResult = true;
 219             mainFrame.dispose();
 220             latch.countDown();
 221 
 222         });
 223         failButton = new JButton("Fail");
 224         failButton.setActionCommand("Fail");
 225         failButton.addActionListener(new ActionListener() {
 226             @Override
 227             public void actionPerformed(ActionEvent e) {
 228                 outputImageFrame.dispose();
 229                 inputImageFrame.dispose();
 230                 testResult = false;
 231                 mainFrame.dispose();
 232                 latch.countDown();
 233             }
 234         });
 235         gbc.gridx = 0;
 236         gbc.gridy = 0;
 237         resultButtonPanel.add(passButton, gbc);
 238         gbc.gridx = 1;
 239         gbc.gridy = 0;
 240         resultButtonPanel.add(failButton, gbc);
 241 
 242         gbc.gridx = 0;
 243         gbc.gridy = 2;
 244         mainControlPanel.add(resultButtonPanel, gbc);
 245 
 246         mainFrame.add(mainControlPanel);
 247         mainFrame.pack();
 248         mainFrame.setVisible(true);
 249     }
 250 
 251     public void disposeUI() {
 252         outputImageFrame.dispose();
 253         inputImageFrame.dispose();
 254         mainFrame.setVisible(false);
 255         mainFrame.dispose();
 256     }
 257 
 258 }