1 /*
   2  * Copyright (c) 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 
  24 import java.awt.AWTException;
  25 import java.awt.Dimension;
  26 import java.awt.Point;
  27 import java.awt.Rectangle;
  28 import java.awt.Robot;
  29 import javax.swing.Box;
  30 import javax.swing.BoxLayout;
  31 import javax.swing.JComboBox;
  32 import javax.swing.JFrame;
  33 import javax.swing.JPanel;
  34 import javax.swing.JScrollPane;
  35 import javax.swing.SwingUtilities;
  36 import javax.swing.UIManager;
  37 import javax.swing.UIManager.LookAndFeelInfo;
  38 import javax.swing.UnsupportedLookAndFeelException;
  39 import javax.swing.WindowConstants;
  40 
  41 /*
  42  * @test
  43  * @key headful
  44  * @bug 8136998
  45  * @summary Checks that JComboBox does not prevent mouse-wheel scrolling JScrollPane.
  46  * @library ../../regtesthelpers
  47  * @build Util
  48  * @run main bug8136998
  49  * @author Alexey Ivanov
  50  */
  51 public class bug8136998 {
  52 
  53     private static final String[] ITEMS = new String[] {
  54         "A", "B", "C", "D", "E", "F"
  55     };
  56 
  57     private final Robot robot;
  58 
  59     private JFrame frame;
  60     private JComboBox comboBox;
  61     private JScrollPane scrollPane;
  62 
  63     public static void main(String[] args) throws Exception {
  64         iterateLookAndFeels(new bug8136998());
  65     }
  66 
  67     protected static void iterateLookAndFeels(final bug8136998 test) throws Exception {
  68         LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();
  69         for (LookAndFeelInfo info : lafInfo) {
  70             try {
  71                 UIManager.setLookAndFeel(info.getClassName());
  72                 System.out.println("Look and Feel: " + info.getClassName());
  73                 test.runTest();
  74             } catch (UnsupportedLookAndFeelException e) {
  75                 System.out.println("Skipping unsupported LaF: " + info);
  76             }
  77         }
  78     }
  79 
  80     public bug8136998() throws AWTException {
  81         robot = new Robot();
  82         robot.setAutoDelay(200);
  83     }
  84 
  85     private void setupUI() {
  86         frame = new JFrame();
  87         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  88 
  89         comboBox = new JComboBox<>(ITEMS);
  90 
  91         JPanel scrollable = new JPanel();
  92         scrollable.setLayout(new BoxLayout(scrollable, BoxLayout.Y_AXIS));
  93 
  94         scrollable.add(Box.createVerticalStrut(200));
  95         scrollable.add(comboBox);
  96         scrollable.add(Box.createVerticalStrut(200));
  97 
  98         scrollPane = new JScrollPane(scrollable);
  99 
 100         frame.add(scrollPane);
 101 
 102         frame.setSize(100, 200);
 103         frame.setVisible(true);
 104     }
 105 
 106     public void runTest() throws Exception {
 107         try {
 108             SwingUtilities.invokeAndWait(this::setupUI);
 109 
 110             robot.waitForIdle();
 111 
 112             SwingUtilities.invokeAndWait(() ->
 113                 scrollPane.getViewport().scrollRectToVisible(comboBox.getBounds())
 114             );
 115             robot.waitForIdle();
 116 
 117             // Move mouse pointer to the center of the combo box
 118             Point p = comboBox.getLocationOnScreen();
 119             Dimension d = comboBox.getSize();
 120             robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
 121 
 122             // The currently visible rectangle in scrollPane
 123             Rectangle viewRect0 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);
 124 
 125             // Scroll the scrollPane with mouse wheel
 126             robot.mouseWheel(1);
 127             robot.waitForIdle();
 128 
 129             // The updated rectangle
 130             Rectangle viewRect1 = Util.invokeOnEDT(scrollPane.getViewport()::getViewRect);
 131 
 132             if (viewRect0.y == viewRect1.y) {
 133                 throw new RuntimeException("Mouse wheel should have scrolled the JScrollPane");
 134             }
 135         } finally {
 136             if (frame != null) {
 137                 frame.dispose();
 138             }
 139         }
 140 
 141         System.out.println("Test passed");
 142     }
 143 }