1 /*
  2  * Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25   @test
 26   @key headful
 27   @bug 6988428
 28   @summary Tests whether shape is always set
 29   @author anthony.petrov@oracle.com: area=awt.toplevel
 30   @run main ShapeNotSetSometimes
 31 */
 32 
 33 
 34 import java.awt.Color;
 35 import java.awt.EventQueue;
 36 import java.awt.Frame;
 37 import java.awt.Graphics;
 38 import java.awt.Point;
 39 import java.awt.Rectangle;
 40 import java.awt.Robot;
 41 import java.awt.geom.Area;
 42 import java.awt.geom.Ellipse2D;
 43 import java.awt.geom.Rectangle2D;
 44 
 45 
 46 public class ShapeNotSetSometimes {
 47 
 48     private Frame backgroundFrame;
 49     private Frame window;
 50     private static final Color BACKGROUND_COLOR = Color.GREEN;
 51     private static final Color SHAPE_COLOR = Color.WHITE;
 52     private Point[] pointsOutsideToCheck;
 53     private Point[] shadedPointsToCheck;
 54     private Point innerPoint;
 55 
 56     private final Rectangle bounds = new Rectangle(220, 400, 300, 300);
 57 
 58     private static Robot robot;
 59 
 60     public ShapeNotSetSometimes() throws Exception {
 61         EventQueue.invokeAndWait(this::initializeGUI);
 62         robot.waitForIdle();
 63     }
 64 
 65     private void initializeGUI() {
 66         backgroundFrame = new BackgroundFrame();
 67         backgroundFrame.setUndecorated(true);
 68         backgroundFrame.setBounds(bounds);
 69         backgroundFrame.setVisible(true);
 70 
 71         Area area = new Area();
 72         area.add(new Area(new Rectangle2D.Float(100, 50, 100, 150)));
 73         area.add(new Area(new Rectangle2D.Float(50, 100, 200, 50)));
 74         area.add(new Area(new Ellipse2D.Float(50, 50, 100, 100)));
 75         area.add(new Area(new Ellipse2D.Float(50, 100, 100, 100)));
 76         area.add(new Area(new Ellipse2D.Float(150, 50, 100, 100)));
 77         area.add(new Area(new Ellipse2D.Float(150, 100, 100, 100)));
 78 
 79 
 80         innerPoint = new Point(150, 130);
 81         pointsOutsideToCheck = new Point[] {
 82                 new Point(150, 20),
 83                 new Point(280, 120),
 84                 new Point(150, 250),
 85                 new Point(20, 120)
 86         };
 87 
 88         shadedPointsToCheck = new Point[] {
 89                 new Point(62, 62),
 90                 new Point(240, 185)
 91         };
 92 
 93         window = new TestFrame();
 94         window.setUndecorated(true);
 95         window.setBounds(bounds);
 96         window.setShape(area);
 97         window.setVisible(true);
 98     }
 99 
100     static class BackgroundFrame extends Frame {
101 
102         @Override
103         public void paint(Graphics g) {
104 
105             g.setColor(BACKGROUND_COLOR);
106             g.fillRect(0, 0, 300, 300);
107 
108             super.paint(g);
109         }
110     }
111 
112     class TestFrame extends Frame {
113 
114         @Override
115         public void paint(Graphics g) {
116 
117             g.setColor(SHAPE_COLOR);
118             g.fillRect(0, 0, bounds.width, bounds.height);
119 
120             super.paint(g);
121         }
122     }
123 
124     public static void main(String[] args) throws Exception {
125         robot = new Robot();
126 
127         for(int i = 0; i < 50; i++) {
128             System.out.println("Attempt " + i);
129             new ShapeNotSetSometimes().doTest();
130         }
131     }
132 
133     private void doTest() throws Exception {
134         EventQueue.invokeAndWait(backgroundFrame::toFront);
135         robot.waitForIdle();
136 
137         EventQueue.invokeAndWait(window::toFront);
138         robot.waitForIdle();
139 
140         robot.delay(500);
141 
142         try {
143             colorCheck(innerPoint.x, innerPoint.y, SHAPE_COLOR, true);
144 
145             for (Point point : pointsOutsideToCheck) {
146                 colorCheck(point.x, point.y, BACKGROUND_COLOR, true);
147             }
148 
149             for (Point point : shadedPointsToCheck) {
150                 colorCheck(point.x, point.y, SHAPE_COLOR, false);
151             }
152         } finally {
153             EventQueue.invokeAndWait(() -> {
154                 backgroundFrame.dispose();
155                 window.dispose();
156             });
157         }
158     }
159 
160     private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpectedColor) {
161 
162         int screenX = window.getX() + x;
163         int screenY = window.getY() + y;
164 
165         Color actualColor = robot.getPixelColor(screenX, screenY);
166 
167         System.out.printf(
168                 "Checking %3d, %3d, %35s should %sbe %35s\n",
169                 x, y,
170                 actualColor,
171                 (mustBeExpectedColor) ? "" : "not ",
172                 expectedColor
173         );
174 
175         if (mustBeExpectedColor != expectedColor.equals(actualColor)) {
176             System.out.printf("window.getX() = %3d, window.getY() = %3d\n", window.getX(), window.getY());
177 
178             System.err.printf(
179                     "Checking for transparency failed: point: %3d, %3d\n\tactual    %s\n\texpected %s%s\n",
180                     screenX,
181                     screenY,
182                     actualColor,
183                     mustBeExpectedColor ? "" : "not ",
184                     expectedColor);
185             throw new RuntimeException("Test failed. The shape has not been applied.");
186         }
187     }
188 }