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