3020N/A/*
3020N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3020N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3020N/A *
3020N/A * This code is free software; you can redistribute it and/or modify it
3020N/A * under the terms of the GNU General Public License version 2 only, as
3020N/A * published by the Free Software Foundation.
3020N/A *
3020N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3020N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3020N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3020N/A * version 2 for more details (a copy is included in the LICENSE file that
3020N/A * accompanied this code).
3020N/A *
3020N/A * You should have received a copy of the GNU General Public License version
3020N/A * 2 along with this work; if not, write to the Free Software Foundation,
3020N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3020N/A *
3020N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3020N/A * or visit www.oracle.com if you need additional information or have any
3020N/A * questions.
3020N/A */
3020N/A
3020N/A/*
3020N/A @test
3020N/A @bug 6988428
3020N/A @summary Tests whether shape is always set
3020N/A @author anthony.petrov@oracle.com: area=awt.toplevel
3020N/A @run main ShapeNotSetSometimes
3020N/A*/
3020N/A
3020N/A
3020N/Aimport java.awt.*;
3020N/Aimport java.awt.event.InputEvent;
3020N/Aimport java.awt.geom.*;
3020N/A
3020N/A
3020N/Apublic class ShapeNotSetSometimes {
3020N/A
3020N/A private Frame backgroundFrame;
3020N/A private Frame window;
3020N/A private static final Color BACKGROUND_COLOR = Color.BLUE;
3020N/A private Shape shape;
3020N/A private int[][] pointsToCheck;
3020N/A
3020N/A private static Robot robot;
3020N/A
3020N/A public ShapeNotSetSometimes() throws Exception {
3020N/A EventQueue.invokeAndWait(new Runnable() {
3020N/A public void run() {
3020N/A initializeGUI();
3020N/A }
3020N/A });
3020N/A }
3020N/A
3020N/A private void initializeGUI() {
3020N/A backgroundFrame = new BackgroundFrame();
3020N/A backgroundFrame.setUndecorated(true);
3020N/A backgroundFrame.setSize(300, 300);
3020N/A backgroundFrame.setLocation(20, 400);
3020N/A backgroundFrame.setVisible(true);
3020N/A
3020N/A shape = null;
3020N/A String shape_name = null;
3020N/A Area a;
3020N/A GeneralPath gp;
3020N/A shape_name = "Rounded-corners";
3020N/A a = new Area();
3020N/A a.add(new Area(new Rectangle2D.Float(50, 0, 100, 150)));
3020N/A a.add(new Area(new Rectangle2D.Float(0, 50, 200, 50)));
3020N/A a.add(new Area(new Ellipse2D.Float(0, 0, 100, 100)));
3020N/A a.add(new Area(new Ellipse2D.Float(0, 50, 100, 100)));
3020N/A a.add(new Area(new Ellipse2D.Float(100, 0, 100, 100)));
3020N/A a.add(new Area(new Ellipse2D.Float(100, 50, 100, 100)));
3020N/A shape = a;
3020N/A pointsToCheck = new int[][] {
3020N/A // inside shape
3020N/A {106, 86}, {96, 38}, {76, 107}, {180, 25}, {24, 105},
3020N/A {196, 77}, {165, 50}, {14, 113}, {89, 132}, {167, 117},
3020N/A // outside shape
3020N/A {165, 196}, {191, 163}, {146, 185}, {61, 170}, {148, 171},
3020N/A {82, 172}, {186, 11}, {199, 141}, {13, 173}, {187, 3}
3020N/A };
3020N/A
3020N/A window = new TestFrame();
3020N/A window.setUndecorated(true);
3020N/A window.setSize(200, 200);
3020N/A window.setLocation(70, 450);
3020N/A window.setShape(shape);
3020N/A window.setVisible(true);
3020N/A
3020N/A System.out.println("Checking " + window.getClass().getSuperclass().getName() + " with " + shape_name + " shape (" + window.getShape() + ")...");
3020N/A }
3020N/A
3020N/A class BackgroundFrame extends Frame {
3020N/A
3020N/A @Override
3020N/A public void paint(Graphics g) {
3020N/A
3020N/A g.setColor(BACKGROUND_COLOR);
3020N/A g.fillRect(0, 0, 300, 300);
3020N/A
3020N/A super.paint(g);
3020N/A }
3020N/A }
3020N/A
3020N/A class TestFrame extends Frame {
3020N/A
3020N/A @Override
3020N/A public void paint(Graphics g) {
3020N/A
3020N/A g.setColor(Color.WHITE);
3020N/A g.fillRect(0, 0, 200, 200);
3020N/A
3020N/A super.paint(g);
3020N/A }
3020N/A }
3020N/A
3020N/A public static void main(String[] args) throws Exception {
3020N/A robot = new Robot();
3020N/A
3020N/A for(int i = 0; i < 100; i++) {
3020N/A System.out.println("Attempt " + i);
3020N/A new ShapeNotSetSometimes().doTest();
3020N/A }
3020N/A }
3020N/A
3020N/A private void doTest() throws Exception {
3020N/A Point wls = backgroundFrame.getLocationOnScreen();
3020N/A
3020N/A robot.mouseMove(wls.x + 5, wls.y + 5);
3020N/A robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
3020N/A robot.delay(10);
3020N/A robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
3020N/A robot.delay(500);
3020N/A
3020N/A EventQueue.invokeAndWait(new Runnable() {
3020N/A public void run() {
3020N/A window.requestFocus();
3020N/A }
3020N/A });
3020N/A
3020N/A robot.waitForIdle();
3020N/A try {
3020N/A Thread.sleep(300);
3020N/A } catch (InterruptedException e) {
3020N/A // ignore this one
3020N/A }
3020N/A
3020N/A // check transparency
3020N/A final int COUNT_TARGET = 10;
3020N/A
3020N/A // checking outside points only
3020N/A for(int i = COUNT_TARGET; i < COUNT_TARGET * 2; i++) {
3020N/A int x = pointsToCheck[i][0];
3020N/A int y = pointsToCheck[i][1];
3020N/A boolean inside = i < COUNT_TARGET;
3020N/A Color c = robot.getPixelColor(window.getX() + x, window.getY() + y);
3020N/A System.out.println("checking " + x + ", " + y + ", color = " + c);
3020N/A if (inside && BACKGROUND_COLOR.equals(c) || !inside && !BACKGROUND_COLOR.equals(c)) {
3020N/A System.out.println("window.getX() = " + window.getX() + ", window.getY() = " + window.getY());
3020N/A System.err.println("Checking for transparency failed: point: " +
3020N/A (window.getX() + x) + ", " + (window.getY() + y) +
3020N/A ", color = " + c + (inside ? " is of un" : " is not of ") +
3020N/A "expected background color " + BACKGROUND_COLOR);
3020N/A throw new RuntimeException("Test failed. The shape has not been applied.");
3020N/A }
3020N/A }
3020N/A
3020N/A EventQueue.invokeAndWait(new Runnable() {
3020N/A public void run() {
3020N/A backgroundFrame.dispose();
3020N/A window.dispose();
3020N/A }
3020N/A });
3020N/A }
3020N/A}