static void assertEdgeNotInGraphErrorMessage(Throwable throwable) { assertThat(throwable).hasMessageThat().startsWith(EDGE_STRING); assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_NOT_IN_GRAPH); }
static void assertNodeNotInGraphErrorMessage(Throwable throwable) { assertThat(throwable).hasMessageThat().startsWith(NODE_STRING); assertThat(throwable).hasMessageThat().contains(ERROR_ELEMENT_NOT_IN_GRAPH); }
@Test public void shouldWriteToExternalCacheDir() throws Exception { assertThat(context.getExternalCacheDir()).isNotNull(); File cacheTest = new File(context.getExternalCacheDir(), "__test__"); assertThat(cacheTest.getAbsolutePath()) .startsWith(System.getProperty("java.io.tmpdir")); assertThat(cacheTest.getAbsolutePath()) .endsWith(File.separator + "__test__"); try (FileOutputStream fos = new FileOutputStream(cacheTest)) { fos.write("test".getBytes(UTF_8)); } assertThat(cacheTest.exists()).isTrue(); }
@Test public void testGetPackage() { assertThat(DummyClass.class.getClassLoader()).isInstanceOf(SandboxClassLoader.class); assertThat(DummyClass.class.getPackage()).isNotNull(); assertThat(DummyClass.class.getName()).startsWith(DummyClass.class.getPackage().getName()); }
@Test public void shouldWriteToCacheDir() throws Exception { assertThat(context.getCacheDir()).isNotNull(); File cacheTest = new File(context.getCacheDir(), "__test__"); assertThat(cacheTest.getAbsolutePath()) .startsWith(System.getProperty("java.io.tmpdir")); assertThat(cacheTest.getAbsolutePath()) .endsWith(File.separator + "__test__"); try (FileOutputStream fos = new FileOutputStream(cacheTest)) { fos.write("test".getBytes(UTF_8)); } assertThat(cacheTest.exists()).isTrue(); }
@Test public void shouldDelegateToObjectToStringIfShadowHasNone() throws Exception { assertThat(new Toast(ApplicationProvider.getApplicationContext()).toString()) .startsWith("android.widget.Toast@"); }
@Test public void provideLazyFails() { @Module class ProvidesLazyModule { @Provides Lazy<Object> provideObject() { return null; } } try { ObjectGraph.createWith(new TestingLoader(), new ProvidesLazyModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).startsWith("@Provides method must not return Lazy directly: "); assertThat(e.getMessage()).endsWith("ProvidesLazyModule.provideObject"); } }
@Test public void provideProviderFails() { @Module class ProvidesProviderModule { @Provides Provider<Object> provideObject() { return null; } } try { ObjectGraph.createWith(new TestingLoader(), new ProvidesProviderModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).startsWith("@Provides method must not return Provider directly: "); assertThat(e.getMessage()).endsWith("ProvidesProviderModule.provideObject"); } }
@Test public void startActivityForResultAndReceiveResult_whenNoIntentMatches_shouldThrowException() throws Exception { ThrowOnResultActivity activity = Robolectric.buildActivity(ThrowOnResultActivity.class).get(); activity.startActivityForResult(new Intent().setType("audio/*"), 123); activity.startActivityForResult(new Intent().setType("image/*"), 456); Intent requestIntent = new Intent().setType("video/*"); try { shadowOf(activity).receiveResult(requestIntent, Activity.RESULT_OK, new Intent().setData(Uri.parse("content:foo"))); fail(); } catch (Exception e) { assertThat(e.getMessage()).startsWith("No intent matches " + requestIntent); } }
@Test public void provideRawProviderFails() { @Module class ProvidesRawProviderModule { @Provides Provider provideObject() { return null; } } try { ObjectGraph.createWith(new TestingLoader(), new ProvidesRawProviderModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).startsWith("@Provides method must not return Provider directly: "); assertThat(e.getMessage()).endsWith("ProvidesRawProviderModule.provideObject"); } }
@Test public void moduleExtendingClassThrowsException() { try { ObjectGraph.createWith(new TestingLoader(), new ThreadModule()); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).startsWith("Modules must not extend from other classes: "); } }
@Test public void provideRawLazyFails() { @Module class ProvidesRawLazyModule { @Provides Lazy provideObject() { return null; } } try { ObjectGraph.createWith(new TestingLoader(), new ProvidesRawLazyModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).startsWith("@Provides method must not return Lazy directly: "); assertThat(e.getMessage()).endsWith("ProvidesRawLazyModule.provideObject"); } } }
@Test public void queryCreatedDuringTransactionThrows() { //noinspection CheckResult db.newTransaction(); try { //noinspection CheckResult db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).startsWith("Cannot create observable query in transaction."); } }
@Test public void whenPrefixedWithPlus_applyQualifiers_shouldOverlayQualifiers() throws Exception { Bootstrap.applyQualifiers("+en ru-rRU-h123dp-large +fr-w321dp-small", RuntimeEnvironment.getApiLevel(), configuration, displayMetrics); String outQualifiers = ConfigurationV25.resourceQualifierString(configuration, displayMetrics); assertThat(outQualifiers).startsWith("fr-ldltr-sw321dp-w321dp-h426dp-small"); }
@Test public void whenAllPrefixedWithPlus_applyQualifiers_shouldOverlayQualifiers() throws Exception { Bootstrap.applyQualifiers("+xxhdpi +ru-rRU-h123dp-large +fr-w321dp-small", RuntimeEnvironment.getApiLevel(), configuration, displayMetrics); String outQualifiers = ConfigurationV25.resourceQualifierString(configuration, displayMetrics); assertThat(outQualifiers).startsWith("fr-ldltr-sw321dp-w321dp-h426dp-small"); assertThat(outQualifiers).contains("-xxhdpi-"); } }
@Test public void spaceSeparated_applyQualifiers_shouldReplaceQualifiers() throws Exception { Bootstrap.applyQualifiers("ru-rRU-h123dp-large fr-w321dp", RuntimeEnvironment.getApiLevel(), configuration, displayMetrics); String outQualifiers = ConfigurationV25.resourceQualifierString(configuration, displayMetrics); assertThat(outQualifiers).startsWith("fr-ldltr-sw321dp-w321dp-h470dp-normal"); }
@Test public void subscriptionWhenStopped() { subscriptionManager.stop(); SubscriptionManagerCallbackAdapter<Operation.Data> subscriptionManagerCallback = new SubscriptionManagerCallbackAdapter<>(); subscriptionManager.subscribe(subscription1, subscriptionManagerCallback); assertThat(subscriptionManager.state).isEqualTo(RealSubscriptionManager.State.STOPPED); assertThat(subscriptionManagerCallback.error).isInstanceOf(ApolloSubscriptionException.class); assertThat(subscriptionManagerCallback.error.getMessage()).startsWith("Illegal state: STOPPED"); }
@Test public void stringLong() { String str = "aaaaaaaaaabbbbbbbbbbccccccccccdddddddddd"; Value v = Value.string(str); assertThat(v.getString()).isEqualTo(str); assertThat(v.toString()).hasLength(32); assertThat(v.toString()).startsWith(str.substring(0, 32 - 3)); assertThat(v.toString()).endsWith("..."); }
@Test public void illegalResourceQualifierThrowsException() { TypedResource<String> val1 = createStringTypedResource("en-v12"); resMap.put(resName, val1); try { resMap.pick(resName, from("nosuchqualifier")); fail("Expected exception to be caught"); } catch (IllegalArgumentException e) { assertThat(e).hasMessageThat().startsWith("Invalid qualifiers \"nosuchqualifier\""); } }
@Test public void testByteArrayOfNonZeroCannotBeCastedToZeroes() { parcel.writeByteArray(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 1}); assertThat(parcel.dataSize()).named("total size").isEqualTo(16); parcel.setDataPosition(0); assertThat(parcel.readInt()).isEqualTo(9); try { assertThat(parcel.readInt()).isEqualTo(0); fail("expected to fail"); } catch (RuntimeException e) { assertThat(e) .hasCauseThat() .hasMessageThat() .startsWith("Looking for Integer at position 4, found byte[]"); assertThat(e) .hasCauseThat() .hasMessageThat() .endsWith("taking 12 bytes, and it is non-portable to reinterpret it"); } }