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