1 /*
2 * Copyright (c) 2019, 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 import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.BACKGROUND;
25 import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.CHOOSER_TITLE;
26 import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.DEMO_TITLE;
27 import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.GRADIENT_1;
28 import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.GRADIENT_2;
29 import static com.sun.swingset3.demos.colorchooser.ColorChooserDemo.PERIMETER;
30 
31 import java.awt.Color;
32 import java.awt.event.KeyEvent;
33 
34 import org.jemmy2ext.JemmyExt.ByClassChooser;
35 import org.jtregext.GuiTestListener;
36 import org.netbeans.jemmy.ClassReference;
37 import org.netbeans.jemmy.DialogWaiter;
38 import org.netbeans.jemmy.operators.JButtonOperator;
39 import org.netbeans.jemmy.operators.JColorChooserOperator;
40 import org.netbeans.jemmy.operators.JComponentOperator;
41 import org.netbeans.jemmy.operators.JDialogOperator;
42 import org.netbeans.jemmy.operators.JFrameOperator;
43 import org.netbeans.jemmy.operators.JSliderOperator;
44 import org.netbeans.jemmy.operators.JSpinnerOperator;
45 import org.netbeans.jemmy.operators.JTabbedPaneOperator;
46 import org.netbeans.jemmy.operators.JTextFieldOperator;
47 import org.testng.annotations.Listeners;
48 import org.testng.annotations.Test;
49 
50 import com.sun.swingset3.demos.colorchooser.BezierAnimationPanel;
51 import com.sun.swingset3.demos.colorchooser.BezierAnimationPanel.BezierColor;
52 import com.sun.swingset3.demos.colorchooser.ColorChooserDemo;
53 
54 /*
55 * @test
56 * @key headful
57 * @summary Verifies SwingSet3 ColorChooserDemo by performing simple interaction
58 *  with all the controls that are shown in the ColorChooserDialog.
59 *
60 * @library /sanity/client/lib/jemmy/src
61 * @library /sanity/client/lib/Extensions/src
62 * @library /sanity/client/lib/SwingSet3/src
63 * @modules java.desktop
64 *          java.logging
65 * @build org.jemmy2ext.JemmyExt
66 * @build com.sun.swingset3.demos.colorchooser.ColorChooserDemo
67 * @run testng ColorChooserDemoTest
68 */
69 @Listeners(GuiTestListener.class)
70 public class ColorChooserDemoTest {
71 
72     private static final String OK_BUTTON_TITLE = "OK";
73     private static final String CANCEL_BUTTON_TITLE = "Cancel";
74     private static final String RESET_BUTTON_TITLE = "Reset";
75     private static final String HSV = "HSV";
76     private static final String RGB = "RGB";
77     private static final String HSL = "HSL";
78     private static final String CMYK = "CMYK";
79     private static final int HSV_NUMBER_OF_SLIDERS_AND_SPINNERS = 4;
80     private static final int RGB_NUMBER_OF_SLIDERS_AND_SPINNERS = 4;
81     private static final int HSL_NUMBER_OF_SLIDERS_AND_SPINNERS = 4;
82     private static final int CMYK_NUMBER_OF_SLIDERS_AND_SPINNERS = 5;
83     private static final int HSV_HUE_INDEX = 0;
84     private static final int HSV_SATURATION_INDEX = 1;
85     private static final int HSV_VALUE_INDEX = 2;
86     private static final int HSV_TRANSPARENCY_INDEX = 3;
87     private static final int HSL_HUE_INDEX = 0;
88     private static final int HSL_SATURATION_INDEX = 1;
89     private static final int HSL_LIGHTNESS_INDEX = 2;
90     private static final int HSL_TRANSPARENCY_INDEX = 3;
91     private static final int RGB_RED_INDEX = 0;
92     private static final int RGB_GREEN_INDEX = 1;
93     private static final int RGB_BLUE_INDEX = 2;
94     private static final int RGB_ALPHA_INDEX = 3;
95     private static final int RGB_COLORCODE_TEXT_FIELD_INDEX = 4;
96     private static final int CMYK_CYAN_INDEX = 0;
97     private static final int CMYK_MAGENTA_INDEX = 1;
98     private static final int CMYK_YELLOW_INDEX = 2;
99     private static final int CMYK_BLACK_INDEX = 3;
100     private static final int CMYK_ALPHA_INDEX = 4;
101 
102     private final Color resetColor = new Color(125, 125, 125);
103 
104     private JDialogOperator colorChooserDialog;
105     private JButtonOperator okButton;
106     private JButtonOperator cancelButton;
107     private JButtonOperator resetButton;
108     private JColorChooserOperator colorChooser;
109     private JButtonOperator backgroundButton;
110     private JButtonOperator gradient1Button;
111     private JButtonOperator gradient2Button;
112     private JButtonOperator perimeterButton;
113     private JTabbedPaneOperator tabOperator;
114     private JComponentOperator bezierAnimationPanel;
115     private JSliderOperator[] sliders = new JSliderOperator[5];
116     private JSpinnerOperator[] spinners = new JSpinnerOperator[5];
117     private JButtonOperator lastFocusedButton;
118 
119     @Test
test()120     public void test() throws Exception {
121         new ClassReference(ColorChooserDemo.class.getCanonicalName()).startApplication();
122         JFrameOperator frame = new JFrameOperator(DEMO_TITLE);
123         bezierAnimationPanel = new JComponentOperator(frame, new ByClassChooser(BezierAnimationPanel.class));
124         initializePanelButtons(frame);
125         checkBackgroundColorChooser();
126         checkGradient1ColorChooser();
127         checkGradient2ColorChooser();
128         checkPerimeterColorChooser();
129     }
130 
checkBackgroundColorChooser()131     private void checkBackgroundColorChooser() throws Exception {
132         basicCheck(backgroundButton, BezierColor.BACKGROUND);
133         checkAllColorChoosers(backgroundButton);
134     }
135 
checkGradient1ColorChooser()136     private void checkGradient1ColorChooser() throws Exception {
137         basicCheck(gradient1Button, BezierColor.GRADIENT_A);
138     }
139 
checkGradient2ColorChooser()140     private void checkGradient2ColorChooser() throws Exception {
141         basicCheck(gradient2Button, BezierColor.GRADIENT_B);
142     }
143 
checkPerimeterColorChooser()144     private void checkPerimeterColorChooser() throws Exception {
145         basicCheck(perimeterButton, BezierColor.OUTER);
146     }
147 
pushButtonAndInitialize(JButtonOperator jbo)148     private void pushButtonAndInitialize(JButtonOperator jbo) throws InterruptedException {
149         // Wait for focus to return to last focused button
150         lastFocusedButton.waitHasFocus();
151         jbo.pushNoBlock();
152         lastFocusedButton = jbo;
153         // Wait till the ColorChooserDemo Dialog Opens
154         new DialogWaiter().waitDialog(CHOOSER_TITLE, false, false);
155         initializeDialog();
156     }
157 
initializePanelButtons(JFrameOperator frame)158     private void initializePanelButtons(JFrameOperator frame) {
159         backgroundButton = new JButtonOperator(frame, BACKGROUND);
160         gradient1Button = new JButtonOperator(frame, GRADIENT_1);
161         gradient2Button = new JButtonOperator(frame, GRADIENT_2);
162         perimeterButton = new JButtonOperator(frame, PERIMETER);
163         lastFocusedButton = backgroundButton;
164     }
165 
initializeDialog()166     private void initializeDialog() {
167         colorChooserDialog = new JDialogOperator(CHOOSER_TITLE);
168         tabOperator = new JTabbedPaneOperator(colorChooserDialog);
169         colorChooser = new JColorChooserOperator(colorChooserDialog);
170         okButton = new JButtonOperator(colorChooserDialog, OK_BUTTON_TITLE);
171         cancelButton = new JButtonOperator(colorChooserDialog, CANCEL_BUTTON_TITLE);
172         resetButton = new JButtonOperator(colorChooserDialog, RESET_BUTTON_TITLE);
173     }
174 
basicCheck(JButtonOperator jbo, BezierColor bezierColor)175     private void basicCheck(JButtonOperator jbo, BezierColor bezierColor) throws Exception {
176         Color testColor = new Color(100, 26, 155);
177         Color testColor2 = new Color(10, 40, 50);
178         checkDefaultColorChooser(jbo, testColor, bezierColor);
179         checkCancelButton(jbo, testColor2);
180         checkResetButton(jbo, testColor2);
181     }
182 
checkDefaultColorChooser(JButtonOperator jbo, Color testColor, BezierColor bezierColor)183     private void checkDefaultColorChooser(JButtonOperator jbo, Color testColor, BezierColor bezierColor)
184             throws Exception {
185         BezierAnimationPanel bezierPanel;
186         pushButtonAndInitialize(jbo);
187         // Check ColorChooser color is being set and used accordingly
188         // in the animation panel
189         setAndWaitColor(testColor);
190         pushButtonAndWaitDialogClosed(okButton);
191         bezierPanel = (BezierAnimationPanel) bezierAnimationPanel.getSource();
192         colorChooser.waitStateOnQueue(jColorChooser -> (bezierPanel.getBezierColor(bezierColor).equals(testColor)));
193     }
194 
checkCancelButton(JButtonOperator jbo, Color testColor)195     private void checkCancelButton(JButtonOperator jbo, Color testColor) throws Exception {
196         pushButtonAndInitialize(jbo);
197         setAndWaitColor(testColor);
198         pushButtonAndWaitDialogClosed(cancelButton);
199     }
200 
checkResetButton(JButtonOperator jbo, Color testColor)201     private void checkResetButton(JButtonOperator jbo, Color testColor) throws Exception {
202         pushButtonAndInitialize(jbo);
203         Color initialColor = colorChooser.getColor();
204         setAndWaitColor(testColor);
205         resetButton.push();
206         waitJColorChooserColor(initialColor);
207         pushButtonAndWaitDialogClosed(okButton);
208     }
209 
checkAllColorChoosers(JButtonOperator jbo)210     private void checkAllColorChoosers(JButtonOperator jbo) throws Exception {
211         pushButtonAndInitialize(jbo);
212         checkHSV();
213         checkHSL();
214         checkRGB();
215         checkCMYK();
216         pushButtonAndWaitDialogClosed(okButton);
217     }
218 
waitJColorChooserColor(Color expectedColor)219     private void waitJColorChooserColor(Color expectedColor) {
220         colorChooser.waitStateOnQueue(jColorChooser -> colorChooser.getColor().equals(expectedColor));
221     }
222 
setAndWaitColor(Color color)223     private void setAndWaitColor(Color color) {
224         colorChooser.setColor(color);
225         // Wait for the Color to be set
226         waitJColorChooserColor(color);
227     }
228 
resetColor()229     private void resetColor() {
230         colorChooser.setColor(resetColor);
231         // Wait for the Color to be reset
232         waitJColorChooserColor(resetColor);
233     }
234 
checkHSV()235     private void checkHSV() {
236         tabOperator.selectPage(HSV);
237         initializeSliderAndSpinner(HSV_NUMBER_OF_SLIDERS_AND_SPINNERS);
238         resetColor();
239         setAndCheckSlider(sliders[HSV_SATURATION_INDEX], 50, new Color(125, 62, 62));
240         setAndCheckSlider(sliders[HSV_VALUE_INDEX], 80, new Color(204, 102, 102));
241         setAndCheckSlider(sliders[HSV_HUE_INDEX], 50, new Color(204, 187, 102));
242         setAndCheckSlider(sliders[HSV_TRANSPARENCY_INDEX], 50, new Color(204, 187, 102, 127));
243         setAndCheckSpinner(spinners[HSV_SATURATION_INDEX], 25, new Color(204, 195, 153, 127));
244         setAndCheckSpinner(spinners[HSV_VALUE_INDEX], 40, new Color(102, 97, 76, 127));
245         setAndCheckSpinner(spinners[HSV_HUE_INDEX], 25, new Color(102, 87, 76, 127));
246         setAndCheckSpinner(spinners[HSV_TRANSPARENCY_INDEX], 100, new Color(102, 87, 76, 0));
247     }
248 
checkHSL()249     private void checkHSL() {
250         tabOperator.selectPage(HSL);
251         initializeSliderAndSpinner(HSL_NUMBER_OF_SLIDERS_AND_SPINNERS);
252         resetColor();
253         setAndCheckSlider(sliders[HSL_SATURATION_INDEX], 50, new Color(187, 62, 62));
254         setAndCheckSlider(sliders[HSL_LIGHTNESS_INDEX], 80, new Color(229, 178, 178));
255         setAndCheckSlider(sliders[HSL_HUE_INDEX], 180, new Color(178, 229, 229));
256         setAndCheckSlider(sliders[HSL_TRANSPARENCY_INDEX], 50, new Color(178, 229, 229, 127));
257         setAndCheckSpinner(spinners[HSL_SATURATION_INDEX], 25, new Color(191, 216, 216, 127));
258         setAndCheckSpinner(spinners[HSL_LIGHTNESS_INDEX], 40, new Color(76, 127, 127, 127));
259         setAndCheckSpinner(spinners[HSL_HUE_INDEX], 25, new Color(127, 97, 76, 127));
260         setAndCheckSpinner(spinners[HSL_TRANSPARENCY_INDEX], 50, new Color(127, 97, 76, 127));
261     }
262 
checkRGB()263     private void checkRGB() {
264         String sampleColor = "111111";
265         tabOperator.selectPage(RGB);
266         initializeSliderAndSpinner(RGB_NUMBER_OF_SLIDERS_AND_SPINNERS);
267         JTextFieldOperator colorCode = new JTextFieldOperator(colorChooserDialog, RGB_COLORCODE_TEXT_FIELD_INDEX);
268         resetColor();
269         setAndCheckSlider(sliders[RGB_GREEN_INDEX], 50, new Color(125, 50, 125, 255));
270         setAndCheckSlider(sliders[RGB_BLUE_INDEX], 80, new Color(125, 50, 80, 255));
271         setAndCheckSlider(sliders[RGB_RED_INDEX], 50, new Color(50, 50, 80, 255));
272         setAndCheckSlider(sliders[RGB_ALPHA_INDEX], 125, new Color(50, 50, 80, 125));
273         setAndCheckSpinner(spinners[RGB_GREEN_INDEX], 25, new Color(50, 25, 80, 125));
274         setAndCheckSpinner(spinners[RGB_BLUE_INDEX], 40, new Color(50, 25, 40, 125));
275         setAndCheckSpinner(spinners[RGB_RED_INDEX], 25, new Color(25, 25, 40, 125));
276         setAndCheckSpinner(spinners[RGB_ALPHA_INDEX], 255, new Color(25, 25, 40, 255));
277 
278         colorCode.setText(sampleColor);
279         // Wait for the sampleColor to be set in the color code text field.
280         colorCode.waitText(sampleColor);
281         colorCode.getFocus();
282         colorCode.pressKey(KeyEvent.VK_TAB);
283         // Wait for the color to be set
284         waitJColorChooserColor(new Color(17, 17, 17, 255));
285     }
286 
checkCMYK()287     private void checkCMYK() {
288         tabOperator.selectPage(CMYK);
289         initializeSliderAndSpinner(CMYK_NUMBER_OF_SLIDERS_AND_SPINNERS);
290         resetColor();
291         setAndCheckSlider(sliders[CMYK_MAGENTA_INDEX], 50, new Color(125, 100, 125, 255));
292         setAndCheckSlider(sliders[CMYK_YELLOW_INDEX], 80, new Color(125, 100, 85, 255));
293         setAndCheckSlider(sliders[CMYK_CYAN_INDEX], 50, new Color(100, 100, 85, 255));
294         setAndCheckSlider(sliders[CMYK_BLACK_INDEX], 50, new Color(164, 164, 140, 255));
295         setAndCheckSlider(sliders[CMYK_ALPHA_INDEX], 125, new Color(164, 164, 140, 125));
296         setAndCheckSpinner(spinners[CMYK_MAGENTA_INDEX], 25, new Color(164, 184, 140, 125));
297         setAndCheckSpinner(spinners[CMYK_YELLOW_INDEX], 40, new Color(164, 184, 172, 125));
298         setAndCheckSpinner(spinners[CMYK_CYAN_INDEX], 25, new Color(184, 184, 172, 125));
299         setAndCheckSpinner(spinners[CMYK_BLACK_INDEX], 100, new Color(139, 139, 130, 125));
300         setAndCheckSpinner(spinners[CMYK_ALPHA_INDEX], 255, new Color(139, 139, 130, 255));
301     }
302 
setAndCheckSlider(JSliderOperator slider, int sliderValue, Color expectedColor)303     private void setAndCheckSlider(JSliderOperator slider, int sliderValue, Color expectedColor) {
304         slider.setValue(sliderValue);
305         // Wait for slider to attain the specified value
306         slider.waitStateOnQueue(jSlider -> slider.getValue() == sliderValue);
307         colorChooser.waitStateOnQueue(jColorChooser -> (colorChooser.getColor().equals(expectedColor)));
308     }
309 
setAndCheckSpinner(JSpinnerOperator spinner, int spinnerValue, Color expectedColor)310     private void setAndCheckSpinner(JSpinnerOperator spinner, int spinnerValue, Color expectedColor) {
311         spinner.setValue(spinnerValue);
312         // Wait for spinner to attain the specified value
313         spinner.waitStateOnQueue(jSpinner -> (int) spinner.getValue() == spinnerValue);
314         colorChooser.waitStateOnQueue(jColorChooser -> (colorChooser.getColor().equals(expectedColor)));
315     }
316 
initializeSliderAndSpinner(int numberOfSlidersAndSpinners)317     private void initializeSliderAndSpinner(int numberOfSlidersAndSpinners) {
318         for (int i = 0; i < numberOfSlidersAndSpinners; i++) {
319             sliders[i] = new JSliderOperator(colorChooserDialog, i);
320             spinners[i] = new JSpinnerOperator(colorChooserDialog, i);
321         }
322     }
323 
pushButtonAndWaitDialogClosed(JButtonOperator button)324     private void pushButtonAndWaitDialogClosed(JButtonOperator button) {
325         button.push();
326         // Wait for the color chooser dialog to close.
327         colorChooserDialog.waitClosed();
328     }
329 }
330 
331