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