wrap editor actions in command in tests

This commit is contained in:
Alexey Kudravtsev
2015-02-03 14:08:09 +03:00
parent 6fe1292309
commit 467e5411fa
4 changed files with 134 additions and 144 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,24 +16,17 @@
package com.intellij.codeInsight;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.highlighting.HighlightUsagesHandler;
import com.intellij.ide.DataManager;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.command.undo.UndoManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.openapi.editor.actionSystem.TypedAction;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.fileEditor.FileEditorManager;
@@ -181,6 +174,7 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempFile);
assert vFile != null;
new WriteAction() {
@Override
protected void run(@NotNull Result result) throws Throwable {
vFile.setCharset(CharsetToolkit.UTF8_CHARSET);
VfsUtil.saveText(vFile, text);
@@ -354,32 +348,6 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
return editorInfos;
}
/*protected LinkedHashMap<VirtualFile, EditorInfo> copyFilesFillingEditorInfos(final VirtualFile fromDir, final VirtualFile toDir) throws IOException {
final LinkedHashMap<VirtualFile, EditorInfo> map = new LinkedHashMap<VirtualFile, EditorInfo>();
copyFilesFillingEditorInfos(fromDir, toDir, map);
return map;
}
private void copyFilesFillingEditorInfos(final VirtualFile fromDir, final VirtualFile toDir, LinkedHashMap<VirtualFile, EditorInfo> editorInfos) throws IOException {
List<OutputStream> streamsToClose = new ArrayList<OutputStream>();
final VirtualFile[] files = fromDir.getChildren();
for (final VirtualFile fromFile : files) {
if (fromFile.isDirectory()) {
copyFilesFillingEditorInfos(fromFile, toDir.createChildDirectory(this, fromFile.getName()), editorInfos);
} else {
final VirtualFile toFile = toDir.createChildData(this, fromFile.getName());
editorInfos.put(toFile, copyContent(fromFile, toFile, streamsToClose));
}
}
for(int i = streamsToClose.size() -1; i >= 0 ; --i) {
streamsToClose.get(i).close();
}
}*/
private EditorInfo copyContent(@NotNull VirtualFile from, @NotNull VirtualFile to, @NotNull List<OutputStream> streamsToClose) throws IOException {
byte[] content = from.getFileType().isBinary() ? from.contentsToByteArray(): null;
final String fileText = from.getFileType().isBinary() ? null : StringUtil.convertLineSeparators(VfsUtilCore.loadText(from));
@@ -590,40 +558,7 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
}
protected void type(char c) {
type(c, getEditor());
}
protected static void type(char c, Editor editor) {
EditorActionManager actionManager = EditorActionManager.getInstance();
DataContext dataContext = DataManager.getInstance().getDataContext();
if (c == '\n') {
actionManager.getActionHandler(IdeActions.ACTION_EDITOR_ENTER).execute(editor, dataContext);
return;
}
TypedAction action = actionManager.getTypedAction();
action.actionPerformed(editor, c, dataContext);
}
protected void caretRight() {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT);
action.execute(getEditor(), DataManager.getInstance().getDataContext());
}
protected void caretUp() {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
action.execute(getEditor(), DataManager.getInstance().getDataContext());
}
protected void deleteLine() {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_DELETE_LINE);
action.execute(getEditor(), DataManager.getInstance().getDataContext());
}
protected void type(@NonNls @NotNull String s) {
for (char c : s.toCharArray()) {
type(c);
}
LightPlatformCodeInsightTestCase.type(c, getEditor(),getProject());
}
protected void undo() {
@@ -632,39 +567,44 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
undoManager.undo(textEditor);
}
protected void caretRight() {
caretRight(getEditor());
}
protected void caretRight(Editor editor) {
LightPlatformCodeInsightTestCase.caretRight(editor);
}
protected void caretUp() {
LightPlatformCodeInsightTestCase.caretUp(myEditor);
}
protected void deleteLine() {
LightPlatformCodeInsightTestCase.deleteLine(myEditor,getProject());
}
protected void type(@NonNls @NotNull String s) {
for (char c : s.toCharArray()) {
type(c);
}
}
protected void backspace() {
backspace(getEditor());
}
protected void backspace(@NotNull final Editor editor) {
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE);
actionHandler.execute(editor, DataManager.getInstance().getDataContext());
}
}, "backspace", editor.getDocument());
LightPlatformCodeInsightTestCase.backspace(editor,getProject());
}
protected void ctrlShiftF7() {
HighlightUsagesHandler.invoke(getProject(), getEditor(), getFile());
protected void ctrlW() {
LightPlatformCodeInsightTestCase.ctrlW(getEditor(),getProject());
}
public static void ctrlW() {
AnAction action = ActionManager.getInstance().getAction(IdeActions.ACTION_EDITOR_SELECT_WORD_AT_CARET);
DataContext dataContext = DataManager.getInstance().getDataContext();
AnActionEvent event = new AnActionEvent(null, dataContext, "", action.getTemplatePresentation(), ActionManager.getInstance(), 0);
event.setInjectedContext(true);
action.actionPerformed(event);
protected void ctrlD() {
LightPlatformCodeInsightTestCase.ctrlD(getEditor(),getProject());
}
public static void ctrlD() {
AnAction action = ActionManager.getInstance().getAction(IdeActions.ACTION_EDITOR_DUPLICATE);
DataContext dataContext = DataManager.getInstance().getDataContext();
AnActionEvent event = new AnActionEvent(null, dataContext, "", action.getTemplatePresentation(), ActionManager.getInstance(), 0);
event.setInjectedContext(true);
action.actionPerformed(event);
protected void delete(@NotNull final Editor editor) {
LightPlatformCodeInsightTestCase.delete(editor, getProject());
}
@NotNull
@@ -680,16 +620,4 @@ public abstract class CodeInsightTestCase extends PsiTestCase {
assertNotNull("Package " + name + " not found", aPackage);
return aPackage;
}
protected void delete(@NotNull final Editor editor) {
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_DELETE);
actionHandler.execute(editor, DataManager.getInstance().getDataContext());
}
}, "delete", editor.getDocument());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,9 @@
package com.intellij.testFramework;
import com.intellij.ide.DataManager;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.*;
@@ -31,10 +33,12 @@ import com.intellij.openapi.editor.impl.SoftWrapModelImpl;
import com.intellij.openapi.editor.impl.softwrap.SoftWrapDrawingType;
import com.intellij.openapi.editor.impl.softwrap.SoftWrapPainter;
import com.intellij.openapi.editor.impl.softwrap.mapping.SoftWrapApplianceManager;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
@@ -42,6 +46,7 @@ import org.jetbrains.annotations.TestOnly;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
@@ -79,15 +84,15 @@ public class EditorTestUtil {
}
}
public static void executeAction(Editor editor, String actionId) {
public static void executeAction(@NotNull Editor editor, @NotNull String actionId) {
executeAction(editor, actionId, false);
}
public static void executeAction(Editor editor, String actionId, boolean assertActionIsEnabled) {
public static void executeAction(@NotNull Editor editor, @NotNull String actionId, boolean assertActionIsEnabled) {
ActionManager actionManager = ActionManager.getInstance();
AnAction action = actionManager.getAction(actionId);
assertNotNull(action);
DataContext dataContext = DataManager.getInstance().getDataContext(editor.getContentComponent());
DataContext dataContext = createEditorContext(editor);
AnActionEvent event = new AnActionEvent(null, dataContext, "", action.getTemplatePresentation(), actionManager, 0);
action.beforeActionPerformedUpdate(event);
if (!event.getPresentation().isEnabled()) {
@@ -97,6 +102,16 @@ public class EditorTestUtil {
action.actionPerformed(event);
}
@NotNull
private static DataContext createEditorContext(@NotNull Editor editor) {
Object e = editor;
Object hostEditor = editor instanceof EditorWindow ? ((EditorWindow)editor).getDelegate() : editor;
Map<String, Object> map = ContainerUtil.newHashMap(Pair.create(CommonDataKeys.HOST_EDITOR.getName(), hostEditor),
Pair.createNonNull(CommonDataKeys.EDITOR.getName(), e));
DataContext parent = DataManager.getInstance().getDataContext(editor.getContentComponent());
return SimpleDataContext.getSimpleContext(map, parent);
}
public static void performReferenceCopy(Editor editor) {
executeAction(editor, IdeActions.ACTION_COPY_REFERENCE, true);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package com.intellij.testFramework;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.generation.actions.CommentByLineCommentAction;
import com.intellij.codeInsight.highlighting.HighlightUsagesHandler;
import com.intellij.ide.DataManager;
import com.intellij.injected.editor.DocumentWindow;
import com.intellij.injected.editor.EditorWindow;
@@ -30,8 +31,8 @@ import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.openapi.editor.actionSystem.TypedAction;
import com.intellij.openapi.editor.ex.util.EditorUtil;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.editor.impl.EditorImpl;
@@ -39,6 +40,7 @@ import com.intellij.openapi.editor.impl.TrailingSpacesStripper;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.ThrowableComputable;
@@ -142,7 +144,6 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
* Same as configureByFile but text is provided directly.
* @param fileName - name of the file.
* @param fileText - data file text.
* @throws java.io.IOException
*/
@NotNull
protected static Document configureFromFileText(@NonNls @NotNull final String fileName, @NonNls @NotNull final String fileText) {
@@ -384,41 +385,81 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}
}
protected void caretUp() {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
action.execute(getEditor(), DataManager.getInstance().getDataContext());
protected void caretRight() {
caretRight(getEditor());
}
protected void deleteLine() {
EditorActionManager actionManager = EditorActionManager.getInstance();
EditorActionHandler action = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_DELETE_LINE);
action.execute(getEditor(), DataManager.getInstance().getDataContext());
}
protected static void type(char c) {
EditorActionManager actionManager = EditorActionManager.getInstance();
final DataContext dataContext = DataManager.getInstance().getDataContext();
if (c == '\n') {
actionManager.getActionHandler(IdeActions.ACTION_EDITOR_ENTER).execute(getEditor(), dataContext);
}
else if (c == '\b') {
actionManager.getActionHandler(IdeActions.ACTION_EDITOR_BACKSPACE).execute(getEditor(), dataContext);
}
else {
// typed action is always executed in host editor context
actionManager.getTypedAction().actionPerformed(InjectedLanguageUtil.getTopLevelEditor(getEditor()), c, dataContext);
}
public static void caretRight(Editor editor) {
executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT, editor);
}
protected static void type(@NonNls String s) {
protected void caretUp() {
caretUp(getEditor());
}
public static void caretUp(Editor editor) {
executeAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP, editor);
}
protected void deleteLine() {
deleteLine(getEditor(),getProject());
}
public static void deleteLine(Editor editor, Project project) {
executeAction(IdeActions.ACTION_EDITOR_DELETE_LINE, editor,project);
}
protected void type(@NonNls @NotNull String s) {
for (char c : s.toCharArray()) {
type(c);
}
}
protected static void backspace() {
executeAction(IdeActions.ACTION_EDITOR_BACKSPACE);
protected void type(char c) {
type(c, getEditor(),getProject());
}
protected static void delete() {
executeAction(IdeActions.ACTION_EDITOR_DELETE);
public static void type(char c, @NotNull Editor editor, Project project) {
if (c == '\n') {
executeAction(IdeActions.ACTION_EDITOR_ENTER, editor,project);
}
else {
EditorActionManager actionManager = EditorActionManager.getInstance();
final DataContext dataContext = DataManager.getInstance().getDataContext();
TypedAction action = actionManager.getTypedAction();
action.actionPerformed(editor, c, dataContext);
}
}
protected void backspace() {
backspace(getEditor(),getProject());
}
public static void backspace(@NotNull final Editor editor, Project project) {
executeAction(IdeActions.ACTION_EDITOR_BACKSPACE, editor,project);
}
protected void ctrlShiftF7() {
HighlightUsagesHandler.invoke(getProject(), getEditor(), getFile());
}
protected void ctrlW() {
ctrlW(getEditor(),getProject());
}
public static void ctrlW(@NotNull Editor editor, Project project) {
executeAction(IdeActions.ACTION_EDITOR_SELECT_WORD_AT_CARET, editor,project);
}
public void ctrlD() {
ctrlD(getEditor(),getProject());
}
public static void ctrlD(@NotNull Editor editor, Project project) {
executeAction(IdeActions.ACTION_EDITOR_DUPLICATE, editor, project);
}
protected void delete() {
delete(getEditor(), getProject());
}
public static void delete(@NotNull final Editor editor, Project project) {
executeAction(IdeActions.ACTION_EDITOR_DELETE, editor, project);
}
protected static void home() {
@@ -438,11 +479,11 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}
protected static void moveCaretToPreviousWordWithSelection() {
executeAction("EditorPreviousWordWithSelection");
executeAction(IdeActions.ACTION_EDITOR_PREVIOUS_WORD_WITH_SELECTION);
}
protected static void moveCaretToNextWordWithSelection() {
executeAction("EditorNextWordWithSelection");
executeAction(IdeActions.ACTION_EDITOR_NEXT_WORD_WITH_SELECTION);
}
protected static void cutLineBackward() {
@@ -490,12 +531,18 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}
protected static void executeAction(@NonNls @NotNull final String actionId) {
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
executeAction(actionId, getEditor());
}
protected static void executeAction(@NonNls @NotNull final String actionId, @NotNull final Editor editor) {
executeAction(actionId, editor, getProject());
}
protected static void executeAction(@NonNls @NotNull final String actionId, @NotNull final Editor editor, Project project) {
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
EditorTestUtil.executeAction(getEditor(), actionId);
EditorTestUtil.executeAction(editor, actionId);
}
}, "", null);
}, "", null, editor.getDocument());
}
protected static DataContext getCurrentEditorDataContext() {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ public class PropertiesEnterTest extends LightPlatformCodeInsightTestCase {
return PluginPathManager.getPluginHomePath("properties") + "/testData";
}
private static void typeEnter() {
private void typeEnter() {
type('\n');
}