Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java
38828 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@bug 698842826@summary Tests whether shape is always set27@author [email protected]: area=awt.toplevel28@run main ShapeNotSetSometimes29*/303132import java.awt.*;33import java.awt.event.InputEvent;34import java.awt.geom.*;353637public class ShapeNotSetSometimes {3839private Frame backgroundFrame;40private Frame window;41private static final Color BACKGROUND_COLOR = Color.BLUE;42private Shape shape;43private int[][] pointsToCheck;4445private static Robot robot;4647public ShapeNotSetSometimes() throws Exception {48EventQueue.invokeAndWait(new Runnable() {49public void run() {50initializeGUI();51}52});53}5455private void initializeGUI() {56backgroundFrame = new BackgroundFrame();57backgroundFrame.setUndecorated(true);58backgroundFrame.setSize(300, 300);59backgroundFrame.setLocation(20, 400);60backgroundFrame.setVisible(true);6162shape = null;63String shape_name = null;64Area a;65GeneralPath gp;66shape_name = "Rounded-corners";67a = new Area();68a.add(new Area(new Rectangle2D.Float(50, 0, 100, 150)));69a.add(new Area(new Rectangle2D.Float(0, 50, 200, 50)));70a.add(new Area(new Ellipse2D.Float(0, 0, 100, 100)));71a.add(new Area(new Ellipse2D.Float(0, 50, 100, 100)));72a.add(new Area(new Ellipse2D.Float(100, 0, 100, 100)));73a.add(new Area(new Ellipse2D.Float(100, 50, 100, 100)));74shape = a;75pointsToCheck = new int[][] {76// inside shape77{106, 86}, {96, 38}, {76, 107}, {180, 25}, {24, 105},78{196, 77}, {165, 50}, {14, 113}, {89, 132}, {167, 117},79// outside shape80{165, 196}, {191, 163}, {146, 185}, {61, 170}, {148, 171},81{82, 172}, {186, 11}, {199, 141}, {13, 173}, {187, 3}82};8384window = new TestFrame();85window.setUndecorated(true);86window.setSize(200, 200);87window.setLocation(70, 450);88window.setShape(shape);89window.setVisible(true);9091System.out.println("Checking " + window.getClass().getSuperclass().getName() + " with " + shape_name + " shape (" + window.getShape() + ")...");92}9394class BackgroundFrame extends Frame {9596@Override97public void paint(Graphics g) {9899g.setColor(BACKGROUND_COLOR);100g.fillRect(0, 0, 300, 300);101102super.paint(g);103}104}105106class TestFrame extends Frame {107108@Override109public void paint(Graphics g) {110111g.setColor(Color.WHITE);112g.fillRect(0, 0, 200, 200);113114super.paint(g);115}116}117118public static void main(String[] args) throws Exception {119robot = new Robot();120121for(int i = 0; i < 100; i++) {122System.out.println("Attempt " + i);123new ShapeNotSetSometimes().doTest();124}125}126127private void doTest() throws Exception {128Point wls = backgroundFrame.getLocationOnScreen();129130robot.mouseMove(wls.x + 5, wls.y + 5);131robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);132robot.delay(10);133robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);134robot.delay(500);135136EventQueue.invokeAndWait(new Runnable() {137public void run() {138window.requestFocus();139}140});141142robot.waitForIdle();143try {144Thread.sleep(300);145} catch (InterruptedException e) {146// ignore this one147}148149// check transparency150final int COUNT_TARGET = 10;151152// checking outside points only153for(int i = COUNT_TARGET; i < COUNT_TARGET * 2; i++) {154int x = pointsToCheck[i][0];155int y = pointsToCheck[i][1];156boolean inside = i < COUNT_TARGET;157Color c = robot.getPixelColor(window.getX() + x, window.getY() + y);158System.out.println("checking " + x + ", " + y + ", color = " + c);159if (inside && BACKGROUND_COLOR.equals(c) || !inside && !BACKGROUND_COLOR.equals(c)) {160System.out.println("window.getX() = " + window.getX() + ", window.getY() = " + window.getY());161System.err.println("Checking for transparency failed: point: " +162(window.getX() + x) + ", " + (window.getY() + y) +163", color = " + c + (inside ? " is of un" : " is not of ") +164"expected background color " + BACKGROUND_COLOR);165throw new RuntimeException("Test failed. The shape has not been applied.");166}167}168169EventQueue.invokeAndWait(new Runnable() {170public void run() {171backgroundFrame.dispose();172window.dispose();173}174});175}176}177178179