Search in sources :

Example 21 with Scheduler

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

Example 22 with Scheduler

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

Example 23 with Scheduler

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

Example 24 with Scheduler

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

Example 25 with Scheduler

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

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