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