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 import java.awt.*;
  24 import java.awt.event.MouseAdapter;
  25 import java.awt.event.MouseEvent;
  26 
  27 /*
  28  * @test
  29  * @summary Check if components present in a window set with opacity less than 1.0
  30  *          triggers events correctly
  31  *
  32  * Test Description: Check if TRANSLUCENT Translucency type is supported on the
  33  *      current platform. Proceed if supported. Show a window which contains some
  34  *      components and set with opacity less than 1.0. Another Window having a
  35  *      canvas component drawn with an image can be used as the background for the
  36  *      test window. Click on the components present in the window and check if
  37  *      events trigger correctly.
  38  * Expected Result: The components should trigger events correctly
  39  *
  40  * @author mrkam
  41  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  42  * @library ../../../../lib/testlibrary
  43  * @build Common ExtendedRobot
  44  * @run main TranslucentWindowClick
  45  */
  46 public class TranslucentWindowClick extends Common {
  47 
  48     private Component south;
  49     private Component center;
  50     private Component north;
  51 
  52     volatile int clicked;
  53 
  54     public static void main(String[] args) throws Exception{
  55         if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.TRANSLUCENT))
  56             new TranslucentWindowClick(Window.class).doTest();
  57     }
  58 
  59     public TranslucentWindowClick(Class windowClass) throws Exception {
  60         super(windowClass);
  61     }
  62 
  63     public void initGUI() {
  64         if (windowClass.equals(Frame.class)) {
  65             window = new Frame();
  66             ((Frame) window).setUndecorated(true);
  67         } else  if (windowClass.equals(Dialog.class)) {
  68             window = new Dialog(background);
  69             ((Dialog) window).setUndecorated(true);
  70         } else {
  71             window = new Window(background);
  72         }
  73 
  74         window.setPreferredSize(new Dimension(200, 200));
  75         window.setLocation(2 * dl, 2 * dl);
  76         window.setLayout(new BorderLayout());
  77         window.setBackground(FG_COLOR);
  78 
  79         south = new Button("South");
  80         south.addMouseListener(new MouseAdapter() {
  81             @Override
  82             public void mouseClicked(MouseEvent e) { clicked |= 1 << 2; }
  83         });
  84         window.add(south, BorderLayout.SOUTH);
  85 
  86         center = new List(5);
  87         center.addMouseListener(new MouseAdapter() {
  88             @Override
  89             public void mouseClicked(MouseEvent e) { clicked |= 1 << 1; }
  90         });
  91         window.add(center, BorderLayout.CENTER);
  92 
  93         north = new TextField("North");
  94         north.addMouseListener(new MouseAdapter() {
  95             @Override
  96             public void mouseClicked(MouseEvent e) { clicked |= 1 << 0; }
  97         });
  98         window.add(north, BorderLayout.NORTH);
  99         window.setOpacity(0.2f);
 100         window.pack();
 101         window.setVisible(true);
 102 
 103         System.out.println("Checking " + window.getClass().getName() + "...");
 104     }
 105 
 106     public void doTest() throws Exception {
 107         Point ls;
 108         robot.waitForIdle();
 109 
 110         ls = north.getLocationOnScreen();
 111         checkClick(ls.x + north.getWidth() / 3, ls.y + north.getHeight() / 2, 0);
 112 
 113         ls = center.getLocationOnScreen();
 114         checkClick(ls.x + center.getWidth() / 4, ls.y + center.getHeight() / 4, 1);
 115 
 116         ls = center.getLocationOnScreen();
 117         checkClick(ls.x + center.getWidth() * 3 / 4, ls.y + center.getHeight() * 3 / 4, 1);
 118 
 119         ls = south.getLocationOnScreen();
 120         checkClick(ls.x + south.getWidth() * 2 / 3, ls.y + south.getHeight() / 2, 2);
 121 
 122         EventQueue.invokeAndWait(() -> {
 123             background.dispose();
 124             window.dispose();
 125         });
 126 
 127         robot.waitForIdle();
 128     }
 129 
 130     @Override
 131     public void applyShape() { }
 132 
 133     void checkClick(int x, int y, int flag) throws Exception {
 134 
 135         System.out.println("Trying to click point " + x + ", " + y + ", looking for " + flag + " flag to trigger.");
 136 
 137         clicked = 0;
 138         robot.mouseMove(x, y);
 139         robot.click();
 140 
 141         for (int i = 0; i < 100; i++)
 142             if ((clicked & (1 << flag)) == 0)
 143                 robot.delay(50);
 144             else
 145                 break;
 146 
 147         if ((clicked & (1 << flag)) == 0)
 148             throw new RuntimeException("FAIL: Flag " + flag + " is not triggered for point " + x + ", " + y + "!");
 149     }
 150 }