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