funcTests: playback refctoring

This commit is contained in:
Kirill Kalishev
2011-08-18 08:56:55 +04:00
parent de30404984
commit c774fdd050
15 changed files with 73 additions and 77 deletions
@@ -20,6 +20,6 @@ import com.intellij.openapi.util.ActionCallback;
import java.awt.*;
public interface PlaybackCommand {
ActionCallback execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean useDirectActionCall);
ActionCallback execute(PlaybackContext context);
boolean canGoFurther();
}
@@ -15,6 +15,8 @@
*/
package com.intellij.openapi.ui.playback;
import java.awt.*;
/**
* Created by IntelliJ IDEA.
* User: kirillk
@@ -26,10 +28,14 @@ public class PlaybackContext {
private PlaybackRunner.StatusCallback myCallback;
private int myCurrentLine;
private Robot myRobot;
private boolean myUseDirectActionCall;
public PlaybackContext(PlaybackRunner.StatusCallback callback, int currentLine) {
public PlaybackContext(PlaybackRunner.StatusCallback callback, int currentLine, Robot robot, boolean useDriectActionCall) {
myCallback = callback;
myCurrentLine = currentLine;
myRobot = robot;
myUseDirectActionCall = useDriectActionCall;
}
public PlaybackRunner.StatusCallback getCallback() {
@@ -39,4 +45,12 @@ public class PlaybackContext {
public int getCurrentLine() {
return myCurrentLine;
}
public Robot getRobot() {
return myRobot;
}
public boolean isUseDirectActionCall() {
return myUseDirectActionCall;
}
}
@@ -26,7 +26,6 @@ import com.intellij.util.text.StringTokenizer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@@ -92,7 +91,7 @@ public class PlaybackRunner {
myActionCallback.setRejected();
return;
}
final ActionCallback cmdCallback = cmd.execute(myCallback, myRobot, myUseDirectActionCall);
final ActionCallback cmdCallback = cmd.execute(new PlaybackContext(myCallback, cmdIndex, myRobot, myUseDirectActionCall));
cmdCallback.doWhenDone(new Runnable() {
public void run() {
if (cmd.canGoFurther()) {
@@ -17,11 +17,10 @@ package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.playback.PlaybackCommand;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import com.intellij.openapi.util.ActionCallback;
import java.awt.*;
public abstract class AbstractCommand implements PlaybackCommand {
public static String CMD_PREFIX = "%";
@@ -46,17 +45,17 @@ public abstract class AbstractCommand implements PlaybackCommand {
return true;
}
public final ActionCallback execute(final PlaybackRunner.StatusCallback cb, final Robot robot, final boolean useDirectActionCall) {
public final ActionCallback execute(final PlaybackContext context) {
try {
dumpCommand(cb);
dumpCommand(context.getCallback());
final ActionCallback result = new ActionCallback();
if (isAwtThread()) {
_execute(cb, robot, useDirectActionCall).notify(result);
_execute(context).notify(result);
} else {
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
@Override
public void run() {
_execute(cb, robot, useDirectActionCall).notify(result);
_execute(context).notify(result);
}
});
}
@@ -64,7 +63,7 @@ public abstract class AbstractCommand implements PlaybackCommand {
return result;
}
catch (Exception e) {
cb.error(e.getMessage(), getLine());
context.getCallback().error(e.getMessage(), getLine());
return new ActionCallback.Rejected();
}
}
@@ -73,7 +72,7 @@ public abstract class AbstractCommand implements PlaybackCommand {
return false;
}
protected abstract ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall);
protected abstract ActionCallback _execute(PlaybackContext context);
public void dumpCommand(final PlaybackRunner.StatusCallback cb) {
cb.message(getText(), getLine());
@@ -15,7 +15,7 @@
*/
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.actionSystem.AnAction;
@@ -25,7 +25,6 @@ import com.intellij.openapi.actionSystem.KeyboardShortcut;
import com.intellij.openapi.keymap.KeymapManager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
@@ -38,17 +37,17 @@ public class ActionCommand extends TypeCommand {
super(text, line);
}
protected ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
protected ActionCallback _execute(PlaybackContext context) {
final String actionName = getText().substring(PREFIX.length()).trim();
final AnAction action = ActionManager.getInstance().getAction(actionName);
if (action == null) {
dumpError(cb, "Unknown action: " + actionName);
dumpError(context.getCallback(), "Unknown action: " + actionName);
return new ActionCallback.Rejected();
}
if (!directActionCall) {
if (!context.isUseDirectActionCall()) {
final Shortcut[] sc = KeymapManager.getInstance().getActiveKeymap().getShortcuts(actionName);
KeyStroke stroke = null;
for (Shortcut each : sc) {
@@ -64,8 +63,8 @@ public class ActionCommand extends TypeCommand {
}
if (stroke != null) {
cb.message("Invoking action via shortcut: " + stroke.toString(), getLine());
type(robot, stroke);
context.getCallback().message("Invoking action via shortcut: " + stroke.toString(), getLine());
type(context.getRobot(), stroke);
return new ActionCallback.Done();
}
}
@@ -74,7 +73,7 @@ public class ActionCommand extends TypeCommand {
final ActionCallback result = new ActionCallback();
robot.delay(Registry.intValue("actionSystem.playback.autodelay"));
context.getRobot().delay(Registry.intValue("actionSystem.playback.autodelay"));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ActionManager.getInstance().tryToExecute(action, input, null, null, false).doWhenProcessed(new Runnable() {
@@ -16,8 +16,8 @@
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.TypingTarget;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import com.intellij.openapi.util.registry.Registry;
import org.jetbrains.annotations.Nullable;
@@ -30,8 +30,8 @@ public class AlphaNumericTypeCommand extends TypeCommand {
super(text, line);
}
public ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
return type(robot, getText());
public ActionCallback _execute(PlaybackContext context) {
return type(context.getRobot(), getText());
}
protected ActionCallback type(final Robot robot, final String text) {
@@ -16,6 +16,7 @@
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.Queryable;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.wm.IdeFocusManager;
@@ -31,7 +32,7 @@ public class AssertFocused extends AbstractCommand {
super(text, line);
}
protected ActionCallback _execute(final PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
protected ActionCallback _execute(final PlaybackContext context) {
final ActionCallback result = new ActionCallback();
String text = getText().substring(PREFIX.length()).trim();
@@ -42,7 +43,7 @@ public class AssertFocused extends AbstractCommand {
for (String each : keyValue) {
final String[] eachPair = each.split("=");
if (eachPair.length != 2) {
cb.error("Syntax error, must be comma-separated pairs key=value", getLine());
context.getCallback().error("Syntax error, must be comma-separated pairs key=value", getLine());
result.setRejected();
return result;
}
@@ -54,11 +55,11 @@ public class AssertFocused extends AbstractCommand {
IdeFocusManager.findInstance().doWhenFocusSettlesDown(new Runnable() {
public void run() {
try {
doAssert(expected, cb);
doAssert(expected, context.getCallback());
result.setDone();
}
catch (AssertionError error) {
cb.error("Assertion failed: " + error.getMessage(), getLine());
context.getCallback().error("Assertion failed: " + error.getMessage(), getLine());
result.setRejected();
}
}
@@ -17,11 +17,9 @@ package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackCallFacade;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.AsyncResult;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -41,19 +39,19 @@ public class CallCommand extends AbstractCommand {
}
@Override
protected ActionCallback _execute(final PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
protected ActionCallback _execute(final PlaybackContext context) {
final ActionCallback cmdResult = new ActionCallback();
final String cmd = getText().substring(PREFIX.length()).trim();
final int open = cmd.indexOf("(");
if (open == -1) {
cb.error("( expected", getLine());
context.getCallback().error("( expected", getLine());
return new ActionCallback.Done();
}
final int close = cmd.lastIndexOf(")");
if (close == -1) {
cb.error(") expected", getLine());
context.getCallback().error(") expected", getLine());
return new ActionCallback.Done();
}
@@ -71,12 +69,12 @@ public class CallCommand extends AbstractCommand {
try {
final Method m = PlaybackCallFacade.class.getMethod(methodName, types);
if (!m.getReturnType().isAssignableFrom(AsyncResult.class)) {
cb.error("Method " + methodName + " must return AsyncResult object", getLine());
context.getCallback().error("Method " + methodName + " must return AsyncResult object", getLine());
return new ActionCallback.Rejected();
}
Object[] actualArgs = noArgs ? new Object[1] : new Object[args.length + 1];
actualArgs[0] = new PlaybackContext(cb, getLine());
actualArgs[0] = new PlaybackContext(context.getCallback(), getLine(), context.getRobot(), context.isUseDirectActionCall());
for (int i = 1; i < actualArgs.length; i++) {
actualArgs[i] = args[i - 1];
}
@@ -84,7 +82,7 @@ public class CallCommand extends AbstractCommand {
AsyncResult result = (AsyncResult<String>)m.invoke(null, actualArgs);
if (result == null) {
cb.error("Method " + methodName + " must return AsyncResult object, but was null", getLine());
context.getCallback().error("Method " + methodName + " must return AsyncResult object, but was null", getLine());
return new ActionCallback.Done();
}
@@ -92,27 +90,27 @@ public class CallCommand extends AbstractCommand {
@Override
public void run(String s) {
if (s != null) {
cb.message("[" + methodName + "] " + s, getLine());
context.getCallback().message("[" + methodName + "] " + s, getLine());
}
cmdResult.setDone();
}
}).doWhenRejected(new AsyncResult.Handler<String>() {
@Override
public void run(String s) {
cb.error("[" + methodName + "] " + s, getLine());
context.getCallback().error("[" + methodName + "] " + s, getLine());
cmdResult.setDone();
}
});
}
catch (NoSuchMethodException e) {
cb.error("No method found in PlaybackCallFacade", getLine());
context.getCallback().error("No method found in PlaybackCallFacade", getLine());
}
catch (InvocationTargetException e) {
cb.error("InvocationTargetException while executing command: " + cmd, getLine());
context.getCallback().error("InvocationTargetException while executing command: " + cmd, getLine());
}
catch (IllegalAccessException e) {
cb.error("IllegalAccessException while executing command: " + cmd, getLine());
context.getCallback().error("IllegalAccessException while executing command: " + cmd, getLine());
}
return cmdResult;
}
@@ -15,10 +15,8 @@
*/
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import java.awt.*;
public class DelayCommand extends AbstractCommand {
public static String PREFIX = CMD_PREFIX + "delay";
@@ -27,15 +25,15 @@ public class DelayCommand extends AbstractCommand {
super(text, line);
}
public ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
public ActionCallback _execute(PlaybackContext context) {
final String s = getText().substring(PREFIX.length()).trim();
try {
final Integer delay = Integer.valueOf(s);
robot.delay(delay.intValue());
context.getRobot().delay(delay.intValue());
}
catch (NumberFormatException e) {
dumpError(cb, "Invalid delay value: " + s);
dumpError(context.getCallback(), "Invalid delay value: " + s);
return new ActionCallback.Rejected();
}
@@ -15,17 +15,15 @@
*/
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import java.awt.*;
public class EmptyCommand extends AbstractCommand {
public EmptyCommand(int line) {
super("", line);
}
public ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
public ActionCallback _execute(PlaybackContext context) {
return new ActionCallback.Done();
}
}
@@ -15,10 +15,8 @@
*/
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import java.awt.*;
public class ErrorCommand extends AbstractCommand {
@@ -26,8 +24,8 @@ public class ErrorCommand extends AbstractCommand {
super(text, line);
}
public ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
dumpError(cb, getText());
public ActionCallback _execute(PlaybackContext context) {
dumpError(context.getCallback(), getText());
return new ActionCallback.Rejected();
}
}
@@ -16,14 +16,12 @@
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.TypingTarget;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.registry.Registry;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.awt.List;
import java.util.*;
public class KeyCodeTypeCommand extends AlphaNumericTypeCommand {
@@ -37,7 +35,7 @@ public class KeyCodeTypeCommand extends AlphaNumericTypeCommand {
}
@Override
public ActionCallback _execute(final PlaybackRunner.StatusCallback cb, final Robot robot, boolean directActionCall) {
public ActionCallback _execute(final PlaybackContext context) {
String text = getText().substring(PREFIX.length()).trim();
int textDelim = text.indexOf(" ");
@@ -66,11 +64,11 @@ public class KeyCodeTypeCommand extends AlphaNumericTypeCommand {
}
}).doWhenRejected(new Runnable() {
public void run() {
typeCodes(cb, robot, codes).notify(result);
typeCodes(context.getCallback(), context.getRobot(), codes).notify(result);
}
});
} else {
typeCodes(cb, robot, codes).notify(result);
typeCodes(context.getCallback(), context.getRobot(), codes).notify(result);
}
return result;
@@ -15,10 +15,8 @@
*/
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import java.awt.*;
public class KeyShortcutCommand extends TypeCommand {
@@ -29,14 +27,14 @@ public class KeyShortcutCommand extends TypeCommand {
super(text, line);
}
public ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
public ActionCallback _execute(PlaybackContext context) {
final String one = getText().substring(PREFIX.length());
if (!one.endsWith(POSTFIX)) {
dumpError(cb, "Expected " + "]");
dumpError(context.getCallback(), "Expected " + "]");
return new ActionCallback.Rejected();
}
type(robot, getFromShortcut(one.substring(0, one.length() - 1).trim()));
type(context.getRobot(), getFromShortcut(one.substring(0, one.length() - 1).trim()));
return new ActionCallback.Done();
}
@@ -15,11 +15,9 @@
*/
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import java.awt.*;
/**
* Created by IntelliJ IDEA.
* User: kirillk
@@ -37,8 +35,8 @@ public class PrintCommand extends AbstractCommand {
}
@Override
protected ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
cb.message(myText, getLine());
protected ActionCallback _execute(PlaybackContext context) {
context.getCallback().message(myText, getLine());
return new ActionCallback.Done();
}
}
@@ -15,10 +15,8 @@
*/
package com.intellij.openapi.ui.playback.commands;
import com.intellij.openapi.ui.playback.PlaybackContext;
import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.ui.playback.PlaybackRunner;
import java.awt.*;
public class StopCommand extends AbstractCommand {
@@ -28,8 +26,8 @@ public class StopCommand extends AbstractCommand {
super(text, line);
}
protected ActionCallback _execute(PlaybackRunner.StatusCallback cb, Robot robot, boolean directActionCall) {
cb.message("Stopped", getLine());
protected ActionCallback _execute(PlaybackContext context) {
context.getCallback().message("Stopped", getLine());
return new ActionCallback.Done();
}