ViewGroupTest.java revision 88fd0dbccc29a37545c186115f2a6b6f91302a56
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.*; 24 25@RunWith(WithTestDefaultsRunner.class) 26public class ViewGroupTest { 27 private String defaultLineSeparator; 28 private ViewGroup root; 29 private View child1; 30 private View child2; 31 private ViewGroup child3; 32 private View child3a; 33 private View child3b; 34 35 @Before public void setUp() throws Exception { 36 Application context = new Application(); 37 ShadowApplication.bind(context, new ResourceLoader(10, R.class, null, null)); 38 39 root = new FrameLayout(context); 40 41 child1 = new View(context); 42 child2 = new View(context); 43 child3 = new FrameLayout(context); 44 child3a = new View(context); 45 child3b = new View(context); 46 47 root.addView(child1); 48 root.addView(child2); 49 root.addView(child3); 50 51 child3.addView(child3a); 52 child3.addView(child3b); 53 54 defaultLineSeparator = System.getProperty("line.separator"); 55 System.setProperty("line.separator", "\n"); 56 } 57 58 @After 59 public void tearDown() throws Exception { 60 System.setProperty("line.separator", defaultLineSeparator); 61 } 62 63 @Test 64 public void testRemoveChildAt() throws Exception { 65 root.removeViewAt(1); 66 67 assertThat(root.getChildCount(), equalTo(2)); 68 assertThat(root.getChildAt(0), sameInstance(child1)); 69 assertThat(root.getChildAt(1), sameInstance((View) child3)); 70 71 assertThat(child2.getParent(), nullValue()); 72 } 73 74 @Test 75 public void testAddViewAt() throws Exception { 76 root.removeAllViews(); 77 root.addView(child1); 78 root.addView(child2); 79 root.addView(child3, 1); 80 assertThat(root.getChildAt(0), sameInstance(child1)); 81 assertThat(root.getChildAt(1), sameInstance((View) child3)); 82 assertThat(root.getChildAt(2), sameInstance(child2)); 83 } 84 85 @Test 86 public void shouldfindViewWithTag() { 87 root.removeAllViews(); 88 String tag1 = "tag1"; 89 String tag2 = "tag2"; 90 String tag3 = "tag3"; 91 child1.setTag(tag1); 92 child2.setTag(tag2); 93 child3.setTag(tag3); 94 root.addView(child1); 95 root.addView(child2); 96 root.addView(child3, 1); 97 assertThat(root.findViewWithTag("tag1"), sameInstance(child1)); 98 assertThat(root.findViewWithTag("tag2"), sameInstance((View) child2)); 99 assertThat((ViewGroup)root.findViewWithTag("tag3"), sameInstance(child3)); 100 } 101 102 @Test 103 public void shouldNotfindViewWithTagReturnNull() { 104 root.removeAllViews(); 105 String tag1 = "tag1"; 106 String tag2 = "tag2"; 107 String tag3 = "tag3"; 108 child1.setTag(tag1); 109 child2.setTag(tag2); 110 child3.setTag(tag3); 111 root.addView(child1); 112 root.addView(child2); 113 root.addView(child3, 1); 114 assertThat(root.findViewWithTag("tag21"), equalTo(null)); 115 assertThat((ViewGroup)root.findViewWithTag("tag23"), equalTo(null)); 116 } 117 118 @Test 119 public void shouldfindViewWithTagFromCorrectViewGroup() { 120 root.removeAllViews(); 121 String tag1 = "tag1"; 122 String tag2 = "tag2"; 123 String tag3 = "tag3"; 124 child1.setTag(tag1); 125 child2.setTag(tag2); 126 child3.setTag(tag3); 127 root.addView(child1); 128 root.addView(child2); 129 root.addView(child3); 130 131 child3a.setTag(tag1); 132 child3b.setTag(tag2); 133 134 //can find views by tag from root 135 assertThat(root.findViewWithTag("tag1"), sameInstance(child1)); 136 assertThat(root.findViewWithTag("tag2"), sameInstance((View) child2)); 137 assertThat((ViewGroup)root.findViewWithTag("tag3"), sameInstance(child3)); 138 139 //can find views by tag from child3 140 assertThat(child3.findViewWithTag("tag1"), sameInstance(child3a)); 141 assertThat(child3.findViewWithTag("tag2"), sameInstance(child3b)); 142 } 143 144 145 @Test 146 public void hasFocus_shouldReturnTrueIfAnyChildHasFocus() throws Exception { 147 assertFalse(root.hasFocus()); 148 149 child1.requestFocus(); 150 assertTrue(root.hasFocus()); 151 152 child1.clearFocus(); 153 assertFalse(root.hasFocus()); 154 155 child3b.requestFocus(); 156 assertTrue(root.hasFocus()); 157 158 child3b.clearFocus(); 159 assertFalse(root.hasFocus()); 160 161 root.requestFocus(); 162 assertTrue(root.hasFocus()); 163 } 164 165 @Test 166 public void clearFocus_shouldRecursivelyClearTheFocusOfAllChildren() throws Exception { 167 child3a.requestFocus(); 168 169 root.clearFocus(); 170 171 assertFalse(child3a.hasFocus()); 172 assertFalse(child3.hasFocus()); 173 assertFalse(root.hasFocus()); 174 175 root.requestFocus(); 176 root.clearFocus(); 177 assertFalse(root.hasFocus()); 178 } 179 180 @Test 181 public void dump_shouldDumpStructure() throws Exception { 182 child3.setId(R.id.snippet_text); 183 184 ByteArrayOutputStream out = new ByteArrayOutputStream(); 185 shadowOf(root).dump(new PrintStream(out), 0); 186 assertEquals("<FrameLayout>\n" + 187 " <View/>\n" + 188 " <View/>\n" + 189 " <FrameLayout id=\"id/snippet_text\">\n" + 190 " <View/>\n" + 191 " <View/>\n" + 192 " </FrameLayout>\n" + 193 "</FrameLayout>\n", out.toString()); 194 } 195} 196