GripPlatform { public void runAsSoonAsPossible(Runnable action) { if (Platform.isFxApplicationThread()) { action.run(); return; } else { eventBus.post(new JavaFXRunnerEvent(action)); } } @Inject  GripPlatform(EventBus eventBus); void runAsSoonAsPossible(Runnable action); @Subscribe void onJavaFXRunnerEvent(JavaFXRunnerEvent event);  }
@Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") public void testRunAsSoonAsPossibleRunsInCorrectThreadWhenNotCalledFromFXThread() throws Exception { final Waiter waiter = new Waiter(); platform.runAsSoonAsPossible(() -> { waiter.assertTrue(Platform.isFxApplicationThread()); waiter.resume(); }); waiter.await(); }
@Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") public void testRunAsSoonAsPossibleWhenCalledFromFXThread() { interact(() -> { final boolean[] hasRun = {false}; unRegisteredPlatform.runAsSoonAsPossible(() -> { assertTrue("Should have run in the JavaFX thread!", Platform.isFxApplicationThread()); hasRun[0] = true; }); assertTrue("When running in the JavaFX thread this should have run immediately in the same " + "thread", hasRun[0]); }); }
@Test @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") public void testRunAsSoonAsPossibleDoesNotDeadlockWhenRunInsideItself() throws Exception { final Waiter waiter = new Waiter(); platform.runAsSoonAsPossible(() -> { waiter.assertTrue(Platform.isFxApplicationThread()); platform.runAsSoonAsPossible(() -> waiter.assertTrue(Platform.isFxApplicationThread())); waiter.resume(); }); waiter.await(); }
@Test public void testRunAsSoonAsPossibleWillNotCallIfInterrupted() throws Exception { final boolean[] hasRun = {false}; Thread.currentThread().interrupt(); platform.runAsSoonAsPossible(() -> { hasRun[0] = true; }); assertFalse("runAsSoonAsPossible ran when interrupted", hasRun[0]); }