IntelliJ BasePlatformTestCase with javax.swing.Timer

62 Views Asked by At

I am having trouble writing a JUnit Test for a custom Intellij Plugin. The Plugin registers a EditorFactoryListener, that instantiates a EditorCustomElementRenderer which starts a javax.swing.Timer whose ActionListener inserts some inline text to the current Caret Position in the open Document after some seconds. This works fine in the runIde but not in a Unit Test.

The custom EditorFactoryListener (registered in plugin.xml):

public class InlineRendererInitializer implements EditorFactoryListener {
    private static final Logger LOG = Logger.getInstance(InlineRenderernInitializer.class);

    @Override
    public void editorCreated(@NotNull EditorFactoryEvent event) {
        Editor editor = event.getEditor();
        LOG.warn("Editor created");
        new InlineRenderer(editor);
    }

    @Override
    public void editorReleased(@NotNull EditorFactoryEvent event) {
        LOG.warn("Editor released");
    }
}

The custom EditorCustomElementRenderer:

public class InlineRenderer implements EditorCustomElementRenderer {
   private Editor editor;
   private Timer timer;

   public InlineRenderer(Editor editor)  {
       timer = new Timer(5000, e -> {
          ApplicationManager.getApplication().invokeLater(() -> {
              // do some stuff in editor
          });
       });
       
       timer.setRepeats(true);
       timer.start();
   }
}

The Test Class:


public class InlineRendererTest extends BasePlatformTestCase {

  public void testInline() throws InterruptedException {
    EditorFactory editorFactory = EditorFactory.getInstance();
    Document document = editorFactory.createDocument("Test Test");
    Editor editor = editorFactory.createEditor(document, getProject());
    // Set cursor in body of toUpperCase() method
    editor.getCaretModel().addCaret(new VisualPosition(0,4));
    await()
            .pollDelay(5, SECONDS)
            .timeout(10, SECONDS)
        .untilAsserted(() -> {
            // Some assertions
        });
  }
}

The timer.start() is executed, but the Timer does not trigger until the Awaitility Timeout of 10 seconds is triggered, why is that? Thread.sleep instead of Awaitility is also not working.

0

There are 0 best solutions below