Path: blob/master/test/jdk/javax/swing/JSlider/6848475/bug6848475.java
41153 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 684847527* @summary JSlider does not display the correct value of its BoundedRangeModel28* @author Pavel Porvatov29* @modules java.desktop/javax.swing.plaf.basic:open30* @run main bug684847531*/3233import javax.swing.*;34import javax.swing.plaf.SliderUI;35import javax.swing.plaf.basic.BasicSliderUI;36import java.awt.*;37import java.awt.event.InputEvent;38import java.lang.reflect.Field;3940public class bug6848475 {41private static JFrame frame;4243private static JSlider slider;4445private static Robot robot;4647private static int thumbRectX;4849public static void main(String[] args) throws Exception {5051robot = new Robot();52robot.setAutoDelay(100);5354SwingUtilities.invokeAndWait(new Runnable() {55public void run() {56frame = new JFrame();5758DefaultBoundedRangeModel sliderModel = new DefaultBoundedRangeModel() {59public void setValue(int n) {60// Don't allow value to be changed61}62};6364slider = new JSlider(sliderModel);6566frame.getContentPane().add(slider);67frame.pack();68frame.setVisible(true);69}70});7172robot.waitForIdle();7374SwingUtilities.invokeAndWait(new Runnable() {75public void run() {76Point p = slider.getLocationOnScreen();7778robot.mouseMove(p.x, p.y);79}80});8182robot.waitForIdle();8384SwingUtilities.invokeAndWait(new Runnable() {85public void run() {86thumbRectX = getThumbRectField().x;8788Point p = slider.getLocationOnScreen();8990robot.mouseMove(p.x, p.y);91robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);92robot.mouseMove(p.x + 20, p.y);93robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);94}95});9697robot.waitForIdle();9899SwingUtilities.invokeAndWait(new Runnable() {100public void run() {101Rectangle newThumbRect = getThumbRectField();102103if (newThumbRect.x != thumbRectX) {104throw new RuntimeException("Test failed: the thumb was moved");105}106107frame.dispose();108}109});110}111112private static Rectangle getThumbRectField() {113try {114SliderUI ui = slider.getUI();115116Field field = BasicSliderUI.class.getDeclaredField("thumbRect");117118field.setAccessible(true);119120return (Rectangle) field.get(ui);121} catch (Exception e) {122throw new RuntimeException(e);123}124}125}126127128