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