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