Search in sources :

Example 1 with LooperMode

use of org.robolectric.annotation.LooperMode in project robolectric by robolectric.

the class RuntimeEnvironmentTest method isMainThread_forNewThread_withoutSwitch.

@Test
@LooperMode(LEGACY)
public void isMainThread_forNewThread_withoutSwitch() throws InterruptedException {
    final AtomicBoolean res = new AtomicBoolean();
    final CountDownLatch finished = new CountDownLatch(1);
    Thread t = new Thread() {

        @Override
        public void run() {
            res.set(RuntimeEnvironment.isMainThread());
            finished.countDown();
        }
    };
    RuntimeEnvironment.setMainThread(Thread.currentThread());
    t.start();
    if (!finished.await(1000, MILLISECONDS)) {
        throw new InterruptedException("Thread " + t + " didn't finish timely");
    }
    assertWithMessage("testThread").that(RuntimeEnvironment.isMainThread()).isTrue();
    assertWithMessage("thread t").that(res.get()).isFalse();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 2 with LooperMode

use of org.robolectric.annotation.LooperMode in project robolectric by robolectric.

the class RuntimeEnvironmentTest method isMainThread_forNewThread_withSwitch.

@Test
@LooperMode(LEGACY)
public void isMainThread_forNewThread_withSwitch() throws InterruptedException {
    final AtomicBoolean res = new AtomicBoolean();
    final CountDownLatch finished = new CountDownLatch(1);
    Thread t = new Thread(() -> {
        res.set(RuntimeEnvironment.isMainThread());
        finished.countDown();
    });
    RuntimeEnvironment.setMainThread(t);
    t.start();
    if (!finished.await(1000, MILLISECONDS)) {
        throw new InterruptedException("Thread " + t + " didn't finish timely");
    }
    assertWithMessage("testThread").that(RuntimeEnvironment.isMainThread()).isFalse();
    assertWithMessage("thread t").that(res.get()).isTrue();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 3 with LooperMode

use of org.robolectric.annotation.LooperMode in project robolectric by robolectric.

the class ShadowApplicationTest method getBackgroundThreadScheduler_shouldDifferFromRuntimeEnvironment_byDefault.

@Test
@LooperMode(LEGACY)
public void getBackgroundThreadScheduler_shouldDifferFromRuntimeEnvironment_byDefault() {
    Scheduler s = new Scheduler();
    RuntimeEnvironment.setMasterScheduler(s);
    assertThat(Shadows.shadowOf(context).getBackgroundThreadScheduler()).isNotSameInstanceAs(RuntimeEnvironment.getMasterScheduler());
}
Also used : Scheduler(org.robolectric.util.Scheduler) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 4 with LooperMode

use of org.robolectric.annotation.LooperMode in project robolectric by robolectric.

the class ShadowActivityTest method shouldQueueUiTasksWhenUiThreadIsPaused.

@Test
@LooperMode(LEGACY)
public void shouldQueueUiTasksWhenUiThreadIsPaused() throws Exception {
    shadowOf(getMainLooper()).pause();
    activity = Robolectric.setupActivity(DialogLifeCycleActivity.class);
    TestRunnable runnable = new TestRunnable();
    activity.runOnUiThread(runnable);
    assertFalse(runnable.wasRun);
    shadowOf(getMainLooper()).idle();
    assertTrue(runnable.wasRun);
}
Also used : TestRunnable(org.robolectric.util.TestRunnable) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 5 with LooperMode

use of org.robolectric.annotation.LooperMode in project robolectric by robolectric.

the class LooperModeConfigurerClassTest method shouldUseRealisticShadows.

@Test
@LooperMode(Mode.PAUSED)
public void shouldUseRealisticShadows() {
    assertThat(ConfigurationRegistry.get(LooperMode.Mode.class)).isSameInstanceAs(Mode.PAUSED);
    ShadowLooper looper = Shadow.extract(Looper.getMainLooper());
    assertThat(looper).isInstanceOf(ShadowPausedLooper.class);
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) LooperMode(org.robolectric.annotation.LooperMode) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Aggregations

LooperMode (org.robolectric.annotation.LooperMode)17 Test (org.junit.Test)14 Scheduler (org.robolectric.util.Scheduler)5 BroadcastReceiver (android.content.BroadcastReceiver)2 Intent (android.content.Intent)2 Handler (android.os.Handler)2 HandlerThread (android.os.HandlerThread)2 Looper (android.os.Looper)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Resetter (org.robolectric.annotation.Resetter)2 ShadowLooper (org.robolectric.shadows.ShadowLooper)2 TestRunnable (org.robolectric.util.TestRunnable)2 Dialog (android.app.Dialog)1 Context (android.content.Context)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 AssumptionViolatedException (org.junit.AssumptionViolatedException)1 Mode (org.robolectric.annotation.LooperMode.Mode)1 SQLiteMode (org.robolectric.annotation.SQLiteMode)1