Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JSlider/6848475/bug6848475.java
38855 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/* @test24* @bug 684847525* @summary JSlider does not display the correct value of its BoundedRangeModel26* @author Pavel Porvatov27* @run main bug684847528*/2930import javax.swing.*;31import javax.swing.plaf.SliderUI;32import javax.swing.plaf.basic.BasicSliderUI;33import java.awt.*;34import java.awt.event.InputEvent;35import java.lang.reflect.Field;3637public class bug6848475 {38private static JFrame frame;3940private static JSlider slider;4142private static Robot robot;4344private static int thumbRectX;4546public static void main(String[] args) throws Exception {4748robot = new Robot();49robot.setAutoDelay(100);5051SwingUtilities.invokeAndWait(new Runnable() {52public void run() {53frame = new JFrame();5455DefaultBoundedRangeModel sliderModel = new DefaultBoundedRangeModel() {56public void setValue(int n) {57// Don't allow value to be changed58}59};6061slider = new JSlider(sliderModel);6263frame.getContentPane().add(slider);64frame.pack();65frame.setVisible(true);66}67});6869robot.waitForIdle();7071SwingUtilities.invokeAndWait(new Runnable() {72public void run() {73Point p = slider.getLocationOnScreen();7475robot.mouseMove(p.x, p.y);76}77});7879robot.waitForIdle();8081SwingUtilities.invokeAndWait(new Runnable() {82public void run() {83thumbRectX = getThumbRectField().x;8485Point p = slider.getLocationOnScreen();8687robot.mouseMove(p.x, p.y);88robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);89robot.mouseMove(p.x + 20, p.y);90robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);91}92});9394robot.waitForIdle();9596SwingUtilities.invokeAndWait(new Runnable() {97public void run() {98Rectangle newThumbRect = getThumbRectField();99100if (newThumbRect.x != thumbRectX) {101throw new RuntimeException("Test failed: the thumb was moved");102}103104frame.dispose();105}106});107}108109private static Rectangle getThumbRectField() {110try {111SliderUI ui = slider.getUI();112113Field field = BasicSliderUI.class.getDeclaredField("thumbRect");114115field.setAccessible(true);116117return (Rectangle) field.get(ui);118} catch (Exception e) {119throw new RuntimeException(e);120}121}122}123124125