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