1 /*
   2  * Copyright (c) 2010, 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 6848475
  26  * @summary JSlider does not display the correct value of its BoundedRangeModel
  27  * @author Pavel Porvatov
  28  * @run main bug6848475
  29  */
  30 
  31 import sun.awt.SunToolkit;
  32 
  33 import javax.swing.*;
  34 import javax.swing.plaf.SliderUI;
  35 import javax.swing.plaf.basic.BasicSliderUI;
  36 import java.awt.*;
  37 import java.awt.event.InputEvent;
  38 import java.lang.reflect.Field;
  39 
  40 public class bug6848475 {
  41     private static JFrame frame;
  42 
  43     private static JSlider slider;
  44 
  45     private static Robot robot;
  46 
  47     private static int thumbRectX;
  48 
  49     public static void main(String[] args) throws Exception {
  50         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  51 
  52         robot = new Robot();
  53         robot.setAutoDelay(100);
  54 
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56             public void run() {
  57                 frame = new JFrame();
  58 
  59                 DefaultBoundedRangeModel sliderModel = new DefaultBoundedRangeModel() {
  60                     public void setValue(int n) {
  61                         // Don't allow value to be changed
  62                     }
  63                 };
  64 
  65                 slider = new JSlider(sliderModel);
  66 
  67                 frame.getContentPane().add(slider);
  68                 frame.pack();
  69                 frame.setVisible(true);
  70             }
  71         });
  72 
  73         toolkit.realSync();
  74 
  75         SwingUtilities.invokeAndWait(new Runnable() {
  76             public void run() {
  77                 Point p = slider.getLocationOnScreen();
  78 
  79                 robot.mouseMove(p.x, p.y);
  80             }
  81         });
  82 
  83         toolkit.realSync();
  84 
  85         SwingUtilities.invokeAndWait(new Runnable() {
  86             public void run() {
  87                 thumbRectX = getThumbRectField().x;
  88 
  89                 Point p = slider.getLocationOnScreen();
  90 
  91                 robot.mouseMove(p.x, p.y);
  92                 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  93                 robot.mouseMove(p.x + 20, p.y);
  94                 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  95             }
  96         });
  97 
  98         toolkit.realSync();
  99 
 100         SwingUtilities.invokeAndWait(new Runnable() {
 101             public void run() {
 102                 Rectangle newThumbRect = getThumbRectField();
 103 
 104                 if (newThumbRect.x != thumbRectX) {
 105                     throw new RuntimeException("Test failed: the thumb was moved");
 106                 }
 107 
 108                 frame.dispose();
 109             }
 110         });
 111     }
 112 
 113     private static Rectangle getThumbRectField() {
 114         try {
 115             SliderUI ui = slider.getUI();
 116 
 117             Field field = BasicSliderUI.class.getDeclaredField("thumbRect");
 118 
 119             field.setAccessible(true);
 120 
 121             return (Rectangle) field.get(ui);
 122         } catch (Exception e) {
 123             throw new RuntimeException(e);
 124         }
 125     }
 126 }