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