1 package org.robolectric.shadows;
2 
3 import android.app.ActivityThread;
4 import android.content.Context;
5 import android.content.ContextWrapper;
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.os.UserHandle;
9 import java.util.List;
10 import org.robolectric.RuntimeEnvironment;
11 import org.robolectric.annotation.Implements;
12 import org.robolectric.annotation.RealObject;
13 import org.robolectric.shadow.api.Shadow;
14 import org.robolectric.shadows.ShadowActivity.IntentForResult;
15 
16 @Implements(ContextWrapper.class)
17 public class ShadowContextWrapper {
18 
19   @RealObject private ContextWrapper realContextWrapper;
20 
21   /** Returns the broadcast intents sent during the tests (for all users). */
getBroadcastIntents()22   public List<Intent> getBroadcastIntents() {
23     return getShadowInstrumentation().getBroadcastIntents();
24   }
25 
26   /** Returns the broadcast options when the intent was last sent. */
getBroadcastOptions(Intent intent)27   public Bundle getBroadcastOptions(Intent intent) {
28     return getShadowInstrumentation().getBroadcastOptions(intent);
29   }
30 
31   /** Returns the broadcast intents sent to the given user. */
getBroadcastIntentsForUser(UserHandle userHandle)32   public List<Intent> getBroadcastIntentsForUser(UserHandle userHandle) {
33     return getShadowInstrumentation().getBroadcastIntentsForUser(userHandle);
34   }
35 
36   /** Clears the broadcast intents sent during the tests (for all users). */
clearBroadcastIntents()37   public void clearBroadcastIntents() {
38     getShadowInstrumentation().clearBroadcastIntents();
39   }
40 
41   /**
42    * Consumes the most recent {@code Intent} started by {@link
43    * ContextWrapper#startActivity(android.content.Intent)} and returns it.
44    *
45    * @return the most recently started {@code Intent}
46    */
getNextStartedActivity()47   public Intent getNextStartedActivity() {
48     return getShadowInstrumentation().getNextStartedActivity();
49   }
50 
51   /**
52    * Returns the most recent {@code Intent} started by {@link
53    * ContextWrapper#startActivity(android.content.Intent)} without consuming it.
54    *
55    * @return the most recently started {@code Intent}
56    */
peekNextStartedActivity()57   public Intent peekNextStartedActivity() {
58     return getShadowInstrumentation().peekNextStartedActivity();
59   }
60 
61   /**
62    * Clears all {@code Intent}s started by {@link
63    * ContextWrapper#startActivity(android.content.Intent)}.
64    */
clearNextStartedActivities()65   public void clearNextStartedActivities() {
66     getShadowInstrumentation().clearNextStartedActivities();
67   }
68 
69   /**
70    * Consumes the most recent {@code IntentForResult} started by {@link *
71    * ContextWrapper#startActivity(android.content.Intent, android.os.Bundle)} and returns it.
72    *
73    * @return the most recently started {@code IntentForResult}
74    */
getNextStartedActivityForResult()75   public IntentForResult getNextStartedActivityForResult() {
76     return getShadowInstrumentation().getNextStartedActivityForResult();
77   }
78 
79   /**
80    * Returns the most recent {@code IntentForResult} started by {@link
81    * ContextWrapper#startActivity(android.content.Intent, android.os.Bundle)} without consuming it.
82    *
83    * @return the most recently started {@code IntentForResult}
84    */
peekNextStartedActivityForResult()85   public IntentForResult peekNextStartedActivityForResult() {
86     return getShadowInstrumentation().peekNextStartedActivityForResult();
87   }
88 
89   /**
90    * Consumes the most recent {@code Intent} started by {@link
91    * android.content.Context#startService(android.content.Intent)} and returns it.
92    *
93    * @return the most recently started {@code Intent}
94    */
getNextStartedService()95   public Intent getNextStartedService() {
96     return getShadowInstrumentation().getNextStartedService();
97   }
98 
99   /**
100    * Returns the most recent {@code Intent} started by {@link
101    * android.content.Context#startService(android.content.Intent)} without consuming it.
102    *
103    * @return the most recently started {@code Intent}
104    */
peekNextStartedService()105   public Intent peekNextStartedService() {
106     return getShadowInstrumentation().peekNextStartedService();
107   }
108 
109   /**
110    * Returns all {@code Intent} started by {@link #startService(android.content.Intent)} without
111    * consuming them.
112    *
113    * @return the list of {@code Intent}
114    */
getAllStartedServices()115   public List<Intent> getAllStartedServices() {
116     return getShadowInstrumentation().getAllStartedServices();
117   }
118 
119   /**
120    * Clears all {@code Intent} started by {@link
121    * android.content.Context#startService(android.content.Intent)}.
122    */
clearStartedServices()123   public void clearStartedServices() {
124     getShadowInstrumentation().clearStartedServices();
125   }
126 
127   /**
128    * Consumes the {@code Intent} requested to stop a service by {@link
129    * android.content.Context#stopService(android.content.Intent)} from the bottom of the stack of
130    * stop requests.
131    */
getNextStoppedService()132   public Intent getNextStoppedService() {
133     return getShadowInstrumentation().getNextStoppedService();
134   }
135 
136   /** Grant the given permissions for the current process and user. */
grantPermissions(String... permissionNames)137   public void grantPermissions(String... permissionNames) {
138     getShadowInstrumentation().grantPermissions(permissionNames);
139   }
140 
141   /** Grant the given permissions for the given process and user. */
grantPermissions(int pid, int uid, String... permissions)142   public void grantPermissions(int pid, int uid, String... permissions) {
143     getShadowInstrumentation().grantPermissions(pid, uid, permissions);
144   }
145 
146   /**
147    * Revoke the given permissions for the current process and user.
148    *
149    * <p>Has no effect if permissions were not previously granted.
150    */
denyPermissions(String... permissionNames)151   public void denyPermissions(String... permissionNames) {
152     getShadowInstrumentation().denyPermissions(permissionNames);
153   }
154 
155   /** Revoke the given permissions for the given process and user. */
denyPermissions(int pid, int uid, String... permissions)156   public void denyPermissions(int pid, int uid, String... permissions) {
157     getShadowInstrumentation().denyPermissions(pid, uid, permissions);
158   }
159 
getShadowInstrumentation()160   static ShadowInstrumentation getShadowInstrumentation() {
161     ActivityThread activityThread = (ActivityThread) RuntimeEnvironment.getActivityThread();
162     return Shadow.extract(activityThread.getInstrumentation());
163   }
164 
165   /**
166    * Makes {@link Context#getSystemService(String)} return {@code null} for the given system service
167    * name, mimicking a device that doesn't have that system service.
168    */
removeSystemService(String name)169   public void removeSystemService(String name) {
170     ((ShadowContextImpl) Shadow.extract(realContextWrapper.getBaseContext()))
171         .removeSystemService(name);
172   }
173 }
174