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