Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static org.assertj.core.api.Assertions.assertThat;
      4 import static org.robolectric.Shadows.shadowOf;
      5 
      6 import android.app.Notification;
      7 import android.app.NotificationManager;
      8 import android.app.Service;
      9 import android.content.Context;
     10 import android.content.Intent;
     11 import android.content.ServiceConnection;
     12 import android.media.MediaScannerConnection;
     13 import android.os.IBinder;
     14 import org.junit.Before;
     15 import org.junit.Test;
     16 import org.junit.runner.RunWith;
     17 import org.robolectric.Robolectric;
     18 import org.robolectric.RobolectricTestRunner;
     19 import org.robolectric.RuntimeEnvironment;
     20 import org.robolectric.shadow.api.Shadow;
     21 
     22 @RunWith(RobolectricTestRunner.class)
     23 public class ShadowServiceTest {
     24   private MyService service ;
     25   private ShadowService shadow;
     26   private Notification.Builder notBuilder;
     27 
     28   private final ShadowNotificationManager nm = shadowOf((NotificationManager) RuntimeEnvironment.application
     29       .getSystemService(Context.NOTIFICATION_SERVICE));
     30 
     31   @Before
     32   public void setup() {
     33     service = Robolectric.setupService(MyService.class);
     34     shadow = shadowOf(service);
     35     notBuilder = new Notification.Builder(service)
     36         .setSmallIcon(1)
     37         .setContentTitle("Test")
     38         .setContentText("Hi there");
     39   }
     40 
     41   @Test
     42   public void shouldUnbindServiceAtShadowApplication() {
     43     ShadowApplication shadowApplication = shadowOf(RuntimeEnvironment.application);
     44     ServiceConnection conn = Shadow.newInstanceOf(MediaScannerConnection.class);
     45     service.bindService(new Intent("dummy"), conn, 0);
     46     assertThat(shadowApplication.getUnboundServiceConnections()).isEmpty();
     47     service.unbindService(conn);
     48     assertThat(shadowApplication.getUnboundServiceConnections()).hasSize(1);
     49   }
     50 
     51   @Test
     52   public void shouldUnbindServiceSuccessfully() {
     53     ServiceConnection conn = Shadow.newInstanceOf(MediaScannerConnection.class);
     54     service.unbindService(conn);
     55   }
     56 
     57   @Test(expected = IllegalArgumentException.class)
     58   public void shouldUnbindServiceWithExceptionWhenRequested() {
     59     ShadowApplication.getInstance().setUnbindServiceShouldThrowIllegalArgument(true);
     60     ServiceConnection conn = Shadow.newInstanceOf(MediaScannerConnection.class);
     61     service.unbindService(conn);
     62   }
     63 
     64   @Test
     65   public void startForeground() {
     66     Notification n = notBuilder.build();
     67     service.startForeground(23, n);
     68     assertThat(shadow.getLastForegroundNotification()).isSameAs(n);
     69     assertThat(shadow.getLastForegroundNotificationId()).isEqualTo(23);
     70     assertThat(nm.getNotification(23)).isSameAs(n);
     71     assertThat(n.flags & Notification.FLAG_FOREGROUND_SERVICE).isNotZero();
     72   }
     73 
     74   @Test
     75   public void stopForeground() {
     76     service.stopForeground(true);
     77     assertThat(shadow.isForegroundStopped()).isTrue();
     78     assertThat(shadow.getNotificationShouldRemoved()).isTrue();
     79   }
     80 
     81   @Test
     82   public void stopForegroundRemovesNotificationIfAsked() {
     83     service.startForeground(21, notBuilder.build());
     84     service.stopForeground(true);
     85     assertThat(nm.getNotification(21)).isNull();
     86   }
     87 
     88   /**
     89    * According to spec, if the foreground notification is not removed earlier,
     90    * then it will be removed when the service is destroyed.
     91    */
     92   @Test
     93   public void stopForegroundDoesntRemoveNotificationUnlessAsked() {
     94     Notification n = notBuilder.build();
     95     service.startForeground(21, n);
     96     service.stopForeground(false);
     97     assertThat(nm.getNotification(21)).isSameAs(n);
     98   }
     99 
    100   /**
    101    * According to spec, if the foreground notification is not removed earlier,
    102    * then it will be removed when the service is destroyed.
    103    */
    104   @Test
    105   public void onDestroyRemovesNotification() {
    106     Notification n = notBuilder.build();
    107     service.startForeground(21, n);
    108     service.onDestroy();
    109     assertThat(nm.getNotification(21)).isNull();
    110   }
    111 
    112   @Test
    113   public void shouldStopSelf() {
    114     service.stopSelf();
    115     assertThat(shadow.isStoppedBySelf()).isTrue();
    116   }
    117 
    118   @Test
    119   public void shouldStopSelfWithId() {
    120     service.stopSelf(1);
    121     assertThat(shadow.isStoppedBySelf()).isTrue();
    122   }
    123 
    124   public static class MyService extends Service {
    125     @Override
    126     public void onDestroy() {
    127       super.onDestroy();
    128     }
    129 
    130     @Override
    131     public IBinder onBind(Intent intent) {
    132       return null;
    133     }
    134   }
    135 }
    136