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