/** @deprecated Use `shadowOf({@link Looper#getMainLooper()})` instead. */ @Deprecated public static ShadowLooper getShadowMainLooper() { return shadowOf(Looper.getMainLooper()); }
/** * 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 shadow looper in an "unpaused" state (this is the default state). This means that * during tests, tasks sent to the looper will execute inline, immediately, on the calling (main) * thread instead of being queued, in a way similar to how Guava's "DirectExecutorService" works. * This is likely not to be what you want: it will cause code to be potentially executed in a * different order than how it would execute on the device, and if you are using certain Android * APIs (such as view animations) that are non-reentrant, they may not work at all or do * unpredictable things. For more information, see <a * href="https://github.com/robolectric/robolectric/issues/3369">this discussion</a>. * * @param looper the looper to pause */ public static void unPauseLooper(Looper looper) { shadowOf(looper).unPause(); }
@Implementation protected static void loop() { shadowOf(Looper.myLooper()).doLoop(); }
/** * 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; }
@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(); } }
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()); } }
/** @deprecated Use `shadowOf({@link Looper#getMainLooper()})` instead. */ @Deprecated public static ShadowLooper getShadowMainLooper() { return shadowOf(Looper.getMainLooper()); }
public static void pauseLooper(Looper looper) { shadowOf(looper).pause(); }
public static void unPauseLooper(Looper looper) { shadowOf(looper).unPause(); }
@Implementation protected static void loop() { shadowOf(Looper.myLooper()).doLoop(); }
/** * 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(); }
@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(); } }
public void quitUnchecked() { synchronized (realObject) { quit = true; realObject.notifyAll(); getScheduler().reset(); shadowOf(realObject.getQueue()).reset(); } }
/** * 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()); } }