How to make "EditorActionHandler" work in unit test?

I use "EditorActionHandler" to insert an enter in editor. It works in real IDE, but causes assertion failure in unit test

public class CreateFunctionFixTest extends LightCodeInsightFixtureTestCase {
    public void testSimple() throws Exception {
        myFixture.configureByText(PlainTextFileType.INSTANCE, "");
        EditorActionManager manager = EditorActionManager.getInstance();
        EditorActionHandler enterHandler = manager.getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
        Editor editor = myFixture.getEditor();
        DataContext dc = DataManager.getInstance().getDataContext(editor.getContentComponent());
        enterHandler.execute(editor, dc);
    }
}


The stack trace is:

ERROR: Assertion failed:
java.lang.Throwable
     at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:98)
     at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:105)
     at com.intellij.openapi.command.impl.CommandProcessorImpl.setCurrentCommandName(CommandProcessorImpl.java:236)
     at com.intellij.codeInsight.editorActions.EnterHandler.executeWriteActionInner(EnterHandler.java:94)
     at com.intellij.codeInsight.editorActions.EnterHandler.access$000(EnterHandler.java:53)
     at com.intellij.codeInsight.editorActions.EnterHandler$1.run(EnterHandler.java:71)
     at com.intellij.psi.impl.source.PostprocessReformattingAspect$2.compute(PostprocessReformattingAspect.java:101)
     at com.intellij.psi.impl.source.PostprocessReformattingAspect.disablePostprocessFormattingInside(PostprocessReformattingAspect.java:110)
     at com.intellij.psi.impl.source.PostprocessReformattingAspect.disablePostprocessFormattingInside(PostprocessReformattingAspect.java:98)
     at com.intellij.codeInsight.editorActions.EnterHandler.executeWriteAction(EnterHandler.java:69)
     at com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler$1.run(EditorWriteActionHandler.java:52)
     at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:902)
     at com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler.execute(EditorWriteActionHandler.java:36)
     at com.intellij.codeInsight.template.impl.editorActions.EnterHandler.executeWriteAction(EnterHandler.java:51)
     at com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler$1.run(EditorWriteActionHandler.java:52)
     at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:902)
     at com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler.execute(EditorWriteActionHandler.java:36)
     at ro.redeul.google.go.inspection.fix.CreateFunctionFixTest.testSimple(CreateFunctionFixTest.java:28)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at junit.framework.TestCase.runTest(TestCase.java:168)
     at com.intellij.testFramework.UsefulTestCase.access$001(UsefulTestCase.java:68)
     at com.intellij.testFramework.UsefulTestCase$2.run(UsefulTestCase.java:237)
     at com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded(UIUtil.java:1778)
     at com.intellij.testFramework.UsefulTestCase.invokeTestRunnable(UsefulTestCase.java:279)
     at com.intellij.testFramework.UsefulTestCase.runTest(UsefulTestCase.java:253)
     at junit.framework.TestCase.runBare(TestCase.java:134)
     at com.intellij.testFramework.UsefulTestCase.defaultRunBare(UsefulTestCase.java:284)
     at com.intellij.testFramework.UsefulTestCase$3.run(UsefulTestCase.java:296)
     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


In CommandProcessorImpl.java line 236, it seems that the field "myCurrentCommand" is null.

    CommandLog.LOG.assertTrue(myCurrentCommand != null);


How to set the field in unit test?
It seems that the action itself doesn't matter, I tried IdeActions.ACTION_EDITOR_TAB, it's the same result.
Thanks.

2 comments
Comment actions Permalink

Hi Leo,

Try the following:

CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {       
  @Override                                                                        
  public void run() {                                                              
    EditorActionManager actionManager = EditorActionManager.getInstance();         
    EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_ENTER);  
    actionHandler.execute(getEditor(), DataManager.getInstance().getDataContext());   }                                                                                 }, "", null);                                                                      




Denis

0
Comment actions Permalink

Ah, It works.
Thank you.:)

0

Please sign in to leave a comment.