jdk/test/java/awt/Frame/ShapeNotSetSometimes/ShapeNotSetSometimes.java
author anthony
Mon, 08 Nov 2010 17:51:35 +0300
changeset 7149 2fe42889b598
child 32282 9c9ff2c42e0d
permissions -rw-r--r--
6988428: Shape is not applied sometimes Summary: Always call ::SetWindowRgn() on the toolkit thread Reviewed-by: art, dcherepanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7149
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     1
/*
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     4
 *
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     8
 *
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    13
 * accompanied this code).
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    14
 *
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    18
 *
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    21
 * questions.
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    22
 */
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    23
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    24
/*
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    25
  @test
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    26
  @bug 6988428
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    27
  @summary Tests whether shape is always set
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    28
  @author anthony.petrov@oracle.com: area=awt.toplevel
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    29
  @run main ShapeNotSetSometimes
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    30
*/
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    31
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    32
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    33
import java.awt.*;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    34
import java.awt.event.InputEvent;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    35
import java.awt.geom.*;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    36
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    37
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    38
public class ShapeNotSetSometimes {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    39
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    40
    private Frame backgroundFrame;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    41
    private Frame window;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    42
    private static final Color BACKGROUND_COLOR = Color.BLUE;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    43
    private Shape shape;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    44
    private int[][] pointsToCheck;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    45
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    46
    private static Robot robot;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    47
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    48
    public ShapeNotSetSometimes() throws Exception {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    49
        EventQueue.invokeAndWait(new Runnable() {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    50
            public void run() {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    51
                initializeGUI();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    52
            }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    53
        });
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    54
    }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    55
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    56
    private void initializeGUI() {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    57
        backgroundFrame = new BackgroundFrame();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    58
        backgroundFrame.setUndecorated(true);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    59
        backgroundFrame.setSize(300, 300);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    60
        backgroundFrame.setLocation(20, 400);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    61
        backgroundFrame.setVisible(true);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    62
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    63
        shape = null;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    64
        String shape_name = null;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    65
        Area a;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    66
        GeneralPath gp;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    67
        shape_name = "Rounded-corners";
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    68
        a = new Area();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    69
        a.add(new Area(new Rectangle2D.Float(50, 0, 100, 150)));
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    70
        a.add(new Area(new Rectangle2D.Float(0, 50, 200, 50)));
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    71
        a.add(new Area(new Ellipse2D.Float(0, 0, 100, 100)));
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    72
        a.add(new Area(new Ellipse2D.Float(0, 50, 100, 100)));
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    73
        a.add(new Area(new Ellipse2D.Float(100, 0, 100, 100)));
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    74
        a.add(new Area(new Ellipse2D.Float(100, 50, 100, 100)));
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    75
        shape = a;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    76
        pointsToCheck = new int[][] {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    77
            // inside shape
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    78
            {106, 86}, {96, 38}, {76, 107}, {180, 25}, {24, 105},
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    79
            {196, 77}, {165, 50}, {14, 113}, {89, 132}, {167, 117},
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    80
            // outside shape
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    81
            {165, 196}, {191, 163}, {146, 185}, {61, 170}, {148, 171},
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    82
            {82, 172}, {186, 11}, {199, 141}, {13, 173}, {187, 3}
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    83
        };
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    84
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    85
        window = new TestFrame();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    86
        window.setUndecorated(true);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    87
        window.setSize(200, 200);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    88
        window.setLocation(70, 450);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    89
        window.setShape(shape);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    90
        window.setVisible(true);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    91
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    92
        System.out.println("Checking " + window.getClass().getSuperclass().getName() + " with " + shape_name + " shape (" + window.getShape() + ")...");
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    93
    }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    94
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    95
    class BackgroundFrame extends Frame {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    96
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    97
        @Override
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    98
        public void paint(Graphics g) {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
    99
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   100
            g.setColor(BACKGROUND_COLOR);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   101
            g.fillRect(0, 0, 300, 300);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   102
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   103
            super.paint(g);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   104
        }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   105
    }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   106
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   107
    class TestFrame extends Frame {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   108
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   109
        @Override
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   110
        public void paint(Graphics g) {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   111
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   112
            g.setColor(Color.WHITE);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   113
            g.fillRect(0, 0, 200, 200);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   114
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   115
            super.paint(g);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   116
        }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   117
    }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   118
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   119
    public static void main(String[] args) throws Exception {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   120
        robot = new Robot();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   121
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   122
        for(int i = 0; i < 100; i++) {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   123
            System.out.println("Attempt " + i);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   124
            new ShapeNotSetSometimes().doTest();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   125
        }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   126
    }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   127
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   128
    private void doTest() throws Exception {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   129
        Point wls = backgroundFrame.getLocationOnScreen();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   130
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   131
        robot.mouseMove(wls.x + 5, wls.y + 5);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   132
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   133
        robot.delay(10);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   134
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   135
        robot.delay(500);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   136
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   137
        EventQueue.invokeAndWait(new Runnable() {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   138
            public void run() {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   139
                window.requestFocus();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   140
            }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   141
        });
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   142
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   143
        robot.waitForIdle();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   144
        try {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   145
            Thread.sleep(300);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   146
        } catch (InterruptedException e) {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   147
            // ignore this one
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   148
        }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   149
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   150
        // check transparency
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   151
        final int COUNT_TARGET = 10;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   152
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   153
        // checking outside points only
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   154
        for(int i = COUNT_TARGET; i < COUNT_TARGET * 2; i++) {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   155
            int x = pointsToCheck[i][0];
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   156
            int y = pointsToCheck[i][1];
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   157
            boolean inside = i < COUNT_TARGET;
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   158
            Color c = robot.getPixelColor(window.getX() + x, window.getY() + y);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   159
            System.out.println("checking " + x + ", " + y + ", color = " + c);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   160
            if (inside && BACKGROUND_COLOR.equals(c) || !inside && !BACKGROUND_COLOR.equals(c)) {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   161
                System.out.println("window.getX() = " + window.getX() + ", window.getY() = " + window.getY());
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   162
                System.err.println("Checking for transparency failed: point: " +
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   163
                        (window.getX() + x) + ", " + (window.getY() + y) +
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   164
                        ", color = " + c + (inside ? " is of un" : " is not of ") +
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   165
                        "expected background color " + BACKGROUND_COLOR);
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   166
                throw new RuntimeException("Test failed. The shape has not been applied.");
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   167
            }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   168
        }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   169
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   170
        EventQueue.invokeAndWait(new Runnable() {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   171
            public void run() {
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   172
                backgroundFrame.dispose();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   173
                window.dispose();
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   174
            }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   175
        });
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   176
    }
2fe42889b598 6988428: Shape is not applied sometimes
anthony
parents:
diff changeset
   177
}