ViewTest.java revision cf952a3ce2fd0bf7d9236ea7b909924e6091cf09
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.*;
14import org.junit.Before;
15import org.junit.Test;
16import org.junit.runner.RunWith;
17
18import static com.xtremelabs.robolectric.Robolectric.shadowOf;
19import static junit.framework.Assert.assertEquals;
20import static org.hamcrest.CoreMatchers.*;
21import static org.junit.Assert.*;
22
23@RunWith(WithTestDefaultsRunner.class)
24public class ViewTest {
25    private View view;
26
27    @Before
28    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 performLongClick_shouldClickOnView() throws Exception {
108        TestOnLongClickListener clickListener = new TestOnLongClickListener();
109        view.setOnLongClickListener(clickListener);
110        shadowOf(view).performLongClick();
111
112        assertTrue(clickListener.clicked);
113    }
114
115
116    @Test
117    public void checkedClick_shouldClickOnView() throws Exception {
118        TestOnClickListener clickListener = new TestOnClickListener();
119        view.setOnClickListener(clickListener);
120        shadowOf(view).checkedPerformClick();
121
122        assertTrue(clickListener.clicked);
123    }
124
125    @Test(expected = RuntimeException.class)
126    public void checkedClick_shouldThrowIfViewIsNotVisible() throws Exception {
127        ViewGroup grandParent = new LinearLayout(null);
128        ViewGroup parent = new LinearLayout(null);
129        grandParent.addView(parent);
130        parent.addView(view);
131        grandParent.setVisibility(View.GONE);
132
133        shadowOf(view).checkedPerformClick();
134    }
135
136    @Test(expected = RuntimeException.class)
137    public void checkedClick_shouldThrowIfViewIsDisabled() throws Exception {
138        view.setEnabled(false);
139        shadowOf(view).checkedPerformClick();
140    }
141
142    @Test
143    public void getBackground_shouldReturnNullIfNoBackgroundHasBeenSet() throws Exception {
144        assertThat(view.getBackground(), nullValue());
145    }
146
147    @Test
148    public void shouldSetBackgroundColor() {
149        view.setBackgroundColor(R.color.android_red);
150        int intColor = view.getResources().getColor(R.color.android_red);
151
152        assertThat((ColorDrawable) view.getBackground(), equalTo(new ColorDrawable(intColor)));
153    }
154
155    @Test
156    public void shouldSetBackgroundResource() throws Exception {
157        view.setBackgroundResource(R.drawable.an_image);
158        assertThat(view.getBackground(), equalTo(view.getResources().getDrawable(R.drawable.an_image)));
159    }
160
161    @Test
162    public void shouldRecordBackgroundColor() {
163        int[] colors = {0, 1, 727};
164
165        for (int color : colors) {
166            view.setBackgroundColor(color);
167            assertThat(shadowOf(view).getBackgroundColor(), equalTo(color));
168        }
169    }
170
171    @Test
172    public void shouldPostActionsToTheMessageQueue() throws Exception {
173        Robolectric.pauseMainLooper();
174
175        TestRunnable runnable = new TestRunnable();
176        view.post(runnable);
177        assertFalse(runnable.wasRun);
178
179        Robolectric.unPauseMainLooper();
180        assertTrue(runnable.wasRun);
181    }
182
183    @Test
184    public void shouldPostInvalidateDelayed() throws Exception {
185        Robolectric.pauseMainLooper();
186
187        view.postInvalidateDelayed(100);
188        ShadowView shadowView = shadowOf(view);
189        assertFalse(shadowView.wasInvalidated());
190
191        Robolectric.unPauseMainLooper();
192        assertTrue(shadowView.wasInvalidated());
193    }
194
195    @Test
196    public void shouldPostActionsToTheMessageQueueWithDelay() throws Exception {
197        Robolectric.pauseMainLooper();
198
199        TestRunnable runnable = new TestRunnable();
200        view.postDelayed(runnable, 1);
201        assertFalse(runnable.wasRun);
202
203        Robolectric.getUiThreadScheduler().advanceBy(1);
204        assertTrue(runnable.wasRun);
205    }
206
207    @Test
208    public void shouldSupportAllConstructors() throws Exception {
209        new View(null);
210        new View(null, null);
211        new View(null, null, 0);
212    }
213
214    @Test
215    public void shouldSetAnimation() throws Exception {
216        Animation anim = new TestAnimation();
217        view.setAnimation(anim);
218        assertThat(view.getAnimation(), sameInstance(anim));
219    }
220
221    @Test
222    public void shouldStartAndClearAnimation() throws Exception {
223        Animation anim = new TestAnimation();
224        TestAnimationListener listener = new TestAnimationListener();
225        anim.setAnimationListener(listener);
226        assertThat(listener.wasStartCalled, equalTo(false));
227        assertThat(listener.wasRepeatCalled, equalTo(false));
228        assertThat(listener.wasEndCalled, equalTo(false));
229        view.startAnimation(anim);
230        assertThat(listener.wasStartCalled, equalTo(true));
231        assertThat(listener.wasRepeatCalled, equalTo(false));
232        assertThat(listener.wasEndCalled, equalTo(false));
233        view.clearAnimation();
234        assertThat(listener.wasStartCalled, equalTo(true));
235        assertThat(listener.wasRepeatCalled, equalTo(false));
236        assertThat(listener.wasEndCalled, equalTo(true));
237    }
238
239    @Test
240    public void shouldfindViewWithTag() {
241    	String tagged = "tagged";
242    	String tagged2 = "tagged";
243    	view.setTag(tagged);
244    	assertThat(view.findViewWithTag(tagged2),sameInstance(view));
245    }
246
247    @Test
248    public void scrollTo_shouldStoreTheScrolledCoordinates() throws Exception {
249        view.scrollTo(1, 2);
250        assertThat(shadowOf(view).scrollToCoordinates, equalTo(new Point(1, 2)));
251    }
252
253    @Test
254    public void shouldScrollTo() throws Exception {
255        view.scrollTo(7, 6);
256
257        assertEquals(7, view.getScrollX());
258        assertEquals(6, view.getScrollY());
259    }
260
261    @Test
262    public void shouldGetScrollXAndY() {
263        assertEquals(0, view.getScrollX());
264        assertEquals(0, view.getScrollY());
265    }
266
267    private class TestAnimation extends Animation {
268    }
269}
270