ShadowContextWrapper.java revision 08f79939304d82b7cfbb80d20b88fbe26eeab7a9
1package com.xtremelabs.robolectric.shadows;
2
3import android.content.*;
4import android.content.pm.PackageManager;
5import android.content.res.AssetManager;
6import android.content.res.Resources;
7import android.os.Looper;
8import com.xtremelabs.robolectric.RobolectricConfig;
9import com.xtremelabs.robolectric.internal.Implementation;
10import com.xtremelabs.robolectric.internal.Implements;
11import com.xtremelabs.robolectric.internal.RealObject;
12import com.xtremelabs.robolectric.res.RobolectricPackageManager;
13import com.xtremelabs.robolectric.tester.android.content.TestSharedPreferences;
14
15import java.io.File;
16
17import static com.xtremelabs.robolectric.Robolectric.shadowOf;
18
19@SuppressWarnings({"UnusedDeclaration"})
20@Implements(ContextWrapper.class)
21public class ShadowContextWrapper extends ShadowContext {
22    @RealObject private ContextWrapper realContextWrapper;
23    private Context baseContext;
24
25    private PackageManager packageManager;
26
27    private String packageName;
28
29    public void __constructor__(Context baseContext) {
30        this.baseContext = baseContext;
31    }
32
33    @Implementation
34    public Context getApplicationContext() {
35        return baseContext.getApplicationContext();
36    }
37
38    @Implementation
39    public Resources.Theme getTheme() {
40        return getResources().newTheme();
41    }
42
43    @Implementation
44    public Resources getResources() {
45        return getApplicationContext().getResources();
46    }
47
48    @Implementation
49    public ContentResolver getContentResolver() {
50        return getApplicationContext().getContentResolver();
51    }
52
53    @Implementation
54    public Object getSystemService(String name) {
55        return getApplicationContext().getSystemService(name);
56    }
57
58    @Implementation
59    public void sendBroadcast(Intent intent) {
60        getApplicationContext().sendBroadcast(intent);
61    }
62
63    @Implementation
64    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
65        return ((ShadowApplication) shadowOf(getApplicationContext())).registerReceiverWithContext(receiver, filter, realContextWrapper);
66    }
67
68    @Implementation
69    public void unregisterReceiver(BroadcastReceiver broadcastReceiver) {
70        getApplicationContext().unregisterReceiver(broadcastReceiver);
71    }
72
73    @Implementation
74    public String getPackageName() {
75        return realContextWrapper == getApplicationContext() ? packageName : getApplicationContext().getPackageName();
76    }
77
78    /**
79     * Implements Android's {@code PackageManager}.
80     *
81     * @return a {@code RobolectricPackageManager}
82     */
83    @Implementation
84    public PackageManager getPackageManager() {
85        if (packageManager == null) {
86            packageManager = new RobolectricPackageManager(realContextWrapper, new RobolectricConfig(new File(".")));
87        }
88        return packageManager;
89    }
90
91    @Implementation
92    public ComponentName startService(Intent service) {
93        return getApplicationContext().startService(service);
94    }
95
96    @Implementation
97    public void startActivity(Intent intent) {
98        getApplicationContext().startActivity(intent);
99    }
100
101    @Implementation
102    public SharedPreferences getSharedPreferences(String name, int mode) {
103        return new TestSharedPreferences(getShadowApplication().getSharedPreferenceMap(), name, mode);
104    }
105
106    @Implementation
107    public AssetManager getAssets() {
108        return getResources().getAssets();
109    }
110
111    /**
112     * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the
113     * started activities stack.
114     *
115     * @return the next started {@code Intent} for an activity
116     */
117    public Intent getNextStartedActivity() {
118        return getShadowApplication().getNextStartedActivity();
119    }
120
121    /**
122     * Non-Android accessor that delegates to the application to return (without consuming) the next {@code Intent} on
123     * the started activities stack.
124     *
125     * @return the next started {@code Intent} for an activity
126     */
127    public Intent peekNextStartedActivity() {
128        return getShadowApplication().peekNextStartedActivity();
129    }
130
131    /**
132     * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the
133     * started services stack.
134     *
135     * @return the next started {@code Intent} for a service
136     */
137    public Intent getNextStartedService() {
138        return getShadowApplication().getNextStartedService();
139    }
140
141    /**
142     * Return (without consuming) the next {@code Intent} on the started services stack.
143     *
144     * @return the next started {@code Intent} for a service
145     */
146    public Intent peekNextStartedService() {
147        return getShadowApplication().peekNextStartedService();
148    }
149
150    /**
151     * Non-Android accessor that is used at start-up to set the package name
152     *
153     * @param packageName the package name
154     */
155    public void setPackageName(String packageName) {
156        this.packageName = packageName;
157    }
158
159    @Implementation
160    public Looper getMainLooper() {
161        return getShadowApplication().getMainLooper();
162    }
163
164    private ShadowApplication getShadowApplication() {
165        return ((ShadowApplication) shadowOf(getApplicationContext()));
166    }
167
168}
169