Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.app.Application;
      4 import android.app.Notification;
      5 import android.app.Service;
      6 import android.content.Context;
      7 import android.content.ServiceConnection;
      8 import com.xtremelabs.robolectric.Robolectric;
      9 import com.xtremelabs.robolectric.internal.Implementation;
     10 import com.xtremelabs.robolectric.internal.Implements;
     11 import com.xtremelabs.robolectric.internal.RealObject;
     12 
     13 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     14 
     15 @SuppressWarnings({"UnusedDeclaration"})
     16 @Implements(Service.class)
     17 public class ShadowService extends ShadowContextWrapper {
     18     @RealObject Service realService;
     19 
     20     private Notification lastForegroundNotification;
     21     private boolean selfStopped = false;
     22     private boolean unbindServiceShouldThrowIllegalArgument = false;
     23     private boolean foregroundStopped;
     24     private boolean notificationShouldRemoved;
     25 
     26     @Implementation
     27     public final Application getApplication() {
     28         return Robolectric.application;
     29     }
     30 
     31     @Implementation @Override
     32     public Context getApplicationContext() {
     33         return Robolectric.application;
     34     }
     35 
     36     @Implementation
     37     public void onDestroy() {
     38         assertNoBroadcastListenersRegistered();
     39     }
     40 
     41     @Implementation
     42     public void unbindService(ServiceConnection conn) {
     43     	if (unbindServiceShouldThrowIllegalArgument) {
     44     		throw new IllegalArgumentException();
     45     	}
     46     }
     47 
     48     @Implementation
     49     public void stopSelf() {
     50     	selfStopped = true;
     51     }
     52 
     53     public void setUnbindServiceShouldThrowIllegalArgument(boolean flag) {
     54     	unbindServiceShouldThrowIllegalArgument = flag;
     55     }
     56 
     57     @Implementation
     58     public final void startForeground(int id, Notification notification) {
     59         lastForegroundNotification = notification;
     60     }
     61 
     62     @Implementation
     63     public void stopForeground(boolean removeNotification) {
     64         foregroundStopped = true;
     65         notificationShouldRemoved = removeNotification;
     66     }
     67 
     68     public Notification getLastForegroundNotification() {
     69         return lastForegroundNotification;
     70     }
     71 
     72     /**
     73      * Utility method that throws a {@code RuntimeException} if any {@code BroadcastListener}s are still registered.
     74      */
     75     public void assertNoBroadcastListenersRegistered() {
     76         ((ShadowApplication) shadowOf(getApplicationContext())).assertNoBroadcastListenersRegistered(realService, "Service");
     77     }
     78 
     79     /**
     80      * Non-Android accessor, to use in assertions.
     81      * @return
     82      */
     83     public boolean isStoppedBySelf() {
     84     	return selfStopped;
     85     }
     86 
     87     public boolean isForegroundStopped() {
     88         return foregroundStopped;
     89     }
     90 
     91     public boolean getNotificationShouldRemoved() {
     92         return notificationShouldRemoved;
     93     }
     94 }