1 /*
2  * Copyright (c) 2011, 2018, 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 org.jtregext.GuiTestListener;
25 import com.sun.swingset3.demos.JHyperlink;
26 import com.sun.swingset3.demos.button.ButtonDemo;
27 import java.util.concurrent.ArrayBlockingQueue;
28 import java.util.concurrent.BlockingQueue;
29 import javax.swing.ButtonModel;
30 import javax.swing.JButton;
31 import javax.swing.UIManager;
32 import javax.swing.event.ChangeEvent;
33 import static org.testng.AssertJUnit.*;
34 import org.testng.annotations.Test;
35 import org.jemmy2ext.JemmyExt.ByToolTipChooser;
36 import static org.jemmy2ext.JemmyExt.EXACT_STRING_COMPARATOR;
37 import org.netbeans.jemmy.ClassReference;
38 import org.netbeans.jemmy.operators.JButtonOperator;
39 import org.netbeans.jemmy.operators.JFrameOperator;
40 import static com.sun.swingset3.demos.button.ButtonDemo.*;
41 import org.jemmy2ext.JemmyExt;
42 import org.jemmy2ext.JemmyExt.MultiThreadedTryCatch;
43 import org.testng.annotations.Listeners;
44 
45 /*
46  * @test
47  * @key headful
48  * @summary Verifies buttons on SwingSet3 ButtonDemo page by clicking each button
49  *          and checking model change events. It also verifies tooltips and text
50  *          on buttons before and after click.
51  *
52  * @library /sanity/client/lib/jemmy/src
53  * @library /sanity/client/lib/Extensions/src
54  * @library /sanity/client/lib/SwingSet3/src
55  * @modules java.desktop
56  *          java.logging
57  * @build org.jemmy2ext.JemmyExt
58  * @build com.sun.swingset3.demos.button.ButtonDemo
59  * @run testng/timeout=600 ButtonDemoTest
60  */
61 @Listeners(GuiTestListener.class)
62 public class ButtonDemoTest {
63 
64     private static final String[] BUTTON_TEXT_AFTER = {
65         DO_IT_AGAIN,};
66 
67     private static final String[] BUTTON_TEXT_BEFORE = {
68         DO_IT,
69         "",
70         FIND,
71         GO,
72         CONNECT,
73         "",
74         GET_MORE_INFO,
75         null
76     };
77 
78     private static final String[] BUTTON_TOOLTIP = {
79         SIMPLE_BUTTON,
80         IMAGE_BUTTON,
81         BUTTON_WITH_TEXT_AND_IMAGE,
82         BUTTON_WITH_BACKGROUND_COLOR,
83         BUTTON_WITH_NO_BORDER,
84         BUTTON_WITH_ROLLOVER_IMAGE,
85         JAVA_SE_URL,
86         JAVA_BLOGS_URL
87     };
88 
89     private static final String[] GOLDEN = {
90         "isArmed = false, isEnabled = true, isPressed = false, isSelected = false",
91         "isArmed = true, isEnabled = true, isPressed = false, isSelected = false",
92         "isArmed = true, isEnabled = true, isPressed = true, isSelected = false",
93         "isArmed = true, isEnabled = true, isPressed = false, isSelected = false",
94         "isArmed = false, isEnabled = true, isPressed = false, isSelected = false"
95     };
96 
97     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = TestHelpers.class)
test(String lookAndFeel)98     public void test(String lookAndFeel) throws Exception {
99         UIManager.setLookAndFeel(lookAndFeel);
100 
101         new ClassReference(ButtonDemo.class.getCanonicalName()).startApplication();
102 
103         JFrameOperator mainFrame = new JFrameOperator(DEMO_TITLE);
104         mainFrame.setComparator(EXACT_STRING_COMPARATOR);
105 
106         // Check all the buttons
107         for (int i = 0; i < BUTTON_TOOLTIP.length; i++) {
108             String tooltip = BUTTON_TOOLTIP[i];
109 
110             JButtonOperator button = new JButtonOperator(mainFrame, new ByToolTipChooser(tooltip));
111 
112             assertEquals(BUTTON_TEXT_BEFORE[i], button.getText());
113 
114             // Two buttons are hyperlinks, we don't want to click them
115             if (!button.getSource().getClass().equals(JHyperlink.class)) {
116                 checkButton(button);
117             }
118 
119             if (BUTTON_TEXT_AFTER.length > i) {
120                 assertEquals(BUTTON_TEXT_AFTER[i], button.getText());
121             } else {
122                 assertEquals(BUTTON_TEXT_BEFORE[i], button.getText());
123             }
124         }
125     }
126 
checkButton(JButtonOperator button)127     private void checkButton(JButtonOperator button) throws Exception {
128         MultiThreadedTryCatch tryCatch = new JemmyExt.MultiThreadedTryCatch();
129         try {
130             BlockingQueue<String> modelStateChanges = new ArrayBlockingQueue<>(GOLDEN.length);
131             button.getQueueTool().invokeAndWait(() -> {
132                 try {
133                     JButton jButton = (JButton) button.getSource();
134                     ButtonModel model = jButton.getModel();
135                     String line = toString(model);
136                     System.out.println("Inital: " + line);
137                     modelStateChanges.add(line);
138                     model.addChangeListener((ChangeEvent e) -> {
139                         try {
140                             String line2 = toString(model);
141                             System.out.println("ChangeEvent: " + line2);
142 
143                             // We are only interested in the first GOLDEN.length events
144                             if (modelStateChanges.remainingCapacity() > 0) {
145                                 modelStateChanges.add(line2);
146                             }
147                         } catch (RuntimeException | Error t) {
148                             tryCatch.register(t);
149                         }
150                     });
151                 } catch (Error error) {
152                     // All exceptions are already handled by Jemmy but Errors are not
153                     tryCatch.register(error);
154                     throw error;
155                 }
156             });
157 
158             assertEquals("Initial state check", GOLDEN[0], modelStateChanges.take());
159 
160             button.clickMouse();
161 
162             for (int state = 1; state < GOLDEN.length; state++) {
163                 assertEquals(GOLDEN[state], modelStateChanges.take());
164             }
165         } catch (RuntimeException | Error | InterruptedException t) {
166             tryCatch.registerRoot(t);
167         } finally {
168             tryCatch.throwRegistered();
169         }
170     }
171 
toString(ButtonModel model)172     private static String toString(ButtonModel model) {
173         return "isArmed = " + model.isArmed()
174                 + ", isEnabled = " + model.isEnabled()
175                 + ", isPressed = " + model.isPressed()
176                 + ", isSelected = " + model.isSelected();
177     }
178 
179 }
180