@SuppressWarnings("unchecked") public void testEnum() throws Exception { // As Label Contact contact = new PersonalContact(); contact.setGender(Gender.MALE); AndroidMetawidget metawidget = new AndroidMetawidget(null); SimpleBindingProcessorConfig config = new SimpleBindingProcessorConfig(); config.setConverter( Gender.class, new Converter<Gender>() { public Gender convertFromView(View widget, Object value, Class<?> intoClass) { return Gender.valueOf((String) value); } public Object convertForView(View widget, Gender value) { if (widget instanceof Spinner) { return value; } return value.name(); } }); metawidget.setWidgetProcessors(new SimpleBindingProcessor(config)); metawidget.setToInspect(contact); Map<String, String> attributes = CollectionUtils.newHashMap(); TextView label = new TextView(null); attributes.put(NAME, "gender"); attributes.put(TYPE, Gender.class.getName()); metawidget .getWidgetProcessor(SimpleBindingProcessor.class) .processWidget(label, PROPERTY, attributes, metawidget); assertEquals("MALE", label.getText()); // As Spinner metawidget.setToInspect(contact); Spinner spinner = metawidget.findViewWithTags("gender"); assertEquals(Gender.MALE, spinner.getSelectedItem()); assertEquals(null, ((ArrayAdapter<Gender>) spinner.getAdapter()).getItem(0)); assertEquals(Gender.MALE, ((ArrayAdapter<Gender>) spinner.getAdapter()).getItem(1)); assertEquals(Gender.FEMALE, ((ArrayAdapter<Gender>) spinner.getAdapter()).getItem(2)); assertEquals(3, ((ArrayAdapter<String>) spinner.getAdapter()).getCount()); }
public void testNestedMetawidget() { Contact contact = new PersonalContact(); AndroidMetawidget metawidget = new AndroidMetawidget(null); Stub stub = new Stub(null); stub.setTag("dateOfBirth"); metawidget.addView(stub); metawidget.setToInspect(contact); // Just TableLayout assertEquals(null, contact.getFirstname()); assertEquals(null, contact.getAddress().getStreet()); ((EditText) metawidget.findViewWithTags("firstname")).setText("Foo"); ((EditText) metawidget.findViewWithTags("address", "street")).setText("Bar"); assertEquals( metawidget, metawidget .findViewWithTags("address", "street") .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent()); assertEquals( metawidget, metawidget .findViewWithTags("address", "postcode") .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent()); metawidget.getWidgetProcessor(SimpleBindingProcessor.class).save(metawidget); assertEquals("Foo", contact.getFirstname()); assertEquals("Bar", contact.getAddress().getStreet()); // With TabSheetLayoutDecorator metawidget.setLayout( new TabHostLayoutDecorator( new LayoutDecoratorConfig<View, ViewGroup, AndroidMetawidget>() .setLayout( new org.metawidget.android.widget.layout.TableLayout( new LinearLayoutConfig())))); ((EditText) metawidget.findViewWithTags("firstname")).setText("Foo1"); ((EditText) metawidget.findViewWithTags("address", "street")).setText("Bar1"); assertTrue( metawidget .findViewWithTags("address", "street") .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() .getParent() instanceof TabHost); metawidget.getWidgetProcessor(SimpleBindingProcessor.class).save(metawidget); assertEquals("Foo1", contact.getFirstname()); assertEquals("Bar1", contact.getAddress().getStreet()); }