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