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 java.awt.*;
  25 import java.awt.event.MouseAdapter;
  26 import java.awt.event.MouseEvent;
  27 
  28 /*
  29  * @test
  30  * @summary Check if a Choice present in a window set with opacity less than 1.0
  31  *          shows a translucent drop down
  32  *
  33  * Test Description: Check if TRANSLUCENT Translucency type is supported on the
  34  *      current platform. Proceed if supported. Show a window which contains an
  35  *      awt Choice and set with opacity less than 1.0. Another Window having a
  36  *      canvas component drawn with an image can be used as the background for
  37  *      the test window. Click on the ComboBox to show the drop down. Check if
  38  *      the drop down appears translucent. Repeat this for Window, Dialog and
  39  *      Frame.
  40  * Expected Result: If TRANSLUCENT Translucency type is supported, the drop down
  41  *      should appear translucent.
  42  *
  43  * @author mrkam
  44  * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
  45  * @library ../../../../lib/testlibrary
  46  * @build Common ExtendedRobot
  47  * @run main TranslucentChoice
  48  */
  49 
  50 public class TranslucentChoice extends Common {
  51 
  52     Choice south;
  53     Component center;
  54     Component north;
  55     volatile int clicked;
  56 
  57     public static void main(String[] ignored) throws Exception {
  58         if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.TRANSLUCENT))
  59             for (Class<Window> windowClass: WINDOWS_TO_TEST){
  60                 new TranslucentChoice(windowClass).doTest();
  61             }
  62     }
  63 
  64     public TranslucentChoice(Class windowClass) throws Exception {
  65         super(windowClass);
  66     }
  67 
  68     public void initBackgroundFrame() {
  69         super.initBackgroundFrame();
  70         background.addMouseListener(new MouseAdapter() {
  71             @Override
  72             public void mouseClicked(MouseEvent e) {
  73                 clicked |= 1 << 0;
  74             }
  75         });
  76     }
  77     
  78     public void initGUI() {
  79         if (windowClass.equals(Frame.class)) {
  80             window = new Frame();
  81             ((Frame) window).setUndecorated(true);
  82         } else  if (windowClass.equals(Dialog.class)) {
  83             window = new Dialog(background);
  84             ((Dialog) window).setUndecorated(true);
  85         } else {
  86             window = new Window(background);
  87         }
  88 
  89         window.setBackground(FG_COLOR);
  90         north = new Button("north");
  91         window.add(north, BorderLayout.NORTH);
  92 
  93         center = new List(5);
  94         window.add(center, BorderLayout.CENTER);
  95 
  96         Choice choice = new Choice();
  97         for(int i = 0; i < 20; i++) {
  98             choice.add("item " + i);
  99         }
 100         south = choice;
 101 
 102         south.addMouseListener(new MouseAdapter() {
 103             @Override
 104             public void mouseClicked(MouseEvent e) {
 105                 clicked |= 1 << 1;
 106             }
 107         });
 108 
 109         window.add(south, BorderLayout.SOUTH);
 110         window.setAlwaysOnTop(true);
 111         window.setOpacity(0.3f);
 112         window.setLocation(2 * dl, 2 * dl);
 113         window.setSize(255, 255);
 114         window.pack();
 115         window.setVisible(true);
 116 
 117         System.out.println("Checking " + window.getClass().getName() + "...");
 118     }
 119 
 120     public void doTest() throws Exception {
 121 
 122         Point ls = window.getLocationOnScreen();
 123         clicked = 0;
 124         checkClick(ls.x + window.getWidth() / 2, ls.y - 5, 0);
 125 
 126         robot.waitForIdle(2000);
 127 
 128         ls = south.getLocationOnScreen();
 129         
 130         Point p1 = new Point((int) (ls.x + south.getWidth() * 0.75), ls.y + south.getHeight() * 3);
 131         Point p2 = new Point((int) (ls.x + south.getWidth() * 0.75), ls.y - south.getHeight() * 2);
 132         Color c1 = robot.getPixelColor(p1.x, p1.y);
 133         Color c2 = robot.getPixelColor(p2.x, p2.y);
 134 
 135         checkClick(ls.x + south.getWidth() / 2, ls.y + south.getHeight() / 2, 1);
 136 
 137         robot.waitForIdle(2000);
 138 
 139         Color c1b = robot.getPixelColor(p1.x, p1.y);
 140         Color c2b = robot.getPixelColor(p2.x, p2.y);
 141 
 142         if (!aproximatelyEquals(c1, c1b) && !aproximatelyEquals(south.getBackground(), c1b))
 143             throw new RuntimeException("Check for opaque drop down failed. Before click: " + c1 + ", after click: " + c1b + ", expected is " + south.getBackground());
 144 
 145         if (!aproximatelyEquals(c2,c2b) && !aproximatelyEquals(south.getBackground(), c2b))
 146             throw new RuntimeException("Check for opaque drop down failed. Before click: " + c2 + ", after click: " + c2b + ", expected is " + south.getBackground());
 147 
 148         EventQueue.invokeAndWait(() -> {
 149             background.dispose();
 150             window.dispose();
 151         });
 152 
 153         robot.waitForIdle();
 154     }
 155 
 156     boolean aproximatelyEquals(Color c1, Color c2) {
 157         return ((Math.abs(c1.getRed()-c2.getRed())/256.0 +
 158                 Math.abs(c1.getGreen()-c2.getGreen())/256.0 +
 159                 Math.abs(c1.getBlue()-c2.getBlue())/256.0)) / 3 < 0.02;
 160     }
 161 
 162     @Override
 163     public void applyShape() { }
 164 
 165     void checkClick(int x, int y, int flag) throws Exception {
 166 
 167         System.out.println("Trying to click point " + x + ", " + y + ", looking for " + flag + " flag to trigger.");
 168 
 169         clicked = 0;
 170         robot.mouseMove(x, y);
 171         robot.click();
 172 
 173         for (int i = 0; i < 100; i++)
 174             if ((clicked & (1 << flag)) == 0)
 175                 robot.delay(50);
 176             else
 177                 break;
 178 
 179         if ((clicked & (1 << flag)) == 0)
 180             throw new RuntimeException("FAIL: Flag " + flag + " is not triggered for point " + x + ", " + y + "!");
 181     }
 182 }