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     @Override
  64     public void initGUI() {
  65         if (windowClass.equals(Frame.class)) {
  66             window = new Frame();
  67             ((Frame) window).setUndecorated(true);
  68         } else  if (windowClass.equals(Dialog.class)) {
  69             window = new Dialog(background);
  70             ((Dialog) window).setUndecorated(true);
  71         } else {
  72             window = new Window(background);
  73         }
  74 
  75         window.setPreferredSize(new Dimension(200, 200));
  76         window.setLocation(2 * dl, 2 * dl);
  77         window.setLayout(new BorderLayout());
  78         window.setBackground(FG_COLOR);
  79 
  80         south = new Button("South");
  81         south.addMouseListener(new MouseAdapter() {
  82             @Override
  83             public void mouseClicked(MouseEvent e) { clicked |= 1 << 2; }
  84         });
  85         window.add(south, BorderLayout.SOUTH);
  86 
  87         center = new List(5);
  88         center.addMouseListener(new MouseAdapter() {
  89             @Override
  90             public void mouseClicked(MouseEvent e) { clicked |= 1 << 1; }
  91         });
  92         window.add(center, BorderLayout.CENTER);
  93 
  94         north = new TextField("North");
  95         north.addMouseListener(new MouseAdapter() {
  96             @Override
  97             public void mouseClicked(MouseEvent e) { clicked |= 1 << 0; }
  98         });
  99         window.add(north, BorderLayout.NORTH);
 100         window.setOpacity(0.2f);
 101         window.pack();
 102         window.setVisible(true);
 103 
 104         System.out.println("Checking " + window.getClass().getName() + "...");
 105     }
 106 
 107     @Override
 108     public void doTest() throws Exception {
 109         Point ls;
 110         robot.waitForIdle();
 111 
 112         ls = north.getLocationOnScreen();
 113         checkClick(ls.x + north.getWidth() / 3, ls.y + north.getHeight() / 2, 0);
 114 
 115         ls = center.getLocationOnScreen();
 116         checkClick(ls.x + center.getWidth() / 4, ls.y + center.getHeight() / 4, 1);
 117 
 118         ls = center.getLocationOnScreen();
 119         checkClick(ls.x + center.getWidth() * 3 / 4, ls.y + center.getHeight() * 3 / 4, 1);
 120 
 121         ls = south.getLocationOnScreen();
 122         checkClick(ls.x + south.getWidth() * 2 / 3, ls.y + south.getHeight() / 2, 2);
 123 
 124         EventQueue.invokeAndWait(() -> {
 125             background.dispose();
 126             window.dispose();
 127         });
 128 
 129         robot.waitForIdle();
 130     }
 131 
 132     @Override
 133     public void applyShape() { }
 134 
 135     void checkClick(int x, int y, int flag) throws Exception {
 136 
 137         System.out.println("Trying to click point " + x + ", " + y + ", looking for " + flag + " flag to trigger.");
 138 
 139         clicked = 0;
 140         robot.mouseMove(x, y);
 141         robot.click();
 142 
 143         for (int i = 0; i < 100; i++)
 144             if ((clicked & (1 << flag)) == 0)
 145                 robot.delay(50);
 146             else
 147                 break;
 148 
 149         if ((clicked & (1 << flag)) == 0)
 150             throw new RuntimeException("FAIL: Flag " + flag + " is not triggered for point " + x + ", " + y + "!");
 151     }
 152 }