/** * Return the background scheduler. * * @return Background scheduler. */ public static Scheduler getBackgroundThreadScheduler() { return ShadowApplication.getInstance().getBackgroundThreadScheduler(); }
static long now() { if (ShadowApplication.getInstance() == null) { return 0; } return ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime(); }
/** * Return the foreground scheduler (e.g. the UI thread scheduler). * * @return Foreground scheduler. */ public static Scheduler getForegroundThreadScheduler() { return ShadowApplication.getInstance().getForegroundThreadScheduler(); }
@Implementation protected static void sleep(long millis) { if (ShadowApplication.getInstance() == null) { return; } nanoTime = millis * MILLIS_PER_NANO; ShadowApplication.getInstance().getForegroundThreadScheduler().advanceBy(millis); }
/** * Runs any background tasks previously queued by {@link android.os.AsyncTask#execute(Object[])}. * * Note: calling this method does not pause or un-pause the scheduler. */ public static void runBackgroundTasks() { getInstance().getBackgroundThreadScheduler().advanceBy(0); }
@Implementation protected static LocalBroadcastManager getInstance(final Context context) { return ShadowApplication.getInstance().getSingleton(LocalBroadcastManager.class, new Provider<LocalBroadcastManager>() { @Override public LocalBroadcastManager get() { return ReflectionHelpers.callConstructor(LocalBroadcastManager.class, ClassParameter.from(Context.class, context)); } }); }
/** Discards the most recent {@code PowerManager.WakeLock}s */ @Resetter public static void reset() { ShadowApplication shadowApplication = ShadowApplication.getInstance(); if (shadowApplication != null) { shadowApplication.clearWakeLocks(); } }
private List<BroadcastReceiver> getConnectivityReceivers() { Intent connectivity = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); return ShadowApplication.getInstance().getReceiversForIntent(connectivity); }
@Implementation protected PowerManager.WakeLock newWakeLock(int flags, String tag) { PowerManager.WakeLock wl = Shadow.newInstanceOf(PowerManager.WakeLock.class); getInstance().addWakeLock(wl); return wl; }
@Implementation protected void postInvalidateDelayed(long delayMilliseconds) { ShadowApplication.getInstance().getForegroundThreadScheduler().postDelayed(new Runnable() { @Override public void run() { realView.invalidate(); } }, delayMilliseconds); }
@Implementation protected boolean postDelayed(Runnable action, long delayMills) { ShadowApplication.getInstance().getForegroundThreadScheduler().postDelayed(action, delayMills); return true; }
@Implementation protected void invokePopup(WindowManager.LayoutParams p) { ShadowApplication.getInstance().setLatestPopupWindow(realPopupWindow); directlyOn(realPopupWindow, PopupWindow.class, "invokePopup", ReflectionHelpers.ClassParameter.from(WindowManager.LayoutParams.class, p)); } }
@Test public void startActivities_withBundle_shouldStartAllActivities() { final Intent view = new Intent(Intent.ACTION_VIEW).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final Intent pick = new Intent(Intent.ACTION_PICK).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); contextWrapper.startActivities(new Intent[] {view, pick}, new Bundle()); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(pick); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(view); }
@Test public void startActivities_shouldStartAllActivities() { final Intent view = new Intent(Intent.ACTION_VIEW).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final Intent pick = new Intent(Intent.ACTION_PICK).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); contextWrapper.startActivities(new Intent[] {view, pick}); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(pick); assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(view); }
@Test public void testReturnsDefaultConnectivityMonitorWhenHasPermission() { ShadowApplication.getInstance().grantPermissions("android.permission.ACCESS_NETWORK_STATE"); ConnectivityMonitor connectivityMonitor = factory.build(RuntimeEnvironment.application, mock(ConnectivityMonitor.ConnectivityListener.class)); assertThat(connectivityMonitor).isInstanceOf(DefaultConnectivityMonitor.class); }
@Test(expected = ActivityNotFoundException.class) public void checkActivities_shouldSetValueOnShadowApplication() throws Exception { ShadowApplication.getInstance().checkActivities(true); context.startActivity( new Intent("i.dont.exist.activity").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); }
@Test @Config(minSdk = VERSION_CODES.O) public void startForegroundService() { Intent intent = new Intent(); context.startForegroundService(intent); assertThat(ShadowApplication.getInstance().getNextStartedService()).isEqualTo(intent); }
@Test public void getLatestPopupWindow() { PopupWindow pw = new PopupWindow(new LinearLayout(context)); pw.showAtLocation(new LinearLayout(context), Gravity.CENTER, 0, 0); PopupWindow latestPopupWindow = ShadowApplication.getInstance().getLatestPopupWindow(); assertThat(latestPopupWindow).isSameAs(pw); }
@Test public void removeFrameCallback_shouldRemoveCallback() { Choreographer instance = ShadowChoreographer.getInstance(); Choreographer.FrameCallback callback = mock(Choreographer.FrameCallback.class); instance.postFrameCallbackDelayed(callback, 1000); instance.removeFrameCallback(callback); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); verify(callback, never()).doFrame(anyLong()); }
@Test public void setUpApplicationState_configuresGlobalScheduler() { bootstrapWrapper.callSetUpApplicationState(); assertThat(RuntimeEnvironment.getMasterScheduler()) .isSameAs(ShadowLooper.getShadowMainLooper().getScheduler()); assertThat(RuntimeEnvironment.getMasterScheduler()) .isSameAs(ShadowApplication.getInstance().getForegroundThreadScheduler()); }