author | rfield |
Fri, 27 May 2016 14:45:20 -0700 | |
changeset 38618 | f14423456446 |
parent 37389 | 9c137b83a8b8 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
/* |
|
25 |
* @test |
|
38618
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
26 |
* @bug 8139872 |
33362 | 27 |
* @summary Testing built-in editor. |
38618
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
28 |
* @modules java.desktop/java.awt |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
29 |
* jdk.jshell/jdk.internal.jshell.tool |
33362 | 30 |
* @build ReplToolTesting EditorTestBase |
31 |
* @run testng EditorPadTest |
|
32 |
*/ |
|
33 |
||
34 |
import java.awt.AWTException; |
|
35 |
import java.awt.Component; |
|
36 |
import java.awt.Container; |
|
37 |
import java.awt.Dimension; |
|
38 |
import java.awt.Frame; |
|
38618
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
39 |
import java.awt.GraphicsEnvironment; |
33362 | 40 |
import java.awt.Point; |
41 |
import java.awt.Robot; |
|
42 |
import java.awt.event.InputEvent; |
|
43 |
import java.awt.event.WindowEvent; |
|
44 |
import java.lang.reflect.InvocationTargetException; |
|
45 |
import java.util.concurrent.ExecutionException; |
|
46 |
import java.util.concurrent.Future; |
|
47 |
import java.util.function.Consumer; |
|
48 |
||
49 |
import javax.swing.JButton; |
|
50 |
import javax.swing.JFrame; |
|
51 |
import javax.swing.JPanel; |
|
52 |
import javax.swing.JScrollPane; |
|
53 |
import javax.swing.JTextArea; |
|
54 |
import javax.swing.JViewport; |
|
55 |
import javax.swing.SwingUtilities; |
|
56 |
||
57 |
import org.testng.annotations.AfterClass; |
|
58 |
import org.testng.annotations.BeforeClass; |
|
59 |
import org.testng.annotations.Test; |
|
60 |
||
61 |
public class EditorPadTest extends EditorTestBase { |
|
62 |
||
63 |
private static final int DELAY = 500; |
|
64 |
||
65 |
private static Robot robot; |
|
66 |
private static JFrame frame = null; |
|
67 |
private static JTextArea area = null; |
|
68 |
private static JButton cancel = null; |
|
69 |
private static JButton accept = null; |
|
70 |
private static JButton exit = null; |
|
71 |
||
38618
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
72 |
// Do not actually run if we are headless |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
73 |
@Override |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
74 |
public void testEditor(boolean defaultStartup, String[] args, ReplTest... tests) { |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
75 |
if (!GraphicsEnvironment.isHeadless()) { |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
76 |
test(defaultStartup, args, tests); |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
77 |
} |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
78 |
} |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
79 |
|
33362 | 80 |
@BeforeClass |
37389 | 81 |
public static void setUpEditorPadTest() { |
38618
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
82 |
if (!GraphicsEnvironment.isHeadless()) { |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
83 |
try { |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
84 |
robot = new Robot(); |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
85 |
robot.setAutoWaitForIdle(true); |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
86 |
robot.setAutoDelay(DELAY); |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
87 |
} catch (AWTException e) { |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
88 |
throw new ExceptionInInitializerError(e); |
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
89 |
} |
33362 | 90 |
} |
91 |
} |
|
92 |
||
93 |
@AfterClass |
|
94 |
public static void shutdown() { |
|
95 |
executorShutdown(); |
|
96 |
} |
|
97 |
||
98 |
@Override |
|
99 |
public void writeSource(String s) { |
|
100 |
SwingUtilities.invokeLater(() -> area.setText(s)); |
|
101 |
} |
|
102 |
||
103 |
@Override |
|
104 |
public String getSource() { |
|
105 |
try { |
|
106 |
String[] s = new String[1]; |
|
107 |
SwingUtilities.invokeAndWait(() -> s[0] = area.getText()); |
|
108 |
return s[0]; |
|
109 |
} catch (InvocationTargetException | InterruptedException e) { |
|
110 |
throw new RuntimeException(e); |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
@Override |
|
115 |
public void accept() { |
|
116 |
clickOn(accept); |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
120 |
public void exit() { |
|
121 |
clickOn(exit); |
|
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
public void cancel() { |
|
126 |
clickOn(cancel); |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
public void shutdownEditor() { |
|
131 |
SwingUtilities.invokeLater(this::clearElements); |
|
132 |
waitForIdle(); |
|
133 |
} |
|
134 |
||
135 |
@Test |
|
136 |
public void testShuttingDown() { |
|
137 |
testEditor( |
|
38618
f14423456446
8139872: JShell tests: EditorPadTest failing on headless
rfield
parents:
37389
diff
changeset
|
138 |
(a) -> assertEditOutput(a, "/ed", "", this::shutdownEditor) |
33362 | 139 |
); |
140 |
} |
|
141 |
||
142 |
private void waitForIdle() { |
|
143 |
robot.waitForIdle(); |
|
144 |
robot.delay(DELAY); |
|
145 |
} |
|
146 |
||
147 |
private Future<?> task; |
|
148 |
@Override |
|
149 |
public void assertEdit(boolean after, String cmd, |
|
150 |
Consumer<String> checkInput, Consumer<String> checkOutput, Action action) { |
|
151 |
if (!after) { |
|
152 |
setCommandInput(cmd + "\n"); |
|
153 |
task = getExecutor().submit(() -> { |
|
154 |
try { |
|
155 |
waitForIdle(); |
|
156 |
SwingUtilities.invokeLater(this::seekElements); |
|
157 |
waitForIdle(); |
|
158 |
checkInput.accept(getSource()); |
|
159 |
action.accept(); |
|
160 |
} catch (Throwable e) { |
|
161 |
shutdownEditor(); |
|
162 |
if (e instanceof AssertionError) { |
|
163 |
throw (AssertionError) e; |
|
164 |
} |
|
165 |
throw new RuntimeException(e); |
|
166 |
} |
|
167 |
}); |
|
168 |
} else { |
|
169 |
try { |
|
170 |
task.get(); |
|
171 |
waitForIdle(); |
|
172 |
checkOutput.accept(getCommandOutput()); |
|
173 |
} catch (ExecutionException e) { |
|
174 |
if (e.getCause() instanceof AssertionError) { |
|
175 |
throw (AssertionError) e.getCause(); |
|
176 |
} |
|
177 |
throw new RuntimeException(e); |
|
178 |
} catch (Exception e) { |
|
179 |
throw new RuntimeException(e); |
|
180 |
} finally { |
|
181 |
shutdownEditor(); |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
private void seekElements() { |
|
187 |
for (Frame f : Frame.getFrames()) { |
|
188 |
if (f.getTitle().contains("Edit Pad")) { |
|
189 |
frame = (JFrame) f; |
|
190 |
// workaround |
|
191 |
frame.setLocation(0, 0); |
|
192 |
Container root = frame.getContentPane(); |
|
193 |
for (Component c : root.getComponents()) { |
|
194 |
if (c instanceof JScrollPane) { |
|
195 |
JScrollPane scrollPane = (JScrollPane) c; |
|
196 |
for (Component comp : scrollPane.getComponents()) { |
|
197 |
if (comp instanceof JViewport) { |
|
198 |
JViewport view = (JViewport) comp; |
|
199 |
area = (JTextArea) view.getComponent(0); |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
if (c instanceof JPanel) { |
|
204 |
JPanel p = (JPanel) c; |
|
205 |
for (Component comp : p.getComponents()) { |
|
206 |
if (comp instanceof JButton) { |
|
207 |
JButton b = (JButton) comp; |
|
208 |
switch (b.getText()) { |
|
209 |
case "Cancel": |
|
210 |
cancel = b; |
|
211 |
break; |
|
212 |
case "Exit": |
|
213 |
exit = b; |
|
214 |
break; |
|
215 |
case "Accept": |
|
216 |
accept = b; |
|
217 |
break; |
|
218 |
} |
|
219 |
} |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
private void clearElements() { |
|
228 |
if (frame != null) { |
|
229 |
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); |
|
230 |
frame = null; |
|
231 |
} |
|
232 |
area = null; |
|
233 |
accept = null; |
|
234 |
cancel = null; |
|
235 |
exit = null; |
|
236 |
} |
|
237 |
||
238 |
private void clickOn(JButton button) { |
|
239 |
waitForIdle(); |
|
240 |
Point p = button.getLocationOnScreen(); |
|
241 |
Dimension d = button.getSize(); |
|
242 |
robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2); |
|
243 |
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
|
244 |
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
|
245 |
} |
|
246 |
} |