@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 testUppercase() throws Exception { AndroidMetawidget metawidget = new AndroidMetawidget(null); // Saving UppercaseFoo uppercaseFoo = new UppercaseFoo(); metawidget.setToInspect(uppercaseFoo); ((EditText) metawidget.findViewWithTags("WEPKey")).setText("1234567890"); metawidget.getWidgetProcessor(SimpleBindingProcessor.class).save(metawidget); assertEquals("1234567890", uppercaseFoo.getWEPKey()); // Loading uppercaseFoo.setWEPKey("0987654321"); metawidget.setToInspect(uppercaseFoo); assertEquals("0987654321", ((EditText) metawidget.findViewWithTags("WEPKey")).getText()); }
public void testSingleComponentBinding() throws Exception { // Model Foo foo = new Foo(); foo.setBar(42); // Inspect AndroidMetawidget metawidget = new AndroidMetawidget(null); metawidget.setToInspect(foo); metawidget.setLayout(new SimpleLayout()); metawidget.setPath(Foo.class.getName() + "/bar"); EditText view = (EditText) metawidget.getChildAt(0); assertEquals("42", view.getText()); view.setText("43"); assertEquals(42, foo.getBar()); metawidget.getWidgetProcessor(SimpleBindingProcessor.class).save(metawidget); assertEquals(43, foo.getBar()); }
public void testBinding() throws Exception { // Model Foo foo = new Foo(); foo.setBar(42); // Inspect AndroidMetawidget metawidget = new AndroidMetawidget(null); metawidget.setToInspect(foo); // Load and save EditText view = metawidget.findViewWithTags("bar"); assertEquals("42", view.getText()); view.setText("43"); assertEquals(42, foo.getBar()); metawidget.getWidgetProcessor(SimpleBindingProcessor.class).save(metawidget); assertEquals(43, foo.getBar()); }
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()); }