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