1package org.robolectric.manifest;
2
3import static org.assertj.core.api.Assertions.assertThat;
4import static org.robolectric.util.TestUtil.resourceFile;
5
6import android.Manifest;
7import android.content.Intent;
8import android.os.Build.VERSION_CODES;
9import com.google.common.base.Charsets;
10import com.google.common.io.Files;
11import java.io.File;
12import java.io.IOException;
13import java.lang.reflect.Field;
14import java.util.ArrayList;
15import java.util.Arrays;
16import java.util.Collections;
17import java.util.List;
18import java.util.Map;
19import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.TemporaryFolder;
22import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
24import org.robolectric.annotation.Config;
25import org.robolectric.res.Fs;
26
27@RunWith(JUnit4.class)
28public class AndroidManifestTest {
29  @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
30
31  @Test
32  public void parseManifest_shouldReadContentProviders() throws Exception {
33    AndroidManifest config = newConfig("TestAndroidManifestWithContentProviders.xml");
34
35    assertThat(config.getContentProviders().get(0).getName())
36        .isEqualTo("org.robolectric.tester.FullyQualifiedClassName");
37    assertThat(config.getContentProviders().get(0).getAuthorities()).isEqualTo("org.robolectric.authority1");
38
39    assertThat(config.getContentProviders().get(1).getName())
40        .isEqualTo("org.robolectric.tester.PartiallyQualifiedClassName");
41    assertThat(config.getContentProviders().get(1).getAuthorities()).isEqualTo("org.robolectric.authority2");
42  }
43
44  @Test
45  public void parseManifest_shouldReadPermissions() throws Exception {
46    AndroidManifest config = newConfig("TestAndroidManifestWithPermissions.xml");
47
48    assertThat(config.getPermissions().keySet())
49        .contains("some_permission",
50            "permission_with_literal_label",
51            "permission_with_minimal_fields");
52    PermissionItemData permissionItemData = config.getPermissions().get("some_permission");
53    assertThat(permissionItemData.getMetaData().getValueMap()).containsEntry("meta_data_name", "meta_data_value");
54    assertThat(permissionItemData.getName()).isEqualTo("some_permission");
55    assertThat(permissionItemData.getPermissionGroup()).isEqualTo("my_permission_group");
56    assertThat(permissionItemData.getDescription()).isEqualTo("@string/test_permission_description");
57    assertThat(permissionItemData.getProtectionLevel()).isEqualTo("dangerous");
58  }
59
60  @Test
61  public void parseManifest_shouldReadPermissionGroups() throws Exception {
62    AndroidManifest config = newConfig("TestAndroidManifestWithPermissions.xml");
63
64    assertThat(config.getPermissionGroups().keySet())
65        .contains("permission_group");
66    PermissionGroupItemData permissionGroupItemData = config.getPermissionGroups().get("permission_group");
67    assertThat(permissionGroupItemData.getName()).isEqualTo("permission_group");
68    assertThat(permissionGroupItemData.getDescription()).isEqualTo("@string/test_permission_description");
69  }
70
71  @Test
72  public void parseManifest_shouldReadBroadcastReceivers() throws Exception {
73    AndroidManifest config = newConfig("TestAndroidManifestWithReceivers.xml");
74    assertThat(config.getBroadcastReceivers()).hasSize(8);
75
76    assertThat(config.getBroadcastReceivers().get(0).getName())
77        .isEqualTo("org.robolectric.ConfigTestReceiver.InnerReceiver");
78    assertThat(config.getBroadcastReceivers().get(0).getActions()).contains("org.robolectric.ACTION1", "org.robolectric.ACTION2");
79
80    assertThat(config.getBroadcastReceivers().get(1).getName())
81        .isEqualTo("org.robolectric.fakes.ConfigTestReceiver");
82    assertThat(config.getBroadcastReceivers().get(1).getActions()).contains("org.robolectric.ACTION_SUPERSET_PACKAGE");
83
84    assertThat(config.getBroadcastReceivers().get(2).getName())
85        .isEqualTo("org.robolectric.ConfigTestReceiver");
86    assertThat(config.getBroadcastReceivers().get(2).getActions()).contains("org.robolectric.ACTION_SUBSET_PACKAGE");
87
88    assertThat(config.getBroadcastReceivers().get(3).getName())
89        .isEqualTo("org.robolectric.DotConfigTestReceiver");
90    assertThat(config.getBroadcastReceivers().get(3).getActions()).contains("org.robolectric.ACTION_DOT_PACKAGE");
91
92    assertThat(config.getBroadcastReceivers().get(4).getName())
93        .isEqualTo("org.robolectric.test.ConfigTestReceiver");
94    assertThat(config.getBroadcastReceivers().get(4).getActions()).contains("org.robolectric.ACTION_DOT_SUBPACKAGE");
95
96    assertThat(config.getBroadcastReceivers().get(5).getName()).isEqualTo("com.foo.Receiver");
97    assertThat(config.getBroadcastReceivers().get(5).getActions()).contains("org.robolectric.ACTION_DIFFERENT_PACKAGE");
98    assertThat(config.getBroadcastReceivers().get(5).getIntentFilters()).hasSize(1);
99    IntentFilterData filter = config.getBroadcastReceivers().get(5).getIntentFilters().get(0);
100    assertThat(filter.getActions()).containsExactly("org.robolectric.ACTION_DIFFERENT_PACKAGE");
101
102    assertThat(config.getBroadcastReceivers().get(6).getName())
103        .isEqualTo("com.bar.ReceiverWithoutIntentFilter");
104    assertThat(config.getBroadcastReceivers().get(6).getActions()).isEmpty();
105
106    assertThat(config.getBroadcastReceivers().get(7).getName())
107        .isEqualTo("org.robolectric.ConfigTestReceiverPermissionsAndActions");
108    assertThat(config.getBroadcastReceivers().get(7).getActions()).contains("org.robolectric.ACTION_RECEIVER_PERMISSION_PACKAGE");
109  }
110
111  @Test
112  public void parseManifest_shouldReadServices() throws Exception {
113    AndroidManifest config = newConfig("TestAndroidManifestWithServices.xml");
114    assertThat(config.getServices()).hasSize(2);
115
116    assertThat(config.getServices().get(0).getClassName()).isEqualTo("com.foo.Service");
117    assertThat(config.getServices().get(0).getActions()).contains("org.robolectric.ACTION_DIFFERENT_PACKAGE");
118    assertThat(config.getServices().get(0).getIntentFilters()).isNotEmpty();
119    assertThat(config.getServices().get(0).getIntentFilters().get(0).getMimeTypes()).containsExactly("image/jpeg");
120
121    assertThat(config.getServices().get(1).getClassName()).isEqualTo("com.bar.ServiceWithoutIntentFilter");
122    assertThat(config.getServices().get(1).getActions()).isEmpty();
123    assertThat(config.getServices().get(1).getIntentFilters()).isEmpty();
124
125    assertThat(config.getServiceData("com.foo.Service").getClassName()).isEqualTo("com.foo.Service");
126    assertThat(config.getServiceData("com.bar.ServiceWithoutIntentFilter").getClassName()).isEqualTo("com.bar.ServiceWithoutIntentFilter");
127    assertThat(config.getServiceData("com.foo.Service").getPermission())
128        .isEqualTo("com.foo.Permission");
129  }
130
131  @Test
132  public void testManifestWithNoApplicationElement() throws Exception {
133    AndroidManifest config = newConfig("TestAndroidManifestNoApplicationElement.xml");
134    assertThat(config.getPackageName()).isEqualTo("org.robolectric");
135  }
136
137  @Test
138  public void parseManifest_shouldReadBroadcastReceiversWithMetaData() throws Exception {
139    AndroidManifest config = newConfig("TestAndroidManifestWithReceivers.xml");
140
141    assertThat(config.getBroadcastReceivers().get(4).getName())
142        .isEqualTo("org.robolectric.test.ConfigTestReceiver");
143    assertThat(config.getBroadcastReceivers().get(4).getActions())
144        .contains("org.robolectric.ACTION_DOT_SUBPACKAGE");
145
146    Map<String, Object> meta = config.getBroadcastReceivers().get(4).getMetaData().getValueMap();
147    Object metaValue = meta.get("org.robolectric.metaName1");
148    assertThat(metaValue).isEqualTo("metaValue1");
149
150    metaValue = meta.get("org.robolectric.metaName2");
151    assertThat(metaValue).isEqualTo("metaValue2");
152
153    metaValue = meta.get("org.robolectric.metaFalse");
154    assertThat(metaValue).isEqualTo("false");
155
156    metaValue = meta.get("org.robolectric.metaTrue");
157    assertThat(metaValue).isEqualTo("true");
158
159    metaValue = meta.get("org.robolectric.metaInt");
160    assertThat(metaValue).isEqualTo("123");
161
162    metaValue = meta.get("org.robolectric.metaFloat");
163    assertThat(metaValue).isEqualTo("1.23");
164
165    metaValue = meta.get("org.robolectric.metaColor");
166    assertThat(metaValue).isEqualTo("#FFFFFF");
167
168    metaValue = meta.get("org.robolectric.metaBooleanFromRes");
169    assertThat(metaValue).isEqualTo("@bool/false_bool_value");
170
171    metaValue = meta.get("org.robolectric.metaIntFromRes");
172    assertThat(metaValue).isEqualTo("@integer/test_integer1");
173
174    metaValue = meta.get("org.robolectric.metaColorFromRes");
175    assertThat(metaValue).isEqualTo("@color/clear");
176
177    metaValue = meta.get("org.robolectric.metaStringFromRes");
178    assertThat(metaValue).isEqualTo("@string/app_name");
179
180    metaValue = meta.get("org.robolectric.metaStringOfIntFromRes");
181    assertThat(metaValue).isEqualTo("@string/str_int");
182
183    metaValue = meta.get("org.robolectric.metaStringRes");
184    assertThat(metaValue).isEqualTo("@string/app_name");
185  }
186
187  @Test
188  public void shouldReadBroadcastReceiverPermissions() throws Exception {
189    AndroidManifest config = newConfig("TestAndroidManifestWithReceivers.xml");
190
191    assertThat(config.getBroadcastReceivers().get(7).getName())
192        .isEqualTo("org.robolectric.ConfigTestReceiverPermissionsAndActions");
193    assertThat(config.getBroadcastReceivers().get(7).getActions())
194        .contains("org.robolectric.ACTION_RECEIVER_PERMISSION_PACKAGE");
195
196    assertThat(config.getBroadcastReceivers().get(7).getPermission())
197        .isEqualTo("org.robolectric.CUSTOM_PERM");
198  }
199
200  @Test
201  public void shouldReadTargetSdkVersionFromAndroidManifestOrDefaultToMin() throws Exception {
202    assertThat(
203            newConfigWith(
204                    "targetsdk42minsdk6.xml",
205                    "android:targetSdkVersion=\"42\" android:minSdkVersion=\"7\"")
206                .getTargetSdkVersion())
207        .isEqualTo(42);
208    assertThat(newConfigWith("minsdk7.xml", "android:minSdkVersion=\"7\"").getTargetSdkVersion())
209        .isEqualTo(7);
210    assertThat(newConfigWith("noattributes.xml", "").getTargetSdkVersion())
211        .isEqualTo(VERSION_CODES.JELLY_BEAN);
212  }
213
214  @Test
215  public void shouldReadMinSdkVersionFromAndroidManifestOrDefaultToJellyBean() throws Exception {
216    assertThat(newConfigWith("minsdk17.xml", "android:minSdkVersion=\"17\"").getMinSdkVersion())
217        .isEqualTo(17);
218    assertThat(newConfigWith("noattributes.xml", "").getMinSdkVersion())
219        .isEqualTo(VERSION_CODES.JELLY_BEAN);
220  }
221
222  /**
223   * For Android O preview, apps are encouraged to use targetSdkVersion="O".
224   *
225   * @see <a href="http://google.com">https://developer.android.com/preview/migration.html</a>
226   */
227  @Test
228  public void shouldReadTargetSDKVersionOPreview() throws Exception {
229    assertThat(
230            newConfigWith("TestAndroidManifestForPreview.xml", "android:targetSdkVersion=\"O\"")
231                .getTargetSdkVersion())
232        .isEqualTo(26);
233  }
234
235  @Test
236  public void shouldReadProcessFromAndroidManifest() throws Exception {
237    assertThat(newConfig("TestAndroidManifestWithProcess.xml").getProcessName())
238        .isEqualTo("robolectricprocess");
239  }
240
241  @Test
242  public void shouldReturnPackageNameWhenNoProcessIsSpecifiedInTheManifest() {
243    assertThat(newConfig("TestAndroidManifestWithNoProcess.xml").getProcessName())
244        .isEqualTo("org.robolectric");
245  }
246
247  @Test
248  @Config(manifest = "TestAndroidManifestWithAppMetaData.xml")
249  public void shouldReturnApplicationMetaData() throws Exception {
250    Map<String, Object> meta =
251        newConfig("TestAndroidManifestWithAppMetaData.xml").getApplicationMetaData();
252
253    Object metaValue = meta.get("org.robolectric.metaName1");
254    assertThat(metaValue).isEqualTo("metaValue1");
255
256    metaValue = meta.get("org.robolectric.metaName2");
257    assertThat(metaValue).isEqualTo("metaValue2");
258
259    metaValue = meta.get("org.robolectric.metaFalse");
260    assertThat(metaValue).isEqualTo("false");
261
262    metaValue = meta.get("org.robolectric.metaTrue");
263    assertThat(metaValue).isEqualTo("true");
264
265    metaValue = meta.get("org.robolectric.metaInt");
266    assertThat(metaValue).isEqualTo("123");
267
268    metaValue = meta.get("org.robolectric.metaFloat");
269    assertThat(metaValue).isEqualTo("1.23");
270
271    metaValue = meta.get("org.robolectric.metaColor");
272    assertThat(metaValue).isEqualTo("#FFFFFF");
273
274    metaValue = meta.get("org.robolectric.metaBooleanFromRes");
275    assertThat(metaValue).isEqualTo("@bool/false_bool_value");
276
277    metaValue = meta.get("org.robolectric.metaIntFromRes");
278    assertThat(metaValue).isEqualTo("@integer/test_integer1");
279
280    metaValue = meta.get("org.robolectric.metaColorFromRes");
281    assertThat(metaValue).isEqualTo("@color/clear");
282
283    metaValue = meta.get("org.robolectric.metaStringFromRes");
284    assertThat(metaValue).isEqualTo("@string/app_name");
285
286    metaValue = meta.get("org.robolectric.metaStringOfIntFromRes");
287    assertThat(metaValue).isEqualTo("@string/str_int");
288
289    metaValue = meta.get("org.robolectric.metaStringRes");
290    assertThat(metaValue).isEqualTo("@string/app_name");
291  }
292
293  @Test
294  public void shouldTolerateMissingRFile() throws Exception {
295    AndroidManifest appManifest = new AndroidManifest(resourceFile("TestAndroidManifestWithNoRFile.xml"), resourceFile("res"), resourceFile("assets"));
296    assertThat(appManifest.getPackageName()).isEqualTo("org.no.resources.for.me");
297    assertThat(appManifest.getRClass()).isNull();
298  }
299
300  @Test
301  public void whenNullManifestFile_getRClass_shouldComeFromPackageName() throws Exception {
302    AndroidManifest appManifest = new AndroidManifest(null, resourceFile("res"), resourceFile("assets"), "org.robolectric.lib1");
303    assertThat(appManifest.getRClass()).isEqualTo(org.robolectric.lib1.R.class);
304    assertThat(appManifest.getPackageName()).isEqualTo("org.robolectric.lib1");
305  }
306
307  @Test
308  public void whenMissingManifestFile_getRClass_shouldComeFromPackageName() throws Exception {
309    AndroidManifest appManifest = new AndroidManifest(resourceFile("none.xml"), resourceFile("res"), resourceFile("assets"), "org.robolectric.lib1");
310    assertThat(appManifest.getRClass()).isEqualTo(org.robolectric.lib1.R.class);
311    assertThat(appManifest.getPackageName()).isEqualTo("org.robolectric.lib1");
312  }
313
314  @Test
315  public void whenMissingManifestFile_getPackageName_shouldBeDefault() throws Exception {
316    AndroidManifest appManifest = new AndroidManifest(null, resourceFile("res"), resourceFile("assets"), null);
317    assertThat(appManifest.getPackageName()).isEqualTo("org.robolectric.default");
318    assertThat(appManifest.getRClass()).isEqualTo(null);
319  }
320
321  @Test
322  public void shouldRead1IntentFilter() {
323    AndroidManifest appManifest = newConfig("TestAndroidManifestForActivitiesWithIntentFilter.xml");
324    appManifest.getMinSdkVersion(); // Force parsing
325
326    ActivityData activityData = appManifest.getActivityData("org.robolectric.shadows.TestActivity");
327    final List<IntentFilterData> ifd = activityData.getIntentFilters();
328    assertThat(ifd).isNotNull();
329    assertThat(ifd.size()).isEqualTo(1);
330
331    final IntentFilterData data = ifd.get(0);
332    assertThat(data.getActions().size()).isEqualTo(1);
333    assertThat(data.getActions().get(0)).isEqualTo(Intent.ACTION_MAIN);
334    assertThat(data.getCategories().size()).isEqualTo(1);
335    assertThat(data.getCategories().get(0)).isEqualTo(Intent.CATEGORY_LAUNCHER);
336  }
337
338  @Test
339  public void shouldReadMultipleIntentFilters() {
340    AndroidManifest appManifest = newConfig("TestAndroidManifestForActivitiesWithMultipleIntentFilters.xml");
341    appManifest.getMinSdkVersion(); // Force parsing
342
343    ActivityData activityData = appManifest.getActivityData("org.robolectric.shadows.TestActivity");
344    final List<IntentFilterData> ifd = activityData.getIntentFilters();
345    assertThat(ifd).isNotNull();
346    assertThat(ifd.size()).isEqualTo(2);
347
348    IntentFilterData data = ifd.get(0);
349    assertThat(data.getActions().size()).isEqualTo(1);
350    assertThat(data.getActions().get(0)).isEqualTo(Intent.ACTION_MAIN);
351    assertThat(data.getCategories().size()).isEqualTo(1);
352    assertThat(data.getCategories().get(0)).isEqualTo(Intent.CATEGORY_LAUNCHER);
353
354    data = ifd.get(1);
355    assertThat(data.getActions().size()).isEqualTo(3);
356    assertThat(data.getActions().get(0)).isEqualTo(Intent.ACTION_VIEW);
357    assertThat(data.getActions().get(1)).isEqualTo(Intent.ACTION_EDIT);
358    assertThat(data.getActions().get(2)).isEqualTo(Intent.ACTION_PICK);
359
360    assertThat(data.getCategories().size()).isEqualTo(3);
361    assertThat(data.getCategories().get(0)).isEqualTo(Intent.CATEGORY_DEFAULT);
362    assertThat(data.getCategories().get(1)).isEqualTo(Intent.CATEGORY_ALTERNATIVE);
363    assertThat(data.getCategories().get(2)).isEqualTo(Intent.CATEGORY_SELECTED_ALTERNATIVE);
364  }
365
366  @Test
367  public void shouldReadTaskAffinity() {
368    AndroidManifest appManifest = newConfig("TestAndroidManifestForActivitiesWithTaskAffinity.xml");
369    assertThat(appManifest.getTargetSdkVersion()).isEqualTo(16);
370
371    ActivityData activityData = appManifest.getActivityData("org.robolectric.shadows.TestTaskAffinityActivity");
372    assertThat(activityData).isNotNull();
373    assertThat(activityData.getTaskAffinity()).isEqualTo("org.robolectric.shadows.TestTaskAffinity");
374  }
375
376  @Test
377  public void shouldReadPermissions() throws Exception {
378    AndroidManifest config = newConfig("TestAndroidManifestWithPermissions.xml");
379
380    assertThat(config.getUsedPermissions()).hasSize(3);
381    assertThat(config.getUsedPermissions().get(0)).isEqualTo(Manifest.permission.INTERNET);
382    assertThat(config.getUsedPermissions().get(1)).isEqualTo(Manifest.permission.SYSTEM_ALERT_WINDOW);
383    assertThat(config.getUsedPermissions().get(2)).isEqualTo(Manifest.permission.GET_TASKS);
384  }
385
386  @Test
387  public void shouldReadPartiallyQualifiedActivities() throws Exception {
388    AndroidManifest config = newConfig("TestAndroidManifestForActivities.xml");
389    assertThat(config.getActivityDatas()).hasSize(2);
390    assertThat(config.getActivityDatas()).containsKey("org.robolectric.shadows.TestActivity");
391    assertThat(config.getActivityDatas()).containsKey("org.robolectric.shadows.TestActivity2");
392  }
393
394  @Test
395  public void shouldReadActivityAliases() throws Exception {
396    AndroidManifest config = newConfig("TestAndroidManifestForActivityAliases.xml");
397    assertThat(config.getActivityDatas()).hasSize(2);
398    assertThat(config.getActivityDatas()).containsKey("org.robolectric.shadows.TestActivity");
399    assertThat(config.getActivityDatas()).containsKey("org.robolectric.shadows.TestActivityAlias");
400  }
401
402  @Test
403  public void shouldReadIntentFilterWithData() {
404    AndroidManifest appManifest = newConfig("TestAndroidManifestForActivitiesWithIntentFilterWithData.xml");
405    appManifest.getMinSdkVersion(); // Force parsing
406
407    ActivityData activityData = appManifest.getActivityData("org.robolectric.shadows.TestActivity");
408    final List<IntentFilterData> ifd = activityData.getIntentFilters();
409    assertThat(ifd).isNotNull();
410    assertThat(ifd.size()).isEqualTo(1);
411
412    final IntentFilterData intentFilterData = ifd.get(0);
413    assertThat(intentFilterData.getActions().size()).isEqualTo(1);
414    assertThat(intentFilterData.getActions().get(0)).isEqualTo(Intent.ACTION_VIEW);
415    assertThat(intentFilterData.getCategories().size()).isEqualTo(1);
416    assertThat(intentFilterData.getCategories().get(0)).isEqualTo(Intent.CATEGORY_DEFAULT);
417
418    assertThat(intentFilterData.getSchemes().size()).isEqualTo(3);
419    assertThat(intentFilterData.getAuthorities().size()).isEqualTo(3);
420    assertThat(intentFilterData.getMimeTypes().size()).isEqualTo(3);
421    assertThat(intentFilterData.getPaths().size()).isEqualTo(1);
422    assertThat(intentFilterData.getPathPatterns().size()).isEqualTo(1);
423    assertThat(intentFilterData.getPathPrefixes().size()).isEqualTo(1);
424
425
426    assertThat(intentFilterData.getSchemes().get(0)).isEqualTo("content");
427    assertThat(intentFilterData.getPaths().get(0)).isEqualTo("/testPath/test.jpeg");
428    assertThat(intentFilterData.getMimeTypes().get(0)).isEqualTo("video/mpeg");
429    assertThat(intentFilterData.getAuthorities().get(0).getHost()).isEqualTo("testhost1.com");
430    assertThat(intentFilterData.getAuthorities().get(0).getPort()).isEqualTo("1");
431
432    assertThat(intentFilterData.getSchemes().get(1)).isEqualTo("http");
433    assertThat(intentFilterData.getPathPrefixes().get(0)).isEqualTo("/testPrefix");
434    assertThat(intentFilterData.getMimeTypes().get(1)).isEqualTo("image/jpeg");
435    assertThat(intentFilterData.getAuthorities().get(1).getHost()).isEqualTo("testhost2.com");
436    assertThat(intentFilterData.getAuthorities().get(1).getPort()).isEqualTo("2");
437
438    assertThat(intentFilterData.getSchemes().get(2)).isEqualTo("https");
439    assertThat(intentFilterData.getPathPatterns().get(0)).isEqualTo("/.*testPattern");
440    assertThat(intentFilterData.getMimeTypes().get(2)).isEqualTo("image/*");
441    assertThat(intentFilterData.getAuthorities().get(2).getHost()).isEqualTo("testhost3.com");
442    assertThat(intentFilterData.getAuthorities().get(2).getPort()).isEqualTo("3");
443  }
444
445  @Test
446  public void shouldHaveStableHashCode() throws Exception {
447    AndroidManifest manifest = newConfig("TestAndroidManifestWithContentProviders.xml");
448    int hashCode1 = manifest.hashCode();
449    manifest.getServices();
450    int hashCode2 = manifest.hashCode();
451    assertThat(hashCode2).isEqualTo(hashCode1);
452  }
453
454  @Test
455  public void shouldReadApplicationAttrsFromAndroidManifest() throws Exception {
456    AndroidManifest config = newConfig("TestAndroidManifestWithFlags.xml");
457    assertThat(config.getApplicationAttributes().get("android:allowBackup")).isEqualTo("true");
458  }
459
460  @Test
461  public void allFieldsShouldBePrimitivesOrJavaLangOrRobolectric() throws Exception {
462    List<Field> wrongFields = new ArrayList<>();
463    for (Field field : AndroidManifest.class.getDeclaredFields()) {
464      Class<?> type = field.getType();
465      if (type.isPrimitive()) continue;
466
467      String packageName = type.getPackage().getName();
468      if (packageName.startsWith("java.")
469          || packageName.equals("org.robolectric.res")
470          || packageName.equals("org.robolectric.manifest")
471          ) continue;
472
473      wrongFields.add(field);
474    }
475
476    assertThat(wrongFields).isEmpty();
477  }
478
479  @Test
480  public void activitiesWithoutIntentFiltersNotExportedByDefault() throws Exception {
481    AndroidManifest config = newConfig("TestAndroidManifestForActivities.xml");
482    ActivityData activityData = config.getActivityData("org.robolectric.shadows.TestActivity");
483    assertThat(activityData.isExported()).isFalse();
484  }
485
486  @Test
487  public void activitiesWithIntentFiltersExportedByDefault() throws Exception {
488    AndroidManifest config = newConfig("TestAndroidManifestForActivitiesWithIntentFilter.xml");
489    ActivityData activityData = config.getActivityData("org.robolectric.shadows.TestActivity");
490    assertThat(activityData.isExported()).isTrue();
491  }
492
493  @Test
494  public void servicesWithoutIntentFiltersNotExportedByDefault() throws Exception {
495    AndroidManifest config = newConfig("TestAndroidManifestWithServices.xml");
496    ServiceData serviceData = config.getServiceData("com.bar.ServiceWithoutIntentFilter");
497    assertThat(serviceData.isExported()).isFalse();
498  }
499
500  @Test
501  public void servicesWithIntentFiltersExportedByDefault() throws Exception {
502    AndroidManifest config = newConfig("TestAndroidManifestWithServices.xml");
503    ServiceData serviceData = config.getServiceData("com.foo.Service");
504    assertThat(serviceData.isExported()).isTrue();
505  }
506
507  @Test
508  public void receiversWithoutIntentFiltersNotExportedByDefault() throws Exception {
509    AndroidManifest config = newConfig("TestAndroidManifestWithReceivers.xml");
510    BroadcastReceiverData receiverData =
511        config.getBroadcastReceiver("com.bar.ReceiverWithoutIntentFilter");
512    assertThat(receiverData).isNotNull();
513    assertThat(receiverData.isExported()).isFalse();
514  }
515
516  @Test
517  public void receiversWithIntentFiltersExportedByDefault() throws Exception {
518    AndroidManifest config = newConfig("TestAndroidManifestWithReceivers.xml");
519    BroadcastReceiverData receiverData = config.getBroadcastReceiver("com.foo.Receiver");
520    assertThat(receiverData).isNotNull();
521    assertThat(receiverData.isExported()).isTrue();
522  }
523
524  @Test
525  public void getTransitiveManifests() throws Exception {
526    AndroidManifest lib1 = new AndroidManifest(resourceFile("lib1/AndroidManifest.xml"), null, null);
527    AndroidManifest lib2 = new AndroidManifest(resourceFile("lib2/AndroidManifest.xml"), null, null,
528        Collections.singletonList(lib1), null);
529    AndroidManifest app = new AndroidManifest(
530        resourceFile("TestAndroidManifestWithReceivers.xml"), null, null,
531        Arrays.asList(lib1, lib2), null);
532    assertThat(app.getAllManifests()).containsExactly(app, lib1, lib2);
533  }
534
535  /////////////////////////////
536
537  private AndroidManifest newConfigWith(String fileName, String usesSdkAttrs) throws IOException {
538    String contents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
539        "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
540        "          package=\"org.robolectric\">\n" +
541        "    <uses-sdk " + usesSdkAttrs + "/>\n" +
542        "</manifest>\n";
543    File f = temporaryFolder.newFile(fileName);
544    Files.write(contents, f, Charsets.UTF_8);
545    return new AndroidManifest(Fs.newFile(f), null, null);
546  }
547
548  private static AndroidManifest newConfig(String androidManifestFile) {
549    return new AndroidManifest(resourceFile(androidManifestFile), null, null);
550  }
551}
552