Mvc { public static MvcGraph graph() { if (graph == null) { graph = new MvcGraph(); try { graph.getRootComponent().register(new Object() { @Provides @EventBusC public EventBus eventBusC() { return new EventBusImpl(); } @Provides @EventBusV public EventBus eventBusV() { return new EventBusImpl() { @Override public void post(final Object event) { if (graph.uiThreadRunner.isOnUiThread()) { postEvent(event); } else { graph.uiThreadRunner.post(new Runnable() { @Override public void run() { postEvent(event); } }); } } private void postEvent(Object event) { super.post(event); } }; } @Provides public ExecutorService executorService() { return Executors.newFixedThreadPool(10, new ThreadFactory() { @Override public Thread newThread(final Runnable r) { return new Thread(new Runnable() { @Override public void run() { Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1); r.run(); } }, "MvcBackgroundThread"); } }); } }); } catch (ProvideException e) { throw new MvcGraphException("Failed to register base Mvc dependencies", e); } catch (ProviderConflictException e) { throw new MvcGraphException("Failed to register base Mvc dependencies", e); } } return graph; }  static MvcGraph graph();  }
@Test public void eventBusV_should_post_event_to_ui_thread_strait_away_from_ui_thread() throws Exception { Mvc.graph().inject(this); when(uiThreadRunnerMock.isOnUiThread()).thenReturn(true); eventBusV.post(""); verify(uiThreadRunnerMock, times(2)).isOnUiThread(); verify(uiThreadRunnerMock, times(0)).post(any(Runnable.class)); }
@Test public void eventBusV_should_post_event_to_ui_thread_from_non_ui_thread() throws Exception { Mvc.graph().inject(this); when(uiThreadRunnerMock.isOnUiThread()).thenReturn(false); eventBusV.post(""); verify(uiThreadRunnerMock, times(2)).isOnUiThread(); verify(uiThreadRunnerMock, times(1)).post(any(Runnable.class)); }
@Test public void should_be_able_to_schedule_runnable_on_new_thread() { Mvc.graph().inject(this); executorService.submit(new Runnable() { @Override public void run() { } }); }