@Test
  public void testCustomRunnable() throws Exception {
    ActivityHint activityHint = new ActivityHint(DatasourceActivity.class);
    controller.startOrResume(activityHint);

    CountDownLatch latch = new CountDownLatch(1);

    Runnable runner =
        () -> {
          try {
            latch.await(60, TimeUnit.SECONDS);
          } catch (InterruptedException e) {
            //
          }
        };
    assertFalse(store.loadingProperty().get());

    store.executeCustomRunnable(runner);
    FXPlatform.waitForFX();
    assertTrue(store.loadingProperty().get());

    latch.countDown();
    controller.waitForDataSource();
    FXPlatform.waitForFX();
    assertFalse(store.loadingProperty().get());
  }
  @Test
  public void testImageInsertion() throws Exception {
    imageProvider.addImage(new ImageData("test", "/de/ks/images/keymap.jpg"));

    InsertImage command = adocEditor.getCommand(InsertImage.class);
    command.collectImages();
    SelectImageController selectImageController = command.getSelectImageController();
    for (Future<?> future : selectImageController.getLoadingFutures()) {
      future.get();
    }
    activityController.waitForTasks();
    FXPlatform.waitForFX();
    assertEquals(1, selectImageController.getImagePane().getChildren().size());

    FXPlatform.invokeLater(
        () -> {
          GridPane grid =
              (GridPane) command.getSelectImageController().getImagePane().getChildren().get(0);
          Button node = (Button) grid.getChildren().get(0);
          node.getOnAction().handle(null);
        });

    assertThat(
        adocEditor.editor.getText(),
        Matchers.containsString("image::file:////de/ks/images/keymap.jpg"));
  }
  @Test
  public void testSearching() throws Exception {
    FXPlatform.invokeLater(
        () -> {
          adocEditor.searchField.setText("b");
          adocEditor.setText("a\na\na\nb\nb\na\nb");
          adocEditor.searchForText();
        });
    activityController.waitForTasks();
    FXPlatform.waitForFX();
    assertEquals(6, adocEditor.editor.getCaretPosition());

    FXPlatform.invokeLater(() -> adocEditor.searchForText());
    activityController.waitForTasks();
    FXPlatform.waitForFX();
    assertEquals(8, adocEditor.editor.getCaretPosition());

    FXPlatform.invokeLater(() -> adocEditor.searchForText());
    activityController.waitForTasks();
    FXPlatform.waitForFX();
    assertEquals(12, adocEditor.editor.getCaretPosition());

    FXPlatform.invokeLater(() -> adocEditor.searchForText());
    activityController.waitForTasks();
    FXPlatform.waitForFX();
    assertEquals(6, adocEditor.editor.getCaretPosition());
  }
  @Test
  public void testAdocParsing() throws Exception {
    FXPlatform.invokeLater(() -> adocEditor.editor.setText("= Title\n== more"));
    FXPlatform.invokeLater(() -> adocEditor.tabPane.getSelectionModel().select(1));
    JunitMatchers.withRetry(() -> adocEditor.preview.getCurrentHtml() != null);
    assertNotNull("preview string is null", adocEditor.preview.getCurrentHtml());

    WebEngine engine = adocEditor.preview.getWebView().getEngine();
    JunitMatchers.withRetry(() -> adocEditor.preview.getWebView().getEngine() != null);
    Condition.waitFor5s(() -> engine.getDocument(), Matchers.nullValue());

    FXPlatform.invokeLater(() -> adocEditor.tabPane.getSelectionModel().select(1));
    JunitMatchers.withRetry(() -> engine.getDocument() != null);
    assertNotNull("document did not load, is still null", engine.getDocument());

    FXPlatform.invokeLater(() -> adocEditor.tabPane.getSelectionModel().select(0));
    FXPlatform.waitForFX();
    //    assertTrue(adocEditor.editor.isFocused()); FIXME doesn't work in jdk8u11 but jdk8u20
  }
  @Test
  public void testPersistentStoreBack() throws Exception {
    File file = new File(System.getProperty("java.io.tmpdir"));
    adocEditor.setPersistentStoreBack("test", file);
    FXPlatform.invokeLater(() -> adocEditor.setText("bla blubb"));
    Thread.sleep(LastTextChange.WAIT_TIME * 2);
    activityController.waitForTasks();

    List<String> strings = Files.readLines(new File(file, "test"), Charsets.UTF_8);
    assertEquals(1, strings.size());

    adocEditor.removePersistentStoreBack();
    FXPlatform.invokeLater(() -> adocEditor.setText(""));
    Thread.sleep(LastTextChange.WAIT_TIME * 2);
    activityController.waitForTasks();

    adocEditor.setPersistentStoreBack("test", file);
    FXPlatform.invokeLater(() -> adocEditor.onRefresh(null));

    activityController.waitForTasks();
    assertEquals("bla blubb\n", adocEditor.getText());
  }