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();
}
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);
}
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);
}
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);
}
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();
}
}
Aggregations