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     @Override
  69     public void initBackgroundFrame() {
  70         super.initBackgroundFrame();
  71         background.addMouseListener(new MouseAdapter() {
  72             @Override
  73             public void mouseClicked(MouseEvent e) {
  74                 clicked |= 1 << 0;
  75             }
  76         });
  77     }
  78 
  79     @Override
  80     public void initGUI() {
  81         if (windowClass.equals(Frame.class)) {
  82             window = new Frame();
  83             ((Frame) window).setUndecorated(true);
  84         } else  if (windowClass.equals(Dialog.class)) {
  85             window = new Dialog(background);
  86             ((Dialog) window).setUndecorated(true);
  87         } else {
  88             window = new Window(background);
  89         }
  90 
  91         window.setBackground(FG_COLOR);
  92         north = new Button("north");
  93         window.add(north, BorderLayout.NORTH);
  94 
  95         center = new List(5);
  96         window.add(center, BorderLayout.CENTER);
  97 
  98         Choice choice = new Choice();
  99         for(int i = 0; i < 20; i++) {
 100             choice.add("item " + i);
 101         }
 102         south = choice;
 103 
 104         south.addMouseListener(new MouseAdapter() {
 105             @Override
 106             public void mouseClicked(MouseEvent e) {
 107                 clicked |= 1 << 1;
 108             }
 109         });
 110 
 111         window.add(south, BorderLayout.SOUTH);
 112         window.setAlwaysOnTop(true);
 113         window.setOpacity(0.3f);
 114         window.setLocation(2 * dl, 2 * dl);
 115         window.setSize(255, 255);
 116         window.pack();
 117         window.setVisible(true);
 118 
 119         System.out.println("Checking " + window.getClass().getName() + "...");
 120     }
 121 
 122     @Override
 123     public void doTest() throws Exception {
 124 
 125         Point ls = window.getLocationOnScreen();
 126         clicked = 0;
 127         checkClick(ls.x + window.getWidth() / 2, ls.y - 5, 0);
 128 
 129         robot.waitForIdle(2000);
 130 
 131         ls = south.getLocationOnScreen();
 132         
 133         Point p1 = new Point((int) (ls.x + south.getWidth() * 0.75), ls.y + south.getHeight() * 3);
 134         Point p2 = new Point((int) (ls.x + south.getWidth() * 0.75), ls.y - south.getHeight() * 2);
 135         Color c1 = robot.getPixelColor(p1.x, p1.y);
 136         Color c2 = robot.getPixelColor(p2.x, p2.y);
 137 
 138         checkClick(ls.x + south.getWidth() / 2, ls.y + south.getHeight() / 2, 1);
 139 
 140         robot.waitForIdle(2000);
 141 
 142         Color c1b = robot.getPixelColor(p1.x, p1.y);
 143         Color c2b = robot.getPixelColor(p2.x, p2.y);
 144 
 145         if (!aproximatelyEquals(c1, c1b) && !aproximatelyEquals(south.getBackground(), c1b))
 146             throw new RuntimeException("Check for opaque drop down failed. Before click: " + c1 + ", after click: " + c1b + ", expected is " + south.getBackground());
 147 
 148         if (!aproximatelyEquals(c2,c2b) && !aproximatelyEquals(south.getBackground(), c2b))
 149             throw new RuntimeException("Check for opaque drop down failed. Before click: " + c2 + ", after click: " + c2b + ", expected is " + south.getBackground());
 150 
 151         EventQueue.invokeAndWait(() -> {
 152             background.dispose();
 153             window.dispose();
 154         });
 155 
 156         robot.waitForIdle();
 157     }
 158 
 159     boolean aproximatelyEquals(Color c1, Color c2) {
 160         return ((Math.abs(c1.getRed()-c2.getRed())/256.0 +
 161                 Math.abs(c1.getGreen()-c2.getGreen())/256.0 +
 162                 Math.abs(c1.getBlue()-c2.getBlue())/256.0)) / 3 < 0.02;
 163     }
 164 
 165     @Override
 166     public void applyShape() { }
 167 
 168 
 169     void checkClick(int x, int y, int flag) throws Exception {
 170 
 171         System.out.println("Trying to click point " + x + ", " + y + ", looking for " + flag + " flag to trigger.");
 172 
 173         clicked = 0;
 174         robot.mouseMove(x, y);
 175         robot.click();
 176 
 177         for (int i = 0; i < 100; i++)
 178             if ((clicked & (1 << flag)) == 0)
 179                 robot.delay(50);
 180             else
 181                 break;
 182 
 183         if ((clicked & (1 << flag)) == 0)
 184             throw new RuntimeException("FAIL: Flag " + flag + " is not triggered for point " + x + ", " + y + "!");
 185     }
 186 }