1/*
2 * Copyright (c) 2010, 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
34import java.awt.*;
35import java.awt.event.InputEvent;
36import java.awt.geom.*;
37
38
39public class ShapeNotSetSometimes {
40
41    private Frame backgroundFrame;
42    private Frame window;
43    private static final Color BACKGROUND_COLOR = Color.BLUE;
44    private Shape shape;
45    private int[][] pointsToCheck;
46
47    private static Robot robot;
48
49    public ShapeNotSetSometimes() throws Exception {
50        EventQueue.invokeAndWait(this::initializeGUI);
51        robot.waitForIdle();
52    }
53
54    private void initializeGUI() {
55        backgroundFrame = new BackgroundFrame();
56        backgroundFrame.setUndecorated(true);
57        backgroundFrame.setSize(300, 300);
58        backgroundFrame.setLocation(20, 400);
59        backgroundFrame.setVisible(true);
60
61        shape = null;
62        String shape_name = null;
63        Area a;
64        GeneralPath gp;
65        shape_name = "Rounded-corners";
66        a = new Area();
67        a.add(new Area(new Rectangle2D.Float(50, 0, 100, 150)));
68        a.add(new Area(new Rectangle2D.Float(0, 50, 200, 50)));
69        a.add(new Area(new Ellipse2D.Float(0, 0, 100, 100)));
70        a.add(new Area(new Ellipse2D.Float(0, 50, 100, 100)));
71        a.add(new Area(new Ellipse2D.Float(100, 0, 100, 100)));
72        a.add(new Area(new Ellipse2D.Float(100, 50, 100, 100)));
73        shape = a;
74        pointsToCheck = new int[][] {
75            // inside shape
76            {106, 86}, {96, 38}, {76, 107}, {180, 25}, {24, 105},
77            {196, 77}, {165, 50}, {14, 113}, {89, 132}, {167, 117},
78            // outside shape
79            {165, 196}, {191, 163}, {146, 185}, {61, 170}, {148, 171},
80            {82, 172}, {186, 11}, {199, 141}, {13, 173}, {187, 3}
81        };
82
83        window = new TestFrame();
84        window.setUndecorated(true);
85        window.setSize(200, 200);
86        window.setLocation(70, 450);
87        window.setShape(shape);
88        window.setVisible(true);
89
90        System.out.println("Checking " + window.getClass().getSuperclass().getName() + " with " + shape_name + " shape (" + window.getShape() + ")...");
91    }
92
93    class BackgroundFrame extends Frame {
94
95        @Override
96        public void paint(Graphics g) {
97
98            g.setColor(BACKGROUND_COLOR);
99            g.fillRect(0, 0, 300, 300);
100
101            super.paint(g);
102        }
103    }
104
105    class TestFrame extends Frame {
106
107        @Override
108        public void paint(Graphics g) {
109
110            g.setColor(Color.WHITE);
111            g.fillRect(0, 0, 200, 200);
112
113            super.paint(g);
114        }
115    }
116
117    public static void main(String[] args) throws Exception {
118        robot = new Robot();
119
120        for(int i = 0; i < 50; i++) {
121            System.out.println("Attempt " + i);
122            new ShapeNotSetSometimes().doTest();
123        }
124    }
125
126    private void doTest() throws Exception {
127        Point wls = backgroundFrame.getLocationOnScreen();
128
129        robot.mouseMove(wls.x + 5, wls.y + 5);
130        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
131        robot.delay(10);
132        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
133        robot.delay(500);
134
135        EventQueue.invokeAndWait(window::requestFocus);
136
137        robot.waitForIdle();
138        try {
139            Thread.sleep(300);
140        } catch (InterruptedException e) {
141            // ignore this one
142        }
143
144        // check transparency
145        final int COUNT_TARGET = 10;
146
147        // checking outside points only
148        for(int i = COUNT_TARGET; i < COUNT_TARGET * 2; i++) {
149            int x = pointsToCheck[i][0];
150            int y = pointsToCheck[i][1];
151            boolean inside = i < COUNT_TARGET;
152            Color c = robot.getPixelColor(window.getX() + x, window.getY() + y);
153            System.out.println("checking " + x + ", " + y + ", color = " + c);
154            if (inside && BACKGROUND_COLOR.equals(c) || !inside && !BACKGROUND_COLOR.equals(c)) {
155                System.out.println("window.getX() = " + window.getX() + ", window.getY() = " + window.getY());
156                System.err.println("Checking for transparency failed: point: " +
157                        (window.getX() + x) + ", " + (window.getY() + y) +
158                        ", color = " + c + (inside ? " is of un" : " is not of ") +
159                        "expected background color " + BACKGROUND_COLOR);
160                throw new RuntimeException("Test failed. The shape has not been applied.");
161            }
162        }
163
164        EventQueue.invokeAndWait(new Runnable() {
165            public void run() {
166                backgroundFrame.dispose();
167                window.dispose();
168            }
169        });
170    }
171}
172