Search in sources :

Example 16 with ResName

use of org.robolectric.res.ResName in project robolectric by robolectric.

the class ResourceTableFactoryIntegrationTest method shouldIncludeStyleableAttributesThatDoNotHaveACorrespondingEntryInAttrClass.

@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);
}
Also used : ResName(org.robolectric.res.ResName) Test(org.junit.Test)

Example 17 with ResName

use of org.robolectric.res.ResName in project robolectric by robolectric.

the class ResourceLoaderTest method shouldMakeInternalResourcesAvailable.

@Test
public void shouldMakeInternalResourcesAvailable() throws Exception {
    ResourceTable resourceProvider = RuntimeEnvironment.getSystemResourceTable();
    ResName internalResource = new ResName("android", "string", "badPin");
    Integer resId = resourceProvider.getResourceId(internalResource);
    assertThat(resId).isNotNull();
    assertThat(resourceProvider.getResName(resId)).isEqualTo(internalResource);
    Class<?> internalRIdClass = Robolectric.class.getClassLoader().loadClass("com.android.internal.R$" + internalResource.type);
    int internalResourceId;
    internalResourceId = (Integer) internalRIdClass.getDeclaredField(internalResource.name).get(null);
    assertThat(resId).isEqualTo(internalResourceId);
    assertThat(ApplicationProvider.getApplicationContext().getResources().getString(resId)).isEqualTo("The old PIN you typed isn't correct.");
}
Also used : ResName(org.robolectric.res.ResName) Robolectric(org.robolectric.Robolectric) ResourceTable(org.robolectric.res.ResourceTable) Test(org.junit.Test)

Example 18 with ResName

use of org.robolectric.res.ResName in project robolectric by robolectric.

the class ShadowLegacyAssetManager method convertAndFill.

private void convertAndFill(AttributeResource attribute, TypedValue outValue, ResTable_config config, boolean resolveRefs) {
    if (attribute.isNull()) {
        outValue.type = TypedValue.TYPE_NULL;
        outValue.data = TypedValue.DATA_NULL_UNDEFINED;
        return;
    } else if (attribute.isEmpty()) {
        outValue.type = TypedValue.TYPE_NULL;
        outValue.data = TypedValue.DATA_NULL_EMPTY;
        return;
    }
    // short-circuit Android caching of loaded resources cuz our string positions don't remain stable...
    outValue.assetCookie = Converter.getNextStringCookie();
    outValue.changingConfigurations = 0;
    // TODO: Handle resource and style references
    if (attribute.isStyleReference()) {
        return;
    }
    while (attribute.isResourceReference()) {
        Integer resourceId;
        ResName resName = attribute.getResourceReference();
        if (attribute.getReferenceResId() != null) {
            resourceId = attribute.getReferenceResId();
        } else {
            resourceId = getResourceTable().getResourceId(resName);
        }
        if (resourceId == null) {
            throw new Resources.NotFoundException("unknown resource " + resName);
        }
        outValue.type = TypedValue.TYPE_REFERENCE;
        if (!resolveRefs) {
            // Just return the resourceId if resolveRefs is false.
            outValue.data = resourceId;
            return;
        }
        outValue.resourceId = resourceId;
        TypedResource dereferencedRef = getResourceTable().getValue(resName, config);
        if (dereferencedRef == null) {
            Logger.strict("couldn't resolve %s from %s", resName.getFullyQualifiedName(), attribute);
            return;
        } else {
            if (dereferencedRef.isFile()) {
                outValue.type = TypedValue.TYPE_STRING;
                outValue.data = 0;
                outValue.assetCookie = Converter.getNextStringCookie();
                outValue.string = dereferencedRef.asString();
                return;
            } else if (dereferencedRef.getData() instanceof String) {
                attribute = new AttributeResource(attribute.resName, dereferencedRef.asString(), resName.packageName);
                if (attribute.isResourceReference()) {
                    continue;
                }
                if (resolveRefs) {
                    Converter.getConverter(dereferencedRef.getResType()).fillTypedValue(attribute.value, outValue);
                    return;
                }
            }
        }
        break;
    }
    if (attribute.isNull()) {
        outValue.type = TypedValue.TYPE_NULL;
        return;
    }
    TypedResource attrTypeData = getAttrTypeData(attribute.resName);
    if (attrTypeData != null) {
        AttrData attrData = (AttrData) attrTypeData.getData();
        String format = attrData.getFormat();
        String[] types = format.split("\\|");
        Arrays.sort(types, ATTRIBUTE_TYPE_PRECIDENCE);
        for (String type : types) {
            // already handled above
            if ("reference".equals(type))
                continue;
            Converter converter = Converter.getConverterFor(attrData, type);
            if (converter != null) {
                if (converter.fillTypedValue(attribute.value, outValue)) {
                    return;
                }
            }
        }
    } else {
        /**
         * In cases where the runtime framework doesn't know this attribute, e.g: viewportHeight (added in 21) on a
         * KitKat runtine, then infer the attribute type from the value.
         *
         * TODO: When we are able to pass the SDK resources from the build environment then we can remove this
         * and replace the NullResourceLoader with simple ResourceProvider that only parses attribute type information.
         */
        ResType resType = ResType.inferFromValue(attribute.value);
        Converter.getConverter(resType).fillTypedValue(attribute.value, outValue);
    }
}
Also used : AttributeResource(org.robolectric.res.AttributeResource) AttrData(org.robolectric.res.AttrData) TypedResource(org.robolectric.res.TypedResource) FileTypedResource(org.robolectric.res.FileTypedResource) FileNotFoundException(java.io.FileNotFoundException) ResName(org.robolectric.res.ResName) ResType(org.robolectric.res.ResType)

Example 19 with ResName

use of org.robolectric.res.ResName in project robolectric by robolectric.

the class ShadowLegacyAssetManager method loadXmlResourceParser.

protected XmlResourceParser loadXmlResourceParser(int resId, String type) throws Resources.NotFoundException {
    ResName resName = getResName(resId);
    ResName resolvedResName = resolveResName(resName, config);
    if (resolvedResName == null) {
        throw new RuntimeException("couldn't resolve " + resName.getFullyQualifiedName());
    }
    resName = resolvedResName;
    XmlBlock block = getResourceTable().getXml(resName, config);
    if (block == null) {
        throw new Resources.NotFoundException(resName.getFullyQualifiedName());
    }
    ResourceTable resourceProvider = ResourceIds.isFrameworkResource(resId) ? RuntimeEnvironment.getSystemResourceTable() : RuntimeEnvironment.getCompileTimeResourceTable();
    return getXmlResourceParser(resourceProvider, block, resName.packageName);
}
Also used : XmlBlock(org.robolectric.res.builder.XmlBlock) FileNotFoundException(java.io.FileNotFoundException) ResName(org.robolectric.res.ResName) ResourceTable(org.robolectric.res.ResourceTable)

Example 20 with ResName

use of org.robolectric.res.ResName in project robolectric by robolectric.

the class ShadowLegacyAssetManager method openNonAsset.

@HiddenApi
@Implementation
public final InputStream openNonAsset(int cookie, String fileName, int accessMode) throws IOException {
    final ResName resName = qualifyFromNonAssetFileName(fileName);
    final FileTypedResource typedResource = (FileTypedResource) getResourceTable().getValue(resName, config);
    if (typedResource == null) {
        throw new IOException("Unable to find resource for " + fileName);
    }
    InputStream stream;
    if (accessMode == AssetManager.ACCESS_STREAMING) {
        stream = Fs.getInputStream(typedResource.getPath());
    } else {
        stream = new ByteArrayInputStream(Fs.getBytes(typedResource.getPath()));
    }
    if (RuntimeEnvironment.getApiLevel() >= P) {
        Asset asset = Asset.newFileAsset(typedResource);
        long assetPtr = Registries.NATIVE_ASSET_REGISTRY.register(asset);
        // Camouflage the InputStream as an AssetInputStream so subsequent instanceof checks pass.
        stream = ShadowAssetInputStream.createAssetInputStream(stream, assetPtr, realObject);
    }
    return stream;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Asset(org.robolectric.res.android.Asset) ResName(org.robolectric.res.ResName) FileTypedResource(org.robolectric.res.FileTypedResource) IOException(java.io.IOException) HiddenApi(org.robolectric.annotation.HiddenApi) Implementation(org.robolectric.annotation.Implementation)

Aggregations

ResName (org.robolectric.res.ResName)26 AttributeResource (org.robolectric.res.AttributeResource)9 TypedValue (android.util.TypedValue)6 FileTypedResource (org.robolectric.res.FileTypedResource)6 ResourceTable (org.robolectric.res.ResourceTable)6 HiddenApi (org.robolectric.annotation.HiddenApi)4 Implementation (org.robolectric.annotation.Implementation)4 TypedResource (org.robolectric.res.TypedResource)4 SuppressLint (android.annotation.SuppressLint)2 PermissionInfo (android.content.pm.PermissionInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 PermissionItemData (org.robolectric.manifest.PermissionItemData)2 AttrData (org.robolectric.res.AttrData)2 EmptyStyle (org.robolectric.res.EmptyStyle)2 ResType (org.robolectric.res.ResType)2 Style (org.robolectric.res.Style)2 ThemeStyleSet (org.robolectric.res.ThemeStyleSet)2