Path: blob/master/test/jdk/javax/swing/JProgressBar/8015748/JProgressBarOrientationRobotTest.java
41153 views
/*1* Copyright (c) 2015, 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 801574827* @summary verifies ProgressBar RightToLeft orientations for all Look and Feels28* @library ../../regtesthelpers29* @build Util30* @run main JProgressBarOrientationRobotTest31*/32import java.awt.Color;33import java.awt.ComponentOrientation;34import java.awt.Point;35import java.awt.Robot;36import javax.swing.JFrame;37import javax.swing.JProgressBar;38import javax.swing.SwingUtilities;39import javax.swing.UIManager;40import javax.swing.UnsupportedLookAndFeelException;4142public class JProgressBarOrientationRobotTest {4344private static JFrame frame;45private static JProgressBar progressBar;46private static Robot robot;47private static Color colorCenter;48private static Color colorLeft;49private static Color colorRight;50private static final int widthBuffer = 20;51private static volatile String errorString = "";5253public static void main(String[] args) throws Exception {54robot = new Robot();55robot.waitForIdle();56UIManager.LookAndFeelInfo[] lookAndFeelArray57= UIManager.getInstalledLookAndFeels();58for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {59executeCase(lookAndFeelItem.getClassName(),60lookAndFeelItem.getName());6162}63if (!"".equals(errorString)) {64System.err.println(errorString);65}66}6768private static void executeCase(String lookAndFeelString,69String shortenedLandFeelString) throws Exception {70if (tryLookAndFeel(lookAndFeelString)) {71createUI(shortenedLandFeelString);72robot.waitForIdle();7374createLTR();75robot.delay(1000);76runTestCase();77robot.delay(1000);78testCaseLTR(shortenedLandFeelString);79robot.delay(1000);8081createRTL();82robot.delay(1000);83runTestCase();84robot.delay(1000);85testCaseRTL(shortenedLandFeelString);86robot.delay(1000);8788cleanUp();89}9091}9293private static void createUI(final String shortenedLookAndFeelString)94throws Exception {95SwingUtilities.invokeAndWait(new Runnable() {96@Override97public void run() {98progressBar = new JProgressBar();99progressBar.setValue(30);100frame = new JFrame(shortenedLookAndFeelString);101frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);102frame.getContentPane().add(progressBar);103frame.pack();104frame.setSize(500, frame.getSize().height);105frame.setLocationRelativeTo(null);106frame.setVisible(true);107frame.toFront();108}109});110}111112private static void createLTR()113throws Exception {114SwingUtilities.invokeAndWait(new Runnable() {115@Override116public void run() {117progressBar.applyComponentOrientation(118ComponentOrientation.LEFT_TO_RIGHT);119progressBar.repaint();120}121});122}123124private static void createRTL()125throws Exception {126SwingUtilities.invokeAndWait(new Runnable() {127@Override128public void run() {129progressBar.applyComponentOrientation(130ComponentOrientation.RIGHT_TO_LEFT);131progressBar.repaint();132}133});134}135136private static void runTestCase() throws Exception {137Point centerPoint = Util.getCenterPoint(progressBar);138colorCenter = robot.getPixelColor(centerPoint.x, centerPoint.y);139colorRight = robot.getPixelColor(140(centerPoint.x + progressBar.getWidth() / 2 - widthBuffer),141centerPoint.y);142colorLeft = robot.getPixelColor(143(centerPoint.x - progressBar.getWidth() / 2 + widthBuffer),144centerPoint.y);145robot.waitForIdle();146}147148private static void testCaseLTR(String shortenedLookAndFeelString)149throws Exception {150SwingUtilities.invokeAndWait(new Runnable() {151@Override152public void run() {153154if (colorCenter.equals(colorRight)) {155if (!colorCenter.equals(colorLeft)) {156System.out.println("[" + shortenedLookAndFeelString157+ "]: LTR orientation test passed");158}159} else {160frame.dispose();161String error = "[" + shortenedLookAndFeelString162+ "]: [Error]: LTR orientation test failed";163errorString += error;164System.err.println(error);165}166}167});168169}170171private static void testCaseRTL(String shortenedLookAndFeelString)172throws Exception {173SwingUtilities.invokeAndWait(new Runnable() {174@Override175public void run() {176if (colorCenter.equals(colorLeft)) {177if (!colorCenter.equals(colorRight)) {178System.out.println("[" + shortenedLookAndFeelString179+ "]: RTL orientation test passed");180}181} else {182frame.dispose();183String error = "[" + shortenedLookAndFeelString184+ "]: [Error]: LTR orientation test failed";185errorString += error;186System.err.println(error);187}188}189});190}191192private static void cleanUp() throws Exception {193SwingUtilities.invokeAndWait(new Runnable() {194@Override195public void run() {196frame.dispose();197}198});199}200201private static boolean tryLookAndFeel(String lookAndFeelString)202throws Exception {203try {204UIManager.setLookAndFeel(205lookAndFeelString);206207} catch (UnsupportedLookAndFeelException208| ClassNotFoundException209| InstantiationException210| IllegalAccessException e) {211errorString += e.getMessage() + "\n";212System.err.println("[Exception]: " + e.getMessage());213return false;214}215return true;216}217}218219220