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