Path: blob/master/test/jdk/sun/java2d/DirectX/OverriddenInsetsTest/OverriddenInsetsTest.java
41153 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 6694230 819861327* @summary Tests that components overriding getInsets paint correctly28* @author [email protected]: area=Graphics29* @run main/othervm OverriddenInsetsTest30*/3132import java.awt.Color;33import java.awt.Frame;34import java.awt.Graphics;35import java.awt.GraphicsEnvironment;36import java.awt.Insets;37import java.awt.Panel;38import java.awt.Point;39import java.awt.Rectangle;40import java.awt.Robot;41import java.awt.event.WindowAdapter;42import java.awt.event.WindowEvent;43import java.awt.image.BufferedImage;44import java.io.File;45import java.io.IOException;46import java.util.concurrent.CountDownLatch;47import javax.imageio.ImageIO;4849public class OverriddenInsetsTest {5051public static final Insets INSETS1 = new Insets(25,25,0,0);52public static final Insets INSETS2 = new Insets(100,100,0,0);53static final CountDownLatch lock = new CountDownLatch(1);54static boolean failed = false;5556public static void main(String[] args) {5758if (GraphicsEnvironment.getLocalGraphicsEnvironment().59getDefaultScreenDevice().getDefaultConfiguration().60getColorModel().getPixelSize() < 16)61{62System.out.println("<16 bit mode detected, test passed");63}6465final Frame f = new Frame("OverriddenInsetsTest");66f.setSize(260,260);6768f.addWindowListener(new WindowAdapter() {69public void windowClosing(WindowEvent e) {70f.setVisible(false);71System.exit(0);72}73});7475f.setBackground(Color.gray);76Panel p1 = new Panel() {77public Insets getInsets() {78return INSETS1;79}80};81p1.setLayout(null);82p1.setSize(250, 250);8384Panel p = new Panel(){85@Override86public Insets getInsets() {87return INSETS2;88}8990public void paint(Graphics g) {91// make sure Vista is done with its effects92try {93Thread.sleep(2000);94} catch (InterruptedException ex) {}95g.setColor(Color.red);96g.drawRect(0,0,getWidth()-1,getHeight()-1 );97g.setColor(Color.blue);98g.fillRect(0,0,getWidth()/2,getHeight()/2);99100Point p = getLocationOnScreen();101try {102Robot r = new Robot();103BufferedImage bi =104r.createScreenCapture(new105Rectangle(p.x, p.y, getWidth()/2, getHeight()/2));106for (int y = 0; y < bi.getHeight(); y++) {107for (int x = 0; x < bi.getWidth(); x++) {108if (bi.getRGB(x, y) != Color.blue.getRGB()) {109failed = true;110System.err.printf("Test failed at %d %d c=%x\n",111x, y, bi.getRGB(x, y));112String name = "OverriddenInsetsTest_res.png";113try {114ImageIO.write(bi, "png", new File(name));115System.out.println("Dumped res to: "+name);116} catch (IOException e) {}117return;118}119}120}121} catch (Exception e) {122failed = true;123} finally {124lock.countDown();125}126}127};128p.setSize(200, 200);129130p1.add(p);131p.setLocation(50, 50);132f.add(p1);133f.setVisible(true);134135try {136lock.await();137} catch (InterruptedException ex) {138ex.printStackTrace();139}140if (args.length <= 0 || !"-show".equals(args[0])) {141f.dispose();142}143144if (failed) {145throw new RuntimeException("Test FAILED.");146}147System.out.println("Test PASSED");148}149}150151152