public String asString() { T data = getData(); return data instanceof String ? (String) data : null; }
@Override public List<TypedResource> getItems(TypedResource typedResource) { return (List<TypedResource>) typedResource.getData(); } }
public boolean isReference() { Object data = getData(); if (data instanceof String) { String s = (String) data; return !s.isEmpty() && s.charAt(0) == '@'; } return false; }
private StyleData getParent(StyleData style) { String parent = getParentStyleName(style); if (parent == null) return null; if (parent.startsWith("@")) parent = parent.substring(1); ResName styleRef = ResName.qualifyResName(parent, style.getPackageName(), "style"); styleRef = dereferenceResName(styleRef); // TODO: Refactor this to a ResourceLoaderChooser ResourceTable resourceProvider = "android".equals(styleRef.packageName) ? systemResourceTable : appResourceTable; TypedResource typedResource = resourceProvider.getValue(styleRef, config); if (typedResource == null) { StringBuilder builder = new StringBuilder("Could not find any resource") .append(" from reference ").append(styleRef) .append(" from ").append(style) .append(" with ").append(theme); throw new RuntimeException(builder.toString()); } Object data = typedResource.getData(); if (data instanceof StyleData) { return (StyleData) data; } else { StringBuilder builder = new StringBuilder(styleRef.toString()) .append(" does not resolve to a Style.") .append(" got ").append(data).append(" instead. ") .append(" from ").append(style) .append(" with ").append(theme); throw new RuntimeException(builder.toString()); } }
@HiddenApi @Implementation public CharSequence getResourceText(int ident) { TypedResource value = getAndResolve(ident, config, true); if (value == null) return null; return (CharSequence) value.getData(); }
if (typedResource.getData() == null || resType == ResType.NULL) { type = TypedValue.TYPE_NULL; } else if (typedResource.isReference()) {
private Style resolveStyle(@Nonnull ResName themeStyleName, Style themeStyleSet) { TypedResource themeStyleResource = resourceTable.getValue(themeStyleName, config); if (themeStyleResource == null) return null; StyleData themeStyleData = (StyleData) themeStyleResource.getData(); if (themeStyleSet == null) { themeStyleSet = new ThemeStyleSet(); } return new StyleResolver(resourceTable, legacyShadowOf(AssetManager.getSystem()).getResourceTable(), themeStyleData, themeStyleSet, themeStyleName, config); }
TypedResource attrTypeData = shadowAssetManager.getAttrTypeData(attribute.resName); if (attrTypeData != null) { AttrData attrData = (AttrData) attrTypeData.getData(); String format = attrData.getFormat(); String[] types = format.split("\\|");
@HiddenApi @Implementation public boolean getResourceValue(int ident, int density, TypedValue outValue, boolean resolveRefs) { TypedResource value = getAndResolve(ident, config, resolveRefs); if (value == null) return false; getConverter(value).fillTypedValue(value.getData(), outValue); return true; }
@Test public void shouldReturnRawResourcesWithExtensions() throws Exception { String f = (String) resourceTable.getValue(R.raw.raw_resource, new ResTable_config()).getData(); assertThat(f) .isEqualTo( TestUtil.testResources() .getResourceBase() .resolve("raw") .resolve("raw_resource.txt") .toString()); }
@Test public void shouldReturnRawResourcesWithoutExtensions() throws Exception { String f = (String) resourceTable.getValue(R.raw.raw_no_ext, new ResTable_config()).getData(); assertThat(f) .isEqualTo( TestUtil.testResources() .getResourceBase() .resolve("raw") .resolve("raw_no_ext") .toString()); } }
@Test public void shouldLoadDrawableXmlResources() { TypedResource value = resourceTable.getValue(new ResName("org.robolectric", "drawable", "rainbow"), config); assertThat(value).isNotNull(); assertThat(value.getResType()).isEqualTo(ResType.DRAWABLE); assertThat(value.isFile()).isTrue(); assertThat((String) value.getData()).contains("rainbow.xml"); }
@Test public void shouldLoadIdResourcesDefinedByItemTag() { TypedResource value = resourceTable.getValue(new ResName("org.robolectric", "id", "id_declared_in_item_tag"), config); assertThat(value).isNotNull(); assertThat(value.getResType()).isEqualTo(ResType.CHAR_SEQUENCE); assertThat(value.isReference()).isFalse(); assertThat(value.asString()).isEmpty(); assertThat((String) value.getData()).isEmpty(); }
@Test public void shouldLoadDrawableBitmapResources() { TypedResource value = resourceTable.getValue(new ResName("org.robolectric", "drawable", "an_image"), config); assertThat(value).isNotNull(); assertThat(value.getResType()).isEqualTo(ResType.DRAWABLE); assertThat(value.isFile()).isTrue(); assertThat((String) value.getData()).contains("an_image.png"); }
@Test public void shouldLoadDrawableBitmapResourcesDefinedByItemTag() { TypedResource value = resourceTable.getValue(new ResName("org.robolectric", "drawable", "example_item_drawable"), config); assertThat(value).isNotNull(); assertThat(value.getResType()).isEqualTo(ResType.DRAWABLE); assertThat(value.isReference()).isTrue(); assertThat((String) value.getData()).isEqualTo("@drawable/an_image"); }
@Test public void shouldNotOverwriteValuesWithMatchingQualifiers() { ResBundle bundle = new ResBundle(); XmlContext xmlContext = mock(XmlContext.class); when(xmlContext.getQualifiers()).thenReturn(Qualifiers.parse("--")); when(xmlContext.getConfig()).thenReturn(new ResTable_config()); when(xmlContext.getPackageName()).thenReturn("org.robolectric"); TypedResource firstValue = new TypedResource<>("first_value", ResType.CHAR_SEQUENCE, xmlContext); TypedResource secondValue = new TypedResource<>("second_value", ResType.CHAR_SEQUENCE, xmlContext); bundle.put(new ResName("org.robolectric", "string", "resource_name"), firstValue); bundle.put(new ResName("org.robolectric", "string", "resource_name"), secondValue); assertThat(bundle.get(new ResName("org.robolectric", "string", "resource_name"), from("")).getData()).isEqualTo("first_value"); }
@Test public void testStyleDataIsLoadedCorrectly() throws Exception { TypedResource typedResource = resourceTable.getValue(new ResName("android", "style", "Theme_Holo"), new ResTable_config()); StyleData styleData = (StyleData) typedResource.getData(); assertThat(styleData.getName()).isEqualTo("Theme_Holo"); assertThat(styleData.getParent()).isEqualTo("Theme"); assertThat(styleData.getPackageName()).isEqualTo("android"); assertThat(styleData.getAttrValue(new ResName("android", "attr", "colorForeground")).value) .isEqualTo("@android:color/bright_foreground_holo_dark"); } }
@Test public void ignoresBTags() throws Exception { topLevelNodeHandler.addHandler("resources", new NodeHandler() .addHandler("item[@type='string']", new StaxValueLoader(resourceTable, "string", ResType.CHAR_SEQUENCE)) ); parse("<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">" + "<item type=\"string\" name=\"sms_short_code_details\">This <b>may cause charges</b> on your mobile account.</item>" + "</resources>"); assertThat(resourceTable.getValue(new ResName("pkg:string/sms_short_code_details"), new ResTable_config()).getData()) .isEqualTo("This may cause charges on your mobile account."); }
@Test public void ignoresXliffTags() throws Exception { topLevelNodeHandler.addHandler("resources", new NodeHandler() .addHandler("string", new StaxValueLoader(resourceTable, "string", ResType.CHAR_SEQUENCE)) ); parse("<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">" + "<string name=\"preposition_for_date\">on <xliff:g id=\"date\" example=\"May 29\">%s</xliff:g></string>" + "</resources>"); assertThat(resourceTable.getValue(new ResName("pkg:string/preposition_for_date"), new ResTable_config()).getData()) .isEqualTo("on %s"); }