From 467e5411fabdbfebd44362c767eacc821a6c7c2c Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Tue, 27 Jan 2015 13:02:22 +0300 Subject: [PATCH] wrap editor actions in command in tests --- .../codeInsight/CodeInsightTestCase.java | 134 ++++-------------- .../testFramework/EditorTestUtil.java | 23 ++- .../LightPlatformCodeInsightTestCase.java | 117 ++++++++++----- .../lang/properties/PropertiesEnterTest.java | 4 +- 4 files changed, 134 insertions(+), 144 deletions(-) diff --git a/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java b/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java index 285957652de4..d56b8d375176 100644 --- a/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java @@ -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 copyFilesFillingEditorInfos(final VirtualFile fromDir, final VirtualFile toDir) throws IOException { - final LinkedHashMap map = new LinkedHashMap(); - copyFilesFillingEditorInfos(fromDir, toDir, map); - return map; - } - - - private void copyFilesFillingEditorInfos(final VirtualFile fromDir, final VirtualFile toDir, LinkedHashMap editorInfos) throws IOException { - - List streamsToClose = new ArrayList(); - - 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 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()); - } } diff --git a/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java index 86d67e864ae4..d37e2b615ad2 100644 --- a/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/EditorTestUtil.java @@ -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 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); } diff --git a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java index 40d8994e1cb1..491204878e5e 100644 --- a/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/LightPlatformCodeInsightTestCase.java @@ -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() { diff --git a/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesEnterTest.java b/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesEnterTest.java index b67445efb50d..24d7e8a54a97 100644 --- a/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesEnterTest.java +++ b/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesEnterTest.java @@ -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'); }