ViewGroupTest.java revision 40c6251719cccc0a84ae99c976d2836b14374ce6
1package com.xtremelabs.robolectric.shadows;
2
3import android.app.Application;
4import android.view.View;
5import android.view.ViewGroup;
6import android.widget.FrameLayout;
7import com.xtremelabs.robolectric.R;
8import com.xtremelabs.robolectric.Robolectric;
9import com.xtremelabs.robolectric.WithTestDefaultsRunner;
10import com.xtremelabs.robolectric.res.ResourceLoader;
11import org.junit.After;
12import org.junit.Before;
13import org.junit.Test;
14import org.junit.runner.RunWith;
15
16import java.io.ByteArrayOutputStream;
17import java.io.PrintStream;
18
19import static com.xtremelabs.robolectric.Robolectric.shadowOf;
20import static org.hamcrest.CoreMatchers.equalTo;
21import static org.hamcrest.CoreMatchers.sameInstance;
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.core.IsNull.nullValue;
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertFalse;
26import static org.junit.Assert.assertTrue;
27
28@RunWith(WithTestDefaultsRunner.class)
29public class ViewGroupTest {
30    private String defaultLineSeparator;
31    private ViewGroup root;
32    private View child1;
33    private View child2;
34    private ViewGroup child3;
35    private View child3a;
36    private View child3b;
37
38    @Before public void setUp() throws Exception {
39        Robolectric.bindDefaultShadowClasses();
40
41        Application context = new Application();
42        ShadowApplication.bind(context, new ResourceLoader(R.class, null, null));
43
44        root = new FrameLayout(context);
45
46        child1 = new View(context);
47        child2 = new View(context);
48        child3 = new FrameLayout(context);
49        child3a = new View(context);
50        child3b = new View(context);
51
52        root.addView(child1);
53        root.addView(child2);
54        root.addView(child3);
55
56        child3.addView(child3a);
57        child3.addView(child3b);
58
59        defaultLineSeparator = System.getProperty("line.separator");
60        System.setProperty("line.separator", "\n");
61    }
62
63    @After
64    public void tearDown() throws Exception {
65        System.setProperty("line.separator", defaultLineSeparator);
66    }
67
68    @Test
69    public void testRemoveChildAt() throws Exception {
70        root.removeViewAt(1);
71
72        assertThat(root.getChildCount(), equalTo(2));
73        assertThat(root.getChildAt(0), sameInstance(child1));
74        assertThat(root.getChildAt(1), sameInstance((View) child3));
75
76        assertThat(child2.getParent(), nullValue());
77    }
78
79    @Test
80    public void hasFocus_shouldReturnTrueIfAnyChildHasFocus() throws Exception {
81        assertFalse(root.hasFocus());
82
83        child1.requestFocus();
84        assertTrue(root.hasFocus());
85
86        child1.clearFocus();
87        assertFalse(root.hasFocus());
88
89        child3b.requestFocus();
90        assertTrue(root.hasFocus());
91
92        child3b.clearFocus();
93        assertFalse(root.hasFocus());
94
95        root.requestFocus();
96        assertTrue(root.hasFocus());
97    }
98
99    @Test
100    public void clearFocus_shouldRecursivelyClearTheFocusOfAllChildren() throws Exception {
101        child3a.requestFocus();
102
103        root.clearFocus();
104
105        assertFalse(child3a.hasFocus());
106        assertFalse(child3.hasFocus());
107        assertFalse(root.hasFocus());
108
109        root.requestFocus();
110        root.clearFocus();
111        assertFalse(root.hasFocus());
112    }
113
114    @Test
115    public void dump_shouldDumpStructure() throws Exception {
116        child3.setId(R.id.snippet_text);
117
118        ByteArrayOutputStream out = new ByteArrayOutputStream();
119        shadowOf(root).dump(new PrintStream(out), 0);
120        assertEquals("<FrameLayout>\n" +
121                "  <View/>\n" +
122                "  <View/>\n" +
123                "  <FrameLayout id=\"id/snippet_text\">\n" +
124                "    <View/>\n" +
125                "    <View/>\n" +
126                "  </FrameLayout>\n" +
127                "</FrameLayout>\n", out.toString());
128    }
129}
130