bug6848475.java revision 16177:89ef4b822745
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 * @modules java.desktop/javax.swing.plaf.basic:open
31 * @run main bug6848475
32 */
33
34import javax.swing.*;
35import javax.swing.plaf.SliderUI;
36import javax.swing.plaf.basic.BasicSliderUI;
37import java.awt.*;
38import java.awt.event.InputEvent;
39import java.lang.reflect.Field;
40
41public class bug6848475 {
42    private static JFrame frame;
43
44    private static JSlider slider;
45
46    private static Robot robot;
47
48    private static int thumbRectX;
49
50    public static void main(String[] args) throws Exception {
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        robot.waitForIdle();
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        robot.waitForIdle();
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        robot.waitForIdle();
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}
127