1 /*
   2  * Copyright (c) 2010, 2014, 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 import java.awt.*;
  25 import java.awt.event.ComponentAdapter;
  26 import java.awt.event.ComponentEvent;
  27 import java.awt.geom.*;
  28 
  29 /*
  30  * @test
  31  * @summary Check whether a window set with shape appears in the expected shape
  32  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  33  * @library ../../../../lib/testlibrary
  34  * @build Common ExtendedRobot
  35  * @run main SetShape
  36  */
  37 
  38 public class SetShape extends Common {
  39 
  40     static int[][] pointsToCheck;
  41     static Shape shape;
  42 
  43     public static void main(String[] args) throws Exception {
  44         if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) {
  45             for (int i = 0; i < 6; i++) {
  46                 System.out.println("Checking shape "+i);
  47                 Area area;
  48                 GeneralPath path;
  49                 switch (i) {
  50                     case 0:
  51                         path = new GeneralPath();
  52                         path.moveTo(0, 40);
  53                         path.lineTo(40, 0);
  54                         path.lineTo(110, 0);
  55                         path.lineTo(150, 40);
  56                         path.lineTo(150, 110);
  57                         path.lineTo(110, 150);
  58                         path.lineTo(40, 150);
  59                         path.lineTo(0, 110);
  60                         path.closePath();
  61                         shape = path;
  62                         pointsToCheck = new int[][]{
  63                                 // inside shape
  64                                 {230, 240}, {286, 332}, {314, 267}, {220, 327}, {223, 246},
  65                                 {229, 274}, {335, 257}, {231, 278}, {317, 299}, {266, 236},
  66                                 // outside shape
  67                                 {340, 353}, {373, 320}, {330, 220}, {384, 300}, {349, 406},
  68                                 {213, 355}, {361, 260}, {399, 251}, {201, 374}, {199, 257}
  69                         };
  70                         break;
  71                     case 1:
  72                         area = new Area();
  73                         area.add(new Area(new Rectangle2D.Float(50, 0, 100, 150)));
  74                         area.add(new Area(new Rectangle2D.Float(0, 50, 200, 50)));
  75                         area.add(new Area(new Ellipse2D.Float(0, 0, 100, 100)));
  76                         area.add(new Area(new Ellipse2D.Float(0, 50, 100, 100)));
  77                         area.add(new Area(new Ellipse2D.Float(100, 0, 100, 100)));
  78                         area.add(new Area(new Ellipse2D.Float(100, 50, 100, 100)));
  79                         shape = area;
  80                         pointsToCheck = new int[][]{
  81                                 // inside shape
  82                                 {306, 314}, {296, 266}, {276, 335}, {380, 253}, {224, 333},
  83                                 {396, 305}, {365, 278}, {214, 331}, {289, 349}, {367, 345},
  84                                 // outside shape
  85                                 {365, 424}, {391, 391}, {346, 413}, {261, 398}, {348, 399},
  86                                 {282, 400}, {386, 215}, {399, 369}, {213, 401}, {387, 215}
  87                         };
  88                         break;
  89                     case 2:
  90                         path = new GeneralPath();
  91                         path.moveTo(100, 0);
  92                         double angle = -Math.PI / 2;
  93                         double angle_step = Math.PI * 2 / 8;
  94                         for (int c = 0; c < 8; c++, angle += angle_step) {
  95                             path.lineTo(100 + 100 * Math.cos(angle), 100 + 100 * Math.sin(angle));
  96                             path.lineTo(100 + 40 * Math.cos(angle + angle_step / 2), 100 + 40 * Math.sin(angle + angle_step / 2));
  97                         }
  98                         path.closePath();
  99                         shape = path;
 100                         pointsToCheck = new int[][]{
 101                                 // inside shape
 102                                 {246, 257}, {300, 314}, {255, 347}, {291, 364}, {287, 320},
 103                                 {319, 276}, {269, 345}, {325, 291}, {289, 271}, {273, 339},
 104                                 // outside shape
 105                                 {373, 267}, {269, 229}, {390, 326}, {204, 216}, {379, 408},
 106                                 {375, 330}, {296, 213}, {367, 340}, {376, 409}, {378, 308}
 107                         };
 108                         break;
 109                     case 3:
 110                         area = new Area();
 111                         area.add(new Area(new Rectangle2D.Float(-15, 85, 230, 30)));
 112                         area.transform(AffineTransform.getRotateInstance(-Math.PI / 4, 100, 100));
 113                         shape = area;
 114                         pointsToCheck = new int[][]{
 115                                 // inside shape
 116                                 {240, 366}, {254, 338}, {342, 244}, {264, 344}, {343, 240},
 117                                 {292, 303}, {225, 374}, {263, 348}, {329, 290}, {278, 327},
 118                                 // outside shape
 119                                 {353, 289}, {334, 377}, {391, 354}, {266, 358}, {280, 364},
 120                                 {232, 225}, {327, 309}, {375, 208}, {397, 292}, {204, 335}
 121                         };
 122                         break;
 123                     case 4:
 124                         area = new Area();
 125                         area.add(new Area(new Arc2D.Float(0, -100, 400, 400, 155, 50, Arc2D.PIE)));
 126                         area.subtract(new Area(new Ellipse2D.Float(70, 115, 20, 20)));
 127                         area.subtract(new Area(new Ellipse2D.Float(30, 90, 18, 18)));
 128                         area.subtract(new Area(new Ellipse2D.Float(17, 50, 30, 30)));
 129                         area.subtract(new Area(new Ellipse2D.Float(26, 124, 26, 26)));
 130                         area.subtract(new Area(new Ellipse2D.Float(100, 85, 25, 25)));
 131                         area.subtract(new Area(new Ellipse2D.Float(135, 100, 14, 14)));
 132                         shape = area;
 133                         pointsToCheck = new int[][]{
 134                                 // inside shape
 135                                 {225, 286}, {296, 276}, {318, 269}, {211, 357}, {295, 327},
 136                                 {207, 300}, {322, 265}, {319, 262}, {259, 294}, {261, 250},
 137                                 // outside shape
 138                                 {322, 303}, {330, 367}, {302, 395}, {227, 251}, {263, 382},
 139                                 {228, 383}, {280, 366}, {294, 248}, {316, 349}, {313, 294}
 140                         };
 141                         break;
 142                     case 5:
 143                         area = new Area();
 144                         area.add(new Area(new Rectangle2D.Float(0, 0, 90, 90)));
 145                         area.add(new Area(new Rectangle2D.Float(100, 0, 100, 200)));
 146                         area.add(new Area(new Ellipse2D.Float(0, 100, 100, 100)));
 147                         shape = area;
 148                         pointsToCheck = new int[][]{
 149                                 // inside shape
 150                                 {275, 345}, {358, 327}, {373, 374}, {273, 331}, {251, 234},
 151                                 {285, 356}, {360, 287}, {319, 343}, {232, 210}, {323, 323},
 152                                 // outside shape
 153                                 {219, 291}, {270, 302}, {296, 383}, {298, 203}, {228, 293},
 154                                 {276, 300}, {292, 294}, {293, 216}, {298, 331}, {228, 295}
 155                         };
 156                         break;
 157                     default:
 158                         break;
 159                 }
 160 
 161                 for (Class<Window> windowClass : WINDOWS_TO_TEST) {
 162                     new SetShape(windowClass).doTest();
 163                 }
 164             }
 165         }
 166     }
 167 
 168     public SetShape(Class windowClass) throws Exception {
 169         super(windowClass);
 170     }
 171 
 172     public void initGUI() {
 173         if (windowClass.equals(Frame.class)) {
 174             window = new Frame();
 175             ((Frame) window).setUndecorated(true);
 176         } else  if (windowClass.equals(Dialog.class)) {
 177             window = new Dialog(background);
 178             ((Dialog) window).setUndecorated(true);
 179         } else {
 180             window = new Window(background);
 181         }
 182         window.setBackground(FG_COLOR);
 183         window.addComponentListener(new ComponentAdapter() {
 184             @Override
 185             public void componentResized(ComponentEvent e) {
 186                 window.setShape(shape);
 187             }
 188         });
 189         window.setSize(200, 200);
 190         window.setLocation(2*dl, 2*dl);
 191         window.setVisible(true);
 192 
 193         System.out.println("Checking " + window.getClass().getName() + "...");
 194     }
 195 
 196     public void doTest() throws Exception {
 197         robot.waitForIdle();
 198 
 199         final int COUNT_TARGET = 10;
 200         for(int i = 0; i < COUNT_TARGET * 2; i++) {
 201             int x = pointsToCheck[i][0];
 202             int y = pointsToCheck[i][1];
 203             boolean inside = i < COUNT_TARGET;
 204             Color c = robot.getPixelColor(x, y);
 205             System.out.println("checking " + x + ", " + y + ", color = " + c);
 206             boolean matchToForeground = FG_COLOR.equals(c);
 207 
 208             if (!inside && matchToForeground || inside && !matchToForeground) {
 209                 System.err.println("Checking for transparency failed: point: " +
 210                         x + ", " + y +
 211                         ", color = " + c + (inside ? " is not of " : " is of un") +
 212                         "expected foreground color " + FG_COLOR);
 213                 final Frame[] f = new Frame[1];
 214                 EventQueue.invokeAndWait(() -> {
 215                     f[0] = new Frame();
 216                     f[0].setUndecorated(true);
 217                     f[0].setBackground(Color.YELLOW);
 218                     f[0].setPreferredSize(new Dimension(2, 2));
 219                     f[0].pack();
 220                     f[0].setVisible(true);
 221                 });
 222                 robot.delay(1000);
 223                 EventQueue.invokeAndWait(() -> {
 224                     f[0].setLocation(x, y);
 225                 });
 226                 robot.delay(1000);
 227             }
 228         }
 229         EventQueue.invokeAndWait(window::dispose);
 230         EventQueue.invokeAndWait(background::dispose);
 231 
 232         robot.waitForIdle();
 233     }
 234 
 235     @Override
 236     public void applyShape() { window.setShape(shape); }
 237 
 238 }