TabParser(@NonNull Context context, @NonNull BottomBarTab.Config defaultTabConfig, @XmlRes int tabsXmlResId) { this.context = context; this.defaultTabConfig = defaultTabConfig; this.parser = context.getResources().getXml(tabsXmlResId); }
public XmlResourceParser getParser(Context context, ServiceInfo serviceInfo, String name) { Bundle meta = serviceInfo.metaData; if (meta != null) { int xmlId = meta.getInt(name); if (xmlId != 0) { try { return getResources(context, serviceInfo.applicationInfo).getXml(xmlId); } catch (Exception e) { e.printStackTrace(); } } } return null; }
public synchronized void parse(Context action) { // First checking the class namespace for config.xml int id = action.getResources().getIdentifier("app_config", "xml", action.getClass().getPackage() .getName()); if (id == 0) { // If we couldn't find config.xml there, we'll look in the namespace from AndroidManifest.xml id = action.getResources().getIdentifier("app_config", "xml", action.getPackageName()); if (id == 0) { Log.e(TAG, "res/xml/app_config.xml is missing!"); return; } } parse(action.getResources().getXml(id)); }
@Override public void subscribe(ObservableEmitter<String> emitter) { try { XmlPullParser xpp=resources.getXml(R.xml.words); while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) { if (xpp.getEventType()==XmlPullParser.START_TAG) { if (xpp.getName().equals("word")) { emitter.onNext(xpp.getAttributeValue(0)); } } xpp.next(); } emitter.onComplete(); } catch (Exception e) { emitter.onError(e); } } }
@Test(expected = Resources.NotFoundException.class) public void testGetXml_nonxmlfile() { resources.getXml(R.drawable.an_image); }
@Test(expected = Resources.NotFoundException.class) public void testGetXml_nonexistentResource() { resources.getXml(0); }
@Test public void obtainAttributes_shouldReturnValuesFromResources() throws Exception { XmlPullParser parser = resources.getXml(R.xml.xml_attrs); parser.next(); parser.next(); AttributeSet attributes = Xml.asAttributeSet(parser); TypedArray typedArray = resources .obtainAttributes(attributes, new int[]{android.R.attr.title, android.R.attr.scrollbarFadeDuration}); assertThat(typedArray.getString(0)).isEqualTo("Android Title"); assertThat(typedArray.getInt(1, 0)).isEqualTo(1111); typedArray.recycle(); }
@Test public void getXml_withNonFile_throwsNotFoundException() throws Exception { try { resources.getXml(R.string.hello); fail("should throw"); } catch (Resources.NotFoundException e) { // cool } try { resources.getXml(-1234); fail("should throw"); } catch (Resources.NotFoundException e) { // cool } }
@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"); } }
@Before public void setUp() throws Exception { context = ApplicationProvider.getApplicationContext(); parser = context.getResources().getXml(R.xml.preferences); }
private XmlResourceParser getFirstElementAttrSet(int resId) throws Exception { XmlResourceParser xml = resources.getXml(resId); assertThat(xml.next()).isEqualTo(XmlPullParser.START_DOCUMENT); assertThat(xml.nextTag()).isEqualTo(XmlPullParser.START_TAG); return (XmlResourceParser) Xml.asAttributeSet(xml); }
@Test public void obtainStyledAttributes_shouldCheckXmlFirst_fromXmlLoadedFromResources() throws Exception { // 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. XmlResourceParser xml = context.getResources().getXml(R.drawable.vector); xml.next(); xml.next(); AttributeSet attributeSet = Xml.asAttributeSet(xml); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributeSet, new int[] { android.R.attr.viewportWidth, android.R.attr.viewportHeight }, 0, 0); assertThat(typedArray.getFloat(0, 0)).isEqualTo(12.0f); assertThat(typedArray.getFloat(1, 0)).isEqualTo(24.0f); typedArray.recycle(); }
@Test public void testGetXml() throws Exception { XmlResourceParser parser = resources.getXml(R.xml.preferences); assertThat(parser).isNotNull(); assertThat(findRootTag(parser)).isEqualTo("PreferenceScreen"); parser = resources.getXml(R.layout.custom_layout); assertThat(parser).isNotNull(); assertThat(findRootTag(parser)).isEqualTo("org.robolectric.android.CustomView"); parser = resources.getXml(R.menu.test); assertThat(parser).isNotNull(); assertThat(findRootTag(parser)).isEqualTo("menu"); parser = resources.getXml(R.drawable.rainbow); assertThat(parser).isNotNull(); assertThat(findRootTag(parser)).isEqualTo("layer-list"); parser = resources.getXml(R.anim.test_anim_1); assertThat(parser).isNotNull(); assertThat(findRootTag(parser)).isEqualTo("set"); parser = resources.getXml(R.color.color_state_list); assertThat(parser).isNotNull(); assertThat(findRootTag(parser)).isEqualTo("selector"); }
@Test public void whenMissingXml_throwNotFoundException() throws Exception { try { resources.getXml(0x3038); fail(); } catch (Resources.NotFoundException e) { assertThat(e.getMessage()).contains("Resource ID #0x3038"); } }
@Test public void testGetAttributeResourceValueIntInt() throws Exception { parser = context.getResources().getXml(R.xml.has_attribute_resource_value); parseUntilNext(XmlResourceParser.START_TAG); assertThat(parser.getAttributeResourceValue(0, 42)).isEqualTo(R.layout.main); }
@Test public void getStyleAttribute_allowStyleAttrReferenceLackingExplicitAttrType() throws Exception { parser = context.getResources().getXml(R.xml.has_parent_style_reference); parseUntilNext(XmlResourceParser.START_TAG); assertThat(parser.getStyleAttribute()).isEqualTo(R.attr.parentStyleReference); }
@Test public void getStyleAttribute_allowStyleAttrReference() throws Exception { parser = context.getResources().getXml(R.xml.has_style_attribute_reference); parseUntilNext(XmlResourceParser.START_TAG); assertThat(parser.getStyleAttribute()).isEqualTo(R.attr.parentStyleReference); }
@Test public void testGetAttributeResourceValueStringStringInt() throws Exception { parser = context.getResources().getXml(R.xml.has_attribute_resource_value); parseUntilNext(XmlResourceParser.START_TAG); assertThat(parser.getAttributeResourceValue(RES_AUTO_NS, "bar", 42)).isEqualTo(R.layout.main); assertThat(parser.getAttributeResourceValue(RES_AUTO_NS, "foo", 42)).isEqualTo(42); }
@Test public void getXml() throws Exception { XmlResourceParser xmlResourceParser = resources.getXml(R.xml.preferences); assertThat(xmlResourceParser).isNotNull(); assertThat(xmlResourceParser.next()).isEqualTo(XmlResourceParser.START_DOCUMENT); assertThat(xmlResourceParser.next()).isEqualTo(XmlResourceParser.START_TAG); assertThat(xmlResourceParser.getName()).isEqualTo("PreferenceScreen"); }
@Test public void testGetIdAttributeResourceValue_defaultValue() throws Exception { assertThat(parser.getIdAttributeResourceValue(12)).isEqualTo(12); parser = context.getResources().getXml(R.xml.has_id); parseUntilNext(XmlResourceParser.START_TAG); assertThat(parser.getIdAttributeResourceValue(12)).isEqualTo(R.id.tacos); }