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