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