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