Home | History | Annotate | Download | only in internal
      1 package org.robolectric.android.internal;
      2 
      3 import static org.assertj.core.api.Assertions.assertThat;
      4 import static org.robolectric.Shadows.shadowOf;
      5 
      6 import android.app.Application;
      7 import com.google.common.base.Charsets;
      8 import com.google.common.io.Files;
      9 import java.io.File;
     10 import java.io.IOException;
     11 import java.util.List;
     12 import org.junit.Rule;
     13 import org.junit.Test;
     14 import org.junit.rules.TemporaryFolder;
     15 import org.junit.runner.RunWith;
     16 import org.robolectric.FakeApp;
     17 import org.robolectric.RobolectricTestRunner;
     18 import org.robolectric.RuntimeEnvironment;
     19 import org.robolectric.TestApplication;
     20 import org.robolectric.TestFakeApp;
     21 import org.robolectric.annotation.Config;
     22 import org.robolectric.manifest.AndroidManifest;
     23 import org.robolectric.res.Fs;
     24 import org.robolectric.shadows.ShadowApplication;
     25 
     26 @RunWith(RobolectricTestRunner.class)
     27 public class ParallelUniverseCreateApplicationTest {
     28 
     29   @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
     30 
     31   @Test(expected = RuntimeException.class)
     32   public void shouldThrowWhenManifestContainsBadApplicationClassName() throws Exception {
     33     ParallelUniverse.createApplication(
     34         newConfigWith("<application android:name=\"org.robolectric.BogusTestApplication\"/>)"), null);
     35   }
     36 
     37   @Test
     38   public void shouldReturnDefaultAndroidApplicationWhenManifestDeclaresNoAppName() throws Exception {
     39     assertThat(ParallelUniverse.createApplication(newConfigWith(""), null))
     40         .isExactlyInstanceOf(Application.class);
     41   }
     42 
     43   @Test
     44   public void shouldReturnSpecifiedApplicationWhenManifestDeclaresAppName() throws Exception {
     45     assertThat(ParallelUniverse.createApplication(
     46         newConfigWith("<application android:name=\"org.robolectric.TestApplication\"/>"), null))
     47         .isExactlyInstanceOf(TestApplication.class);
     48   }
     49 
     50   @Test public void shouldAssignThePackageNameFromTheManifest() throws Exception {
     51     Application application = RuntimeEnvironment.application;
     52 
     53     assertThat(application.getPackageName()).isEqualTo("org.robolectric");
     54     assertThat(application).isExactlyInstanceOf(TestApplication.class);
     55   }
     56 
     57   @Test
     58   public void shouldRegisterReceiversFromTheManifest() throws Exception {
     59     AndroidManifest appManifest = newConfigWith(
     60         "<application>"
     61             + "    <receiver android:name=\"org.robolectric.fakes.ConfigTestReceiver\">"
     62             + "      <intent-filter>\n"
     63             + "        <action android:name=\"org.robolectric.ACTION_SUPERSET_PACKAGE\"/>\n"
     64             + "      </intent-filter>"
     65             + "    </receiver>"
     66             + "</application>");
     67     Application application = ParallelUniverse.createApplication(appManifest, null);
     68 
     69     List<ShadowApplication.Wrapper> receivers = shadowOf(application).getRegisteredReceivers();
     70     assertThat(receivers).hasSize(1);
     71     assertThat(receivers.get(0).intentFilter.matchAction("org.robolectric.ACTION_SUPERSET_PACKAGE")).isTrue();
     72   }
     73 
     74   @Test public void shouldDoTestApplicationNameTransform() throws Exception {
     75     assertThat(ParallelUniverse.getTestApplicationName(".Applicationz")).isEqualTo(".TestApplicationz");
     76     assertThat(ParallelUniverse.getTestApplicationName("Applicationz")).isEqualTo("TestApplicationz");
     77     assertThat(ParallelUniverse.getTestApplicationName("com.foo.Applicationz")).isEqualTo("com.foo.TestApplicationz");
     78   }
     79 
     80   @Test public void shouldLoadConfigApplicationIfSpecified() throws Exception {
     81     Application application = ParallelUniverse.createApplication(
     82         newConfigWith("<application android:name=\"" + "ClassNameToIgnore" + "\"/>"),
     83         new Config.Builder().setApplication(TestFakeApp.class).build());
     84     assertThat(application).isExactlyInstanceOf(TestFakeApp.class);
     85   }
     86 
     87   @Test public void shouldLoadConfigInnerClassApplication() throws Exception {
     88     Application application = ParallelUniverse.createApplication(
     89         newConfigWith("<application android:name=\"" + "ClassNameToIgnore" + "\"/>"),
     90         new Config.Builder().setApplication(TestFakeAppInner.class).build());
     91     assertThat(application).isExactlyInstanceOf(TestFakeAppInner.class);
     92   }
     93 
     94   @Test public void shouldLoadTestApplicationIfClassIsPresent() throws Exception {
     95     Application application = ParallelUniverse.createApplication(
     96         newConfigWith("<application android:name=\"" + FakeApp.class.getName() + "\"/>"), null);
     97     assertThat(application).isExactlyInstanceOf(TestFakeApp.class);
     98   }
     99 
    100   @Test public void whenNoAppManifestPresent_shouldCreateGenericApplication() throws Exception {
    101     assertThat(ParallelUniverse.createApplication(null, null)).isExactlyInstanceOf(Application.class);
    102   }
    103 
    104   /////////////////////////////
    105 
    106   public AndroidManifest newConfigWith(String contents) throws IOException {
    107     return newConfigWith("org.robolectric", contents);
    108   }
    109 
    110   private AndroidManifest newConfigWith(String packageName, String contents) throws IOException {
    111     String fileContents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
    112         "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
    113         "          package=\"" + packageName + "\">\n" +
    114         "    " + contents + "\n" +
    115         "</manifest>\n";
    116     File f = temporaryFolder.newFile("whatever.xml");
    117 
    118     Files.write(fileContents, f, Charsets.UTF_8);
    119     return new AndroidManifest(Fs.newFile(f), null, null);
    120   }
    121 
    122   public static class TestFakeAppInner extends Application { }
    123 }
    124