1 /*
   2  * Copyright (c) 2011, 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 /*
  25  * @test
  26  * @bug 4708809
  27  * @summary JScrollBar functionality slightly different from native scrollbar
  28  * @author Andrey Pikalev
  29  * @run main bug4708809
  30  */
  31 import javax.swing.*;
  32 import java.awt.*;
  33 import java.awt.Point;
  34 import java.awt.event.*;
  35 import sun.awt.SunToolkit;
  36 
  37 public class bug4708809 {
  38 
  39     private static volatile boolean do_test = false;
  40     private static volatile boolean passed = true;
  41     private static JScrollPane spane;
  42     private static JScrollBar sbar;
  43 
  44     public static void main(String[] args) throws Exception {
  45         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  46         Robot robot = new Robot();
  47         robot.setAutoDelay(350);
  48 
  49         SwingUtilities.invokeAndWait(new Runnable() {
  50 
  51             public void run() {
  52                 createAndShowGUI();
  53             }
  54         });
  55 
  56         toolkit.realSync();
  57 
  58         SwingUtilities.invokeAndWait(new Runnable() {
  59 
  60             public void run() {
  61                 spane.requestFocus();
  62                 sbar.setValue(sbar.getMaximum());
  63             }
  64         });
  65 
  66         toolkit.realSync();
  67 
  68         Point point = getClickPoint(0.5, 0.5);
  69         robot.mouseMove(point.x, point.y);
  70         robot.mousePress(InputEvent.BUTTON1_MASK);
  71 
  72         toolkit.realSync();
  73 
  74         SwingUtilities.invokeAndWait(new Runnable() {
  75 
  76             public void run() {
  77                 final int oldValue = sbar.getValue();
  78                 sbar.addAdjustmentListener(new AdjustmentListener() {
  79 
  80                     public void adjustmentValueChanged(AdjustmentEvent e) {
  81                         if (e.getValue() >= oldValue) {
  82                             passed = false;
  83                         }
  84                         do_test = true;
  85                     }
  86                 });
  87 
  88             }
  89         });
  90 
  91         toolkit.realSync();
  92 
  93         point = getClickPoint(0.5, 0.2);
  94         robot.mouseMove(point.x, point.y);
  95         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  96         toolkit.realSync();
  97 
  98         if (!do_test || !passed) {
  99             throw new Exception("The scrollbar moved with incorrect direction");
 100         }
 101 
 102     }
 103 
 104     private static Point getClickPoint(final double scaleX, final double scaleY) throws Exception {
 105         final Point[] result = new Point[1];
 106 
 107         SwingUtilities.invokeAndWait(new Runnable() {
 108 
 109             @Override
 110             public void run() {
 111                 Point p = sbar.getLocationOnScreen();
 112                 Rectangle rect = sbar.getBounds();
 113                 result[0] = new Point((int) (p.x + scaleX * rect.width),
 114                         (int) (p.y + scaleY * rect.height));
 115             }
 116         });
 117 
 118         return result[0];
 119 
 120     }
 121 
 122     private static void createAndShowGUI() {
 123         JFrame fr = new JFrame("Test");
 124 
 125         JLabel label = new JLabel("picture");
 126         label.setPreferredSize(new Dimension(500, 500));
 127         spane = new JScrollPane(label);
 128         fr.getContentPane().add(spane);
 129         sbar = spane.getVerticalScrollBar();
 130 
 131         fr.setSize(200, 200);
 132         fr.setVisible(true);
 133     }
 134 }