Search in sources :

Example 6 with LooperMode

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

the class ShadowContextWrapperTest method sendBroadcast_shouldSendIntentUsingHandlerIfOneIsProvided.

@Test
@LooperMode(PAUSED)
public void sendBroadcast_shouldSendIntentUsingHandlerIfOneIsProvided() throws InterruptedException {
    HandlerThread handlerThread = new HandlerThread("test");
    handlerThread.start();
    Handler handler = new Handler(handlerThread.getLooper());
    assertNotSame(handler.getLooper(), Looper.getMainLooper());
    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            transcript.add("notified of " + intent.getAction() + " on thread " + Thread.currentThread().getName());
        }
    };
    contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz"), null, handler);
    assertThat(transcript).isEmpty();
    contextWrapper.sendBroadcast(new Intent("foo"));
    shadowOf(handlerThread.getLooper()).idle();
    assertThat(transcript).containsExactly("notified of foo on thread " + handlerThread.getName());
    handlerThread.quit();
}
Also used : Context(android.content.Context) HandlerThread(android.os.HandlerThread) Handler(android.os.Handler) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 7 with LooperMode

use of org.robolectric.annotation.LooperMode 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 8 with LooperMode

use of org.robolectric.annotation.LooperMode 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)

Example 9 with LooperMode

use of org.robolectric.annotation.LooperMode 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 10 with LooperMode

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

the class ShadowLegacyLooper method resetThreadLoopers.

@Resetter
public static synchronized void resetThreadLoopers() {
    // do not use looperMode() here, because its cached value might already have been reset
    if (ConfigurationRegistry.get(LooperMode.Mode.class) == LooperMode.Mode.PAUSED) {
        // ignore if realistic looper
        return;
    }
    // from being garbage collected.
    if (!isMainThread()) {
        throw new IllegalStateException("you should only be calling this from the main thread!");
    }
    synchronized (loopingLoopers) {
        for (Looper looper : loopingLoopers.values()) {
            synchronized (looper) {
                if (!shadowOf(looper).quit) {
                    looper.quit();
                } else {
                    // Reset the schedulers of all loopers. This prevents un-run tasks queued up in static
                    // background handlers from leaking to subsequent tests.
                    shadowOf(looper).getScheduler().reset();
                    shadowOf(looper.getQueue()).reset();
                }
            }
        }
    }
    // prepareMainLooper() is called, this might be null on that occasion.
    if (mainLooper != null) {
        shadowOf(mainLooper).reset();
    }
}
Also used : Looper(android.os.Looper) LooperMode(org.robolectric.annotation.LooperMode) Resetter(org.robolectric.annotation.Resetter)

Aggregations

LooperMode (org.robolectric.annotation.LooperMode)18 Test (org.junit.Test)15 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 InterstitialView (org.prebid.mobile.rendering.bidding.display.InterstitialView)1 VideoCreative (org.prebid.mobile.rendering.video.VideoCreative)1