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