use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ShadowLooperTest method withAdvancedScheduling_shouldDispatchMessagesOnBothLoopers_whenAdvancingForegroundThread.
@Test
public void withAdvancedScheduling_shouldDispatchMessagesOnBothLoopers_whenAdvancingForegroundThread() {
setAdvancedScheduling();
ShadowLooper.pauseMainLooper();
HandlerThread ht = getHandlerThread();
Handler handler1 = new Handler(ht.getLooper());
Handler handler2 = new Handler();
final ArrayList<String> events = new ArrayList<>();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
events.add("handler1");
}
}, 100);
handler2.postDelayed(new Runnable() {
@Override
public void run() {
events.add("handler2");
}
}, 200);
assertThat(events).as("start").isEmpty();
Scheduler s = ShadowLooper.getShadowMainLooper().getScheduler();
assertThat(s).isSameAs(RuntimeEnvironment.getMasterScheduler()).isSameAs(shadowOf(ht.getLooper()).getScheduler());
final long startTime = s.getCurrentTime();
s.runOneTask();
assertThat(events).as("firstEvent").containsExactly("handler1");
assertThat(s.getCurrentTime()).as("firstEvent:time").isEqualTo(100 + startTime);
s.runOneTask();
assertThat(events).as("secondEvent").containsExactly("handler1", "handler2");
assertThat(s.getCurrentTime()).as("secondEvent:time").isEqualTo(200 + startTime);
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ShadowLooperTest method reset_setsGlobalScheduler_forMainLooper_withAdvancedScheduling.
@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);
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ShadowLooperTest method soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler.
@Test
public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() throws Exception {
Looper mainLooper = Looper.getMainLooper();
Scheduler scheduler = shadowOf(mainLooper).getScheduler();
shadowOf(mainLooper).quit = true;
assertThat(RuntimeEnvironment.application.getMainLooper()).isSameAs(mainLooper);
Scheduler s = new Scheduler();
RuntimeEnvironment.setMasterScheduler(s);
ShadowLooper.resetThreadLoopers();
Application application = new Application();
ReflectionHelpers.callInstanceMethod(application, "attach", ReflectionHelpers.ClassParameter.from(Context.class, RuntimeEnvironment.application.getBaseContext()));
assertThat(Looper.getMainLooper()).as("Looper.getMainLooper()").isSameAs(mainLooper);
assertThat(application.getMainLooper()).as("app.getMainLooper()").isSameAs(mainLooper);
assertThat(shadowOf(mainLooper).getScheduler()).as("scheduler").isNotSameAs(scheduler).isSameAs(s);
assertThat(shadowOf(mainLooper).hasQuit()).as("quit").isFalse();
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class RuntimeEnvironmentTest method getSetMasterScheduler.
@Test
@LooperMode(LEGACY)
public void getSetMasterScheduler() {
Scheduler s = new Scheduler();
RuntimeEnvironment.setMasterScheduler(s);
assertThat(RuntimeEnvironment.getMasterScheduler()).isSameInstanceAs(s);
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ActivityControllerTest method pendingTasks_areRunEagerly_whenActivityIsStarted_andSchedulerUnPaused.
@Test
@LooperMode(LEGACY)
public void pendingTasks_areRunEagerly_whenActivityIsStarted_andSchedulerUnPaused() {
final Scheduler s = Robolectric.getForegroundThreadScheduler();
final long startTime = s.getCurrentTime();
TestDelayedPostActivity activity = Robolectric.setupActivity(TestDelayedPostActivity.class);
assertWithMessage("immediate task").that(activity.r1.wasRun).isTrue();
assertWithMessage("currentTime").that(s.getCurrentTime()).isEqualTo(startTime);
}
Aggregations