OverriddenInsetsTest.java revision 430
430N/A/*
430N/A * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
430N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
430N/A *
430N/A * This code is free software; you can redistribute it and/or modify it
430N/A * under the terms of the GNU General Public License version 2 only, as
430N/A * published by the Free Software Foundation.
430N/A *
430N/A * This code is distributed in the hope that it will be useful, but WITHOUT
430N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
430N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
430N/A * version 2 for more details (a copy is included in the LICENSE file that
430N/A * accompanied this code).
430N/A *
430N/A * You should have received a copy of the GNU General Public License version
430N/A * 2 along with this work; if not, write to the Free Software Foundation,
430N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
430N/A *
430N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
430N/A * CA 95054 USA or visit www.sun.com if you need additional information or
430N/A * have any questions.
430N/A */
430N/A
430N/A/*
430N/A * @test
430N/A * @bug 6694230
430N/A * @summary Tests that components overriding getInsets paint correctly
430N/A * @author Dmitri.Trembovetski@sun.com: area=Graphics
430N/A * @run main/othervm OverriddenInsetsTest
430N/A * @run main/othervm -Dsun.java2d.opengl=True OverriddenInsetsTest
430N/A */
430N/A
430N/Aimport java.awt.Color;
430N/Aimport java.awt.Frame;
430N/Aimport java.awt.Graphics;
430N/Aimport java.awt.GraphicsEnvironment;
430N/Aimport java.awt.Insets;
430N/Aimport java.awt.Panel;
430N/Aimport java.awt.Point;
430N/Aimport java.awt.Rectangle;
430N/Aimport java.awt.Robot;
430N/Aimport java.awt.event.WindowAdapter;
430N/Aimport java.awt.event.WindowEvent;
430N/Aimport java.awt.image.BufferedImage;
430N/Aimport java.io.File;
430N/Aimport java.io.IOException;
430N/Aimport java.util.concurrent.CountDownLatch;
430N/Aimport javax.imageio.ImageIO;
430N/A
430N/Apublic class OverriddenInsetsTest {
430N/A
430N/A public static final Insets INSETS1 = new Insets(25,25,0,0);
430N/A public static final Insets INSETS2 = new Insets(100,100,0,0);
430N/A static final CountDownLatch lock = new CountDownLatch(1);
430N/A static boolean failed = false;
430N/A
430N/A public static void main(String[] args) {
430N/A
430N/A if (GraphicsEnvironment.getLocalGraphicsEnvironment().
430N/A getDefaultScreenDevice().getDefaultConfiguration().
430N/A getColorModel().getPixelSize() < 16)
430N/A {
430N/A System.out.println("<16 bit mode detected, test passed");
430N/A }
430N/A
430N/A final Frame f = new Frame("OverriddenInsetsTest");
430N/A f.setSize(260,260);
430N/A
430N/A f.addWindowListener(new WindowAdapter() {
430N/A public void windowClosing(WindowEvent e) {
430N/A f.setVisible(false);
430N/A System.exit(0);
430N/A }
430N/A });
430N/A
430N/A f.setBackground(Color.gray);
430N/A Panel p1 = new Panel() {
430N/A public Insets getInsets() {
430N/A return INSETS1;
430N/A }
430N/A };
430N/A p1.setLayout(null);
430N/A p1.setSize(250, 250);
430N/A
430N/A Panel p = new Panel(){
430N/A @Override
430N/A public Insets getInsets() {
430N/A return INSETS2;
430N/A }
430N/A
430N/A public void paint(Graphics g) {
430N/A // make sure Vista is done with its effects
430N/A try {
430N/A Thread.sleep(2000);
430N/A } catch (InterruptedException ex) {}
430N/A g.setColor(Color.red);
430N/A g.drawRect(0,0,getWidth()-1,getHeight()-1 );
430N/A g.setColor(Color.blue);
430N/A g.fillRect(0,0,getWidth()/2,getHeight()/2);
430N/A
430N/A Point p = getLocationOnScreen();
430N/A try {
430N/A Robot r = new Robot();
430N/A BufferedImage bi =
430N/A r.createScreenCapture(new
430N/A Rectangle(p.x, p.y, getWidth()/2, getHeight()/2));
430N/A for (int y = 0; y < bi.getHeight(); y++) {
430N/A for (int x = 0; x < bi.getWidth(); x++) {
430N/A if (bi.getRGB(x, y) != Color.blue.getRGB()) {
430N/A failed = true;
430N/A System.err.printf("Test failed at %d %d c=%x\n",
430N/A x, y, bi.getRGB(x, y));
430N/A String name = "OverriddenInsetsTest_res.png";
430N/A try {
430N/A ImageIO.write(bi, "png", new File(name));
430N/A System.out.println("Dumped res to: "+name);
430N/A } catch (IOException e) {}
430N/A return;
430N/A }
430N/A }
430N/A }
430N/A } catch (Exception e) {
430N/A failed = true;
430N/A } finally {
430N/A lock.countDown();
430N/A }
430N/A }
430N/A };
430N/A p.setSize(200, 200);
430N/A
430N/A p1.add(p);
430N/A p.setLocation(50, 50);
430N/A f.add(p1);
430N/A f.setVisible(true);
430N/A
430N/A try {
430N/A lock.await();
430N/A } catch (InterruptedException ex) {
430N/A ex.printStackTrace();
430N/A }
430N/A if (args.length <= 0 || !"-show".equals(args[0])) {
430N/A f.dispose();
430N/A }
430N/A
430N/A if (failed) {
430N/A throw new RuntimeException("Test FAILED.");
430N/A }
430N/A System.out.println("Test PASSED");
430N/A }
430N/A}