@Test public void getAttributeIntValue_shouldReturnStyledValueFromAttribute() throws Exception { roboAttributeSet = new RoboAttributeSet(asList( new Attribute(TEST_PACKAGE + ":attr/gravity", "center|fill_vertical", TEST_PACKAGE), new Attribute("android:attr/orientation", "vertical", TEST_PACKAGE) ), resources, CustomView.class); assertThat(roboAttributeSet.getAttributeIntValue(TEST_PACKAGE_NS, "gravity", 0)).isEqualTo(0x11 | 0x70); assertThat(roboAttributeSet.getAttributeIntValue(ANDROID_NS, "orientation", -1)).isEqualTo(1); // style from LinearLayout }
@Test public void getAttributeIntValue_whenTypeAllowsIntOrEnum_withInt_shouldReturnInt() throws Exception { roboAttributeSet = new RoboAttributeSet(asList( new Attribute(TEST_PACKAGE + ":attr/numColumns", "3", TEST_PACKAGE) ), resources, CustomView.class); assertThat(roboAttributeSet.getAttributeIntValue(TEST_PACKAGE_NS, "numColumns", 0)).isEqualTo(3); }
@Test public void getAttributeIntValue_whenTypeAllowsIntOrEnum_withEnum_shouldReturnInt() throws Exception { roboAttributeSet = new RoboAttributeSet(asList( new Attribute(TEST_PACKAGE + ":attr/numColumns", "auto_fit", TEST_PACKAGE) ), resources, CustomView.class); assertThat(roboAttributeSet.getAttributeIntValue(TEST_PACKAGE_NS, "numColumns", 0)).isEqualTo(-1); }
private void addChildrenInGroup(MenuNode source, int groupId, Menu root) { for (MenuNode child : source.getChildren()) { String name = child.getName(); RoboAttributeSet attributes = shadowOf(context).createAttributeSet(child.getAttributes(), null); if (strictI18n) { attributes.validateStrictI18n(); } if (name.equals("item")) { if (child.isSubMenuItem()) { SubMenu sub = root.addSubMenu(groupId, attributes.getAttributeResourceValue(ANDROID_NS, "id", 0), 0, attributes.getAttributeValue(ANDROID_NS, "title")); MenuNode subMenuNode = child.getChildren().get(0); addChildrenInGroup(subMenuNode, groupId, sub); } else { String menuItemTitle = attributes.getAttributeValue(ANDROID_NS, "title"); if (isFullyQualifiedName(menuItemTitle)) { menuItemTitle = getStringResourceValue(attributes); } int orderInCategory = attributes.getAttributeIntValue(ANDROID_NS, "orderInCategory", 0); int menuItemId = attributes.getAttributeResourceValue(ANDROID_NS, "id", 0); MenuItem item = root.add(groupId, menuItemId, orderInCategory, menuItemTitle); addActionViewToItem(item, attributes); } } else if (name.equals("group")) { int newGroupId = attributes.getAttributeResourceValue(ANDROID_NS, "id", 0); addChildrenInGroup(child, newGroupId, root); } } }
private void addActionViewToItem(MenuItem item, RoboAttributeSet attributes) { String actionViewClassName = attributes.getAttributeValue(APP_NS, "actionViewClass"); if (actionViewClassName != null) { try { View actionView = (View) Class.forName(actionViewClassName) .getConstructor(Context.class) .newInstance(context); item.setActionView(actionView); } catch (Exception e) { throw new RuntimeException("Action View class not found!", e); } } }
@NonNull public AttributeSet build() { return new RoboAttributeSet(attributes, resourceLoader); }
@Test public void getAttributeIntValue_shouldReturnValueFromAttribute() throws Exception { roboAttributeSet = new RoboAttributeSet(asList(new Attribute(TEST_PACKAGE + ":attr/sugarinessPercent", "100", TEST_PACKAGE)), resources, null); assertThat(roboAttributeSet.getAttributeIntValue(TEST_PACKAGE_NS, "sugarinessPercent", 0)).isEqualTo(100); }
@Test public void getAttributeIntValue_shouldReturnHexValueFromAttribute() throws Exception { roboAttributeSet = new RoboAttributeSet(asList(new Attribute(TEST_PACKAGE + ":attr/sugarinessPercent", "0x10", TEST_PACKAGE)), resources, null); assertThat(roboAttributeSet.getAttributeIntValue(TEST_PACKAGE_NS, "sugarinessPercent", 0)).isEqualTo(16); }
@Test public void getAttributeIntValue_shouldNotReturnStyledValueFromAttributeForSuperclass() throws Exception { roboAttributeSet = new RoboAttributeSet(asList(new Attribute(TEST_PACKAGE + ":attr/gravity", "center|fill_vertical", TEST_PACKAGE)), resources, View.class); assertThat(roboAttributeSet.getAttributeIntValue(TEST_PACKAGE_NS, "gravity", 0)).isEqualTo(Gravity.CENTER | Gravity.FILL_VERTICAL); }
@Test public void getAttributeIntValue_shouldReturnEnumValuesForEnumAttributes() throws Exception { roboAttributeSet = new RoboAttributeSet(asList(new Attribute(TEST_PACKAGE + ":attr/itemType", "ungulate", TEST_PACKAGE)), resources, CustomView.class); assertThat(roboAttributeSet.getAttributeIntValue(TEST_PACKAGE_NS, "itemType", 0)).isEqualTo(1); }
private void createTestAttributeSet(Attribute... attributes) { roboAttributeSet = new RoboAttributeSet(asList(attributes), resources, null); }
private String getStringResourceValue(RoboAttributeSet attributes) { int menuItemTitleId = attributes.getAttributeResourceValue(ANDROID_NS, "title", 0); return context.getString(menuItemTitleId); }