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