1 /*
   2  * Copyright (c) 2015, 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 /* @test
  25  * @bug 8015748
  26  * @summary verifies ProgressBar RightToLeft orientations for all Look and Feels
  27  * @library ../../regtesthelpers
  28  * @build Util
  29  * @run main JProgressBarOrientationRobotTest
  30  */
  31 import java.awt.Color;
  32 import java.awt.ComponentOrientation;
  33 import java.awt.Point;
  34 import java.awt.Robot;
  35 import javax.swing.JFrame;
  36 import javax.swing.JProgressBar;
  37 import javax.swing.SwingUtilities;
  38 import javax.swing.UIManager;
  39 import javax.swing.UnsupportedLookAndFeelException;
  40 
  41 public class JProgressBarOrientationRobotTest {
  42 
  43     private static JFrame frame;
  44     private static JProgressBar progressBar;
  45     private static Robot robot;
  46     private static Color colorCenter;
  47     private static Color colorLeft;
  48     private static Color colorRight;
  49     private static final int widthBuffer = 20;
  50     private static volatile String errorString = "";
  51 
  52     public static void main(String[] args) throws Exception {
  53         robot = new Robot();
  54         robot.waitForIdle();
  55         UIManager.LookAndFeelInfo[] lookAndFeelArray
  56                 = UIManager.getInstalledLookAndFeels();
  57         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  58             executeCase(lookAndFeelItem.getClassName(),
  59                     lookAndFeelItem.getName());
  60 
  61         }
  62         if (!"".equals(errorString)) {
  63             System.err.println(errorString);
  64         }
  65     }
  66 
  67     private static void executeCase(String lookAndFeelString,
  68             String shortenedLandFeelString) throws Exception {
  69         if (tryLookAndFeel(lookAndFeelString)) {
  70             createUI(shortenedLandFeelString);
  71             robot.waitForIdle();
  72 
  73             createLTR();
  74             robot.delay(1000);
  75             runTestCase();
  76             robot.delay(1000);
  77             testCaseLTR(shortenedLandFeelString);
  78             robot.delay(1000);
  79 
  80             createRTL();
  81             robot.delay(1000);
  82             runTestCase();
  83             robot.delay(1000);
  84             testCaseRTL(shortenedLandFeelString);
  85             robot.delay(1000);
  86 
  87             cleanUp();
  88         }
  89 
  90     }
  91 
  92     private static void createUI(final String shortenedLookAndFeelString)
  93             throws Exception {
  94         SwingUtilities.invokeAndWait(new Runnable() {
  95             @Override
  96             public void run() {
  97                 progressBar = new JProgressBar();
  98                 progressBar.setValue(30);
  99                 frame = new JFrame(shortenedLookAndFeelString);
 100                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 101                 frame.getContentPane().add(progressBar);
 102                 frame.pack();
 103                 frame.setSize(500, frame.getSize().height);
 104                 frame.setLocationRelativeTo(null);
 105                 frame.setVisible(true);
 106                 frame.toFront();
 107             }
 108         });
 109     }
 110 
 111     private static void createLTR()
 112             throws Exception {
 113         SwingUtilities.invokeAndWait(new Runnable() {
 114             @Override
 115             public void run() {
 116                 progressBar.applyComponentOrientation(
 117                         ComponentOrientation.LEFT_TO_RIGHT);
 118                 progressBar.repaint();
 119             }
 120         });
 121     }
 122 
 123     private static void createRTL()
 124             throws Exception {
 125         SwingUtilities.invokeAndWait(new Runnable() {
 126             @Override
 127             public void run() {
 128                 progressBar.applyComponentOrientation(
 129                         ComponentOrientation.RIGHT_TO_LEFT);
 130                 progressBar.repaint();
 131             }
 132         });
 133     }
 134 
 135     private static void runTestCase() throws Exception {
 136         Point centerPoint = Util.getCenterPoint(progressBar);
 137         colorCenter = robot.getPixelColor(centerPoint.x, centerPoint.y);
 138         colorRight = robot.getPixelColor(
 139                 (centerPoint.x + progressBar.getWidth() / 2 - widthBuffer),
 140                 centerPoint.y);
 141         colorLeft = robot.getPixelColor(
 142                 (centerPoint.x - progressBar.getWidth() / 2 + widthBuffer),
 143                 centerPoint.y);
 144         robot.waitForIdle();
 145     }
 146 
 147     private static void testCaseLTR(String shortenedLookAndFeelString)
 148             throws Exception {
 149         SwingUtilities.invokeAndWait(new Runnable() {
 150             @Override
 151             public void run() {
 152 
 153                 if (colorCenter.equals(colorRight)) {
 154                     if (!colorCenter.equals(colorLeft)) {
 155                         System.out.println("[" + shortenedLookAndFeelString
 156                                 + "]: LTR orientation test passed");
 157                     }
 158                 } else {
 159                     frame.dispose();
 160                     String error = "[" + shortenedLookAndFeelString
 161                             + "]: [Error]: LTR orientation test failed";
 162                     errorString += error;
 163                     System.err.println(error);
 164                 }
 165             }
 166         });
 167 
 168     }
 169 
 170     private static void testCaseRTL(String shortenedLookAndFeelString)
 171             throws Exception {
 172         SwingUtilities.invokeAndWait(new Runnable() {
 173             @Override
 174             public void run() {
 175                 if (colorCenter.equals(colorLeft)) {
 176                     if (!colorCenter.equals(colorRight)) {
 177                         System.out.println("[" + shortenedLookAndFeelString
 178                                 + "]: RTL orientation test passed");
 179                     }
 180                 } else {
 181                     frame.dispose();
 182                     String error = "[" + shortenedLookAndFeelString
 183                             + "]: [Error]: LTR orientation test failed";
 184                     errorString += error;
 185                     System.err.println(error);
 186                 }
 187             }
 188         });
 189     }
 190 
 191     private static void cleanUp() throws Exception {
 192         SwingUtilities.invokeAndWait(new Runnable() {
 193             @Override
 194             public void run() {
 195                 frame.dispose();
 196             }
 197         });
 198     }
 199 
 200     private static boolean tryLookAndFeel(String lookAndFeelString)
 201             throws Exception {
 202         try {
 203             UIManager.setLookAndFeel(
 204                     lookAndFeelString);
 205 
 206         } catch (UnsupportedLookAndFeelException
 207                 | ClassNotFoundException
 208                 | InstantiationException
 209                 | IllegalAccessException e) {
 210             errorString += e.getMessage() + "\n";
 211             System.err.println("[Exception]: " + e.getMessage());
 212             return false;
 213         }
 214         return true;
 215     }
 216 }