@Test public void getMainLooper_shouldBeInitialized_onBackgroundThread_evenWhenRobolectricApplicationIsNull() throws Exception { RuntimeEnvironment.application = null; final AtomicReference<Looper> mainLooperAtomicReference = new AtomicReference<>(); Thread backgroundThread = new Thread(new Runnable() { @Override public void run() { Looper mainLooper = Looper.getMainLooper(); mainLooperAtomicReference.set(mainLooper); } }, testName.getMethodName()); backgroundThread.start(); backgroundThread.join(); assertThat(mainLooperAtomicReference.get()).named("mainLooper").isSameAs(Looper.getMainLooper()); }
@Test public void reset_shouldEmptyMessagePool() { Message dummy1 = Message.obtain(); shadowOf(dummy1).recycleUnchecked(); Message dummy2 = Message.obtain(); assertThat(dummy2).named("before resetting").isSameAs(dummy1); shadowOf(dummy2).recycleUnchecked(); ShadowMessage.reset(); dummy1 = Message.obtain(); assertThat(dummy1).named("after resetting").isNotSameAs(dummy2); } }
@Test public void shouldSetUpSystemResources() { Resources systemResources = Resources.getSystem(); Resources appResources = ApplicationProvider.getApplicationContext().getResources(); assertThat(systemResources).named("system resources").isNotNull(); assertThat(systemResources.getString(android.R.string.copy)).named("system resource") .isEqualTo(appResources.getString(android.R.string.copy)); assertThat(appResources.getString(R.string.howdy)).named("app resource") .isNotNull(); try { systemResources.getString(R.string.howdy); fail("Expected Exception not thrown"); } catch (Resources.NotFoundException e) { } }
@Test public void test_setGetHead() { shadowQueue.setHead(testMessage); assertThat(shadowQueue.getHead()).named("getHead()").isSameAs(testMessage); }
@Test public void enqueueMessage_setsHead() { enqueueMessage(testMessage, 100); assertThat(shadowQueue.getHead()).named("head").isSameAs(testMessage); }
@Test public void testSetDataSourceString() throws IOException { DataSource ds = toDataSource("dummy"); ShadowMediaPlayer.addMediaInfo(ds, info); mediaPlayer.setDataSource("dummy"); assertThat(shadowMediaPlayer.getDataSource()).named("dataSource").isEqualTo(ds); }
@Test public void shouldInitializeAndBindApplicationButNotCallOnCreate() { assertThat((Application) ApplicationProvider.getApplicationContext()) .named("application") .isInstanceOf(MyTestApplication.class); assertThat(((MyTestApplication) ApplicationProvider.getApplicationContext()).onCreateWasCalled) .named("onCreate called") .isTrue(); if (RuntimeEnvironment.useLegacyResources()) { assertThat(RuntimeEnvironment.getAppResourceTable()) .named("Application resource loader") .isNotNull(); } }
@Override public void handleMessage(Message msg) { boolean inUse = callInstanceMethod(msg, "isInUse"); assertThat(inUse).named(msg.what + ":inUse").isTrue(); Message next = reflector(_Message_.class, msg).getNext(); assertThat(next).named(msg.what + ":next").isNull(); } };
@Test public void testSetDataSourceUri() throws IOException { Map<String, String> headers = new HashMap<>(); Uri uri = Uri.parse("file:/test"); DataSource ds = toDataSource(ApplicationProvider.getApplicationContext(), uri, headers); ShadowMediaPlayer.addMediaInfo(ds, info); mediaPlayer.setDataSource(ApplicationProvider.getApplicationContext(), uri, headers); assertThat(shadowMediaPlayer.getSourceUri()).named("sourceUri").isSameAs(uri); assertThat(shadowMediaPlayer.getDataSource()).named("dataSource").isEqualTo(ds); }
@Test public void testSetDataSourceFD() throws IOException { File tmpFile = File.createTempFile("MediaPlayerTest", null); try { tmpFile.deleteOnExit(); FileInputStream is = new FileInputStream(tmpFile); try { FileDescriptor fd = is.getFD(); DataSource ds = toDataSource(fd, 23, 524); ShadowMediaPlayer.addMediaInfo(ds, info); mediaPlayer.setDataSource(fd, 23, 524); assertThat(shadowMediaPlayer.getSourceUri()).named("sourceUri").isNull(); assertThat(shadowMediaPlayer.getDataSource()).named("dataSource") .isEqualTo(ds); } finally { is.close(); } } finally { tmpFile.delete(); } }
@Test public void shouldResetQueue_whenLooperIsReset() { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); Handler h = new Handler(looper); ShadowLooper sLooper = shadowOf(looper); sLooper.pause(); h.post(new Runnable() { @Override public void run() { } }); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNotNull(); sLooper.reset(); assertThat(sLooper.getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNull(); }
@Test public void scheduler_wontDispatchRemovedMessage_evenIfMessageReused() { final ArrayList<Long> runAt = new ArrayList<>(); ShadowLooper.pauseMainLooper(); Handler handler = new Handler() { @Override public void handleMessage(Message msg) { runAt.add(shadowOf(Looper.myLooper()).getScheduler().getCurrentTime()); } }; final long startTime = Robolectric.getForegroundThreadScheduler().getCurrentTime(); Message msg = handler.obtainMessage(123); handler.sendMessageDelayed(msg, 200); handler.removeMessages(123); Message newMsg = handler.obtainMessage(123); assertThat(newMsg).named("new message").isSameAs(msg); handler.sendMessageDelayed(newMsg, 400); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); // Original implementation had a bug which caused reused messages to still // be invoked at their original post time. assertThat(runAt).named("handledAt").containsExactly(startTime + 400L); }
@Test public void soStaticRefsToLoopersInAppWorksAcrossTests_shouldRetainSameLooperForMainThreadBetweenResetsButGiveItAFreshScheduler() throws Exception { Looper mainLooper = Looper.getMainLooper(); Scheduler scheduler = shadowOf(mainLooper).getScheduler(); shadowOf(mainLooper).quit = true; assertThat(ApplicationProvider.getApplicationContext().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, ((Application) ApplicationProvider.getApplicationContext()).getBaseContext())); assertThat(Looper.getMainLooper()).named("Looper.getMainLooper()").isSameAs(mainLooper); assertThat(application.getMainLooper()).named("app.getMainLooper()").isSameAs(mainLooper); assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isNotSameAs(scheduler); assertThat(shadowOf(mainLooper).getScheduler()).named("scheduler").isSameAs(s); assertThat(shadowOf(mainLooper).hasQuit()).named("quit").isFalse(); }
assertThat(binderMethod).named("Binder for " + method.toString()).isNotNull();
ShadowMediaPlayer.setCreateListener(createListener); assertThat(ShadowMediaPlayer.createListener) .named("createListener") .isSameAs(createListener); DataSource dummy = toDataSource("stuff"); assertThat(shadowMediaPlayer.getMediaInfo()).named("mediaInfo:before").isNotNull(); .named("createListener") .isNull();
@Test public void shouldThrowawayRunnableQueueIfLooperQuits() throws Exception { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); shadowOf(looper).pause(); shadowOf(looper).post(new Runnable() { @Override public void run() { } }, 0); looper.quit(); assertThat(shadowOf(looper).hasQuit()).named("hasQuit").isTrue(); assertThat(shadowOf(looper).getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNull(); }
private static void assertPropertyHasAnnotation( Property property, Class<? extends Annotation> annotationClass, String annotationString) { Excerpt annotationExcerpt = property.getAccessorAnnotations() .stream() .filter(excerpt -> asCompilableString(excerpt) .contains(annotationClass.getCanonicalName())) .findFirst() .orElse(null); assertThat(annotationExcerpt).named("property accessor annotations").isNotNull(); assertThat(asString(annotationExcerpt)).contains(String.format("%s%n", annotationString)); }
/** * Soft-deprecated in favor of {@link #named(String)}. */ public S labeled(String label) { return named(label); }
@Override public S named(String name) { return (S)super.named(name); }
public OptionalSubject(FailureStrategy failureStrategy, Optional<T> subject) { super(failureStrategy, subject); check().that((Object) subject).named("Optional<T>").isNotNull(); }