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