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  * @bug 8024627 8164811
  33  * @summary Check if a JComboBox present in a window set with opacity less than
  34  *          1.0 shows a translucent drop down
  35  * Test Description: Check if TRANSLUCENT translucency type is supported on the
  36  *      current platform. Proceed if supported. Show a window which contains an
  37  *      JComboBox and set with opacity less than 1.0. Another Window having a canvas
  38  *      component drawn with an image can be used as the background for the test
  39  *      window. Click on the ComboBox to show the drop down. Check if the drop down
  40  *      appears translucent. Repeat this for JWindow, JDialog and JFrame
  41  * Expected Result: If TRANSLUCENT Translucency type is supported, the drop down
  42  *      should appear translucent.
  43  * @author mrkam
  44  * @library ../../../../lib/testlibrary
  45  * @build Common ExtendedRobot
  46  * @run main TranslucentJComboBox
  47  */
  48 
  49 public class TranslucentJComboBox extends Common {
  50 
  51     JComponent south;
  52     JComponent center;
  53     JPanel north;
  54     volatile boolean southClicked = false;
  55 
  56     public static void main(String[] args) throws Exception {
  57         if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.TRANSLUCENT))
  58             for (Class<Window> windowClass: WINDOWS_TO_TEST)
  59                 new TranslucentJComboBox(windowClass).doTest();
  60     }
  61 
  62     public TranslucentJComboBox(Class windowClass) throws Exception {
  63         super(windowClass, 0.3f, 1.0f, false);
  64     }
  65 
  66     @Override
  67     public void initBackgroundFrame() {
  68         super.initBackgroundFrame();
  69     }
  70 
  71     @Override
  72     public void createSwingComponents() {
  73         Container contentPane = RootPaneContainer.class.cast(window).getContentPane();
  74         window.setLayout(new BorderLayout());
  75 
  76         north = new JPanel();
  77         contentPane.add(north, BorderLayout.NORTH);
  78 
  79         center = new JList(new String [] { "Center" });
  80         contentPane.add(center, BorderLayout.CENTER);
  81 
  82         JComboBox jComboBox = new JComboBox();
  83         for(int i = 0; i < 20; i++) {
  84             jComboBox.addItem("item " + i);
  85         }
  86         south = jComboBox;
  87 
  88         south.addMouseListener(new MouseAdapter() {
  89             @Override
  90             public void mouseClicked(MouseEvent e) {
  91                 southClicked = true;
  92             }
  93         });
  94         contentPane.add(south, BorderLayout.SOUTH);
  95     }
  96 
  97 
  98     @Override
  99     public void doTest() throws Exception {
 100         robot.waitForIdle(delay);
 101         // Make window an active
 102         Point ls = north.getLocationOnScreen();
 103         robot.mouseMove(ls.x + north.getWidth()/2, ls.y + north.getHeight()/2);
 104         robot.click();
 105 
 106         // Invoke list
 107         ls = south.getLocationOnScreen();
 108 
 109         Point p1 = new Point(
 110                 (int) (ls.x + south.getWidth() * 0.75),
 111                 ls.y + south.getHeight() * 3);
 112 
 113         Point p2 = new Point(
 114                 (int) (ls.x + south.getWidth() * 0.75),
 115                 ls.y - south.getHeight() * 2);
 116 
 117         Color c1 = robot.getPixelColor(p1.x, p1.y);
 118         Color c2 = robot.getPixelColor(p2.x, p2.y);
 119 
 120         int x = ls.x + south.getWidth()/2;
 121         int y = ls.y + south.getHeight()/2;
 122 
 123         System.out.println("Trying to click point "+x+", "+y+
 124                 ", looking for flag to trigger.");
 125 
 126         robot.mouseMove(x, y);
 127         robot.waitForIdle(delay);
 128         robot.click();
 129         robot.waitForIdle(delay);
 130 
 131         if (!southClicked)
 132             throw new RuntimeException("Flag is not triggered for point "+x+", "+y+"!");
 133 
 134         robot.waitForIdle();
 135 
 136         Color c1b = robot.getPixelColor(p1.x, p1.y);
 137         Color c2b = robot.getPixelColor(p2.x, p2.y);
 138 
 139         if (!c1.equals(c1b) && !south.getBackground().equals(c1b))
 140             throw new RuntimeException(
 141                     "Check for opaque drop down failed at point " + p1 +
 142                             ". Before click: " + c1 + ", after click: " + c1b +
 143                             ", expected is " + south.getBackground());
 144 
 145         if (!c2.equals(c2b) && !south.getBackground().equals(c2b))
 146             throw new RuntimeException(
 147                     "Check for opaque drop down failed at point " + p2 +
 148                             ". Before click: " + c2 + ", after click: " + c2b +
 149                             ", expected is " + south.getBackground());
 150 
 151         EventQueue.invokeAndWait(this::dispose);
 152     }
 153 }