1 /*
   2  * Copyright (c) 2010, 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 /* @test
  25    @bug 6542335
  26    @summary different behavior on knob of scroll bar between 1.4.2 and 5.0
  27    @author  Alexander Potochkin
  28    @run main bug6542335
  29 */
  30 
  31 import javax.swing.*;
  32 import javax.swing.plaf.basic.BasicScrollBarUI;
  33 import java.awt.*;
  34 import java.awt.event.InputEvent;
  35 
  36 public class bug6542335 {
  37     private static JScrollBar sb;
  38     private static MyScrollBarUI ui;
  39 
  40     public static void main(String[] args) throws Exception {
  41         final Robot robot = new Robot();
  42         robot.setAutoDelay(10);
  43 
  44         final Rectangle[] thumbBounds = new Rectangle[1];
  45 
  46         SwingUtilities.invokeAndWait(new Runnable() {
  47             public void run() {
  48                 final JFrame frame = new JFrame("bug6542335");
  49                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50 
  51                 sb = new JScrollBar(0, 0, 1, 0, 1);
  52 
  53                 ui = new MyScrollBarUI();
  54                 sb.setUI(ui);
  55 
  56                 sb.setPreferredSize(new Dimension(200, 17));
  57                 DefaultBoundedRangeModel rangeModel = new DefaultBoundedRangeModel();
  58                 rangeModel.setMaximum(100);
  59                 rangeModel.setMinimum(0);
  60                 rangeModel.setExtent(50);
  61                 rangeModel.setValue(50);
  62 
  63                 sb.setModel(rangeModel);
  64                 frame.add(sb, BorderLayout.NORTH);
  65 
  66                 frame.setSize(200, 100);
  67                 frame.setVisible(true);
  68             }
  69         });
  70 
  71         robot.waitForIdle();
  72 
  73         SwingUtilities.invokeAndWait(new Runnable() {
  74             public void run() {
  75                 thumbBounds[0] = new Rectangle(ui.getThumbBounds());
  76 
  77                 Point l = sb.getLocationOnScreen();
  78 
  79                 robot.mouseMove(l.x + (int) (0.75 * sb.getWidth()), l.y + sb.getHeight() / 2);
  80                 robot.mousePress(InputEvent.BUTTON1_MASK);
  81                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
  82             }
  83         });
  84 
  85         robot.waitForIdle();
  86 
  87         SwingUtilities.invokeAndWait(new Runnable() {
  88             public void run() {
  89                 Rectangle newThumbBounds = ui.getThumbBounds();
  90 
  91                 if (!thumbBounds[0].equals(newThumbBounds)) {
  92                     throw new RuntimeException("Test failed.\nOld bounds: " + thumbBounds[0] +
  93                     "\nNew bounds: " + newThumbBounds);
  94                 }
  95             }
  96         });
  97     }
  98 
  99     static class MyScrollBarUI extends BasicScrollBarUI {
 100         public Rectangle getThumbBounds() {
 101             return super.getThumbBounds();
 102         }
 103     }
 104 }