Path: blob/master/test/jdk/javax/swing/JSlider/TestJSliderRendering.java
41149 views
/*1* Copyright (c) 2020, 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* @requires (os.family == "linux")26* @key headful27* @bug 821846928* @summary Tests JSlider is rendered properly with gtk329* @run main TestJSliderRendering30*/3132import javax.swing.JFrame;33import javax.swing.JPanel;34import javax.swing.JSlider;35import javax.swing.SwingUtilities;36import javax.swing.UIManager;37import javax.swing.UnsupportedLookAndFeelException;38import java.awt.Color;39import java.awt.Component;40import java.awt.Point;41import java.awt.Rectangle;42import java.awt.Robot;4344public class TestJSliderRendering {45private static JFrame frame;46private static JSlider slider;47private static Point point;48private static Rectangle rect;49private static Robot robot;50private static final String GTK_LAF_CLASS = "GTKLookAndFeel";51private static int minColorDifference = 50;5253private static void blockTillDisplayed(Component comp) {54Point p = null;55while (p == null) {56try {57p = comp.getLocationOnScreen();58} catch (IllegalStateException e) {59try {60Thread.sleep(500);61} catch (InterruptedException ie) {62}63}64}65}6667private static int getMaxColorDiff(Color c1, Color c2) {68return Math.max(Math.abs(c1.getRed() - c2.getRed()),69Math.max(Math.abs(c1.getGreen() - c2.getGreen()),70Math.abs(c1.getBlue() - c2.getBlue())));71}7273public static void main(String[] args) throws Exception {74if (!System.getProperty("os.name").startsWith("Linux")) {75System.out.println("This test is meant for Linux platform only");76return;77}7879for (UIManager.LookAndFeelInfo lookAndFeelInfo :80UIManager.getInstalledLookAndFeels()) {81if (lookAndFeelInfo.getClassName().contains(GTK_LAF_CLASS)) {82try {83UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());84} catch (final UnsupportedLookAndFeelException ignored) {85System.out.println("GTK L&F could not be set, so this " +86"test can not be run in this scenario ");87return;88}89}90}9192robot = new Robot();93robot.setAutoDelay(100);9495try {96SwingUtilities.invokeAndWait(new Runnable() {97public void run() {98JPanel panel = new JPanel();99slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);100panel.add(slider);101frame = new JFrame("TestJSliderRendering");102frame.add(panel);103frame.setSize(200, 200);104frame.setAlwaysOnTop(true);105frame.setLocationRelativeTo(null);106frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);107frame.setVisible(true);108}109});110111robot.waitForIdle();112robot.delay(500);113114blockTillDisplayed(slider);115SwingUtilities.invokeAndWait(() -> {116point = slider.getLocationOnScreen();117rect = slider.getBounds();118});119robot.waitForIdle();120robot.delay(500);121122int h = point.y+rect.height*6/7;123124Color backgroundColor = robot125.getPixelColor(point.x+rect.width/4, h);126robot.waitForIdle();127128boolean knobFound = false;129for (int i=point.x+rect.width/4;i<point.x+rect.width*3/4;i+=2) {130Color highlightColor = robot.getPixelColor(i, h);131if (getMaxColorDiff(backgroundColor, highlightColor)132> minColorDifference) {133knobFound = true;134break;135}136robot.waitForIdle();137}138if (!knobFound) {139throw new RuntimeException("The slider is not rendered properly");140}141} finally {142if (frame != null) {143SwingUtilities.invokeAndWait(frame::dispose);144}145}146}147}148149150