aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/res/ViewLoaderTest.java
blob: 139056a86fb20035815d82726a332df5be860ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package com.xtremelabs.robolectric.res;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.*;
import com.google.android.maps.MapView;
import com.xtremelabs.robolectric.R;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import com.xtremelabs.robolectric.shadows.ShadowImageView;
import com.xtremelabs.robolectric.shadows.ShadowTextView;
import com.xtremelabs.robolectric.util.CustomView;
import com.xtremelabs.robolectric.util.CustomView2;
import com.xtremelabs.robolectric.util.I18nException;
import com.xtremelabs.robolectric.util.TestUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static com.xtremelabs.robolectric.util.TestUtil.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

@RunWith(WithTestDefaultsRunner.class)
public class ViewLoaderTest {
    private ViewLoader viewLoader;
    private Context context;

    @Before
    public void setUp() throws Exception {
        Robolectric.bindDefaultShadowClasses();

        ResourceExtractor resourceExtractor = new ResourceExtractor();
        resourceExtractor.addLocalRClass(R.class);
        resourceExtractor.addSystemRClass(android.R.class);

        StringResourceLoader stringResourceLoader = new StringResourceLoader(resourceExtractor);
        new DocumentLoader(stringResourceLoader).loadResourceXmlDir(resourceFile("res", "values"));
        new DocumentLoader(stringResourceLoader).loadSystemResourceXmlDir(getSystemResourceDir("values"));

        viewLoader =  new ViewLoader(resourceExtractor, new AttrResourceLoader(resourceExtractor));
        new DocumentLoader(viewLoader).loadResourceXmlDir(resourceFile("res", "layout"));
        new DocumentLoader(viewLoader).loadResourceXmlDir(resourceFile("res", "layout-land"));
        new DocumentLoader(viewLoader).loadResourceXmlDir(resourceFile("res", "layout-large-v16"));
        new DocumentLoader(viewLoader).loadResourceXmlDir(resourceFile("res", "layout-v11"));
        new DocumentLoader(viewLoader).loadResourceXmlDir(resourceFile("res", "layout-xlarge"));
        new DocumentLoader(viewLoader).loadResourceXmlDir(resourceFile("res", "layout-xlarge-v11"));
        new DocumentLoader(viewLoader).loadSystemResourceXmlDir(getSystemResourceDir("layout"));

        context = new Activity();
    }

    @Test
    public void testCreatesCorrectClasses() throws Exception {
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/media");
        TestUtil.assertInstanceOf(LinearLayout.class, view);

        assertSame(context, view.getContext());
    }

    @Test
    public void testChoosesLayoutBasedOnDefaultScreenSize() throws Exception {
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/different_screen_sizes");
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        assertThat(textView.getText().toString(), equalTo("default"));
    }

    @Test
    public void testChoosesLayoutBasedOnSearchPath_choosesFirstFileFoundOnPath() throws Exception {
        viewLoader.setLayoutQualifierSearchPath("xlarge", "land");
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/different_screen_sizes");
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        assertThat(textView.getText().toString(), equalTo("xlarge"));
    }

    @Test
    public void testChoosesLayoutBasedOnSearchPath_respectsOrderOfPath() throws Exception {
        viewLoader.setLayoutQualifierSearchPath("does-not-exist", "land", "xlarge");
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/different_screen_sizes");
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        assertThat(textView.getText().toString(), equalTo("land"));
    }

    @Test
    public void testChoosesLayoutBasedOnDefaultVersion() throws Exception {
        Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.FROYO);
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/different_screen_sizes");
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        assertThat(textView.getText().toString(), equalTo("default"));
    }

    @Test
    public void testChoosesLayoutBasedOnNewestVersion() throws Exception {
        Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/different_screen_sizes");
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        assertThat(textView.getText().toString(), equalTo("v11"));
    }

    @Test
    public void testChoosesLayoutBasedOnSearchPath_choosesFirstFileFoundOnPathWithVersionNumber() throws Exception {
        viewLoader.setLayoutQualifierSearchPath("xlarge", "large");
        Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.HONEYCOMB);
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/different_screen_sizes");
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        assertThat(textView.getText().toString(), equalTo("xlarge-v11"));
    }

    @Test
    public void testChoosesLayoutBasedOnSearchPath_choosesBestFileFoundOnPathWithVersionNumber() throws Exception {
        viewLoader.setLayoutQualifierSearchPath("xlarge", "large");
        Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/different_screen_sizes");
        TextView textView = (TextView) view.findViewById(android.R.id.text1);
        assertThat(textView.getText().toString(), equalTo("xlarge-v11"));
    }

    @Test
    public void testWebView() throws Exception {
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/webview_holder");
        WebView webView = (WebView) view.findViewById(R.id.web_view);

        webView.loadUrl("www.example.com");

        assertThat(shadowOf(webView).getLastLoadedUrl(), equalTo("www.example.com"));
    }

    @Test
    public void testAddsChildren() throws Exception {
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/media");
        assertTrue(view.getChildCount() > 0);

        assertSame(context, view.getChildAt(0).getContext());
    }

    @Test
    public void testFindsChildrenById() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/media");
        TestUtil.assertInstanceOf(TextView.class, mediaView.findViewById(R.id.title));

        ViewGroup mainView = (ViewGroup) viewLoader.inflateView(context, "layout/main");
        assertInstanceOf(View.class, mainView.findViewById(R.id.title));
    }

    @Test
    public void testInflatingConflictingSystemAndLocalViewsWorks() throws Exception {
        ViewGroup view = (ViewGroup) viewLoader.inflateView(context, "layout/activity_list_item");
        assertInstanceOf(ImageView.class, view.findViewById(R.id.icon));

        view = (ViewGroup) viewLoader.inflateView(context, "android:layout/activity_list_item");
        assertInstanceOf(ImageView.class, view.findViewById(android.R.id.icon));
    }

    @Test
    public void testInclude() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/media");
        assertInstanceOf(TextView.class, mediaView.findViewById(R.id.include_id));
    }

    @Test
    public void testIncludeShouldRetainAttributes() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/media");
        assertThat(mediaView.findViewById(R.id.include_id).getVisibility(), is(View.GONE));
    }

    @Test
    public void shouldOverwriteIdOnIncludedNonMerge() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/media");
        assertNull(mediaView.findViewById(R.id.snippet_text));
    }

    @Test
    public void shouldRetainIdOnIncludedMergeWhenIncludeSpecifiesNoId() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/override_include");
        assertInstanceOf(TextView.class, mediaView.findViewById(R.id.inner_text));
    }

    @Test
    public void shouldRetainIdOnIncludedNonMergeWhenIncludeSpecifiesNoId() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/override_include");
        assertInstanceOf(TextView.class, mediaView.findViewById(R.id.snippet_text));
    }

    @Test
    public void testIncludedIdShouldNotBeFoundWhenIncludedIsMerge() throws Exception {
        ViewGroup overrideIncludeView = (ViewGroup) viewLoader.inflateView(context, "layout/outer");
        assertInstanceOf(LinearLayout.class, overrideIncludeView.findViewById(R.id.outer_merge));
        assertInstanceOf(TextView.class, overrideIncludeView.findViewById(R.id.inner_text));
        assertNull(overrideIncludeView.findViewById(R.id.include_id));
        assertEquals(1, overrideIncludeView.getChildCount());
    }

    @Test
    public void testIncludeShouldOverrideAttributesOfIncludedRootNode() throws Exception {
        ViewGroup overrideIncludeView = (ViewGroup) viewLoader.inflateView(context, "layout/override_include");
        assertThat(overrideIncludeView.findViewById(R.id.snippet_text).getVisibility(), is(View.INVISIBLE));
    }

    @Test
    public void shouldNotCountRequestFocusElementAsChild() throws Exception {
        ViewGroup viewGroup = (ViewGroup) viewLoader.inflateView(context, "layout/request_focus");
        ViewGroup frameLayout = (ViewGroup) viewGroup.getChildAt(1);
        assertEquals(0, frameLayout.getChildCount());
    }

    @Test
    public void shouldGiveFocusToElementContainingRequestFocusElement() throws Exception {
        ViewGroup viewGroup = (ViewGroup) viewLoader.inflateView(context, "layout/request_focus");
        EditText editText = (EditText) viewGroup.findViewById(R.id.edit_text);
        assertFalse(editText.isFocused());
    }

    @Test
    public void shouldGiveFocusToFirstFocusableElement_butThisMightBeTheWrongBehavior() throws Exception {
        ViewGroup viewGroup = (ViewGroup) viewLoader.inflateView(context, "layout/request_focus_with_two_edit_texts");
        assertTrue(viewGroup.findViewById(R.id.edit_text).isFocused());
        assertFalse(viewGroup.findViewById(R.id.edit_text2).isFocused());
    }

    @Test
    public void testMerge() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/outer");
        TestUtil.assertInstanceOf(TextView.class, mediaView.findViewById(R.id.inner_text));
    }

    @Test
    public void mergeIncludesShouldNotCreateAncestryLoops() throws Exception {
        ViewGroup mediaView = (ViewGroup) viewLoader.inflateView(context, "layout/outer");
        mediaView.hasFocus();
    }

    @Test
    public void testViewGroupsLooksAtItsOwnId() throws Exception {
        TextView mediaView = (TextView) viewLoader.inflateView(context, "layout/snippet");
        assertSame(mediaView, mediaView.findViewById(R.id.snippet_text));
    }

    @Test
    public void shouldConstructCustomViewsWithAttributesConstructor() throws Exception {
        CustomView view = (CustomView) viewLoader.inflateView(context, "layout/custom_layout");
        assertThat(view.attributeResourceValue, equalTo(R.string.hello));
    }

    @Test
    public void testViewVisibilityIsSet() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/media");
        assertThat(mediaView.findViewById(R.id.title).getVisibility(), equalTo(View.VISIBLE));
        assertThat(mediaView.findViewById(R.id.subtitle).getVisibility(), equalTo(View.GONE));
    }

    @Test
    public void testTextViewTextIsSet() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/main");
        assertThat(((TextView) mediaView.findViewById(R.id.title)).getText().toString(), equalTo("Main Layout"));
        assertThat(((TextView) mediaView.findViewById(R.id.subtitle)).getText().toString(), equalTo("Hello"));
    }

    @Test
    public void testTextViewCompoundDrawablesAreSet() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/main");
        ShadowTextView shadowTextView = shadowOf((TextView) mediaView.findViewById(R.id.title));

        assertThat(shadowTextView.getCompoundDrawablesImpl().getTop(), equalTo(R.drawable.an_image));
        assertThat(shadowTextView.getCompoundDrawablesImpl().getRight(), equalTo(R.drawable.an_other_image));
        assertThat(shadowTextView.getCompoundDrawablesImpl().getBottom(), equalTo(R.drawable.third_image));
        assertThat(shadowTextView.getCompoundDrawablesImpl().getLeft(), equalTo(R.drawable.fourth_image));
    }

    @Test
    public void testCheckBoxCheckedIsSet() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/main");
        assertThat(((CheckBox) mediaView.findViewById(R.id.true_checkbox)).isChecked(), equalTo(true));
        assertThat(((CheckBox) mediaView.findViewById(R.id.false_checkbox)).isChecked(), equalTo(false));
        assertThat(((CheckBox) mediaView.findViewById(R.id.default_checkbox)).isChecked(), equalTo(false));
    }

    @Test
    public void testImageViewSrcIsSet() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/main");
        assertThat(((ShadowImageView) shadowOf(mediaView.findViewById(R.id.image))).getResourceId(), equalTo(R.drawable.an_image));
    }

    @Test
    public void shouldInflateMergeLayoutIntoParent() throws Exception {
        View innerMerge = viewLoader.inflateView(context, R.layout.inner_merge, new LinearLayout(null));
        assertNotNull(innerMerge);
    }

    @Test
    public void testMapView() throws Exception {
        RelativeLayout mainView = (RelativeLayout) viewLoader.inflateView(context, "layout/mapview");
        TestUtil.assertInstanceOf(MapView.class, mainView.findViewById(R.id.map_view));
    }

    @Test
    public void testViewEnabled() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/main");
        assertThat(mediaView.findViewById(R.id.time).isEnabled(), equalTo(false));
    }

    @Test
    public void testContentDescriptionIsSet() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/main");
        assertThat(mediaView.findViewById(R.id.time).getContentDescription().toString(), equalTo("Howdy"));
    }

    @Test
    public void testViewBackgroundIdIsSet() throws Exception {
        View mediaView = viewLoader.inflateView(context, "layout/main");
        ImageView imageView = (ImageView) mediaView.findViewById(R.id.image);
        ShadowImageView shadowImageView = Robolectric.shadowOf(imageView);

        assertThat(shadowImageView.getBackgroundResourceId(), equalTo(R.drawable.image_background));
    }

    @Test
    public void testOnClickAttribute() throws Exception {
        ClickActivity activity = new ClickActivity();
        activity.onCreate(null);

        assertThat(activity.clicked, equalTo(false));

        Button button = (Button)activity.findViewById(R.id.button);
        button.performClick();

        assertThat(activity.clicked, equalTo(true));
    }

    @Test
    public void testInvalidOnClickAttribute() throws Exception {
        Activity activity = new Activity();
        activity.setContentView(R.layout.with_invalid_onclick);

        Button button =
            (Button)activity.findViewById(R.id.invalid_onclick_button);

        IllegalStateException exception = null;
        try {
            button.performClick();
        } catch (IllegalStateException e) {
            exception = e;
        } finally {
            assertNotNull(exception);
            assertThat("The error message should contain the id name of the "
                       + "faulty button",
                       exception.getMessage(),
                       containsString("invalid_onclick_button"));
        }
    }

    @Test
    public void shouldInvokeOnFinishInflate() throws Exception {
        CustomView2 outerCustomView = (CustomView2) viewLoader.inflateView(context, "layout/custom_layout2");
        CustomView2 innerCustomView = (CustomView2) outerCustomView.getChildAt(0);
        assertThat(outerCustomView.childCountAfterInflate, equalTo(1));
        assertThat(innerCustomView.childCountAfterInflate, equalTo(3));
    }

    @Test
    public void testIncludesLinearLayoutsOnlyOnce() throws Exception {
        ViewGroup parentView = (ViewGroup) viewLoader.inflateView(context, "layout/included_layout_parent");
        assertEquals(1, parentView.getChildCount());
    }

    @Test(expected=I18nException.class)
    public void shouldThrowI18nExceptionOnLayoutWithBareStrings() throws Exception {
    	viewLoader.setStrictI18n(true);
        new DocumentLoader(viewLoader).loadResourceXmlDir(resourceFile("res", "layout"));
        new DocumentLoader(viewLoader).loadSystemResourceXmlDir(getSystemResourceDir("layout"));

    	viewLoader.inflateView(context,"layout/text_views");
    }

    public static class ClickActivity extends Activity {
        public boolean clicked = false;

        @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }

        public void onButtonClick(View v) {
            clicked = true;
        }
    }
}