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