< prev index next >

test/jdk/java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java

Print this page

  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 }

  1 /*
  2  * Copyright (c) 2010, 2024, 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 import java.awt.Color;
 25 import java.awt.Dimension;
 26 import java.awt.EventQueue;
 27 import java.awt.Frame;
 28 import java.awt.Graphics;
 29 import java.awt.Point;
 30 import java.awt.Rectangle;
 31 import java.awt.Robot;
 32 import java.awt.Toolkit;
 33 import java.awt.geom.Area;
 34 import java.awt.geom.Ellipse2D;
 35 import java.awt.geom.Rectangle2D;
 36 import java.io.File;
 37 import java.io.IOException;
 38 import javax.imageio.ImageIO;
 39 
 40 /*
 41  * @test
 42  * @key headful
 43  * @bug 6988428
 44  * @summary Tests whether shape is always set
 45  * @run main/othervm -Dsun.java2d.uiScale=1 ShapeNotSetSometimes
 46  */
 47 
 48 public class ShapeNotSetSometimes {
 49 
 50     private Frame backgroundFrame;
 51     private Frame window;
 52 

 53     private Point[] pointsOutsideToCheck;
 54     private Point[] shadedPointsToCheck;
 55     private Point innerPoint;
 56 


 57     private static Robot robot;
 58     private static final Color BACKGROUND_COLOR = Color.GREEN;
 59     private static final Color SHAPE_COLOR = Color.WHITE;
 60     private static final int DIM = 300;
 61     private static final int DELTA = 2;
 62 
 63     public ShapeNotSetSometimes() throws Exception {
 64         EventQueue.invokeAndWait(this::initializeGUI);
 65         robot.waitForIdle();
 66         robot.delay(500);
 67     }
 68 
 69     private void initializeGUI() {
 70         backgroundFrame = new BackgroundFrame();
 71         backgroundFrame.setUndecorated(true);
 72         backgroundFrame.setSize(DIM, DIM);
 73         backgroundFrame.setLocationRelativeTo(null);
 74         backgroundFrame.setVisible(true);
 75 
 76         Area area = new Area();
 77         area.add(new Area(new Rectangle2D.Float(100, 50, 100, 150)));
 78         area.add(new Area(new Rectangle2D.Float(50, 100, 200, 50)));
 79         area.add(new Area(new Ellipse2D.Float(50, 50, 100, 100)));
 80         area.add(new Area(new Ellipse2D.Float(50, 100, 100, 100)));
 81         area.add(new Area(new Ellipse2D.Float(150, 50, 100, 100)));
 82         area.add(new Area(new Ellipse2D.Float(150, 100, 100, 100)));
 83 
 84         // point at the center of white ellipse
 85         innerPoint = new Point(150, 130);
 86 
 87         // mid points on the 4 sides - on the green background frame
 88         pointsOutsideToCheck = new Point[] {
 89                 new Point(150, 20),
 90                 new Point(280, 120),
 91                 new Point(150, 250),
 92                 new Point(20, 120)
 93         };
 94 
 95         // points just outside the ellipse (opposite side of diagonal)
 96         shadedPointsToCheck = new Point[] {
 97                 new Point(62, 62),
 98                 new Point(240, 185)
 99         };
100 
101         window = new TestFrame();
102         window.setUndecorated(true);
103         window.setSize(DIM, DIM);
104         window.setLocationRelativeTo(null);
105         window.setShape(area);
106         window.setVisible(true);
107     }
108 
109     static class BackgroundFrame extends Frame {
110 
111         @Override
112         public void paint(Graphics g) {
113 
114             g.setColor(BACKGROUND_COLOR);
115             g.fillRect(0, 0, DIM, DIM);
116 
117             super.paint(g);
118         }
119     }
120 
121     class TestFrame extends Frame {
122 
123         @Override
124         public void paint(Graphics g) {
125 
126             g.setColor(SHAPE_COLOR);
127             g.fillRect(0, 0, DIM, DIM);
128 
129             super.paint(g);
130         }
131     }
132 
133     public static void main(String[] args) throws Exception {
134         robot = new Robot();
135 
136         for (int i = 1; i <= 50; i++) {
137             System.out.println("Attempt " + i);
138             new ShapeNotSetSometimes().doTest();
139         }
140     }
141 
142     private void doTest() throws Exception {
143         EventQueue.invokeAndWait(backgroundFrame::toFront);
144         robot.waitForIdle();
145 
146         EventQueue.invokeAndWait(window::toFront);
147         robot.waitForIdle();

148         robot.delay(500);
149 
150         try {
151             colorCheck(innerPoint.x, innerPoint.y, SHAPE_COLOR, true);
152 
153             for (Point point : pointsOutsideToCheck) {
154                 colorCheck(point.x, point.y, BACKGROUND_COLOR, true);
155             }
156 
157             for (Point point : shadedPointsToCheck) {
158                 colorCheck(point.x, point.y, SHAPE_COLOR, false);
159             }
160         } finally {
161             EventQueue.invokeAndWait(() -> {
162                 if (backgroundFrame != null) {
163                     backgroundFrame.dispose();
164                 }
165                 if (window != null) {
166                     window.dispose();
167                 }
168             });
169         }
170     }
171 
172     private void colorCheck(int x, int y, Color expectedColor, boolean mustBeExpectedColor) {

173         int screenX = window.getX() + x;
174         int screenY = window.getY() + y;
175 
176         robot.mouseMove(screenX, screenY);
177         robot.waitForIdle();
178         robot.delay(50);
179 
180         Color actualColor = robot.getPixelColor(screenX, screenY);
181 
182         System.out.printf(
183                 "Checking %3d, %3d, %35s should %sbe %35s\n",
184                 x, y,
185                 actualColor,
186                 (mustBeExpectedColor) ? "" : "not ",
187                 expectedColor
188         );
189 
190         if (mustBeExpectedColor != colorCompare(expectedColor, actualColor)) {
191             captureScreen();
192             System.out.printf("window.getX() = %3d, window.getY() = %3d\n", window.getX(), window.getY());

193             System.err.printf(
194                     "Checking for transparency failed: point: %3d, %3d\n\tactual    %s\n\texpected %s%s\n",
195                     screenX,
196                     screenY,
197                     actualColor,
198                     mustBeExpectedColor ? "" : "not ",
199                     expectedColor);
200             throw new RuntimeException("Test failed. The shape has not been applied.");
201         }
202     }
203 
204     private static boolean colorCompare(Color expected, Color actual) {
205         if (Math.abs(expected.getRed() - actual.getRed()) <= DELTA
206                 && Math.abs(expected.getGreen() - actual.getGreen()) <= DELTA
207                 && Math.abs(expected.getBlue() - actual.getBlue()) <= DELTA) {
208             return true;
209         }
210         return false;
211     }
212 
213     private static void captureScreen() {
214         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
215         Rectangle screenBounds = new Rectangle(0, 0, screenSize.width, screenSize.height);
216         try {
217             ImageIO.write(
218                     robot.createScreenCapture(screenBounds),
219                     "png",
220                     new File("Screenshot.png")
221             );
222         } catch (IOException e) {
223             e.printStackTrace();
224         }
225     }
226 }
< prev index next >