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