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