ViewTest.java revision 7598f0c0b1cce3bf1902772ea09139ef98083864
1package com.xtremelabs.robolectric.shadows; 2 3import android.app.Activity; 4import android.graphics.drawable.ColorDrawable; 5import android.graphics.drawable.Drawable; 6import android.view.View; 7import android.view.ViewGroup; 8import android.view.animation.Animation; 9import android.widget.LinearLayout; 10import com.xtremelabs.robolectric.R; 11import com.xtremelabs.robolectric.Robolectric; 12import com.xtremelabs.robolectric.WithTestDefaultsRunner; 13import com.xtremelabs.robolectric.util.TestAnimationListener; 14import com.xtremelabs.robolectric.util.TestOnClickListener; 15import com.xtremelabs.robolectric.util.TestRunnable; 16import com.xtremelabs.robolectric.util.Transcript; 17import org.junit.Before; 18import org.junit.Test; 19import org.junit.runner.RunWith; 20 21import static com.xtremelabs.robolectric.Robolectric.shadowOf; 22import static org.hamcrest.CoreMatchers.sameInstance; 23import static org.hamcrest.CoreMatchers.equalTo; 24import static org.hamcrest.CoreMatchers.notNullValue; 25import static org.junit.Assert.*; 26 27@RunWith(WithTestDefaultsRunner.class) 28public class ViewTest { 29 private View view; 30 31 @Before public void setUp() throws Exception { 32 view = new View(new Activity()); 33 } 34 35 @Test 36 public void testHasEmptyLayoutParams() throws Exception { 37 ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); 38 assertThat(layoutParams, notNullValue()); 39 } 40 41 @Test 42 public void layout_shouldAffectWidthAndHeight() throws Exception { 43 assertThat(view.getWidth(), equalTo(0)); 44 assertThat(view.getHeight(), equalTo(0)); 45 46 view.layout(100, 200, 303, 404); 47 assertThat(view.getWidth(), equalTo(303 - 100)); 48 assertThat(view.getHeight(), equalTo(404 - 200)); 49 } 50 51 @Test 52 public void shouldFocus() throws Exception { 53 final Transcript transcript = new Transcript(); 54 55 view.setOnFocusChangeListener(new View.OnFocusChangeListener() { 56 @Override 57 public void onFocusChange(View v, boolean hasFocus) { 58 transcript.add(hasFocus ? "Gained focus" : "Lost focus"); 59 } 60 }); 61 62 assertFalse(view.isFocused()); 63 assertFalse(view.hasFocus()); 64 transcript.assertNoEventsSoFar(); 65 66 view.requestFocus(); 67 assertTrue(view.isFocused()); 68 assertTrue(view.hasFocus()); 69 transcript.assertEventsSoFar("Gained focus"); 70 71 view.clearFocus(); 72 assertFalse(view.isFocused()); 73 assertFalse(view.hasFocus()); 74 transcript.assertEventsSoFar("Lost focus"); 75 } 76 77 @Test 78 public void shouldNotBeFocusableByDefault() throws Exception { 79 assertFalse(view.isFocusable()); 80 81 view.setFocusable(true); 82 assertTrue(view.isFocusable()); 83 } 84 85 @Test 86 public void shouldKnowIfThisOrAncestorsAreVisible() throws Exception { 87 assertTrue(shadowOf(view).derivedIsVisible()); 88 89 ViewGroup grandParent = new LinearLayout(null); 90 ViewGroup parent = new LinearLayout(null); 91 grandParent.addView(parent); 92 parent.addView(view); 93 94 grandParent.setVisibility(View.GONE); 95 96 assertFalse(shadowOf(view).derivedIsVisible()); 97 } 98 99 @Test 100 public void shouldInflateMergeRootedLayoutAndNotCreateReferentialLoops() throws Exception { 101 LinearLayout root = new LinearLayout(null); 102 root.inflate(new Activity(), R.layout.inner_merge, root); 103 for (int i = 0; i < root.getChildCount(); i++) { 104 View child = root.getChildAt(i); 105 assertNotSame(root, child); 106 } 107 } 108 109 @Test 110 public void checkedClick_shouldClickOnView() throws Exception { 111 TestOnClickListener clickListener = new TestOnClickListener(); 112 view.setOnClickListener(clickListener); 113 shadowOf(view).checkedPerformClick(); 114 115 assertTrue(clickListener.clicked); 116 } 117 118 @Test(expected = RuntimeException.class) 119 public void checkedClick_shouldThrowIfViewIsNotVisible() throws Exception { 120 ViewGroup grandParent = new LinearLayout(null); 121 ViewGroup parent = new LinearLayout(null); 122 grandParent.addView(parent); 123 parent.addView(view); 124 grandParent.setVisibility(View.GONE); 125 126 shadowOf(view).checkedPerformClick(); 127 } 128 129 @Test(expected = RuntimeException.class) 130 public void checkedClick_shouldThrowIfViewIsDisabled() throws Exception { 131 view.setEnabled(false); 132 shadowOf(view).checkedPerformClick(); 133 } 134 135 @Test 136 public void shouldReturnSomethingForABackground() throws Exception { 137 assertThat(view.getBackground(), notNullValue()); 138 } 139 140 @Test 141 public void shouldSetBackgroundColor() { 142 Drawable origninalBackground = view.getBackground(); 143 assertNotNull(origninalBackground); 144 view.setBackgroundColor(R.color.android_red); 145 int intColor = view.getResources().getColor(R.color.android_red); 146 147 assertThat((ColorDrawable) view.getBackground(), equalTo(new ColorDrawable(intColor))); 148 } 149 150 @Test 151 public void shouldSetBackgroundResource() throws Exception { 152 view.setBackgroundResource(R.drawable.an_image); 153 assertThat(view.getBackground(), equalTo(view.getResources().getDrawable(R.drawable.an_image))); 154 } 155 156 @Test 157 public void shouldRecordBackgroundColor() { 158 int[] colors = {0, 1, 727}; 159 160 for (int color : colors) { 161 view.setBackgroundColor(color); 162 assertThat(shadowOf(view).getBackgroundColor(), equalTo(color)); 163 } 164 } 165 166 @Test 167 public void shouldPostActionsToTheMessageQueue() throws Exception { 168 Robolectric.pauseMainLooper(); 169 170 TestRunnable runnable = new TestRunnable(); 171 view.post(runnable); 172 assertFalse(runnable.wasRun); 173 174 Robolectric.unPauseMainLooper(); 175 assertTrue(runnable.wasRun); 176 } 177 178 @Test 179 public void shouldPostActionsToTheMessageQueueWithDelay() throws Exception { 180 Robolectric.pauseMainLooper(); 181 182 TestRunnable runnable = new TestRunnable(); 183 view.postDelayed(runnable, 1); 184 assertFalse(runnable.wasRun); 185 186 Robolectric.getUiThreadScheduler().advanceBy(1); 187 assertTrue(runnable.wasRun); 188 } 189 190 @Test 191 public void shouldSupportAllConstructors() throws Exception { 192 new View(null); 193 new View(null, null); 194 new View(null, null, 0); 195 } 196 197 @Test 198 public void shouldSetAnimation() throws Exception { 199 Animation anim = new TestAnimation(); 200 view.setAnimation(anim); 201 assertThat(view.getAnimation(), sameInstance(anim)); 202 } 203 204 @Test 205 public void shouldStartAndClearAnimation() throws Exception { 206 Animation anim = new TestAnimation(); 207 TestAnimationListener listener = new TestAnimationListener(); 208 anim.setAnimationListener(listener); 209 assertThat(listener.wasStartCalled, equalTo(false)); 210 assertThat(listener.wasRepeatCalled, equalTo(false)); 211 assertThat(listener.wasEndCalled, equalTo(false)); 212 view.startAnimation(anim); 213 assertThat(listener.wasStartCalled, equalTo(true)); 214 assertThat(listener.wasRepeatCalled, equalTo(false)); 215 assertThat(listener.wasEndCalled, equalTo(false)); 216 view.clearAnimation(); 217 assertThat(listener.wasStartCalled, equalTo(true)); 218 assertThat(listener.wasRepeatCalled, equalTo(false)); 219 assertThat(listener.wasEndCalled, equalTo(true)); 220 } 221 222 private class TestAnimation extends Animation { } 223} 224