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