public void testNoGetterSetterType() throws Exception {

    AndroidMetawidget metawidget = new AndroidMetawidget(null);
    metawidget.setInspector(
        new PropertyTypeInspector(
            new BaseObjectInspectorConfig()
                .setPropertyStyle(
                    new JavaBeanPropertyStyle(
                        new JavaBeanPropertyStyleConfig().setSupportPublicFields(true)))));
    metawidget.setToInspect(new NoGetSetFoo());

    try {
      metawidget.getChildAt(0);
      fail();
    } catch (Exception e) {
      assertTrue(e.getCause().getMessage().startsWith("Unable to get 'bar'"));
    }
  }
  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());
  }
  @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());
  }
示例#6
0
  public void testTableLayout() {

    AndroidMetawidget androidMetawidget = new AndroidMetawidget(null);

    Stub stub = new Stub(null);
    stub.setTag("stubMe");
    androidMetawidget.addView(stub);

    Facet buttonsFacet = new Facet(null);
    buttonsFacet.setName("buttons");
    androidMetawidget.addView(buttonsFacet);

    Foo foo = new Foo();
    foo.nestedFoo = new Foo();
    androidMetawidget.setToInspect(foo);

    android.widget.TableLayout tableLayout =
        (android.widget.TableLayout) androidMetawidget.getChildAt(0);
    TableRow tableRow = (TableRow) tableLayout.getChildAt(0);
    assertEquals("Bar: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(tableRow.getChildAt(1) instanceof EditText);
    tableRow = (TableRow) tableLayout.getChildAt(1);
    assertEquals("Baz: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(tableRow.getChildAt(1) instanceof CheckBox);

    tableRow = (TableRow) tableLayout.getChildAt(2);
    assertEquals("Foo Section", ((TextView) tableRow.getChildAt(0)).getText());
    tableRow = (TableRow) tableLayout.getChildAt(3);
    android.widget.LinearLayout linearLayout = (android.widget.LinearLayout) tableRow.getChildAt(0);
    tableLayout = (android.widget.TableLayout) linearLayout.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(0);
    assertEquals("Abc: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(tableRow.getChildAt(1) instanceof Spinner);
    tableRow = (TableRow) tableLayout.getChildAt(1);
    assertEquals("Nested foo: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(buttonsFacet == androidMetawidget.getChildAt(1));
    assertTrue(2 == androidMetawidget.getChildCount());

    // nestedFoo

    AndroidMetawidget nestedMetawidget = (AndroidMetawidget) tableRow.getChildAt(1);
    tableLayout = (android.widget.TableLayout) nestedMetawidget.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(0);
    assertEquals("Bar: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(tableRow.getChildAt(1) instanceof EditText);
    tableRow = (TableRow) tableLayout.getChildAt(1);
    assertEquals("Baz: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(tableRow.getChildAt(1) instanceof CheckBox);

    tableRow = (TableRow) tableLayout.getChildAt(2);
    assertEquals("Foo Section", ((TextView) tableRow.getChildAt(0)).getText());
    tableRow = (TableRow) tableLayout.getChildAt(3);
    linearLayout = (android.widget.LinearLayout) tableRow.getChildAt(0);
    tableLayout = (android.widget.TableLayout) linearLayout.getChildAt(0);
    tableRow = (TableRow) tableLayout.getChildAt(0);
    assertEquals("Abc: ", ((TextView) tableRow.getChildAt(0)).getText());
    AdapterView<?> adapterView = (Spinner) tableRow.getChildAt(1);
    assertTrue(tableRow.getChildAt(1) instanceof Spinner);
    tableRow = (TableRow) tableLayout.getChildAt(1);
    assertEquals("Stub me: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(tableRow.getChildAt(1) instanceof EditText);
    tableRow = (TableRow) tableLayout.getChildAt(2);
    assertEquals("Nested foo: ", ((TextView) tableRow.getChildAt(0)).getText());
    assertTrue(tableRow.getChildAt(1) instanceof AndroidMetawidget);
    assertTrue(3 == tableLayout.getChildCount());

    AndroidMetawidget nestedNestedMetawidget = (AndroidMetawidget) tableRow.getChildAt(1);
    assertTrue(1 == nestedNestedMetawidget.getChildCount());

    // (spacer)

    assertTrue(null == ((TextView) nestedNestedMetawidget.getChildAt(0)).getText());
    assertTrue(1 == nestedNestedMetawidget.getChildCount());

    // Get/set nested value

    assertTrue(null == adapterView.getSelectedItem());
    assertTrue(null == adapterView.getAdapter().getItem(0));
    assertEquals("one", adapterView.getAdapter().getItem(1));
    assertEquals("two", adapterView.getAdapter().getItem(2));
    assertEquals("three", adapterView.getAdapter().getItem(3));
    androidMetawidget.setValue("two", "nestedFoo", "abc");
    assertEquals("two", adapterView.getSelectedItem());
    assertEquals("two", androidMetawidget.getValue("nestedFoo", "abc"));
  }
  public View buildWidget(
      String elementName, Map<String, String> attributes, AndroidMetawidget metawidget) {

    // Not read-only?

    if (!WidgetBuilderUtils.isReadOnly(attributes)) {
      return null;
    }

    // Hidden

    if (TRUE.equals(attributes.get(HIDDEN))) {
      return new Stub(metawidget.getContext());
    }

    // Action

    if (ACTION.equals(elementName)) {
      return new Stub(metawidget.getContext());
    }

    // Masked (return an invisible View, so that we DO still
    // render a label and reserve some blank space)

    if (TRUE.equals(attributes.get(MASKED))) {
      TextView view = new TextView(metawidget.getContext());
      view.setVisibility(View.INVISIBLE);

      return view;
    }

    // Lookups

    String lookup = attributes.get(LOOKUP);

    if (lookup != null && !"".equals(lookup)) {
      return new TextView(metawidget.getContext());
    }

    // Lookup the Class

    Class<?> clazz = WidgetBuilderUtils.getActualClassOrType(attributes, String.class);

    if (clazz != null) {
      if (clazz.isPrimitive()) {
        return new TextView(metawidget.getContext());
      }

      if (String.class.equals(clazz)) {
        return new TextView(metawidget.getContext());
      }

      if (Character.class.equals(clazz)) {
        return new TextView(metawidget.getContext());
      }

      if (Date.class.equals(clazz)) {
        return new TextView(metawidget.getContext());
      }

      if (Boolean.class.equals(clazz)) {
        return new TextView(metawidget.getContext());
      }

      if (Number.class.isAssignableFrom(clazz)) {
        return new TextView(metawidget.getContext());
      }

      // Collections

      if (Collection.class.isAssignableFrom(clazz)) {
        return new Stub(metawidget.getContext());
      }
    }

    // Not simple, but don't expand

    if (TRUE.equals(attributes.get(DONT_EXPAND))) {
      return new TextView(metawidget.getContext());
    }

    // Nested Metawidget

    return null;
  }
  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());
  }