Search in sources :

Example 16 with Scheduler

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);
}
Also used : HandlerThread(android.os.HandlerThread) Scheduler(org.robolectric.util.Scheduler) ArrayList(java.util.ArrayList) Handler(android.os.Handler) Test(org.junit.Test)

Example 17 with Scheduler

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);
}
Also used : Scheduler(org.robolectric.util.Scheduler) Test(org.junit.Test)

Example 18 with Scheduler

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();
}
Also used : Context(android.content.Context) Looper(android.os.Looper) Scheduler(org.robolectric.util.Scheduler) Application(android.app.Application) Test(org.junit.Test)

Example 19 with Scheduler

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);
}
Also used : Scheduler(org.robolectric.util.Scheduler) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 20 with Scheduler

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);
}
Also used : Scheduler(org.robolectric.util.Scheduler) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Aggregations

Scheduler (org.robolectric.util.Scheduler)30 Test (org.junit.Test)22 HandlerThread (android.os.HandlerThread)8 Handler (android.os.Handler)6 Looper (android.os.Looper)6 LooperMode (org.robolectric.annotation.LooperMode)5 Application (android.app.Application)4 Context (android.content.Context)3 ApplicationInfo (android.content.pm.ApplicationInfo)2 Message (android.os.Message)2 ArrayList (java.util.ArrayList)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 ShadowLooper.shadowMainLooper (org.robolectric.shadows.ShadowLooper.shadowMainLooper)2 ActivityThread (android.app.ActivityThread)1 Instrumentation (android.app.Instrumentation)1 LoadedApk (android.app.LoadedApk)1 JobScheduler (android.app.job.JobScheduler)1 PackageManager (android.content.pm.PackageManager)1 Configuration (android.content.res.Configuration)1 Resources (android.content.res.Resources)1