1 \/*
   2  * Copyright (c) 1999, 2014, 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  * @test
  25  * @key headful
  26  * @summary Make sure that on changing state of Undecorated Frame,
  27  *          all the components on it are repainted correctly
  28  * @author Jitender(jitender.singh@eng.sun.com) area=AWT
  29  * @author yan
  30  * @library ../../../../lib/testlibrary
  31  * @build ExtendedRobot
  32  * @run main RepaintTest
  33  */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import javax.swing.JFrame;
  38 import javax.swing.JButton;
  39 import javax.swing.JTextField;
  40 import javax.swing.JPanel;
  41 import java.io.*;
  42 import java.awt.image.*;
  43 
  44 public class RepaintTest {
  45     private static int delay = 150;
  46 
  47     private Frame frame;
  48     private Container panel1, panel2;
  49     private Component button;
  50     private Component textField;
  51     private ExtendedRobot robot;
  52     private Object buttonLock = new Object();
  53     private boolean passed = true;
  54     private boolean buttonClicked = false;
  55     private int MAX_TOLERANCE_LEVEL = 10;
  56 
  57     public static void main(String[] args) {
  58         RepaintTest test = new RepaintTest();
  59         test.doTest(false);
  60         try {
  61             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
  62                 public void run() {
  63                     test.frame.dispose();
  64                 }
  65             });
  66         } catch (Exception e) {
  67             e.printStackTrace();
  68             throw new RuntimeException("Unexpected Exception occured");
  69         }
  70         test.doTest(true);
  71         try {
  72             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
  73                 public void run() {
  74                     test.frame.dispose();
  75                 }
  76             });
  77         } catch (Exception e) {
  78             e.printStackTrace();
  79             throw new RuntimeException("Unexpected Exception occured");
  80         }
  81     }
  82 
  83     /**
  84      * Do screen capture and save it as image
  85      */
  86     private static void captureScreenAndSave() {
  87 
  88         try {
  89             Robot robot = new Robot();
  90             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  91             Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);
  92             System.out.println("About to screen capture - " + rectangle);
  93             BufferedImage image = robot.createScreenCapture(rectangle);
  94             javax.imageio.ImageIO.write(image, "jpg", new File("ScreenImage.jpg"));
  95             robot.delay(3000);
  96         } catch (Throwable t) {
  97             System.out.println("WARNING: Exception thrown while screen capture!");
  98             t.printStackTrace();
  99         }
 100     }
 101 
 102     private void initializeGUI(boolean swingControl) {
 103         frame = swingControl ? new JFrame() : new Frame();
 104         frame.setLayout(new BorderLayout());
 105 
 106         frame.setSize(300, 300);
 107         frame.setUndecorated(true);
 108 
 109         button = createButton(swingControl, (swingControl ? "Swing Button" : "AWT Button"));
 110         textField = swingControl ? new JTextField("TextField") : new TextField("TextField");
 111         panel1 = swingControl ? new JPanel() : new Panel();
 112         panel2 = swingControl ? new JPanel() : new Panel();
 113         panel1.add(button);
 114         panel2.add(textField);
 115         frame.add(panel2, BorderLayout.SOUTH);
 116         frame.add(panel1, BorderLayout.NORTH);
 117 
 118         frame.setBackground(Color.green);
 119         frame.setVisible(true);
 120         frame.toFront();
 121     }
 122     private Component createButton(boolean swingControl, String txt) {
 123         if(swingControl) {
 124             JButton jbtn = new JButton(txt);
 125             jbtn.addActionListener(new ActionListener() {
 126                 public void actionPerformed(ActionEvent e) {
 127                     buttonClicked = true;
 128                     synchronized (buttonLock) {
 129                         try {
 130                             buttonLock.notifyAll();
 131                         } catch (Exception ex) {
 132                             ex.printStackTrace();
 133                         }
 134                     }
 135                 }
 136             });
 137             return jbtn;
 138         }else {
 139             Button btn = new Button(txt);
 140             btn.addActionListener(new ActionListener() {
 141                 public void actionPerformed(ActionEvent e) {
 142                     buttonClicked = true;
 143                     synchronized (buttonLock) {
 144                         try {
 145                             buttonLock.notifyAll();
 146                         } catch (Exception ex) {
 147                             ex.printStackTrace();
 148                         }
 149                     }
 150                 }
 151             });
 152             return btn;
 153         }
 154     }
 155 
 156     public void doTest(boolean swingControl) {
 157         try {
 158             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 159                 public void run() {
 160                     initializeGUI(swingControl);
 161                 }
 162             });
 163         } catch (Exception e) {
 164             e.printStackTrace();
 165             throw new RuntimeException("Interrupted or unexpected Exception occured");
 166         }
 167         try {
 168             robot = new ExtendedRobot();
 169             robot.waitForIdle(1000);
 170         } catch (Exception e) {
 171             e.printStackTrace();
 172             throw new RuntimeException("Cannot create robot");
 173         }
 174 
 175         robot.mouseMove(button.getLocationOnScreen().x + button.getSize().width / 2,
 176                         button.getLocationOnScreen().y + button.getSize().height / 2);
 177         robot.waitForIdle(delay);
 178         robot.mousePress(InputEvent.BUTTON1_MASK);
 179         robot.waitForIdle(delay);
 180         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 181 
 182         if (! buttonClicked) {
 183             synchronized (buttonLock) {
 184                 try {
 185                     buttonLock.wait(delay * 10);
 186                 } catch (Exception e) {
 187                 }
 188             }
 189         }
 190         if (! buttonClicked) {
 191             passed = false;
 192             System.err.println("ActionEvent not triggered when " +
 193                     "button is clicked!");
 194             throw new RuntimeException("ActionEvent not triggered");
 195         }
 196 
 197         robot.waitForIdle(delay * 5); // Need to wait until look of the button
 198                                       // returns to normal undepressed
 199         passed = paintAndRepaint(button, (swingControl? "J": "")+"Button");
 200         if( !paintAndRepaint(button, (swingControl? "J": "")+"TextField") ) {
 201             passed = false;
 202         }
 203         if(!passed) {
 204             throw new RuntimeException("Test failed");
 205         }
 206     }
 207     private boolean paintAndRepaint(Component comp, String prefix) {
 208         //Capture the component & compare it's dimensions
 209         //before iconifying & after frame comes back from
 210         //iconified to normal state
 211         System.out.println("paintAndRepaint "+prefix);
 212         Point p = comp.getLocationOnScreen();
 213         Rectangle bRect = new Rectangle((int)p.getX(), (int)p.getY(),
 214                                                 comp.getWidth(), comp.getHeight());
 215         BufferedImage capturedImage = robot.createScreenCapture(bRect);
 216 
 217         try {
 218             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 219                 public void run() {
 220                     frame.setExtendedState(Frame.ICONIFIED);
 221                 }
 222             });
 223         } catch (Exception e) {
 224             e.printStackTrace();
 225             throw new RuntimeException("Exception while setting extended state ICONIFIED");
 226         }
 227         robot.waitForIdle(delay * 5);
 228         try {
 229             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 230                 public void run() {
 231                     frame.setExtendedState(Frame.NORMAL);
 232                     frame.toFront();
 233                 }
 234             });
 235         } catch (Exception e) {
 236             e.printStackTrace();
 237             throw new RuntimeException("Exception while setting extended state NORMAL");
 238         }
 239         robot.waitForIdle(delay * 5);
 240 
 241         if (! p.equals(comp.getLocationOnScreen())) {
 242             passed = false;
 243             System.err.println("FAIL: Frame or component did not get positioned in the same place");
 244         }
 245 
 246         p = comp.getLocationOnScreen();
 247         bRect = new Rectangle((int)p.getX(), (int)p.getY(),
 248                                   comp.getWidth(), comp.getHeight());
 249         BufferedImage capturedImage2 = robot.createScreenCapture(bRect);
 250 
 251         if (! compareImages(capturedImage, capturedImage2)) {
 252             passed = false;
 253             try {
 254                 javax.imageio.ImageIO.write(capturedImage, "jpg", new File(
 255                                    prefix+"BeforeMinimize.jpg"));
 256                 javax.imageio.ImageIO.write(capturedImage2, "jpg", new File(
 257                                    prefix+"AfterMinimize.jpg"));
 258             } catch (Exception e) {
 259                 e.printStackTrace();
 260             }
 261 
 262             System.err.println("FAIL: The frame or component did not get repainted correctly");
 263         }
 264         return passed;
 265     }
 266 
 267     //method for comparing two images
 268     public boolean compareImages(BufferedImage capturedImg, BufferedImage realImg) {
 269         int capturedPixels[], realPixels[];
 270         int imgWidth, imgHeight;
 271         boolean comparison = true;
 272         int toleranceLevel = 0;
 273 
 274         imgWidth = capturedImg.getWidth(null);
 275         imgHeight = capturedImg.getHeight(null);
 276         capturedPixels = new int[imgWidth * imgHeight];
 277         realPixels = new int[imgWidth * imgHeight];
 278 
 279         try {
 280             PixelGrabber pgCapturedImg = new PixelGrabber(capturedImg, 0, 0,
 281                               imgWidth, imgHeight, capturedPixels, 0, imgWidth);
 282             pgCapturedImg.grabPixels();
 283 
 284             PixelGrabber pgRealImg = new PixelGrabber(realImg, 0, 0,
 285                               imgWidth, imgHeight, realPixels, 0, imgWidth);
 286             pgRealImg.grabPixels();
 287 
 288             for(int i=0; i<(imgWidth * imgHeight); i++) {
 289                 if(capturedPixels[i] != realPixels[i]) {
 290                     toleranceLevel++;
 291                 }
 292             }
 293 
 294             if (toleranceLevel > MAX_TOLERANCE_LEVEL) {
 295                 comparison = false;
 296             }
 297         } catch(Exception ie) {
 298             ie.printStackTrace();
 299             comparison = false;
 300         }
 301         return comparison;
 302     }
 303 }