Path: blob/master/test/jdk/java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java
41153 views
/*1* Copyright (c) 2010, 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 698842827@summary Tests whether shape is always set28@author [email protected]: area=awt.toplevel29@run main ShapeNotSetSometimes30*/313233import java.awt.*;34import java.awt.event.InputEvent;35import java.awt.geom.*;363738public class ShapeNotSetSometimes {3940private Frame backgroundFrame;41private Frame window;42private static final Color BACKGROUND_COLOR = Color.BLUE;43private Shape shape;44private int[][] pointsToCheck;4546private static Robot robot;4748public ShapeNotSetSometimes() throws Exception {49EventQueue.invokeAndWait(this::initializeGUI);50robot.waitForIdle();51}5253private void initializeGUI() {54backgroundFrame = new BackgroundFrame();55backgroundFrame.setUndecorated(true);56backgroundFrame.setSize(300, 300);57backgroundFrame.setLocation(20, 400);58backgroundFrame.setVisible(true);5960shape = null;61String shape_name = null;62Area a;63GeneralPath gp;64shape_name = "Rounded-corners";65a = new Area();66a.add(new Area(new Rectangle2D.Float(50, 0, 100, 150)));67a.add(new Area(new Rectangle2D.Float(0, 50, 200, 50)));68a.add(new Area(new Ellipse2D.Float(0, 0, 100, 100)));69a.add(new Area(new Ellipse2D.Float(0, 50, 100, 100)));70a.add(new Area(new Ellipse2D.Float(100, 0, 100, 100)));71a.add(new Area(new Ellipse2D.Float(100, 50, 100, 100)));72shape = a;73pointsToCheck = new int[][] {74// inside shape75{106, 86}, {96, 38}, {76, 107}, {180, 25}, {24, 105},76{196, 77}, {165, 50}, {14, 113}, {89, 132}, {167, 117},77// outside shape78{165, 196}, {191, 163}, {146, 185}, {61, 170}, {148, 171},79{82, 172}, {186, 11}, {199, 141}, {13, 173}, {187, 3}80};8182window = new TestFrame();83window.setUndecorated(true);84window.setSize(200, 200);85window.setLocation(70, 450);86window.setShape(shape);87window.setVisible(true);8889System.out.println("Checking " + window.getClass().getSuperclass().getName() + " with " + shape_name + " shape (" + window.getShape() + ")...");90}9192class BackgroundFrame extends Frame {9394@Override95public void paint(Graphics g) {9697g.setColor(BACKGROUND_COLOR);98g.fillRect(0, 0, 300, 300);99100super.paint(g);101}102}103104class TestFrame extends Frame {105106@Override107public void paint(Graphics g) {108109g.setColor(Color.WHITE);110g.fillRect(0, 0, 200, 200);111112super.paint(g);113}114}115116public static void main(String[] args) throws Exception {117robot = new Robot();118119for(int i = 0; i < 50; i++) {120System.out.println("Attempt " + i);121new ShapeNotSetSometimes().doTest();122}123}124125private void doTest() throws Exception {126Point wls = backgroundFrame.getLocationOnScreen();127128robot.mouseMove(wls.x + 5, wls.y + 5);129robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);130robot.delay(10);131robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);132robot.delay(500);133134EventQueue.invokeAndWait(window::requestFocus);135136robot.waitForIdle();137try {138Thread.sleep(300);139} catch (InterruptedException e) {140// ignore this one141}142143// check transparency144final int COUNT_TARGET = 10;145146// checking outside points only147for(int i = COUNT_TARGET; i < COUNT_TARGET * 2; i++) {148int x = pointsToCheck[i][0];149int y = pointsToCheck[i][1];150boolean inside = i < COUNT_TARGET;151Color c = robot.getPixelColor(window.getX() + x, window.getY() + y);152System.out.println("checking " + x + ", " + y + ", color = " + c);153if (inside && BACKGROUND_COLOR.equals(c) || !inside && !BACKGROUND_COLOR.equals(c)) {154System.out.println("window.getX() = " + window.getX() + ", window.getY() = " + window.getY());155System.err.println("Checking for transparency failed: point: " +156(window.getX() + x) + ", " + (window.getY() + y) +157", color = " + c + (inside ? " is of un" : " is not of ") +158"expected background color " + BACKGROUND_COLOR);159throw new RuntimeException("Test failed. The shape has not been applied.");160}161}162163EventQueue.invokeAndWait(new Runnable() {164public void run() {165backgroundFrame.dispose();166window.dispose();167}168});169}170}171172173