1 package com.xtremelabs.robolectric; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 6 import android.content.BroadcastReceiver; 7 import android.content.Context; 8 import android.content.Intent; 9 10 import static android.content.pm.ApplicationInfo.*; 11 import static com.xtremelabs.robolectric.util.TestUtil.newConfig; 12 import static org.junit.Assert.assertEquals; 13 import static org.junit.Assert.assertTrue; 14 15 @RunWith(WithTestDefaultsRunner.class) 16 public class RobolectricConfigTest { 17 @Test shouldReadBroadcastReceivers()18 public void shouldReadBroadcastReceivers() throws Exception { 19 RobolectricConfig config = newConfig("TestAndroidManifestWithReceivers.xml"); 20 21 assertEquals(7, config.getReceiverCount()); 22 23 assertEquals("com.xtremelabs.robolectric.RobolectricConfigTest.ConfigTestReceiver", config.getReceiverClassName(0)); 24 assertEquals("com.xtremelabs.robolectric.ACTION1", config.getReceiverIntentFilterActions(0).get(0)); 25 26 assertEquals("com.xtremelabs.robolectric.RobolectricConfigTest.ConfigTestReceiver", config.getReceiverClassName(1)); 27 assertEquals("com.xtremelabs.robolectric.ACTION2", config.getReceiverIntentFilterActions(1).get(0)); 28 29 assertEquals("com.xtremelabs.robolectric.test.ConfigTestReceiver", config.getReceiverClassName(2)); 30 assertEquals("com.xtremelabs.robolectric.ACTION_SUPERSET_PACKAGE", config.getReceiverIntentFilterActions(2).get(0)); 31 32 assertEquals("com.xtremelabs.ConfigTestReceiver", config.getReceiverClassName(3)); 33 assertEquals("com.xtremelabs.robolectric.ACTION_SUBSET_PACKAGE", config.getReceiverIntentFilterActions(3).get(0)); 34 35 assertEquals("com.xtremelabs.robolectric.DotConfigTestReceiver", config.getReceiverClassName(4)); 36 assertEquals("com.xtremelabs.robolectric.ACTION_DOT_PACKAGE", config.getReceiverIntentFilterActions(4).get(0)); 37 38 assertEquals("com.xtremelabs.robolectric.test.ConfigTestReceiver", config.getReceiverClassName(5)); 39 assertEquals("com.xtremelabs.robolectric.ACTION_DOT_SUBPACKAGE", config.getReceiverIntentFilterActions(5).get(0)); 40 41 assertEquals("com.foo.Receiver", config.getReceiverClassName(6)); 42 assertEquals("com.xtremelabs.robolectric.ACTION_DIFFERENT_PACKAGE", config.getReceiverIntentFilterActions(6).get(0)); 43 } 44 45 @Test shouldReadSdkVersionFromAndroidManifest()46 public void shouldReadSdkVersionFromAndroidManifest() throws Exception { 47 assertEquals(42, newConfig("TestAndroidManifestWithSdkVersion.xml").getSdkVersion()); 48 assertEquals(3, newConfig("TestAndroidManifestWithSdkVersion.xml").getMinSdkVersion()); 49 } 50 51 @Test shouldRessolveSdkVersionForResources()52 public void shouldRessolveSdkVersionForResources() throws Exception { 53 assertEquals(3, newConfig("TestAndroidManifestWithMinSdkVersionOnly.xml").getRealSdkVersion()); 54 assertEquals(42, newConfig("TestAndroidManifestWithSdkVersion.xml").getRealSdkVersion()); 55 } 56 57 @Test shouldReadProcessFromAndroidManifest()58 public void shouldReadProcessFromAndroidManifest() throws Exception { 59 assertEquals("robolectricprocess", newConfig("TestAndroidManifestWithProcess.xml").getProcessName()); 60 } 61 62 @Test shouldReturnPackageNameWhenNoProcessIsSpecifiedInTheManifest()63 public void shouldReturnPackageNameWhenNoProcessIsSpecifiedInTheManifest() { 64 assertEquals("com.xtremelabs.robolectric", newConfig("TestAndroidManifestWithNoProcess.xml").getProcessName()); 65 } 66 67 @Test shouldReadFlagsFromAndroidManifest()68 public void shouldReadFlagsFromAndroidManifest() throws Exception { 69 RobolectricConfig config = newConfig("TestAndroidManifestWithFlags.xml"); 70 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_ALLOW_BACKUP)); 71 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_ALLOW_CLEAR_USER_DATA)); 72 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_ALLOW_TASK_REPARENTING)); 73 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_DEBUGGABLE)); 74 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_HAS_CODE)); 75 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_KILL_AFTER_RESTORE)); 76 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_PERSISTENT)); 77 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_RESIZEABLE_FOR_SCREENS)); 78 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_RESTORE_ANY_VERSION)); 79 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_SUPPORTS_LARGE_SCREENS)); 80 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_SUPPORTS_NORMAL_SCREENS)); 81 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_SUPPORTS_SCREEN_DENSITIES)); 82 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_SUPPORTS_SMALL_SCREENS)); 83 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_TEST_ONLY)); 84 assertTrue(hasFlag(config.getApplicationFlags(), FLAG_VM_SAFE_MODE)); 85 } 86 hasFlag(final int flags, final int flag)87 private boolean hasFlag(final int flags, final int flag) { 88 return (flags & flag) != 0; 89 } 90 91 public static class ConfigTestReceiver extends BroadcastReceiver { 92 @Override onReceive(Context context, Intent intent)93 public void onReceive(Context context, Intent intent) { 94 } 95 } 96 } 97