use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ActivityControllerTest method delayedTasks_areNotRunEagerly_whenActivityIsStarted_andSchedulerUnPaused.
@Test
@LooperMode(LEGACY)
public void delayedTasks_areNotRunEagerly_whenActivityIsStarted_andSchedulerUnPaused() {
// Regression test for issue #1509
final Scheduler s = Robolectric.getForegroundThreadScheduler();
final long startTime = s.getCurrentTime();
TestDelayedPostActivity activity = Robolectric.setupActivity(TestDelayedPostActivity.class);
assertWithMessage("before flush").that(activity.r2.wasRun).isFalse();
assertWithMessage("currentTime before flush").that(s.getCurrentTime()).isEqualTo(startTime);
s.advanceToLastPostedRunnable();
assertWithMessage("after flush").that(activity.r2.wasRun).isTrue();
assertWithMessage("currentTime after flush").that(s.getCurrentTime()).isEqualTo(startTime + 60000);
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ShadowHandlerTest method testDifferentLoopersGetDifferentQueues.
@Test
public void testDifferentLoopersGetDifferentQueues() {
Looper looper1 = newLooper(true);
ShadowLooper.pauseLooper(looper1);
Looper looper2 = newLooper(true);
ShadowLooper.pauseLooper(looper2);
// Make sure looper has a different scheduler to the first
shadowOf(looper2.getQueue()).setScheduler(new Scheduler());
Handler handler1 = new Handler(looper1);
handler1.post(new Say("first thing"));
Handler handler2 = new Handler(looper2);
handler2.post(new Say("second thing"));
shadowOf(looper2).idle();
assertThat(transcript).containsExactly("second thing");
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ShadowApplicationTest method getForegroundThreadScheduler_shouldMatchRuntimeEnvironment.
@Test
public void getForegroundThreadScheduler_shouldMatchRuntimeEnvironment() {
Scheduler s = new Scheduler();
RuntimeEnvironment.setMasterScheduler(s);
assertThat(Shadows.shadowOf(context).getForegroundThreadScheduler()).isSameInstanceAs(s);
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ShadowApplicationTest method getBackgroundThreadScheduler_shouldDifferFromRuntimeEnvironment_withAdvancedScheduling.
@Test
@LooperMode(LEGACY)
public void getBackgroundThreadScheduler_shouldDifferFromRuntimeEnvironment_withAdvancedScheduling() {
Scheduler s = new Scheduler();
RuntimeEnvironment.setMasterScheduler(s);
assertThat(Shadows.shadowOf(context).getBackgroundThreadScheduler()).isNotSameInstanceAs(s);
}
use of org.robolectric.util.Scheduler in project robolectric by robolectric.
the class ShadowLegacyLooperTest 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);
assertWithMessage("start").that(events).isEmpty();
Scheduler s = ShadowLooper.getShadowMainLooper().getScheduler();
assertThat(s).isSameInstanceAs(RuntimeEnvironment.getMasterScheduler());
assertThat(s).isSameInstanceAs(shadowOf(ht.getLooper()).getScheduler());
final long startTime = s.getCurrentTime();
s.runOneTask();
assertWithMessage("firstEvent").that(events).containsExactly("handler1");
assertWithMessage("firstEvent:time").that(s.getCurrentTime()).isEqualTo(100 + startTime);
s.runOneTask();
assertWithMessage("secondEvent").that(events).containsExactly("handler1", "handler2");
assertWithMessage("secondEvent:time").that(s.getCurrentTime()).isEqualTo(200 + startTime);
}
Aggregations