1 /*
   2  * Copyright (c) 2015, 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         "A", "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 
  62     public static void main(String[] args) throws Exception {
  63         iterateLookAndFeels(new bug8033069NoScrollBar(NO_SCROLL_ITEMS));
  64     }
  65 
  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 
  79     public bug8033069NoScrollBar(String[] items) throws AWTException {
  80         this.items = items;
  81 
  82         robot = new Robot();
  83         robot.setAutoDelay(200);
  84     }
  85 
  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 
 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             robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
 114             // Click it to open popup
 115             robot.mousePress(InputEvent.BUTTON1_MASK);
 116             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 117 
 118             robot.waitForIdle();
 119             assertTrue("cb1 popup is not visible",
 120                        Util.invokeOnEDT(cb1::isPopupVisible));
 121 
 122             robot.mouseWheel(1);
 123             robot.waitForIdle();
 124             assertTrue("cb1 popup is not visible after mouse wheel up on combo box",
 125                        Util.invokeOnEDT(cb1::isPopupVisible));
 126 
 127             robot.mouseWheel(-1);
 128             robot.waitForIdle();
 129             assertTrue("cb1 popup is not visible after mouse wheel down on combo box",
 130                        Util.invokeOnEDT(cb1::isPopupVisible));
 131 
 132             // Move mouse down on the popup
 133             robot.mouseMove(p.x + d.width / 2, p.y + d.height * 3);
 134 
 135             robot.mouseWheel(1);
 136             robot.waitForIdle();
 137             assertTrue("cb1 popup is not visible after mouse wheel up on popup",
 138                        Util.invokeOnEDT(cb1::isPopupVisible));
 139 
 140             robot.mouseWheel(-1);
 141             robot.waitForIdle();
 142             assertTrue("cb1 popup is not visible after mouse wheel down on popup",
 143                        Util.invokeOnEDT(cb1::isPopupVisible));
 144 
 145 
 146             // Move mouse pointer to the center of the second combo box
 147             p = cb2.getLocationOnScreen();
 148             d = cb2.getSize();
 149             robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
 150 
 151             robot.mouseWheel(1);
 152             robot.waitForIdle();
 153             assertFalse("cb1 popup is visible after mouse wheel up on cb2",
 154                         Util.invokeOnEDT(cb1::isPopupVisible));
 155         } finally {
 156             if (frame != null) {
 157                 frame.dispose();
 158             }
 159         }
 160 
 161         System.out.println("Test passed");
 162     }
 163 
 164     @Override
 165     public void run() {
 166         setupUI();
 167     }
 168 
 169     private static void assertTrue(String message, boolean value) {
 170         assertEquals(message, true, value);
 171     }
 172 
 173     private static void assertFalse(String message, boolean value) {
 174         assertEquals(message, false, value);
 175     }
 176 
 177     private static void assertEquals(String message, boolean expected, boolean actual) {
 178         if (expected != actual) {
 179             throw new RuntimeException(message);
 180         }
 181     }
 182 }