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