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