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 javax.swing.*;
  25 import java.awt.*;
  26 import java.awt.event.MouseAdapter;
  27 import java.awt.event.MouseEvent;
  28 
  29 /*
  30  * @test
  31  * @bug 8164811
  32  * @key headful
  33  * @summary Check if swing components present in a window set with opacity less
  34  *          than 1.0 appears translucent
  35  * Test Description: Check if TRANSLUCENT Translucency type is supported for the
  36  *      current platform. Proceed if supported. Show a window containing some swing
  37  *      components and set it with opacity less than 1.0. Check if the swing components
  38  *      appear translucent and check if events trigger correctly for the components
  39  * Expected Result: If TRANSLUCENT Translucency type is supported, the components
  40  *      should appear translucent showing the background. They should trigger events
  41  *      correctly
  42  * @author mrkam
  43  * @library ../../../../lib/testlibrary
  44  * @build Common ExtendedRobot
  45  * @run main TranslucentWindowClickSwing
  46  */
  47 
  48 public class TranslucentWindowClickSwing extends Common {
  49 
  50     private Component south;
  51     private Component center;
  52     private Component north;
  53 
  54     public static void main(String[] args) throws Exception{
  55         if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.TRANSLUCENT))
  56             new TranslucentWindowClickSwing(JWindow.class).doTest();
  57     }
  58 
  59     public TranslucentWindowClickSwing(Class windowClass) throws Exception {
  60         super(windowClass, 0.2f, 1.0f, false);
  61     }
  62 
  63     @Override
  64     public void createSwingComponents() {
  65         south = new JButton("South");
  66         south.addMouseListener(new MouseAdapter() {
  67             @Override
  68             public void mouseClicked(MouseEvent e) { clicked |= 1 << 2; }
  69         });
  70         window.add(south, BorderLayout.SOUTH);
  71 
  72         center = new JList();
  73         center.addMouseListener(new MouseAdapter() {
  74             @Override
  75             public void mouseClicked(MouseEvent e) { clicked |= 1 << 1; }
  76         });
  77         window.add(center, BorderLayout.CENTER);
  78 
  79         north = new JTextField("North");
  80         north.addMouseListener(new MouseAdapter() {
  81             @Override
  82             public void mouseClicked(MouseEvent e) { clicked |= 1 << 0; }
  83         });
  84         window.add(north, BorderLayout.NORTH);
  85     }
  86 
  87     @Override
  88     public void doTest() throws Exception {
  89         Point ls;
  90         robot.waitForIdle();
  91 
  92         ls = north.getLocationOnScreen();
  93         checkClick(ls.x + north.getWidth() / 3, ls.y + north.getHeight() / 2, 0);
  94 
  95         ls = center.getLocationOnScreen();
  96         checkClick(ls.x + center.getWidth() / 4, ls.y + center.getHeight() / 4, 1);
  97 
  98         ls = center.getLocationOnScreen();
  99         checkClick(ls.x + center.getWidth() * 3 / 4, ls.y + center.getHeight() * 3 / 4, 1);
 100 
 101         ls = south.getLocationOnScreen();
 102         checkClick(ls.x + south.getWidth() * 2 / 3, ls.y + south.getHeight() / 2, 2);
 103 
 104         EventQueue.invokeAndWait(this::dispose);
 105         robot.waitForIdle();
 106     }
 107 }