private void updateToolbar(final NavLocation location) { uiThreadRunner.post(new Runnable() { @Override public void run() { doUpdateNavIcon(location); } }); }
@Test public void should_be_able_to_post_on_default_ui_thread_runner() { new MvcGraph().uiThreadRunner.post(mock(Runnable.class)); new MvcGraph().uiThreadRunner.postDelayed(mock(Runnable.class), 100); }
/** * Clear {@link Graph.Monitor} which will be called the graph is about to inject or release an object */ public void clearMonitors() { if (uiThreadRunner.isOnUiThread()) { graph.clearMonitors(); } else { uiThreadRunner.post(new Runnable() { @Override public void run() { graph.clearMonitors(); } }); } }
/** * Clear {@link Provider.DereferenceListener}s which will be called when the last cached * instance of an injected contract is freed. */ public void clearDereferencedListeners() { if (uiThreadRunner.isOnUiThread()) { graph.clearDereferencedListeners(); } else { uiThreadRunner.post(new Runnable() { @Override public void run() { graph.clearDereferencedListeners(); } }); } } }
/** * Register {@link Graph.Monitor} which will be called the graph is about to inject or release an object * * @param monitor The monitor */ public void registerMonitor(final Graph.Monitor monitor) { if (uiThreadRunner.isOnUiThread()) { graph.registerMonitor(monitor); } else { uiThreadRunner.post(new Runnable() { @Override public void run() { graph.registerMonitor(monitor); } }); } }
/** * Register {@link Graph.Monitor} which will be called the graph is about to inject or release an object * * @param monitor The monitor */ public void unregisterMonitor(final Graph.Monitor monitor) { if (uiThreadRunner.isOnUiThread()) { graph.unregisterMonitor(monitor); } else { uiThreadRunner.post(new Runnable() { @Override public void run() { graph.unregisterMonitor(monitor); } }); } }
/** * Dereference an injectable object. When it's not referenced by anything else after this * dereferencing, release its cached instance if possible. * @param instance the instance is to release * @param type the type of the object * @param qualifier the qualifier */ public <T> void dereference(T instance, Class<T> type, Annotation qualifier) throws ProviderMissingException { if (uiThreadRunner.isOnUiThread()) { } else { uiThreadRunner.post(new Runnable() { @Override public void run() { } }); } graph.dereference(instance, type, qualifier, Inject.class); }
/** * Unregister {@link Provider.DereferenceListener} which will be called when the last cached * instance of an injected contract is freed. * * @param onProviderFreedListener The listener */ public void unregisterDereferencedListener( final Provider.DereferenceListener onProviderFreedListener) { if (uiThreadRunner.isOnUiThread()) { graph.unregisterDereferencedListener(onProviderFreedListener); } else { uiThreadRunner.post(new Runnable() { @Override public void run() { graph.unregisterDereferencedListener(onProviderFreedListener); } }); } }
/** * Register {@link Provider.DereferenceListener} which will be called when the provider * * @param onProviderFreedListener The listener */ public void registerDereferencedListener( final Provider.DereferenceListener onProviderFreedListener) { if (uiThreadRunner.isOnUiThread()) { graph.registerDereferencedListener(onProviderFreedListener); } else { uiThreadRunner.post(new Runnable() { @Override public void run() { graph.registerDereferencedListener(onProviderFreedListener); } }); } }
@Test public void default_uiThreadRunner_should_post_on_same_thread() { final Thread thread = Thread.currentThread(); graph.uiThreadRunner.post(new Runnable() { @Override public void run() { Assert.assertTrue(Thread.currentThread() == thread); } }); }
uiThreadRunner.post(new Runnable() { @Override public void run() {
@Override @Before public void setUp() throws Exception { super.setUp(); graph.uiThreadRunner = mock(UiThreadRunner.class); when(graph.uiThreadRunner.isOnUiThread()).thenReturn(true); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return null; } }).when(graph.uiThreadRunner).post(any(Runnable.class)); }
@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)); }
/** * Add {@link Component} to the graph. * * @param component The root {@link Component} of this graph. */ public void setRootComponent(final MvcComponent component) { if (uiThreadRunner.isOnUiThread()) { try { graph.setRootComponent(component); } catch (Graph.IllegalRootComponentException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.setRootComponent(component); } catch (Graph.IllegalRootComponentException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
/** * Same as {@link #use(Class, Annotation, Consumer)} except using un-qualified injectable type. * @param type The type of the injectable instance * @param consumer Consume to use the instance */ public <T> void use(final Class<T> type, final Consumer<T> consumer) { if (uiThreadRunner.isOnUiThread()) { try { graph.use(type, Inject.class, consumer); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.use(type, Inject.class, consumer); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
/** * Inject all fields annotated by {@link Inject}. References of controllers will be * incremented. * * @param target The target object whose fields annotated by {@link Inject} will be injected. */ public void inject(final Object target) { if (uiThreadRunner.isOnUiThread()) { try { graph.inject(target, Inject.class); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.inject(target, Inject.class); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
/** * Release cached instances held by fields of target object. References of instances of the * instances will be decremented. Once the reference count of a contract type reaches 0, it will * be removed from the instances. * * @param target of which the object fields will be released. */ public void release(final Object target) { if (uiThreadRunner.isOnUiThread()) { try { graph.release(target, Inject.class); } catch (ProviderMissingException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.release(target, Inject.class); } catch (ProviderMissingException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
@Override @Before public void setUp() throws Exception { super.setUp(); originalUiThreadRunner = Mvc.graph().uiThreadRunner; uiThreadRunnerMock = mock(UiThreadRunner.class); when(uiThreadRunnerMock.isOnUiThread()).thenReturn(true); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return null; } }).when(uiThreadRunnerMock).post(any(Runnable.class)); Mvc.graph().uiThreadRunner = uiThreadRunnerMock; }
return null; }).when(uiThreadRunner).post(any(Runnable.class));