/** * Return the foreground scheduler (e.g. the UI thread scheduler). * * @return Foreground scheduler. */ public static Scheduler getForegroundThreadScheduler() { return ShadowApplication.getInstance().getForegroundThreadScheduler(); }
private long deltaTime() { return ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime() - startTime; }
@Override protected void done() { try { final D result = get(); ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() { @Override public void run() { realObject.deliverResult(result); } }); } catch (InterruptedException e) { // Ignore } catch (ExecutionException e) { throw new RuntimeException(e.getCause()); } } };
static long now() { if (ShadowApplication.getInstance() == null) { return 0; } return ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime(); }
@Implementation protected boolean post(Runnable action) { ShadowApplication.getInstance().getForegroundThreadScheduler().post(action); return true; }
@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 startScroll(int startX, int startY, int dx, int dy, int duration) { this.startX = startX; this.startY = startY; finalX = startX + dx; finalY = startY + dy; startTime = ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime(); this.duration = duration; started = true; // enque a dummy task so that the scheduler will actually run ShadowApplication.getInstance().getForegroundThreadScheduler().postDelayed(new Runnable() { @Override public void run() { // do nothing } }, duration); }
@Implementation protected void runOnUiThread(Runnable action) { ShadowApplication.getInstance().getForegroundThreadScheduler().post(action); }
/** * Enqueue a call to {@link AsyncTask#onProgressUpdate(Object[])} on UI looper (or run it * immediately if the looper it is not paused). * * @param values The progress values to update the UI with. * @see AsyncTask#publishProgress(Object[]) */ @Implementation protected void publishProgress(final Progress... values) { ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() { @Override public void run() { getBridge().onProgressUpdate(values); } }); }
@Implementation protected static void sleep(long millis) { if (ShadowApplication.getInstance() == null) { return; } nanoTime = millis * MILLIS_PER_NANO; ShadowApplication.getInstance().getForegroundThreadScheduler().advanceBy(millis); }
ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() { @Override public void run() { ShadowApplication.getInstance().getForegroundThreadScheduler().post(new Runnable() { @Override public void run() {
@Implementation protected static boolean setCurrentTimeMillis(long millis) { if (ShadowApplication.getInstance() == null) { return false; } if (now() > millis) { return false; } nanoTime = millis * MILLIS_PER_NANO; ShadowApplication.getInstance().getForegroundThreadScheduler().advanceTo(millis); return true; }
@Test public void getForegroundThreadScheduler_shouldMatchRuntimeEnvironment() { Scheduler s = new Scheduler(); RuntimeEnvironment.setMasterScheduler(s); final ShadowApplication shadowApplication = Shadows.shadowOf(context); assertThat(shadowApplication.getForegroundThreadScheduler()).isSameAs(s); }
@Test public void getThreadScheduler_shouldMatchRobolectricValue() { final ShadowApplication shadowApplication = Shadows.shadowOf(context); assertThat(shadowApplication.getForegroundThreadScheduler()).isSameAs(Robolectric.getForegroundThreadScheduler()); assertThat(shadowApplication.getBackgroundThreadScheduler()).isSameAs(Robolectric.getBackgroundThreadScheduler()); }
@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_setsBackgroundScheduler_toBeDifferentToForeground_byDefault() { bootstrapWrapper.callSetUpApplicationState(); final ShadowApplication shadowApplication = Shadow.extract(ApplicationProvider.getApplicationContext()); assertThat(shadowApplication.getBackgroundThreadScheduler()) .isNotSameAs(shadowApplication.getForegroundThreadScheduler()); }
@Test public void setUpApplicationState_configuresGlobalScheduler() { bootstrapWrapper.callSetUpApplicationState(); assertThat(RuntimeEnvironment.getMasterScheduler()) .isSameAs(ShadowLooper.getShadowMainLooper().getScheduler()); assertThat(RuntimeEnvironment.getMasterScheduler()) .isSameAs(ShadowApplication.getInstance().getForegroundThreadScheduler()); }
@Test public void setUpApplicationState_setsBackgroundScheduler_toBeSameAsForeground_whenAdvancedScheduling() { RoboSettings.setUseGlobalScheduler(true); try { bootstrapWrapper.callSetUpApplicationState(); final ShadowApplication shadowApplication = Shadow.extract(ApplicationProvider.getApplicationContext()); assertThat(shadowApplication.getBackgroundThreadScheduler()) .isSameAs(shadowApplication.getForegroundThreadScheduler()); assertThat(RuntimeEnvironment.getMasterScheduler()) .isSameAs(RuntimeEnvironment.getMasterScheduler()); } finally { RoboSettings.setUseGlobalScheduler(false); } }
@Before public void setUp() { TestApplication.applicationGraph.inject(this); controller = Robolectric.buildActivity(TestListActivity.class); activity = controller.create().postCreate(null).start().resume().get(); // inflate menu, see https://github.com/robolectric/robolectric/issues/1326 ShadowLooper.pauseMainLooper(); controller.visible(); ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable(); }