private boolean isLegacyAssetManager() { return ShadowAssetManager.useLegacy(); }
@Override public Class<? extends T> pickShadowClass() { if (ShadowAssetManager.useLegacy()) { return legacyShadowClass; } else { if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.P) { return binary9ShadowClass; } else { return binaryShadowClass; } } } }
@Test public void attrsToTypedArray_shouldAllowMockedAttributeSets() throws Exception { if (!useLegacy()) return; AttributeSet mockAttributeSet = mock(AttributeSet.class); when(mockAttributeSet.getAttributeCount()).thenReturn(1); when(mockAttributeSet.getAttributeNameResource(0)).thenReturn(android.R.attr.windowBackground); when(mockAttributeSet.getAttributeName(0)).thenReturn("android:windowBackground"); when(mockAttributeSet.getAttributeValue(0)).thenReturn("value"); resources.obtainAttributes(mockAttributeSet, new int[]{android.R.attr.windowBackground}); }
/** * Allows for the programmatic creation of an {@link AttributeSet}. * * Useful for testing {@link View} classes without the need for creating XML snippets. */ public static org.robolectric.android.AttributeSetBuilder buildAttributeSet() { if (useLegacy()) { return new AttributeSetBuilderImpl( new LegacyResourceResolver(RuntimeEnvironment.application, RuntimeEnvironment.getCompileTimeResourceTable())) {}; } else { return new AttributeSetBuilderImpl( new ArscResourceResolver(RuntimeEnvironment.application)) {}; } }
@Test @Config(qualifiers = "hdpi") public void openNonAssetShouldOpenCorrectAssetBasedOnQualifierHdpi() throws IOException { if (!useLegacy()) return; InputStream inputStream = assetManager.openNonAsset(0, "res/drawable/robolectric.png", 0); assertThat(countBytes(inputStream)).isEqualTo(23447); }
@Before public void setup() throws Exception { assumeTrue(useLegacy()); resources = ApplicationProvider.getApplicationContext().getResources(); }
@Test @Config(qualifiers = "mdpi") public void openNonAssetShouldOpenCorrectAssetBasedOnQualifierMdpi() throws IOException { if (!useLegacy()) return; InputStream inputStream = assetManager.openNonAsset(0, "res/drawable/robolectric.png", 0); assertThat(countBytes(inputStream)).isEqualTo(8141); }
@Test public void openRawResourceFd_returnsNull_todo_FIX() throws Exception { if (useLegacy()) { assertThat(resources.openRawResourceFd(R.raw.raw_resource)).isNull(); } else { assertThat(resources.openRawResourceFd(R.raw.raw_resource)).isNotNull(); } }
@Before public void setUp() { assumeTrue(useLegacy()); optsForO = RuntimeEnvironment.getApiLevel() >= O ? "nowidecg-lowdr-" : ""; }
@Test public void openNonAssetShouldOpenFileFromAndroidJar() throws IOException { String fileName = "res/raw/fallbackring.ogg"; if (useLegacy()) { // Not the real full path (it's in .m2/repository), but it only cares about the last folder and file name; // retrieves the uncompressed, un-version-qualified file from raw-res/... fileName = "jar:" + fileName; } InputStream inputStream = assetManager.openNonAsset(0, fileName, 0); assertThat(countBytes(inputStream)).isEqualTo(14611); }
@Test public void obtainStyledAttributesShouldCheckXmlFirst_andFollowReferences() throws Exception { // TODO: investigate failure with binary resources assumeTrue(useLegacy()); // This simulates a ResourceProvider built from a 21+ SDK as viewportHeight / viewportWidth were introduced in API 21 // but the public ID values they are assigned clash with private com.android.internal.R values on older SDKs. This // test ensures that even on older SDKs, on calls to obtainStyledAttributes() Robolectric will first check for matching // resource ID values in the AttributeSet before checking the theme. AttributeSet attributes = Robolectric.buildAttributeSet() .addAttribute(android.R.attr.viewportWidth, "@dimen/dimen20px") .addAttribute(android.R.attr.viewportHeight, "@dimen/dimen30px") .build(); TypedArray typedArray = ApplicationProvider.getApplicationContext() .getTheme() .obtainStyledAttributes( attributes, new int[] {android.R.attr.viewportWidth, android.R.attr.viewportHeight}, 0, 0); assertThat(typedArray.getDimension(0, 0)).isEqualTo(20f); assertThat(typedArray.getDimension(1, 0)).isEqualTo(30f); typedArray.recycle(); }
@Test public void openNonAssetShouldOpenRealAssetFromResources() throws IOException { InputStream inputStream = assetManager.openNonAsset(0, "res/drawable/an_image.png", 0); // expect different sizes in binary vs file resources int expectedFileSize = useLegacy() ? 6559 : 5138; int bytes = countBytes(inputStream); if (bytes != 6559 && bytes != 5138) { fail("Expected 5138 or 6559 bytes for image but got " + bytes); } }
@Test public void getXml_shouldHavePackageContextForReferenceResolution() throws Exception { if (!useLegacy()) { return; } XmlResourceParserImpl xmlResourceParser = (XmlResourceParserImpl) resources.getXml(R.xml.preferences); assertThat(xmlResourceParser.qualify("?ref")).isEqualTo("?org.robolectric:attr/ref"); xmlResourceParser = (XmlResourceParserImpl) resources.getXml(android.R.layout.list_content); assertThat(xmlResourceParser.qualify("?ref")).isEqualTo("?android:attr/ref"); } }
@Test public void forSystemResources_unknownResourceIdsShouldReportPackagesSearched() throws IOException { if (!useLegacy()) return; expectedException.expect(Resources.NotFoundException.class); expectedException.expectMessage("Resource ID #0xffffffff"); Resources.getSystem().newTheme().applyStyle(-1, false); assetManager.openNonAsset(0, "res/drawable/does_not_exist.png", 0); }
@Test public void shouldIncludeStyleableAttributesThatDoNotHaveACorrespondingEntryInAttrClass() throws Exception { assumeTrue(useLegacy()); // This covers a corner case in Framework resources where an attribute is mentioned in a styleable array, e.g: R.styleable.Toolbar_buttonGravity but there is no corresponding R.attr.buttonGravity assertThat(RuntimeEnvironment.getSystemResourceTable() .getResourceId(new ResName("android", "attr", "buttonGravity"))).isGreaterThan(0); } }
@Test public void openFd_shouldProvideFileDescriptorForDeflatedAsset() throws Exception { assumeTrue(!useLegacy()); expectedException.expect(FileNotFoundException.class); expectedException.expectMessage("This file can not be opened as a file descriptor; it is probably compressed"); assetManager.openFd("deflatedAsset.xml"); }
@Test public void openNonAssetShouldThrowExceptionWhenFileDoesNotExist() throws IOException { assumeTrue(useLegacy()); expectedException.expect(IOException.class); expectedException.expectMessage( "res/drawable/does_not_exist.png"); assetManager.openNonAsset(0, "res/drawable/does_not_exist.png", 0); }
@Test public void unknownResourceIdsShouldReportPackagesSearched() throws IOException { assumeTrue(useLegacy()); expectedException.expect(Resources.NotFoundException.class); expectedException.expectMessage("Resource ID #0xffffffff"); resources.newTheme().applyStyle(-1, false); assetManager.openNonAsset(0, "res/drawable/does_not_exist.png", 0); }
if (ShadowAssetManager.useLegacy()) { populateAssetPaths(appResources.getAssets(), appManifest);
@Test public void whenStyleAttrResolutionFails_attrsToTypedArray_returnsNiceErrorMessage() throws Exception { if (!useLegacy()) return; expectedException.expect(RuntimeException.class); expectedException.expectMessage( "no value for org.robolectric:attr/styleNotSpecifiedInAnyTheme " + "in theme with applied styles: [Style org.robolectric:Theme.Robolectric (and parents)]"); Resources.Theme theme = resources.newTheme(); theme.applyStyle(R.style.Theme_Robolectric, false); legacyShadowOf(assetManager) .attrsToTypedArray( resources, Robolectric.buildAttributeSet() .setStyleAttribute("?attr/styleNotSpecifiedInAnyTheme") .build(), new int[] {R.attr.string1}, 0, ((ShadowLegacyTheme) Shadow.extract(theme)).getNativePtr(), 0); }