1 /*
   2  * Copyright (c) 2007, 2008, 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 6694230
  27  * @summary Tests that components overriding getInsets paint correctly
  28  * @author Dmitri.Trembovetski@sun.com: area=Graphics
  29  * @run main/othervm OverriddenInsetsTest
  30  * @run main/othervm -Dsun.java2d.opengl=True OverriddenInsetsTest
  31  */
  32 
  33 import java.awt.Color;
  34 import java.awt.Frame;
  35 import java.awt.Graphics;
  36 import java.awt.GraphicsEnvironment;
  37 import java.awt.Insets;
  38 import java.awt.Panel;
  39 import java.awt.Point;
  40 import java.awt.Rectangle;
  41 import java.awt.Robot;
  42 import java.awt.event.WindowAdapter;
  43 import java.awt.event.WindowEvent;
  44 import java.awt.image.BufferedImage;
  45 import java.io.File;
  46 import java.io.IOException;
  47 import java.util.concurrent.CountDownLatch;
  48 import javax.imageio.ImageIO;
  49 
  50 public class OverriddenInsetsTest {
  51 
  52     public static final Insets INSETS1 = new Insets(25,25,0,0);
  53     public static final Insets INSETS2 = new Insets(100,100,0,0);
  54     static final CountDownLatch lock = new CountDownLatch(1);
  55     static boolean failed = false;
  56 
  57     public static void main(String[] args) {
  58 
  59         if (GraphicsEnvironment.getLocalGraphicsEnvironment().
  60                 getDefaultScreenDevice().getDefaultConfiguration().
  61                     getColorModel().getPixelSize() < 16)
  62         {
  63             System.out.println("<16 bit mode detected, test passed");
  64         }
  65 
  66         final Frame f = new Frame("OverriddenInsetsTest");
  67         f.setSize(260,260);
  68 
  69         f.addWindowListener(new WindowAdapter() {
  70             public void windowClosing(WindowEvent e) {
  71                 f.setVisible(false);
  72                 System.exit(0);
  73             }
  74         });
  75 
  76         f.setBackground(Color.gray);
  77         Panel p1 = new Panel() {
  78             public Insets getInsets() {
  79                 return INSETS1;
  80             }
  81         };
  82         p1.setLayout(null);
  83         p1.setSize(250, 250);
  84 
  85         Panel p = new Panel(){
  86             @Override
  87             public Insets getInsets() {
  88                 return INSETS2;
  89             }
  90 
  91             public void paint(Graphics g) {
  92                 // make sure Vista is done with its effects
  93                 try {
  94                     Thread.sleep(2000);
  95                 } catch (InterruptedException ex) {}
  96                 g.setColor(Color.red);
  97                 g.drawRect(0,0,getWidth()-1,getHeight()-1 );
  98                 g.setColor(Color.blue);
  99                 g.fillRect(0,0,getWidth()/2,getHeight()/2);
 100 
 101                 Point p = getLocationOnScreen();
 102                 try {
 103                     Robot r = new Robot();
 104                     BufferedImage bi =
 105                         r.createScreenCapture(new
 106                             Rectangle(p.x, p.y, getWidth()/2, getHeight()/2));
 107                     for (int y = 0; y < bi.getHeight(); y++) {
 108                         for (int x = 0; x < bi.getWidth(); x++) {
 109                             if (bi.getRGB(x, y) != Color.blue.getRGB()) {
 110                                 failed = true;
 111                                 System.err.printf("Test failed at %d %d c=%x\n",
 112                                                   x, y, bi.getRGB(x, y));
 113                                 String name = "OverriddenInsetsTest_res.png";
 114                                 try {
 115                                     ImageIO.write(bi, "png", new File(name));
 116                                     System.out.println("Dumped res to: "+name);
 117                                 } catch (IOException e) {}
 118                                 return;
 119                             }
 120                         }
 121                     }
 122                 } catch (Exception e) {
 123                     failed = true;
 124                 } finally {
 125                     lock.countDown();
 126                 }
 127             }
 128         };
 129         p.setSize(200, 200);
 130 
 131         p1.add(p);
 132         p.setLocation(50, 50);
 133         f.add(p1);
 134         f.setVisible(true);
 135 
 136         try {
 137             lock.await();
 138         } catch (InterruptedException ex) {
 139             ex.printStackTrace();
 140         }
 141         if (args.length <= 0 || !"-show".equals(args[0])) {
 142             f.dispose();
 143         }
 144 
 145         if (failed) {
 146             throw new RuntimeException("Test FAILED.");
 147         }
 148         System.out.println("Test PASSED");
 149     }
 150 }