Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.app.Activity;
      4 import android.app.Application;
      5 import android.content.BroadcastReceiver;
      6 import android.content.ComponentName;
      7 import android.content.Context;
      8 import android.content.ContextWrapper;
      9 import android.content.Intent;
     10 import android.content.IntentFilter;
     11 import android.os.IBinder;
     12 import android.os.IInterface;
     13 import android.os.Parcel;
     14 import android.os.RemoteException;
     15 import com.xtremelabs.robolectric.ApplicationResolver;
     16 import com.xtremelabs.robolectric.R;
     17 import com.xtremelabs.robolectric.Robolectric;
     18 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
     19 import com.xtremelabs.robolectric.res.ResourceLoader;
     20 import com.xtremelabs.robolectric.res.StringResourceLoader;
     21 import com.xtremelabs.robolectric.util.TestBroadcastReceiver;
     22 import org.junit.Before;
     23 import org.junit.Test;
     24 import org.junit.runner.RunWith;
     25 
     26 import java.io.FileDescriptor;
     27 import java.util.List;
     28 
     29 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     30 import static com.xtremelabs.robolectric.util.TestUtil.newConfig;
     31 import static junit.framework.Assert.assertEquals;
     32 import static junit.framework.Assert.assertTrue;
     33 import static org.hamcrest.CoreMatchers.sameInstance;
     34 import static org.hamcrest.core.IsInstanceOf.instanceOf;
     35 import static org.junit.Assert.assertFalse;
     36 import static org.junit.Assert.assertNull;
     37 import static org.junit.Assert.assertThat;
     38 import static org.mockito.Mockito.mock;
     39 import static org.mockito.Mockito.when;
     40 
     41 @RunWith(WithTestDefaultsRunner.class)
     42 public class ApplicationTest {
     43     @Before
     44     public void setUp() throws Exception {
     45         Robolectric.application = new Application();
     46     }
     47 
     48     @Test
     49     public void shouldBeAContext() throws Exception {
     50         assertThat(new Activity().getApplication(), sameInstance(Robolectric.application));
     51         assertThat(new Activity().getApplication().getApplicationContext(), sameInstance((Context) Robolectric.application));
     52     }
     53 
     54     @Test
     55     public void shouldBeBindableToAResourceLoader() throws Exception {
     56         ResourceLoader resourceLoader1 = new ResourceLoader(mock(StringResourceLoader.class)) {
     57         };
     58         when(resourceLoader1.getStringValue(R.id.title)).thenReturn("title from resourceLoader1");
     59         Application app1 = ShadowApplication.bind(new Application(), resourceLoader1);
     60 
     61         ResourceLoader resourceLoader2 = new ResourceLoader(mock(StringResourceLoader.class)) {
     62         };
     63         when(resourceLoader2.getStringValue(R.id.title)).thenReturn("title from resourceLoader2");
     64         Application app2 = ShadowApplication.bind(new Application(), resourceLoader2);
     65 
     66         assertEquals("title from resourceLoader1", new ContextWrapper(app1).getResources().getString(R.id.title));
     67         assertEquals("title from resourceLoader2", new ContextWrapper(app2).getResources().getString(R.id.title));
     68     }
     69 
     70     @Test
     71     public void shouldProvideServices() throws Exception {
     72         checkSystemService(Context.LAYOUT_INFLATER_SERVICE, android.view.LayoutInflater.class);
     73         checkSystemService(Context.ACTIVITY_SERVICE, android.app.ActivityManager.class);
     74         checkSystemService(Context.POWER_SERVICE, android.os.PowerManager.class);
     75         checkSystemService(Context.ALARM_SERVICE, android.app.AlarmManager.class);
     76         checkSystemService(Context.NOTIFICATION_SERVICE, android.app.NotificationManager.class);
     77         checkSystemService(Context.KEYGUARD_SERVICE, android.app.KeyguardManager.class);
     78         checkSystemService(Context.LOCATION_SERVICE, android.location.LocationManager.class);
     79         checkSystemService(Context.SEARCH_SERVICE, android.app.SearchManager.class);
     80         checkSystemService(Context.SENSOR_SERVICE, android.hardware.TestSensorManager.class);
     81         checkSystemService(Context.STORAGE_SERVICE, android.os.storage.StorageManager.class);
     82         checkSystemService(Context.VIBRATOR_SERVICE, android.os.TestVibrator.class);
     83 //        checkSystemService(Context.CONNECTIVITY_SERVICE, android.net.ConnectivityManager.class);
     84 //        checkSystemService(Context.WIFI_SERVICE, android.net.wifi.WifiManager.class);
     85 //        checkSystemService(Context.AUDIO_SERVICE, android.media.AudioManager.class);
     86 //        checkSystemService(Context.TELEPHONY_SERVICE, android.telephony.TelephonyManager.class);
     87 //        checkSystemService(Context.INPUT_METHOD_SERVICE, android.view.inputmethod.InputMethodManager.class);
     88 //        checkSystemService(Context.UI_MODE_SERVICE, android.app.UiModeManager.class);
     89 //        checkSystemService(Context.DOWNLOAD_SERVICE, android.app.DownloadManager.class);
     90     }
     91 
     92     private void checkSystemService(String name, Class expectedClass) {
     93         Object systemService = Robolectric.application.getSystemService(name);
     94         assertThat(systemService, instanceOf(expectedClass));
     95         assertThat(systemService, sameInstance(Robolectric.application.getSystemService(name)));
     96     }
     97 
     98     @Test
     99     public void packageManager_shouldKnowPackageName() throws Exception {
    100         Application application = new ApplicationResolver(newConfig("TestAndroidManifestWithPackageName.xml")).resolveApplication();
    101         assertEquals("com.wacka.wa", application.getPackageManager().getPackageInfo("com.wacka.wa", 0).packageName);
    102     }
    103 
    104     @Test
    105     public void packageManager_shouldKnowApplicationName() throws Exception {
    106         Application application = new ApplicationResolver(newConfig("TestAndroidManifestWithAppName.xml")).resolveApplication();
    107         assertEquals("com.xtremelabs.robolectric.TestApplication",
    108                 application.getPackageManager().getApplicationInfo("com.xtremelabs.robolectric", 0).name);
    109     }
    110 
    111     @Test
    112     public void bindServiceShouldCallOnServiceConnectedWhenNotPaused() {
    113         Robolectric.pauseMainLooper();
    114         ComponentName expectedComponentName = new ComponentName("", "");
    115         NullBinder expectedBinder = new NullBinder();
    116         Robolectric.shadowOf(Robolectric.application).setComponentNameAndServiceForBindService(expectedComponentName, expectedBinder);
    117 
    118         TestService service = new TestService();
    119         assertTrue(Robolectric.application.bindService(new Intent(""), service, Context.BIND_AUTO_CREATE));
    120 
    121         assertNull(service.name);
    122         assertNull(service.service);
    123 
    124         Robolectric.unPauseMainLooper();
    125 
    126         assertEquals(expectedComponentName, service.name);
    127         assertEquals(expectedBinder, service.service);
    128     }
    129 
    130     @Test
    131     public void unbindServiceShouldCallOnServiceDisconnectedWhenNotPaused() {
    132         TestService service = new TestService();
    133         ComponentName expectedComponentName = new ComponentName("", "");
    134         NullBinder expectedBinder = new NullBinder();
    135         Robolectric.shadowOf(Robolectric.application).setComponentNameAndServiceForBindService(expectedComponentName, expectedBinder);
    136         Robolectric.application.bindService(new Intent(""), service, Context.BIND_AUTO_CREATE);
    137         Robolectric.pauseMainLooper();
    138 
    139         Robolectric.application.unbindService(service);
    140         assertNull(service.nameUnbound);
    141         Robolectric.unPauseMainLooper();
    142         assertEquals(expectedComponentName, service.nameUnbound);
    143     }
    144 
    145     @Test
    146     public void unbindServiceAddsEntryToUnboundServicesCollection() {
    147         TestService service = new TestService();
    148         ComponentName expectedComponentName = new ComponentName("", "");
    149         NullBinder expectedBinder = new NullBinder();
    150         final ShadowApplication shadowApplication = Robolectric.shadowOf(Robolectric.application);
    151         shadowApplication.setComponentNameAndServiceForBindService(expectedComponentName, expectedBinder);
    152         Robolectric.application.bindService(new Intent(""), service, Context.BIND_AUTO_CREATE);
    153         Robolectric.application.unbindService(service);
    154         assertEquals(1, shadowApplication.getUnboundServiceConnections().size());
    155         assertEquals(service, shadowApplication.getUnboundServiceConnections().get(0));
    156     }
    157 
    158     @Test
    159     public void declaringServiceUnbindableMakesBindServiceReturnFalse() {
    160         Robolectric.pauseMainLooper();
    161         TestService service = new TestService();
    162         ComponentName expectedComponentName = new ComponentName("", "");
    163         NullBinder expectedBinder = new NullBinder();
    164         final ShadowApplication shadowApplication = Robolectric.shadowOf(Robolectric.application);
    165         shadowApplication.setComponentNameAndServiceForBindService(expectedComponentName, expectedBinder);
    166         shadowApplication.declareActionUnbindable("refuseToBind");
    167         assertFalse(Robolectric.application.bindService(new Intent("refuseToBind"), service, Context.BIND_AUTO_CREATE));
    168         Robolectric.unPauseMainLooper();
    169         assertNull(service.name);
    170         assertNull(service.service);
    171         assertNull(shadowApplication.peekNextStartedService());
    172     }
    173 
    174     @Test
    175     public void shouldHaveStoppedServiceIntentAndIndicateServiceWasntRunning() {
    176         ShadowApplication shadowApplication = Robolectric.shadowOf(Robolectric.application);
    177 
    178         Activity activity = new Activity();
    179 
    180         Intent intent = getSomeActionIntent("some.action");
    181 
    182         boolean wasRunning = activity.stopService(intent);
    183 
    184         assertFalse(wasRunning);
    185         assertEquals(intent, shadowApplication.getNextStoppedService());
    186     }
    187 
    188     private Intent getSomeActionIntent(String action) {
    189         Intent intent = new Intent();
    190         intent.setAction(action);
    191         return intent;
    192     }
    193 
    194     @Test
    195     public void shouldHaveStoppedServiceIntentAndIndicateServiceWasRunning() {
    196         ShadowApplication shadowApplication = shadowOf(Robolectric.application);
    197 
    198         Activity activity = new Activity();
    199 
    200         Intent intent = getSomeActionIntent("some.action");
    201 
    202         activity.startService(intent);
    203 
    204         boolean wasRunning = activity.stopService(intent);
    205 
    206         assertTrue(wasRunning);
    207         assertEquals(intent, shadowApplication.getNextStoppedService());
    208     }
    209 
    210     @Test
    211     public void shouldClearStartedServiceIntents() {
    212         ShadowApplication shadowApplication = shadowOf(Robolectric.application);
    213         shadowApplication.startService(getSomeActionIntent("some.action"));
    214         shadowApplication.startService(getSomeActionIntent("another.action"));
    215 
    216         shadowApplication.clearStartedServices();
    217 
    218         assertNull(shadowApplication.getNextStartedService());
    219     }
    220 
    221     @Test(expected = IllegalStateException.class)
    222     public void shouldThrowIfContainsRegisteredReceiverOfAction() {
    223         Activity activity = new Activity();
    224         activity.registerReceiver(new TestBroadcastReceiver(), new IntentFilter("Foo"));
    225 
    226         shadowOf(Robolectric.application).assertNoBroadcastListenersOfActionRegistered(activity, "Foo");
    227     }
    228 
    229     @Test
    230     public void shouldNotThrowIfDoesNotContainsRegisteredReceiverOfAction() {
    231         Activity activity = new Activity();
    232         activity.registerReceiver(new TestBroadcastReceiver(), new IntentFilter("Foo"));
    233 
    234         shadowOf(Robolectric.application).assertNoBroadcastListenersOfActionRegistered(activity, "Bar");
    235     }
    236 
    237     @Test
    238     public void canAnswerIfReceiverIsRegisteredForIntent() throws Exception {
    239         BroadcastReceiver expectedReceiver = new TestBroadcastReceiver();
    240         ShadowApplication shadowApplication = shadowOf(Robolectric.application);
    241         assertFalse(shadowApplication.hasReceiverForIntent(new Intent("Foo")));
    242         Robolectric.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
    243 
    244         assertTrue(shadowApplication.hasReceiverForIntent(new Intent("Foo")));
    245     }
    246 
    247     @Test
    248     public void canFindAllReceiversForAnIntent() throws Exception {
    249         BroadcastReceiver expectedReceiver = new TestBroadcastReceiver();
    250         ShadowApplication shadowApplication = shadowOf(Robolectric.application);
    251         assertFalse(shadowApplication.hasReceiverForIntent(new Intent("Foo")));
    252         Robolectric.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
    253         Robolectric.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
    254 
    255         assertTrue(shadowApplication.getReceiversForIntent(new Intent("Foo")).size() == 2);
    256     }
    257 
    258     @Test
    259     public void broadcasts_shouldBeLogged() {
    260         Intent broadcastIntent = new Intent("foo");
    261         Robolectric.application.sendBroadcast(broadcastIntent);
    262 
    263         List<Intent> broadcastIntents = shadowOf(Robolectric.application).getBroadcastIntents();
    264         assertTrue(broadcastIntents.size() == 1);
    265         assertEquals(broadcastIntent, broadcastIntents.get(0));
    266     }
    267 
    268     private static class NullBinder implements IBinder {
    269         @Override
    270         public String getInterfaceDescriptor() throws RemoteException {
    271             return null;
    272         }
    273 
    274         @Override
    275         public boolean pingBinder() {
    276             return false;
    277         }
    278 
    279         @Override
    280         public boolean isBinderAlive() {
    281             return false;
    282         }
    283 
    284         @Override
    285         public IInterface queryLocalInterface(String descriptor) {
    286             return null;
    287         }
    288 
    289         @Override
    290         public void dump(FileDescriptor fd, String[] args) throws RemoteException {
    291         }
    292 
    293         @Override
    294         public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
    295             return false;
    296         }
    297 
    298         @Override
    299         public void linkToDeath(DeathRecipient recipient, int flags) throws RemoteException {
    300         }
    301 
    302         @Override
    303         public boolean unlinkToDeath(DeathRecipient recipient, int flags) {
    304             return false;
    305         }
    306 
    307         @Override
    308         public void dumpAsync(FileDescriptor fd, String[] args) throws RemoteException {
    309         }
    310     }
    311 }
    312