1 /*
2  * Copyright (c) 2015, 2019, 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.AWTException;
25 import java.awt.Dimension;
26 import java.awt.GridLayout;
27 import java.awt.Point;
28 import java.awt.Robot;
29 import java.awt.event.InputEvent;
30 import javax.swing.JComboBox;
31 import javax.swing.JFrame;
32 import javax.swing.JPanel;
33 import javax.swing.SwingUtilities;
34 import javax.swing.UIManager;
35 import javax.swing.UIManager.LookAndFeelInfo;
36 import javax.swing.UnsupportedLookAndFeelException;
37 
38 /* @test
39  * @bug 8033069
40  * @summary Checks that JComboBox popup does not close when mouse wheel is
41  *          rotated over combo box and over its popup. The case where popup
42  *          has no scroll bar.
43  * @library ../../regtesthelpers
44  * @build Util
45  * @run main bug8033069NoScrollBar
46  * @author Alexey Ivanov
47  */
48 public class bug8033069NoScrollBar implements Runnable {
49 
50     private static final String[] NO_SCROLL_ITEMS = new String[] {
51         "AA", "B", "C", "D", "E", "F"
52     };
53 
54     private final Robot robot;
55 
56     private final String[] items;
57 
58     private JFrame frame;
59     private JComboBox cb1;
60     private JComboBox cb2;
61 
main(String[] args)62     public static void main(String[] args) throws Exception {
63         iterateLookAndFeels(new bug8033069NoScrollBar(NO_SCROLL_ITEMS));
64     }
65 
iterateLookAndFeels(final bug8033069NoScrollBar test)66     protected static void iterateLookAndFeels(final bug8033069NoScrollBar test) throws Exception {
67         LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();
68         for (LookAndFeelInfo info : lafInfo) {
69             try {
70                 UIManager.setLookAndFeel(info.getClassName());
71                 System.out.println("Look and Feel: " + info.getClassName());
72                 test.runTest();
73             } catch (UnsupportedLookAndFeelException e) {
74                 System.out.println("Skipping unsupported LaF: " + info);
75             }
76         }
77     }
78 
bug8033069NoScrollBar(String[] items)79     public bug8033069NoScrollBar(String[] items) throws AWTException {
80         this.items = items;
81 
82         robot = new Robot();
83         robot.setAutoDelay(200);
84     }
85 
setupUI()86     private void setupUI() {
87         frame = new JFrame();
88         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
89 
90         cb1 = new JComboBox<>(items);
91         cb2 = new JComboBox<>(items);
92         JPanel panel = new JPanel(new GridLayout(1, 2));
93         panel.add(cb1);
94         panel.add(cb2);
95 
96         frame.add(panel);
97 
98         frame.pack();
99         frame.setVisible(true);
100     }
101 
runTest()102     public void runTest() throws Exception {
103         try {
104             SwingUtilities.invokeAndWait(this);
105 
106             robot.waitForIdle();
107             assertFalse("cb1 popup is visible",
108                         Util.invokeOnEDT(cb1::isPopupVisible));
109 
110             // Move mouse pointer to the center of the fist combo box
111             Point p = cb1.getLocationOnScreen();
112             Dimension d = cb1.getSize();
113             System.out.println(d.width + "," + d.height);
114             robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
115             // Click it to open popup
116             robot.mousePress(InputEvent.BUTTON1_MASK);
117             robot.mouseRelease(InputEvent.BUTTON1_MASK);
118 
119             robot.waitForIdle();
120             assertTrue("cb1 popup is not visible",
121                        Util.invokeOnEDT(cb1::isPopupVisible));
122 
123             robot.mouseWheel(1);
124             robot.waitForIdle();
125             assertTrue("cb1 popup is not visible after mouse wheel up on combo box",
126                        Util.invokeOnEDT(cb1::isPopupVisible));
127 
128             robot.mouseWheel(-1);
129             robot.waitForIdle();
130             assertTrue("cb1 popup is not visible after mouse wheel down on combo box",
131                        Util.invokeOnEDT(cb1::isPopupVisible));
132 
133             // Move mouse down on the popup
134             robot.mouseMove(p.x + d.width / 2, p.y + d.height * 3);
135 
136             robot.mouseWheel(1);
137             robot.waitForIdle();
138             assertTrue("cb1 popup is not visible after mouse wheel up on popup",
139                        Util.invokeOnEDT(cb1::isPopupVisible));
140 
141             robot.mouseWheel(-1);
142             robot.waitForIdle();
143             assertTrue("cb1 popup is not visible after mouse wheel down on popup",
144                        Util.invokeOnEDT(cb1::isPopupVisible));
145 
146 
147             // Move mouse pointer to the center of the second combo box
148             p = cb2.getLocationOnScreen();
149             d = cb2.getSize();
150             robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
151 
152             robot.mouseWheel(1);
153             robot.waitForIdle();
154             assertFalse("cb1 popup is visible after mouse wheel up on cb2",
155                         Util.invokeOnEDT(cb1::isPopupVisible));
156         } finally {
157             if (frame != null) {
158                 frame.dispose();
159             }
160         }
161 
162         System.out.println("Test passed");
163     }
164 
165     @Override
run()166     public void run() {
167         setupUI();
168     }
169 
assertTrue(String message, boolean value)170     private static void assertTrue(String message, boolean value) {
171         assertEquals(message, true, value);
172     }
173 
assertFalse(String message, boolean value)174     private static void assertFalse(String message, boolean value) {
175         assertEquals(message, false, value);
176     }
177 
assertEquals(String message, boolean expected, boolean actual)178     private static void assertEquals(String message, boolean expected, boolean actual) {
179         if (expected != actual) {
180             throw new RuntimeException(message);
181         }
182     }
183 }
184