/** * Returns the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. * This scheduler is managed by the Looper's associated queue. * * @return the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. */ public Scheduler getScheduler() { return shadowOf(realObject.getQueue()).getScheduler(); }
/** * Causes all enqueued tasks to be discarded, and pause state to be reset */ public void reset() { shadowOf(realObject.getQueue()).reset(); resetScheduler(); quit = false; }
public void quitUnchecked() { synchronized (realObject) { quit = true; realObject.notifyAll(); getScheduler().reset(); shadowOf(realObject.getQueue()).reset(); } }
public void resetScheduler() { ShadowMessageQueue shadowMessageQueue = shadowOf(realObject.getQueue()); if (realObject == Looper.getMainLooper() || RoboSettings.isUseGlobalScheduler()) { shadowMessageQueue.setScheduler(RuntimeEnvironment.getMasterScheduler()); } else { shadowMessageQueue.setScheduler(new Scheduler()); } }
@Resetter public static synchronized void resetThreadLoopers() { // Blech. We need to keep the main looper because somebody might refer to it in a static // field. The other loopers need to be wrapped in WeakReferences so that they are not prevented from // being garbage collected. if (!isMainThread()) { throw new IllegalStateException("you should only be calling this from the main thread!"); } synchronized (loopingLoopers) { for (Looper looper : loopingLoopers.values()) { synchronized (looper) { if (!shadowOf(looper).quit) { looper.quit(); } else { // Reset the schedulers of all loopers. This prevents un-run tasks queued up in static // background handlers from leaking to subsequent tests. shadowOf(looper).getScheduler().reset(); shadowOf(looper.getQueue()).reset(); } } } } // Because resetStaticState() is called by ParallelUniverse on startup before prepareMainLooper() is // called, this might be null on that occasion. if (mainLooper != null) { shadowOf(mainLooper).reset(); } }
@Test public void testDifferentLoopersGetDifferentQueues() throws Exception { Looper looper1 = newLooper(true); ShadowLooper.pauseLooper(looper1); Looper looper2 = newLooper(true); ShadowLooper.pauseLooper(looper2); // Make sure looper has a different scheduler to the first shadowOf(looper2.getQueue()).setScheduler(new Scheduler()); Handler handler1 = new Handler(looper1); handler1.post(new Say("first thing")); Handler handler2 = new Handler(looper2); handler2.post(new Say("second thing")); shadowOf(looper2).idle(); assertThat(transcript).containsExactly("second thing"); }
@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 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()); } }
@Before public void setUp() throws Exception { // Queues and loopers are closely linked; can't easily test one without the other. looper = newLooper(); handler = new TestHandler(looper); queue = looper.getQueue(); shadowQueue = shadowOf(queue); scheduler = shadowQueue.getScheduler(); scheduler.pause(); testMessage = handler.obtainMessage(); quitField = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? "mQuitting" : "mQuiting"; }
@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(); }
/** * Returns the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. * This scheduler is managed by the Looper's associated queue. * * @return the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. */ public Scheduler getScheduler() { return shadowOf(realObject.getQueue()).getScheduler(); }
/** * Returns the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. * This scheduler is managed by the Looper's associated queue. * * @return the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. */ public Scheduler getScheduler() { return shadowOf(realObject.getQueue()).getScheduler(); }
/** * Returns the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. * This scheduler is managed by the Looper's associated queue. * * @return the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. */ public Scheduler getScheduler() { return shadowOf(realObject.getQueue()).getScheduler(); }
/** * Returns the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. * This scheduler is managed by the Looper's associated queue. * * @return the {@link org.robolectric.util.Scheduler} that is being used to manage the enqueued tasks. */ public Scheduler getScheduler() { return shadowOf(realObject.getQueue()).getScheduler(); }
/** * Causes all enqueued tasks to be discarded, and pause state to be reset */ public void reset() { shadowOf(realObject.getQueue()).reset(); resetScheduler(); quit = false; }
/** * Causes all enqueued tasks to be discarded, and pause state to be reset */ public void reset() { shadowOf(realObject.getQueue()).reset(); resetScheduler(); quit = false; }
/** * Causes all enqueued tasks to be discarded, and pause state to be reset */ public void reset() { shadowOf(realObject.getQueue()).reset(); resetScheduler(); quit = false; }
public void resetScheduler() { ShadowMessageQueue shadowMessageQueue = shadowOf(realObject.getQueue()); if (realObject == Looper.getMainLooper() || RoboSettings.isUseGlobalScheduler()) { shadowMessageQueue.setScheduler(RuntimeEnvironment.getMasterScheduler()); } else { shadowMessageQueue.setScheduler(new Scheduler()); } }
public void resetScheduler() { ShadowMessageQueue sQueue = shadowOf(realObject.getQueue()); if (this == getShadowMainLooper() || RoboSettings.isUseGlobalScheduler()) { sQueue.setScheduler(RuntimeEnvironment.getMasterScheduler()); } else { sQueue.setScheduler(new Scheduler()); } }
public void resetScheduler() { ShadowMessageQueue sQueue = shadowOf(realObject.getQueue()); if (this == getShadowMainLooper() || RoboSettings.isUseGlobalScheduler()) { sQueue.setScheduler(RuntimeEnvironment.getMasterScheduler()); } else { sQueue.setScheduler(new Scheduler()); } }