aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/shadows/ShadowService.java
blob: 74a9cc1cac02defcf5450b022ca7eac93f1035fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.xtremelabs.robolectric.shadows;

import android.app.Application;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.ServiceConnection;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(Service.class)
public class ShadowService extends ShadowContextWrapper {
    @RealObject Service realService;
    
    private Notification lastForegroundNotification;    
    private boolean selfStopped = false;
    private boolean unbindServiceShouldThrowIllegalArgument = false;
    private boolean foregroundStopped;
    private boolean notificationShouldRemoved;

    @Implementation
    public final Application getApplication() {
        return Robolectric.application;
    }

    @Implementation @Override
    public Context getApplicationContext() {
        return Robolectric.application;
    }

    @Implementation
    public void onDestroy() {
        assertNoBroadcastListenersRegistered();
    }
    
    @Implementation 
    public void unbindService(ServiceConnection conn) {
    	if (unbindServiceShouldThrowIllegalArgument) {
    		throw new IllegalArgumentException();
    	}
    }
    
    @Implementation
    public void stopSelf() {
    	selfStopped = true;
    }
    
    public void setUnbindServiceShouldThrowIllegalArgument(boolean flag) {
    	unbindServiceShouldThrowIllegalArgument = flag;
    }

    @Implementation
    public final void startForeground(int id, Notification notification) {
        lastForegroundNotification = notification;
    }
    
    @Implementation
    public void stopForeground(boolean removeNotification) {
        foregroundStopped = true;
        notificationShouldRemoved = removeNotification;
    }

    public Notification getLastForegroundNotification() {
        return lastForegroundNotification;
    }

    /**
     * Utility method that throws a {@code RuntimeException} if any {@code BroadcastListener}s are still registered.
     */
    public void assertNoBroadcastListenersRegistered() {
        ((ShadowApplication) shadowOf(getApplicationContext())).assertNoBroadcastListenersRegistered(realService, "Service");
    }
    
    /**
     * Non-Android accessor, to use in assertions. 
     * @return
     */
    public boolean isStoppedBySelf() {
    	return selfStopped;
    }
    
    public boolean isForegroundStopped() {
        return foregroundStopped;
    }
    
    public boolean getNotificationShouldRemoved() {
        return notificationShouldRemoved;
    }
}