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