/** * Pauses execution of tasks posted to the ShadowLooper. This means that during tests, tasks sent * to the looper will not execute immediately, but will be queued in a way that is similar to how * a real looper works. These queued tasks must be executed explicitly by calling {@link * #runToEndOftasks} or a similar method, otherwise they will not run at all before your test * ends. * * @param looper the looper to pause */ public static void pauseLooper(Looper looper) { shadowOf(looper).pause(); }
/** * Puts the main ShadowLooper in an "paused" state. * * @see #pauseLooper */ public static void pauseMainLooper() { getShadowMainLooper().pause(); }
public boolean setPaused(boolean shouldPause) { boolean wasPaused = isPaused(); if (shouldPause) { pause(); } else { unPause(); } return wasPaused; }
@Test public void testRemoveCallbacks() throws Exception { Handler handler = new Handler(); ShadowLooper shadowLooper = shadowOf(handler.getLooper()); shadowLooper.pause(); handler.post(scratchRunnable); handler.removeCallbacks(scratchRunnable); shadowLooper.unPause(); assertThat(scratchRunnable.wasRun).isFalse(); }
@Test public void testRecyclesResourceSynchronouslyIfNotAlreadyRecyclingResource() { Resource<?> resource = mockResource(); Shadows.shadowOf(Looper.getMainLooper()).pause(); recycler.recycle(resource); verify(resource).recycle(); }
@Before public void setUp() throws Exception { Shadows.shadowOf(Looper.getMainLooper()).pause(); adapterView = createAdapterView(); }
@Test public void testDoesNotRecycleChildResourceSynchronously() { Resource<?> parent = mockResource(); final Resource<?> child = mockResource(); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { recycler.recycle(child); return null; } }).when(parent).recycle(); Shadows.shadowOf(Looper.getMainLooper()).pause(); recycler.recycle(parent); verify(parent).recycle(); verify(child, never()).recycle(); Shadows.shadowOf(Looper.getMainLooper()).runOneTask(); verify(child).recycle(); } }
@Test public void testCanCallGetInOnAttachToWindowInFragmentInViewPager() { // Robolectric by default runs messages posted to the main looper synchronously, the // framework does not. We post // to the main thread here to work around an issue caused by a recursive method call so we // need (and reasonably // expect) our message to not run immediately Shadows.shadowOf(Looper.getMainLooper()).pause(); Robolectric.buildActivity(Issue117Activity.class).create().start().resume().visible(); }
@Test public void testReleasesResourceIfCancelledOnReady() { Looper looper = harness.mainHandler.getLooper(); Shadows.shadowOf(looper).pause(); final EngineJob<Object> job = harness.getJob(); job.start(harness.decodeJob); job.cancel(); job.onResourceReady(harness.resource, harness.dataSource); verify(harness.resource).recycle(); }
@Test public void shouldNotRefreshObservableIfReactivatedWithinSameCycle() { final Supplier supplier = mock(Supplier.class); when(supplier.get()).thenReturn(new Object()); final Repository repository = repositoryWithInitialValue(new Object()) .observe() .onUpdatesPerLoop() .thenGetFrom(supplier) .compile(); updatable.addToObservable(repository); looper.pause(); updatable.removeFromObservables(); updatable.addToObservable(repository); looper.unPause(); verify(supplier).get(); }
@Test public void shouldOnlyUpdateOncePerLooper() { final Supplier supplier = mock(Supplier.class); when(supplier.get()).thenReturn(new Object()); final Repository repository = repositoryWithInitialValue(new Object()) .observe(updateDispatcher) .onUpdatesPerLoop() .thenGetFrom(supplier) .compile(); updatable.addToObservable(repository); looper.pause(); updateDispatcher.update(); updateDispatcher.update(); updateDispatcher.update(); looper.unPause(); verify(supplier, times(2)).get(); }
@Test public void asyncIgnoredPre16() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 14); ShadowLooper mainLooper = ShadowLooper.getShadowMainLooper(); mainLooper.pause(); ShadowMessageQueue mainMessageQueue = shadowOf(Looper.getMainLooper().getQueue()); Scheduler main = AndroidSchedulers.from(Looper.getMainLooper(), true); main.scheduleDirect(new Runnable() { @Override public void run() { } }); Message message = mainMessageQueue.getHead(); assertFalse(message.isAsynchronous()); } }
@Test public void shouldResetQueue_whenLooperIsReset() { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); Handler h = new Handler(looper); ShadowLooper sLooper = shadowOf(looper); sLooper.pause(); h.post(new Runnable() { @Override public void run() { } }); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNotNull(); sLooper.reset(); assertThat(sLooper.getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNull(); }
@Test public void shouldThrowawayRunnableQueueIfLooperQuits() throws Exception { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); shadowOf(looper).pause(); shadowOf(looper).post(new Runnable() { @Override public void run() { } }, 0); looper.quit(); assertThat(shadowOf(looper).hasQuit()).named("hasQuit").isTrue(); assertThat(shadowOf(looper).getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNull(); }
public static void pauseMainLooper() { getShadowMainLooper().pause(); }
public static void pauseMainLooper() { getShadowMainLooper().pause(); }
public boolean setPaused(boolean shouldPause) { boolean wasPaused = isPaused(); if (shouldPause) { pause(); } else { unPause(); } return wasPaused; }
public boolean setPaused(boolean shouldPause) { boolean wasPaused = isPaused(); if (shouldPause) { pause(); } else { unPause(); } return wasPaused; }
public boolean setPaused(boolean shouldPause) { boolean wasPaused = isPaused(); if (shouldPause) { pause(); } else { unPause(); } return wasPaused; }
public boolean setPaused(boolean shouldPause) { boolean wasPaused = isPaused(); if (shouldPause) { pause(); } else { unPause(); } return wasPaused; }