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