ViewTest.java revision 3f78da998ce73c1c7c25798a7e7e4f19287eff23
1package com.xtremelabs.robolectric.shadows; 2 3import android.app.Activity; 4import android.view.View; 5import android.view.ViewGroup; 6import android.view.animation.Animation; 7import android.widget.LinearLayout; 8import com.xtremelabs.robolectric.R; 9import com.xtremelabs.robolectric.Robolectric; 10import com.xtremelabs.robolectric.WithTestDefaultsRunner; 11import com.xtremelabs.robolectric.util.TestAnimationListener; 12import com.xtremelabs.robolectric.util.TestOnClickListener; 13import com.xtremelabs.robolectric.util.TestRunnable; 14import com.xtremelabs.robolectric.util.Transcript; 15import org.junit.Before; 16import org.junit.Test; 17import org.junit.runner.RunWith; 18 19import static com.xtremelabs.robolectric.Robolectric.shadowOf; 20import static org.hamcrest.CoreMatchers.sameInstance; 21import static org.hamcrest.CoreMatchers.equalTo; 22import static org.hamcrest.CoreMatchers.notNullValue; 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(null); 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 134 @Test 135 public void shouldRecordBackgroundColor() { 136 int[] colors = {0, 1, 727}; 137 138 for (int color : colors) { 139 view.setBackgroundColor(color); 140 assertThat(shadowOf(view).getBackgroundColor(), equalTo(color)); 141 } 142 } 143 144 @Test 145 public void shouldPostActionsToTheMessageQueue() throws Exception { 146 Robolectric.pauseMainLooper(); 147 148 TestRunnable runnable = new TestRunnable(); 149 view.post(runnable); 150 assertFalse(runnable.wasRun); 151 152 Robolectric.unPauseMainLooper(); 153 assertTrue(runnable.wasRun); 154 } 155 156 @Test 157 public void shouldPostActionsToTheMessageQueueWithDelay() throws Exception { 158 Robolectric.pauseMainLooper(); 159 160 TestRunnable runnable = new TestRunnable(); 161 view.postDelayed(runnable, 1); 162 assertFalse(runnable.wasRun); 163 164 Robolectric.getUiThreadScheduler().advanceBy(1); 165 assertTrue(runnable.wasRun); 166 } 167 168 @Test 169 public void shouldSupportAllConstructors() throws Exception { 170 new View(null); 171 new View(null, null); 172 new View(null, null, 0); 173 } 174 175 @Test 176 public void shouldSetAnimation() throws Exception { 177 Animation anim = new TestAnimation(); 178 view.setAnimation(anim); 179 assertThat(view.getAnimation(), sameInstance(anim)); 180 } 181 182 @Test 183 public void shouldStartAndClearAnimation() throws Exception { 184 Animation anim = new TestAnimation(); 185 TestAnimationListener listener = new TestAnimationListener(); 186 anim.setAnimationListener(listener); 187 assertThat(listener.wasStartCalled, equalTo(false)); 188 assertThat(listener.wasRepeatCalled, equalTo(false)); 189 assertThat(listener.wasEndCalled, equalTo(false)); 190 view.startAnimation(anim); 191 assertThat(listener.wasStartCalled, equalTo(true)); 192 assertThat(listener.wasRepeatCalled, equalTo(false)); 193 assertThat(listener.wasEndCalled, equalTo(false)); 194 view.clearAnimation(); 195 assertThat(listener.wasStartCalled, equalTo(true)); 196 assertThat(listener.wasRepeatCalled, equalTo(false)); 197 assertThat(listener.wasEndCalled, equalTo(true)); 198 } 199 200 private class TestAnimation extends Animation { 201 202 } 203 204} 205