@Override protected ActionCallback _execute(PlaybackContext context) { final String[] keyValue = getText().substring(PREFIX.length()).trim().split("="); if (keyValue.length != 2) { context.error("Expected expresstion: " + PREFIX + " key=value", getLine()); return ActionCallback.REJECTED; } final String key = keyValue[0]; final String value = keyValue[1]; context.storeRegistryValue(key); Registry.get(key).setValue(value); return ActionCallback.DONE; }
@Override protected ActionCallback _execute(PlaybackContext context) { File file = context.getPathMacro().resolveFile(myDir, context.getBaseDir()); if (!file.exists()) { context.message("Cannot cd, directory doesn't exist: " + file.getAbsoluteFile(), getLine()); return ActionCallback.REJECTED; } try { context.setBaseDir(file.getCanonicalFile()); } catch (IOException e) { context.setBaseDir(file); } context.message("{base.dir} set to " + context.getBaseDir().getAbsolutePath(), getLine()); return ActionCallback.DONE; }
protected ActionCallback type(final PlaybackContext context, final String text) { final ActionCallback result = new ActionCallback(); IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(new Runnable() { @Override public void run() { TypingTarget typingTarget = findTarget(context); if (typingTarget != null) { typingTarget.type(text).doWhenDone(result.createSetDoneRunnable()).doWhenRejected(new Runnable() { public void run() { typeByRobot(context.getRobot(), text).notify(result); } }); } else { typeByRobot(context.getRobot(), text).notify(result); } } }); return result; }
@Nullable public static TypingTarget findTarget(PlaybackContext context) { if (!context.isUseTypingTargets()) return null; Component each = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); while (each != null) { if (each instanceof TypingTarget) { return (TypingTarget)each; } each = each.getParent(); } return null; }
public static AsyncResult<String> printFocus(final PlaybackContext context) { final AsyncResult result = new AsyncResult<String>(); getUiReady(context).doWhenProcessed(new Runnable() { @Override public void run() { final LinkedHashMap<String, String> focusInfo = getFocusInfo(); if (focusInfo == null) { result.setRejected("No component focused"); return; } StringBuffer text = new StringBuffer(); for (Iterator<String> iterator = focusInfo.keySet().iterator(); iterator.hasNext(); ) { String key = iterator.next(); text.append(key + "=" + focusInfo.get(key)); if (iterator.hasNext()) { text.append("|"); } } result.setDone(text.toString()); } }); return result; }
private void message(@Nullable final PlaybackContext context, final String text, final int currentLine, final Type type, final boolean forced) { final int depth = context != null ? context.getCurrentStageDepth() : 0; UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { if (!forced && (context != null && context.isDisposed())) return; switch (type) { case message: addInfo(text, currentLine, MESSAGE_COLOR, depth); break; case error: addInfo(text, currentLine, ERROR_COLOR, depth); break; case code: addInfo(text, currentLine, CODE_COLOR, depth); break; case test: addInfo(text, currentLine, TEST_COLOR, depth); break; } } }); }
@Override protected ActionCallback _execute(PlaybackContext context) { final String[] keyValue = getText().substring(PREFIX.length()).trim().split("="); if (keyValue.length != 2) { context.error("Expected expresstion: " + PREFIX + " key=value", getLine()); return new ActionCallback.Rejected(); } final String key = keyValue[0]; final String value = keyValue[1]; context.storeRegistryValue(key); Registry.get(key).setValue(value); return new ActionCallback.Done(); }
public final ActionCallback execute(final PlaybackContext context) { try { if (isToDumpCommand()) { dumpCommand(context); } final ActionCallback result = new ActionCallback(); if (isAwtThread()) { _execute(context).notify(result); } else { ApplicationManager.getApplication().executeOnPooledThread(new Runnable() { @Override public void run() { _execute(context).notify(result); } }); } return result; } catch (Exception e) { context.error(e.getMessage(), getLine()); return new ActionCallback.Rejected(); } }
@Override protected ActionCallback _execute(PlaybackContext context) { File file = context.getPathMacro().resolveFile(myDir, context.getBaseDir()); if (!file.exists()) { context.message("Cannot cd, directory doesn't exist: " + file.getAbsoluteFile(), getLine()); return new ActionCallback.Rejected(); } try { context.setBaseDir(file.getCanonicalFile()); } catch (IOException e) { context.setBaseDir(file); } context.message("{base.dir} set to " + context.getBaseDir().getAbsolutePath(), getLine()); return new ActionCallback.Done(); }
private static Pair<Method, Class> findMethod(PlaybackContext context, String methodName, Class[] types) { Set<Class> classes = context.getCallClasses(); for (Class eachClass : classes) { try { Method method = eachClass.getMethod(methodName, types); return Pair.create(method, eachClass); } catch (NoSuchMethodException ignored) { continue; } } return null; }
private ActionCallback typeCodes(final PlaybackContext context, final Robot robot, final String codes) { final ActionCallback result = new ActionCallback(); Runnable runnable = new Runnable() { public void run() { String[] pairs = codes.split(CODE_DELIMITER); for (String eachPair : pairs) { try { String[] splits = eachPair.split(MODIFIER_DELIMITER); Integer code = Integer.valueOf(splits[0]); Integer modifier = Integer.valueOf(splits[1]); type(robot, code.intValue(), modifier.intValue()); } catch (NumberFormatException e) { dumpError(context, "Invalid code: " + eachPair); result.setRejected(); return; } } result.setDone(); } }; if (SwingUtilities.isEventDispatchThread()) { ApplicationManager.getApplication().executeOnPooledThread(runnable); } else { runnable.run(); } return result; }
@Override protected ActionCallback _execute(PlaybackContext context) { StageInfo stage = context.popStage(); if (stage != null) { context.test("Test finished OK: " + stage.getName(), getLine()); context.addPassed(stage); } return ActionCallback.DONE; }
public ActionCallback _execute(PlaybackContext context) { final String one = getText().substring(PREFIX.length()); if (!one.endsWith(POSTFIX)) { dumpError(context, "Expected " + "]"); return ActionCallback.REJECTED; } type(context.getRobot(), getFromShortcut(one.substring(0, one.length() - 1).trim())); return ActionCallback.DONE; }
@Override protected ActionCallback _execute(PlaybackContext context) { String name = getText().substring(PREFIX.length()).trim(); context.test("Test started: " + name, getLine()); context.pushStage(new StageInfo(name)); return ActionCallback.DONE; }
public ActionCallback _execute(PlaybackContext context) { final String s = getText().substring(PREFIX.length()).trim(); try { final Integer delay = Integer.valueOf(s); context.getRobot().delay(delay.intValue()); } catch (NumberFormatException e) { dumpError(context, "Invalid delay value: " + s); return ActionCallback.REJECTED; } return ActionCallback.DONE; }
public static AsyncResult<String> waitForDialog(final PlaybackContext context, final String title) { final AsyncResult<String> result = new AsyncResult<String>(); final Ref<AWTEventListener> listener = new Ref<AWTEventListener>(); listener.set(new AWTEventListener() { @Override public void eventDispatched(AWTEvent event) { if (event.getID() == WindowEvent.WINDOW_ACTIVATED) { final Window wnd = ((WindowEvent)event).getWindow(); if (wnd instanceof JDialog) { if (title.equals(((JDialog)wnd).getTitle())) { Toolkit.getDefaultToolkit().removeAWTEventListener(listener.get()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { getUiReady(context).notify(result); } }); } } } } }); Toolkit.getDefaultToolkit().addAWTEventListener(listener.get(), WindowEvent.WINDOW_EVENT_MASK); SimpleTimer.getInstance().setUp(new Runnable() { @Override public void run() { Toolkit.getDefaultToolkit().removeAWTEventListener(listener.get()); if (!result.isProcessed()) { result.setRejected("Timed out waiting for window: " + title); } } }, Registry.intValue("actionSystem.commandProcessingTimeout")); return result; }
public static AsyncResult<String> checkFocus(final PlaybackContext context, String expected) { final AsyncResult<String> result = new AsyncResult<String>(); final Map<String, String> expectedMap = new LinkedHashMap<String, String>(); if (expected.length() > 0) { final String[] keyValue = expected.split("\\|"); for (String each : keyValue) { final String[] eachPair = each.split("="); if (eachPair.length != 2) { result.setRejected("Syntax error, must be |-separated pairs key=value"); return result; } expectedMap.put(eachPair[0], eachPair[1]); } } getUiReady(context).doWhenDone(new Runnable() { @Override public void run() { try { doAssert(expectedMap, result, context); } catch (AssertionError error) { result.setRejected("Assertion failed: " + error.getMessage()); } } }); return result; }
public static AsyncResult<String> waitForToolWindow(final PlaybackContext context, final String id) { final AsyncResult<String> result = new AsyncResult<String>(); findProject().doWhenDone(new Consumer<Project>() { @Override public void consume(Project project) { ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(id); if (toolWindow == null) { result.setRejected("Cannot find tool window with id: " + id); return; } toolWindow.getReady(context).doWhenDone(result.createSetDoneRunnable()).doWhenRejected(new Runnable() { @Override public void run() { result.setRejected("Cannot activate tool window with id:" + id); } }); } }).doWhenRejected(new Runnable() { @Override public void run() { result.setRejected("Cannot retrieve open project"); } }); return result; }
public static ActionCallback getUiReady(final PlaybackContext context) { final ActionCallback result = new ActionCallback(); context.flushAwtAndRunInEdt(new Runnable() { @Override public void run() { UiActivityMonitor.getInstance().getBusy().getReady(context).notify(result); } }); return result; }
public static AsyncResult<String> openProjectClone(final PlaybackContext context, String path) { try { File parentDir = FileUtil.createTempDirectory("funcTest", ""); File sourceDir = context.getPathMacro().resolveFile(path, context.getBaseDir()); context.message("Cloning project: " + sourceDir.getAbsolutePath(), context.getCurrentLine()); FileUtil.copyDir(sourceDir, parentDir); File projectDir = new File(parentDir, sourceDir.getName()); return openProject(context, projectDir.getAbsolutePath()); } catch (IOException e) { return new AsyncResult.Rejected<String>("Cannot create temp directory for clone"); } }
public static AsyncResult<String> openProject(final PlaybackContext context, final String path) { final AsyncResult<String> result = new AsyncResult<String>(); final ProjectManager pm = ProjectManager.getInstance(); final Ref<ProjectManagerListener> listener = new Ref<ProjectManagerListener>(); listener.set(new ProjectManagerAdapter() { @Override public void projectOpened(final Project project) { StartupManager.getInstance(project).registerPostStartupActivity(new Runnable() { @Override public void run() { pm.removeProjectManagerListener(listener.get()); DumbService.getInstance(project).runWhenSmart(new Runnable() { @Override public void run() { result.setDone("Opened successfully: " + project.getPresentableUrl()); } }); } }); } }); pm.addProjectManagerListener(listener.get()); UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { try { pm.loadAndOpenProject(path); } catch (Exception e) { context.error(e.getMessage(), context.getCurrentLine()); result.setRejected(); } } }); return result; }
public static AsyncResult<String> assertEditorLine(final PlaybackContext context, final String expected) { final AsyncResult<String> result = new AsyncResult<String>(); WindowSystemPlaybackCall.getUiReady(context).doWhenDone(new Runnable() { @Override public void run() { Editor editor = CommonDataKeys.EDITOR.getData(DataManager.getInstance().getDataContextFromFocus().getResult()); if (editor == null) { editor = CommonDataKeys.EDITOR_EVEN_IF_INACTIVE.getData(DataManager.getInstance().getDataContextFromFocus().getResult()); } if (editor == null) { result.setRejected("Cannot find editor"); return; } final int line = editor.getCaretModel().getLogicalPosition().line; final int caret = editor.getCaretModel().getOffset(); final int start = editor.getDocument().getLineStartOffset(line); final int end = editor.getDocument().getLineEndOffset(line); final StringBuffer actualText = new StringBuffer(editor.getDocument().getText(new TextRange(start, caret))); actualText.append("<caret>").append(editor.getDocument().getText(new TextRange(caret, end))); if (expected.equals(actualText.toString())) { result.setDone(); } else { result.setRejected("Expected:" + expected + " but was:" + actualText); } } }); return result; }
public static AsyncResult<String> waitDaemonForFinish(final PlaybackContext context) { final AsyncResult<String> result = new AsyncResult<String>(); final Disposable connection = Disposer.newDisposable(); result.doWhenProcessed(new Runnable() { @Override public void run() { Disposer.dispose(connection); } }); WindowSystemPlaybackCall.findProject().doWhenDone(new Consumer<Project>() { @Override public void consume(Project project) { final MessageBusConnection bus = project.getMessageBus().connect(connection); bus.subscribe(DaemonCodeAnalyzer.DAEMON_EVENT_TOPIC, new DaemonCodeAnalyzer.DaemonListenerAdapter() { @Override public void daemonFinished() { context.flushAwtAndRunInEdt(result.createSetDoneRunnable()); } @Override public void daemonCancelEventOccurred(@NotNull String reason) { result.setDone(); } }); } }).doWhenRejected(new Runnable() { @Override public void run() { result.setRejected("Cannot find project"); } }); return result; }
private Pair<Method, Class> findMethod(PlaybackContext context, String methodName, Class[] types) { Set<Class> classes = context.getCallClasses(); for (Class eachClass : classes) { try { Method method = eachClass.getMethod(methodName, types); return new Pair<Method, Class>(method, eachClass); } catch (NoSuchMethodException e) { continue; } } return null; }
@Override protected ActionCallback _execute(PlaybackContext context) { StageInfo stage = context.popStage(); if (stage != null) { context.test("Test finished OK: " + stage.getName(), getLine()); context.addPassed(stage); } return new ActionCallback.Done(); }
public ActionCallback _execute(PlaybackContext context) { final String one = getText().substring(PREFIX.length()); if (!one.endsWith(POSTFIX)) { dumpError(context, "Expected " + "]"); return new ActionCallback.Rejected(); } type(context.getRobot(), getFromShortcut(one.substring(0, one.length() - 1).trim())); return new ActionCallback.Done(); }
@Override protected ActionCallback _execute(PlaybackContext context) { String name = getText().substring(PREFIX.length()).trim(); context.test("Test started: " + name, getLine()); context.pushStage(new StageInfo(name)); return new ActionCallback.Done(); }
public ActionCallback _execute(PlaybackContext context) { final String s = getText().substring(PREFIX.length()).trim(); try { final Integer delay = Integer.valueOf(s); context.getRobot().delay(delay.intValue()); } catch (NumberFormatException e) { dumpError(context, "Invalid delay value: " + s); return new ActionCallback.Rejected(); } return new ActionCallback.Done(); }