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