aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/xtremelabs/robolectric/shadows/ShadowContextWrapper.java
blob: 9724e6b41571290d0a4b925f3e706c52093ba5c1 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.xtremelabs.robolectric.shadows;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Looper;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;
import com.xtremelabs.robolectric.tester.android.content.TestSharedPreferences;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static com.xtremelabs.robolectric.Robolectric.shadowOf;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(ContextWrapper.class)
public class ShadowContextWrapper extends ShadowContext {
    @RealObject private ContextWrapper realContextWrapper;
    protected Context baseContext;

    private PackageManager packageManager;

    private String appName;
    private String packageName;
    private Set<String> grantedPermissions = new HashSet<String>();

    public void __constructor__(Context baseContext) {
        this.baseContext = baseContext;
    }

    @Implementation
    public Context getApplicationContext() {
        return baseContext.getApplicationContext();
    }

    @Implementation
    public Resources.Theme getTheme() {
        return getResources().newTheme();
    }

    @Implementation
    public Resources getResources() {
        return getApplicationContext().getResources();
    }

    @Implementation
    public ContentResolver getContentResolver() {
        return getApplicationContext().getContentResolver();
    }

    @Implementation
    public Object getSystemService(String name) {
        return getApplicationContext().getSystemService(name);
    }

    @Implementation
    public void sendBroadcast(Intent intent) {
        getApplicationContext().sendBroadcast(intent);
    }

    public List<Intent> getBroadcastIntents() {
        return ((ShadowApplication) shadowOf(getApplicationContext())).getBroadcastIntents();
    }

    @Implementation
    public int checkPermission(java.lang.String permission, int pid, int uid) {
        return grantedPermissions.contains(permission) ? PERMISSION_GRANTED : PERMISSION_DENIED;
    }

    @Implementation
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        return ((ShadowApplication) shadowOf(getApplicationContext())).registerReceiverWithContext(receiver, filter, realContextWrapper);
    }

    @Implementation
    public void unregisterReceiver(BroadcastReceiver broadcastReceiver) {
        getApplicationContext().unregisterReceiver(broadcastReceiver);
    }

    @Implementation
    public String getPackageName() {
        return realContextWrapper == getApplicationContext() ? packageName : getApplicationContext().getPackageName();
    }

    @Implementation
    public ApplicationInfo getApplicationInfo() {
        ApplicationInfo appInfo = new ApplicationInfo();
        appInfo.name = appName;
        appInfo.packageName = packageName;
        appInfo.processName = packageName;
        return appInfo;
    }

    /**
     * Non-Android accessor to set the application name.
     *
     * @param name
     */
    public void setApplicationName(String name) {
        appName = name;
    }

    /**
     * Implements Android's {@code PackageManager}.
     *
     * @return a {@code RobolectricPackageManager}
     */
    @Implementation
    public PackageManager getPackageManager() {
        return realContextWrapper == getApplicationContext() ? packageManager : getApplicationContext().getPackageManager();
    }

    @Implementation
    public ComponentName startService(Intent service) {
        return getApplicationContext().startService(service);
    }

    @Implementation
    public boolean stopService(Intent name) {
        return getApplicationContext().stopService(name);
    }

    @Implementation
    public void startActivity(Intent intent) {
        getApplicationContext().startActivity(intent);
    }

    @Implementation
    public SharedPreferences getSharedPreferences(String name, int mode) {
        return new TestSharedPreferences(getShadowApplication().getSharedPreferenceMap(), name, mode);
    }

    @Implementation
    public AssetManager getAssets() {
        return getResources().getAssets();
    }

    /**
     * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the
     * started activities stack.
     *
     * @return the next started {@code Intent} for an activity
     */
    public Intent getNextStartedActivity() {
        return getShadowApplication().getNextStartedActivity();
    }

    /**
     * Non-Android accessor that delegates to the application to return (without consuming) the next {@code Intent} on
     * the started activities stack.
     *
     * @return the next started {@code Intent} for an activity
     */
    public Intent peekNextStartedActivity() {
        return getShadowApplication().peekNextStartedActivity();
    }

    /**
     * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the
     * started services stack.
     *
     * @return the next started {@code Intent} for a service
     */
    public Intent getNextStartedService() {
        return getShadowApplication().getNextStartedService();
    }

    /**
     * Non-android accessor that delefates to the application to clear the stack of started
     * service intents.
     */
    public void clearStartedServices() {
        getShadowApplication().clearStartedServices();
    }

    /**
     * Return (without consuming) the next {@code Intent} on the started services stack.
     *
     * @return the next started {@code Intent} for a service
     */
    public Intent peekNextStartedService() {
        return getShadowApplication().peekNextStartedService();
    }

    /**
     * Non-Android accessor that delegates to the application to return the next {@code Intent} to stop
     * a service (irrespective of if the service was running)
     *
     * @return {@code Intent} for the next service requested to be stopped
     */
    public Intent getNextStoppedService() {
        return getShadowApplication().getNextStoppedService();
    }

    /**
     * Non-Android accessor that is used at start-up to set the package name
     *
     * @param packageName the package name
     */
    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    /**
     * Non-Android accessor that is used at start-up to set the packageManager =
     *
     * @param packageManager the package manager
     */
    public void setPackageManager(PackageManager packageManager) {
        this.packageManager = packageManager;
    }


    @Implementation
    public Looper getMainLooper() {
        return getShadowApplication().getMainLooper();
    }

    @Implementation
    public Context getBaseContext() {
        return baseContext;
    }

    @Implementation
    public void attachBaseContext(Context context) {
        baseContext = context;
    }

    private ShadowApplication getShadowApplication() {
        return ((ShadowApplication) shadowOf(getApplicationContext()));
    }

    @Implementation
    public boolean bindService(Intent intent, final ServiceConnection serviceConnection, int i) {
        return getShadowApplication().bindService(intent, serviceConnection, i);
    }

    /**
     * Non-Android accessor that is used to grant permissions checked via
     * {@link android.content.ContextWrapper#checkPermission(String, int, int)}
     *
     * @param permissionNames permission names that should be granted
     */
    public void grantPermissions(String... permissionNames) {
        for (String permissionName : permissionNames) {
            grantedPermissions.add(permissionName);
        }
    }
}