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