@Implementation public void removeMessages(Handler handler, int what, Object object) { Looper looper = ShadowLooper.getLooperForThread(looperThread); if (shadowOf(looper) instanceof CustomLooper && shadowOf(looper) != ShadowLooper.getShadowMainLooper()) { ((CustomLooper) shadowOf(looper)).removeMessages(handler, what, object); } } }
public static void runMainLooperOneTask() { getShadowMainLooper().runOneTask(); }
/** @deprecated Use {@link #idleMainLooper(long, TimeUnit)}. */ @Deprecated public static void idleMainLooper(long interval) { idleMainLooper(interval, TimeUnit.MILLISECONDS); }
/** * 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 "unpaused" state. * * @see #unPauseLooper */ public static void unPauseMainLooper() { getShadowMainLooper().unPause(); }
/** * Puts the main ShadowLooper in an "paused" state. * * @see #pauseLooper */ public static void pauseMainLooper() { getShadowMainLooper().pause(); }
public void addToObservable(@NonNull final Observable observable) { observable.addUpdatable(this); observables.add(observable); runUiThreadTasksIncludingDelayedTasks(); }
@Test public void reset_setsGlobalScheduler_forMainLooper_byDefault() { ShadowLooper sMainLooper = ShadowLooper.getShadowMainLooper(); Scheduler s = new Scheduler(); RuntimeEnvironment.setMasterScheduler(s); sMainLooper.reset(); assertThat(sMainLooper.getScheduler()).isSameAs(s); }
/** * Runs all runnable tasks (pending and future) that have been queued on the UI thread. Such tasks may be queued by * e.g. {@link android.app.Activity#runOnUiThread(Runnable)} or {@link android.os.AsyncTask#onPostExecute(Object)}. * * **Note:** calling this method does not pause or un-pause the scheduler, however the clock is advanced as * future tasks are run. * * @see #runUiThreadTasks */ public static void runUiThreadTasksIncludingDelayedTasks() { getShadowMainLooper().runToEndOfTasks(); }
@Override public void run() { // "Commit" layout (it will fail since it doesn't have compatible size specs) mLayoutThreadShadowLooper.runToEndOfTasks(); } });
/** * Runs any immediately runnable tasks previously queued on the UI thread, * e.g. by {@link android.app.Activity#runOnUiThread(Runnable)} or {@link android.os.AsyncTask#onPostExecute(Object)}. * * **Note:** calling this method does not pause or un-pause the scheduler. * * @see #runUiThreadTasksIncludingDelayedTasks */ public static void runUiThreadTasks() { getShadowMainLooper().idle(); }
mLayoutThreadShadowLooper.runOneTask(); mLayoutThreadShadowLooper.runToEndOfTasks();
@Test public void testWarmTexture() { Drawable drawable = mock(Drawable.class); TextureWarmer.WarmDrawable warmDrawable = new TextureWarmer.WarmDrawable(drawable, 1, 1); mTextureWarmer.warmDrawable(warmDrawable); mShadowLooper.runOneTask(); verify(drawable).draw(any(Canvas.class)); }
@Implementation public void quit() { if (this == getShadowMainLooper()) throw new RuntimeException("Main thread not allowed to quit"); quitUnchecked(); }
/** * Causes {@link Runnable}s that have been scheduled to run immediately to actually run. Does not advance the * scheduler's clock; */ public void idle() { idle(0, TimeUnit.MILLISECONDS); }
@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(); }
/** * Causes all of the {@link Runnable}s that have been scheduled to run while advancing the scheduler's clock to the * start time of the last scheduled {@link Runnable}. */ public void runToEndOfTasks() { getScheduler().advanceToLastPostedRunnable(); }
@Test public void sendEmptyMessageDelayed_sendsMessageAtCorrectTime() { ShadowLooper.pauseMainLooper(); Handler handler = new Handler(); handler.sendEmptyMessageDelayed(123, 500); assertThat(handler.hasMessages(123)).isTrue(); ShadowLooper.idleMainLooper(100); assertThat(handler.hasMessages(123)).isTrue(); ShadowLooper.idleMainLooper(400); assertThat(handler.hasMessages(123)).isFalse(); }
@Test public void setUpApplicationState_configuresGlobalScheduler() { bootstrapWrapper.callSetUpApplicationState(); assertThat(RuntimeEnvironment.getMasterScheduler()) .isSameAs(ShadowLooper.getShadowMainLooper().getScheduler()); assertThat(RuntimeEnvironment.getMasterScheduler()) .isSameAs(ShadowApplication.getInstance().getForegroundThreadScheduler()); }