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