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