@Test public void shouldSetNewScheduler_whenLooperIsReset() { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); ShadowLooper sLooper = shadowOf(looper); Scheduler old = sLooper.getScheduler(); sLooper.reset(); assertThat(old).isNotSameAs(sLooper.getScheduler()); }
@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 reset_setsNewScheduler_forNonMainLooper_byDefault() { HandlerThread ht = getHandlerThread(); ShadowLooper sLooper = shadowOf(ht.getLooper()); Scheduler old = sLooper.getScheduler(); sLooper.reset(); assertThat(sLooper.getScheduler()).isNotSameAs(old); assertThat(sLooper.getScheduler()).isNotSameAs(RuntimeEnvironment.getMasterScheduler()); }
@Test public void reset_setsSchedulerToMaster_forNonMainLooper_withAdvancedScheduling() { HandlerThread ht = getHandlerThread(); ShadowLooper sLooper = shadowOf(ht.getLooper()); Scheduler s = new Scheduler(); RuntimeEnvironment.setMasterScheduler(s); setAdvancedScheduling(); sLooper.reset(); assertThat(sLooper.getScheduler()).isSameAs(s); }
@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); }
@Test public void reset_setsGlobalScheduler_forMainLooper_withAdvancedScheduling() { setAdvancedScheduling(); ShadowLooper sMainLooper = ShadowLooper.getShadowMainLooper(); Scheduler s = new Scheduler(); RuntimeEnvironment.setMasterScheduler(s); sMainLooper.reset(); assertThat(sMainLooper.getScheduler()).isSameAs(s); }
@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(); }
@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(); } } } } // 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(); } }
@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(); } } } } // 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(); } }
@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(); } } } } // 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(); } }
@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(); } }