mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 10:34:38 +02:00
8167636: jshell tool: Edit Pad should be in its own module
Reviewed-by: jlahoda
This commit is contained in:
parent
5ff1a63a1c
commit
02e5b77101
7 changed files with 62 additions and 562 deletions
|
@ -29,7 +29,6 @@ import jdk.jshell.SourceCodeAnalysis.Documentation;
|
|||
import jdk.jshell.SourceCodeAnalysis.QualifiedNames;
|
||||
import jdk.jshell.SourceCodeAnalysis.Suggestion;
|
||||
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InterruptedIOException;
|
||||
|
@ -171,10 +170,10 @@ class ConsoleIOContext extends IOContext {
|
|||
return anchor[0];
|
||||
}
|
||||
});
|
||||
bind(DOCUMENTATION_SHORTCUT, (ActionListener) evt -> documentation(repl));
|
||||
bind(DOCUMENTATION_SHORTCUT, (Runnable) () -> documentation(repl));
|
||||
for (FixComputer computer : FIX_COMPUTERS) {
|
||||
for (String shortcuts : SHORTCUT_FIXES) {
|
||||
bind(shortcuts + computer.shortcut, (ActionListener) evt -> fixes(computer));
|
||||
bind(shortcuts + computer.shortcut, (Runnable) () -> fixes(computer));
|
||||
}
|
||||
}
|
||||
try {
|
||||
|
|
|
@ -1,129 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package jdk.internal.jshell.tool;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.function.Consumer;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
/**
|
||||
* A minimal Swing editor as a fallback when the user does not specify an
|
||||
* external editor.
|
||||
*/
|
||||
@SuppressWarnings("serial") // serialVersionUID intentionally omitted
|
||||
public class EditPad extends JFrame implements Runnable {
|
||||
private final Consumer<String> errorHandler; // For possible future error handling
|
||||
private final String initialText;
|
||||
private final CountDownLatch closeLock;
|
||||
private final Consumer<String> saveHandler;
|
||||
|
||||
EditPad(Consumer<String> errorHandler, String initialText,
|
||||
CountDownLatch closeLock, Consumer<String> saveHandler) {
|
||||
super("JShell Edit Pad");
|
||||
this.errorHandler = errorHandler;
|
||||
this.initialText = initialText;
|
||||
this.closeLock = closeLock;
|
||||
this.saveHandler = saveHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
EditPad.this.dispose();
|
||||
closeLock.countDown();
|
||||
}
|
||||
});
|
||||
setLocationRelativeTo(null);
|
||||
setLayout(new BorderLayout());
|
||||
JTextArea textArea = new JTextArea(initialText);
|
||||
add(new JScrollPane(textArea), BorderLayout.CENTER);
|
||||
add(buttons(textArea), BorderLayout.SOUTH);
|
||||
|
||||
setSize(800, 600);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private JPanel buttons(JTextArea textArea) {
|
||||
FlowLayout flow = new FlowLayout();
|
||||
flow.setHgap(35);
|
||||
JPanel buttons = new JPanel(flow);
|
||||
JButton cancel = new JButton("Cancel");
|
||||
cancel.setMnemonic(KeyEvent.VK_C);
|
||||
JButton accept = new JButton("Accept");
|
||||
accept.setMnemonic(KeyEvent.VK_A);
|
||||
JButton exit = new JButton("Exit");
|
||||
exit.setMnemonic(KeyEvent.VK_X);
|
||||
buttons.add(cancel);
|
||||
buttons.add(accept);
|
||||
buttons.add(exit);
|
||||
|
||||
cancel.addActionListener(e -> {
|
||||
close();
|
||||
});
|
||||
accept.addActionListener(e -> {
|
||||
saveHandler.accept(textArea.getText());
|
||||
});
|
||||
exit.addActionListener(e -> {
|
||||
saveHandler.accept(textArea.getText());
|
||||
close();
|
||||
});
|
||||
|
||||
return buttons;
|
||||
}
|
||||
|
||||
private void close() {
|
||||
setVisible(false);
|
||||
dispose();
|
||||
closeLock.countDown();
|
||||
}
|
||||
|
||||
public static void edit(Consumer<String> errorHandler, String initialText,
|
||||
Consumer<String> saveHandler) {
|
||||
CountDownLatch closeLock = new CountDownLatch(1);
|
||||
SwingUtilities.invokeLater(
|
||||
new EditPad(errorHandler, initialText, closeLock, saveHandler));
|
||||
do {
|
||||
try {
|
||||
closeLock.await();
|
||||
break;
|
||||
} catch (InterruptedException ex) {
|
||||
// ignore and loop
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
}
|
|
@ -1,170 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package jdk.internal.jshell.tool;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.ClosedWatchServiceException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
|
||||
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
|
||||
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
|
||||
|
||||
/**
|
||||
* Wrapper for controlling an external editor.
|
||||
*/
|
||||
public class ExternalEditor {
|
||||
private final Consumer<String> errorHandler;
|
||||
private final Consumer<String> saveHandler;
|
||||
private final Consumer<String> printHandler;
|
||||
private final IOContext input;
|
||||
private final boolean wait;
|
||||
|
||||
private WatchService watcher;
|
||||
private Thread watchedThread;
|
||||
private Path dir;
|
||||
private Path tmpfile;
|
||||
|
||||
ExternalEditor(Consumer<String> errorHandler, Consumer<String> saveHandler,
|
||||
IOContext input, boolean wait, Consumer<String> printHandler) {
|
||||
this.errorHandler = errorHandler;
|
||||
this.saveHandler = saveHandler;
|
||||
this.printHandler = printHandler;
|
||||
this.input = input;
|
||||
this.wait = wait;
|
||||
}
|
||||
|
||||
private void edit(String[] cmd, String initialText) {
|
||||
try {
|
||||
setupWatch(initialText);
|
||||
launch(cmd);
|
||||
} catch (IOException ex) {
|
||||
errorHandler.accept(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a WatchService and registers the given directory
|
||||
*/
|
||||
private void setupWatch(String initialText) throws IOException {
|
||||
this.watcher = FileSystems.getDefault().newWatchService();
|
||||
this.dir = Files.createTempDirectory("jshelltemp");
|
||||
this.tmpfile = Files.createTempFile(dir, null, ".java");
|
||||
Files.write(tmpfile, initialText.getBytes(Charset.forName("UTF-8")));
|
||||
dir.register(watcher,
|
||||
ENTRY_CREATE,
|
||||
ENTRY_DELETE,
|
||||
ENTRY_MODIFY);
|
||||
watchedThread = new Thread(() -> {
|
||||
for (;;) {
|
||||
WatchKey key;
|
||||
try {
|
||||
key = watcher.take();
|
||||
} catch (ClosedWatchServiceException ex) {
|
||||
// The watch service has been closed, we are done
|
||||
break;
|
||||
} catch (InterruptedException ex) {
|
||||
// tolerate an interrupt
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!key.pollEvents().isEmpty()) {
|
||||
// Changes have occurred in temp edit directory,
|
||||
// transfer the new sources to JShell (unless the editor is
|
||||
// running directly in JShell's window -- don't make a mess)
|
||||
if (!input.terminalEditorRunning()) {
|
||||
saveFile();
|
||||
}
|
||||
}
|
||||
|
||||
boolean valid = key.reset();
|
||||
if (!valid) {
|
||||
// The watch service has been closed, we are done
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
watchedThread.start();
|
||||
}
|
||||
|
||||
private void launch(String[] cmd) throws IOException {
|
||||
String[] params = Arrays.copyOf(cmd, cmd.length + 1);
|
||||
params[cmd.length] = tmpfile.toString();
|
||||
ProcessBuilder pb = new ProcessBuilder(params);
|
||||
pb = pb.inheritIO();
|
||||
|
||||
try {
|
||||
input.suspend();
|
||||
Process process = pb.start();
|
||||
// wait to exit edit mode in one of these ways...
|
||||
if (wait) {
|
||||
// -wait option -- ignore process exit, wait for carriage-return
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
printHandler.accept("jshell.msg.press.return.to.leave.edit.mode");
|
||||
scanner.nextLine();
|
||||
} else {
|
||||
// wait for process to exit
|
||||
process.waitFor();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
errorHandler.accept("process IO failure: " + ex.getMessage());
|
||||
} catch (InterruptedException ex) {
|
||||
errorHandler.accept("process interrupt: " + ex.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
watcher.close();
|
||||
watchedThread.join(); //so that saveFile() is finished.
|
||||
saveFile();
|
||||
} catch (InterruptedException ex) {
|
||||
errorHandler.accept("process interrupt: " + ex.getMessage());
|
||||
} finally {
|
||||
input.resume();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void saveFile() {
|
||||
try {
|
||||
saveHandler.accept(Files.lines(tmpfile).collect(Collectors.joining("\n", "", "\n")));
|
||||
} catch (IOException ex) {
|
||||
errorHandler.accept("Failure in read edit file: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
static void edit(String[] cmd, Consumer<String> errorHandler, String initialText,
|
||||
Consumer<String> saveHandler, IOContext input, boolean wait, Consumer<String> printHandler) {
|
||||
ExternalEditor ed = new ExternalEditor(errorHandler, saveHandler, input, wait, printHandler);
|
||||
ed.edit(cmd, initialText);
|
||||
}
|
||||
}
|
|
@ -89,6 +89,7 @@ import static java.nio.file.StandardOpenOption.WRITE;
|
|||
import java.util.MissingResourceException;
|
||||
import java.util.Optional;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Spliterators;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
@ -99,6 +100,8 @@ import jdk.internal.jshell.tool.Feedback.FormatErrors;
|
|||
import jdk.internal.jshell.tool.Feedback.FormatResolve;
|
||||
import jdk.internal.jshell.tool.Feedback.FormatUnresolved;
|
||||
import jdk.internal.jshell.tool.Feedback.FormatWhen;
|
||||
import jdk.internal.editor.spi.BuildInEditorProvider;
|
||||
import jdk.internal.editor.external.ExternalEditor;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Arrays.stream;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
@ -323,7 +326,7 @@ public class JShellTool implements MessageHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Print using resource bundle look-up and adding prefix and postfix
|
||||
* Resource bundle look-up
|
||||
*
|
||||
* @param key the resource key
|
||||
*/
|
||||
|
@ -1978,20 +1981,59 @@ public class JShellTool implements MessageHandler {
|
|||
Consumer<String> saveHandler = new SaveHandler(src, srcSet);
|
||||
Consumer<String> errorHandler = s -> hard("Edit Error: %s", s);
|
||||
if (editor == BUILT_IN_EDITOR) {
|
||||
try {
|
||||
EditPad.edit(errorHandler, src, saveHandler);
|
||||
} catch (RuntimeException ex) {
|
||||
errormsg("jshell.err.cant.launch.editor", ex);
|
||||
fluffmsg("jshell.msg.try.set.editor");
|
||||
return false;
|
||||
}
|
||||
return builtInEdit(src, saveHandler, errorHandler);
|
||||
} else {
|
||||
ExternalEditor.edit(editor.cmd, errorHandler, src, saveHandler, input,
|
||||
editor.wait, this::hardrb);
|
||||
// Changes have occurred in temp edit directory,
|
||||
// transfer the new sources to JShell (unless the editor is
|
||||
// running directly in JShell's window -- don't make a mess)
|
||||
String[] buffer = new String[1];
|
||||
Consumer<String> extSaveHandler = s -> {
|
||||
if (input.terminalEditorRunning()) {
|
||||
buffer[0] = s;
|
||||
} else {
|
||||
saveHandler.accept(s);
|
||||
}
|
||||
};
|
||||
ExternalEditor.edit(editor.cmd, src,
|
||||
errorHandler, extSaveHandler,
|
||||
() -> input.suspend(),
|
||||
() -> input.resume(),
|
||||
editor.wait,
|
||||
() -> hardrb("jshell.msg.press.return.to.leave.edit.mode"));
|
||||
if (buffer[0] != null) {
|
||||
saveHandler.accept(buffer[0]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//where
|
||||
// start the built-in editor
|
||||
private boolean builtInEdit(String initialText,
|
||||
Consumer<String> saveHandler, Consumer<String> errorHandler) {
|
||||
try {
|
||||
ServiceLoader<BuildInEditorProvider> sl
|
||||
= ServiceLoader.load(BuildInEditorProvider.class);
|
||||
// Find the highest ranking provider
|
||||
BuildInEditorProvider provider = null;
|
||||
for (BuildInEditorProvider p : sl) {
|
||||
if (provider == null || p.rank() > provider.rank()) {
|
||||
provider = p;
|
||||
}
|
||||
}
|
||||
if (provider != null) {
|
||||
provider.edit(getResourceString("jshell.label.editpad"),
|
||||
initialText, saveHandler, errorHandler);
|
||||
return true;
|
||||
} else {
|
||||
errormsg("jshell.err.no.builtin.editor");
|
||||
}
|
||||
} catch (RuntimeException ex) {
|
||||
errormsg("jshell.err.cant.launch.editor", ex);
|
||||
}
|
||||
fluffmsg("jshell.msg.try.set.editor");
|
||||
return false;
|
||||
}
|
||||
//where
|
||||
// receives editor requests to save
|
||||
private class SaveHandler implements Consumer<String> {
|
||||
|
||||
|
|
|
@ -54,10 +54,12 @@ jshell.err.no.such.command.or.snippet.id = No such command or snippet id: {0}
|
|||
jshell.err.command.ambiguous = Command: ''{0}'' is ambiguous: {1}
|
||||
jshell.msg.set.editor.set = Editor set to: {0}
|
||||
jshell.msg.set.editor.retain = Editor setting retained: {0}
|
||||
jshell.err.cant.launch.editor = Cannot launch editor -- unexpected exception: {0}
|
||||
jshell.msg.try.set.editor = Try /set editor to use external editor.
|
||||
jshell.err.no.builtin.editor = Built-in editor not available.
|
||||
jshell.err.cant.launch.editor = Cannot launch built-in editor -- unexpected exception: {0}
|
||||
jshell.msg.try.set.editor = See ''/help /set editor'' to use external editor.
|
||||
jshell.msg.press.return.to.leave.edit.mode = Press return to leave edit mode.
|
||||
jshell.err.wait.applies.to.external.editor = -wait applies to external editors
|
||||
jshell.label.editpad = JShell Edit Pad
|
||||
|
||||
jshell.err.setting.to.retain.must.be.specified = The setting to retain must be specified -- {0}
|
||||
jshell.msg.set.show.mode.settings = \nTo show mode settings use ''/set prompt'', ''/set truncation'', ...\n\
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -30,14 +30,16 @@
|
|||
*/
|
||||
module jdk.jshell {
|
||||
requires public java.compiler;
|
||||
requires java.desktop;
|
||||
requires java.prefs;
|
||||
requires jdk.compiler;
|
||||
requires jdk.internal.le;
|
||||
requires jdk.internal.ed;
|
||||
requires jdk.internal.opt;
|
||||
requires jdk.jdi;
|
||||
|
||||
exports jdk.jshell;
|
||||
exports jdk.jshell.spi;
|
||||
exports jdk.jshell.execution;
|
||||
|
||||
uses jdk.internal.editor.spi.BuildInEditorProvider;
|
||||
}
|
||||
|
|
|
@ -1,246 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8139872
|
||||
* @summary Testing built-in editor.
|
||||
* @modules java.desktop/java.awt
|
||||
* jdk.jshell/jdk.internal.jshell.tool
|
||||
* @build ReplToolTesting EditorTestBase
|
||||
* @run testng EditorPadTest
|
||||
*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Component;
|
||||
import java.awt.Container;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Frame;
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.awt.Point;
|
||||
import java.awt.Robot;
|
||||
import java.awt.event.InputEvent;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JViewport;
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class EditorPadTest extends EditorTestBase {
|
||||
|
||||
private static final int DELAY = 500;
|
||||
|
||||
private static Robot robot;
|
||||
private static JFrame frame = null;
|
||||
private static JTextArea area = null;
|
||||
private static JButton cancel = null;
|
||||
private static JButton accept = null;
|
||||
private static JButton exit = null;
|
||||
|
||||
// Do not actually run if we are headless
|
||||
@Override
|
||||
public void testEditor(boolean defaultStartup, String[] args, ReplTest... tests) {
|
||||
if (!GraphicsEnvironment.isHeadless()) {
|
||||
test(defaultStartup, args, tests);
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpEditorPadTest() {
|
||||
if (!GraphicsEnvironment.isHeadless()) {
|
||||
try {
|
||||
robot = new Robot();
|
||||
robot.setAutoWaitForIdle(true);
|
||||
robot.setAutoDelay(DELAY);
|
||||
} catch (AWTException e) {
|
||||
throw new ExceptionInInitializerError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void shutdown() {
|
||||
executorShutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSource(String s) {
|
||||
SwingUtilities.invokeLater(() -> area.setText(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSource() {
|
||||
try {
|
||||
String[] s = new String[1];
|
||||
SwingUtilities.invokeAndWait(() -> s[0] = area.getText());
|
||||
return s[0];
|
||||
} catch (InvocationTargetException | InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept() {
|
||||
clickOn(accept);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exit() {
|
||||
clickOn(exit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
clickOn(cancel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdownEditor() {
|
||||
SwingUtilities.invokeLater(this::clearElements);
|
||||
waitForIdle();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShuttingDown() {
|
||||
testEditor(
|
||||
(a) -> assertEditOutput(a, "/ed", "", this::shutdownEditor)
|
||||
);
|
||||
}
|
||||
|
||||
private void waitForIdle() {
|
||||
robot.waitForIdle();
|
||||
robot.delay(DELAY);
|
||||
}
|
||||
|
||||
private Future<?> task;
|
||||
@Override
|
||||
public void assertEdit(boolean after, String cmd,
|
||||
Consumer<String> checkInput, Consumer<String> checkOutput, Action action) {
|
||||
if (!after) {
|
||||
setCommandInput(cmd + "\n");
|
||||
task = getExecutor().submit(() -> {
|
||||
try {
|
||||
waitForIdle();
|
||||
SwingUtilities.invokeLater(this::seekElements);
|
||||
waitForIdle();
|
||||
checkInput.accept(getSource());
|
||||
action.accept();
|
||||
} catch (Throwable e) {
|
||||
shutdownEditor();
|
||||
if (e instanceof AssertionError) {
|
||||
throw (AssertionError) e;
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
task.get();
|
||||
waitForIdle();
|
||||
checkOutput.accept(getCommandOutput());
|
||||
} catch (ExecutionException e) {
|
||||
if (e.getCause() instanceof AssertionError) {
|
||||
throw (AssertionError) e.getCause();
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
shutdownEditor();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void seekElements() {
|
||||
for (Frame f : Frame.getFrames()) {
|
||||
if (f.getTitle().contains("Edit Pad")) {
|
||||
frame = (JFrame) f;
|
||||
// workaround
|
||||
frame.setLocation(0, 0);
|
||||
Container root = frame.getContentPane();
|
||||
for (Component c : root.getComponents()) {
|
||||
if (c instanceof JScrollPane) {
|
||||
JScrollPane scrollPane = (JScrollPane) c;
|
||||
for (Component comp : scrollPane.getComponents()) {
|
||||
if (comp instanceof JViewport) {
|
||||
JViewport view = (JViewport) comp;
|
||||
area = (JTextArea) view.getComponent(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (c instanceof JPanel) {
|
||||
JPanel p = (JPanel) c;
|
||||
for (Component comp : p.getComponents()) {
|
||||
if (comp instanceof JButton) {
|
||||
JButton b = (JButton) comp;
|
||||
switch (b.getText()) {
|
||||
case "Cancel":
|
||||
cancel = b;
|
||||
break;
|
||||
case "Exit":
|
||||
exit = b;
|
||||
break;
|
||||
case "Accept":
|
||||
accept = b;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void clearElements() {
|
||||
if (frame != null) {
|
||||
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
|
||||
frame = null;
|
||||
}
|
||||
area = null;
|
||||
accept = null;
|
||||
cancel = null;
|
||||
exit = null;
|
||||
}
|
||||
|
||||
private void clickOn(JButton button) {
|
||||
waitForIdle();
|
||||
Point p = button.getLocationOnScreen();
|
||||
Dimension d = button.getSize();
|
||||
robot.mouseMove(p.x + d.width / 2, p.y + d.height / 2);
|
||||
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
|
||||
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue