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 java.util.List;
8 import org.robolectric.RuntimeEnvironment;
9 import org.robolectric.annotation.Implements;
10 import org.robolectric.annotation.RealObject;
11 import org.robolectric.shadow.api.Shadow;
12 
13 @Implements(ContextWrapper.class)
14 public class ShadowContextWrapper {
15 
16   @RealObject
17   private ContextWrapper realContextWrapper;
18 
getBroadcastIntents()19   public List<Intent> getBroadcastIntents() {
20     return getShadowInstrumentation().getBroadcastIntents();
21   }
22 
23   /**
24    * Consumes the most recent {@code Intent} started by {@link
25    * ContextWrapper#startActivity(android.content.Intent)} and returns it.
26    *
27    * @return the most recently started {@code Intent}
28    */
getNextStartedActivity()29   public Intent getNextStartedActivity() {
30     return getShadowInstrumentation().getNextStartedActivity();
31   }
32 
33   /**
34    * Returns the most recent {@code Intent} started by {@link
35    * ContextWrapper#startActivity(android.content.Intent)} without consuming it.
36    *
37    * @return the most recently started {@code Intent}
38    */
peekNextStartedActivity()39   public Intent peekNextStartedActivity() {
40     return getShadowInstrumentation().peekNextStartedActivity();
41   }
42 
43   /**
44    * Clears all {@code Intent}s started by {@link
45    * ContextWrapper#startActivity(android.content.Intent)}.
46    */
clearNextStartedActivities()47   public void clearNextStartedActivities() {
48     getShadowInstrumentation().clearNextStartedActivities();
49   }
50 
51   /**
52    * Consumes the most recent {@code Intent} started by
53    * {@link android.content.Context#startService(android.content.Intent)} and returns it.
54    *
55    * @return the most recently started {@code Intent}
56    */
getNextStartedService()57   public Intent getNextStartedService() {
58     return getShadowInstrumentation().getNextStartedService();
59   }
60 
61   /**
62    * Returns the most recent {@code Intent} started by
63    * {@link android.content.Context#startService(android.content.Intent)} without consuming it.
64    *
65    * @return the most recently started {@code Intent}
66    */
peekNextStartedService()67   public Intent peekNextStartedService() {
68     return getShadowInstrumentation().peekNextStartedService();
69   }
70 
71   /**
72    * Clears all {@code Intent} started by
73    * {@link android.content.Context#startService(android.content.Intent)}.
74    */
clearStartedServices()75   public void clearStartedServices() {
76     getShadowInstrumentation().clearStartedServices();
77   }
78 
79   /**
80    * Consumes the {@code Intent} requested to stop a service by
81    * {@link android.content.Context#stopService(android.content.Intent)}
82    * from the bottom of the stack of stop requests.
83    */
getNextStoppedService()84   public Intent getNextStoppedService() {
85     return getShadowInstrumentation().getNextStoppedService();
86   }
87 
88   /** Grant the given permissions for the current process and user. */
grantPermissions(String... permissionNames)89   public void grantPermissions(String... permissionNames) {
90     getShadowInstrumentation().grantPermissions(permissionNames);
91   }
92 
93   /** Grant the given permissions for the given process and user. */
grantPermissions(int pid, int uid, String... permissions)94   public void grantPermissions(int pid, int uid, String... permissions) {
95     getShadowInstrumentation().grantPermissions(pid, uid, permissions);
96   }
97 
98   /**
99    * Revoke the given permissions for the current process and user.
100    *
101    * Has no effect if permissions were not previously granted.
102    */
denyPermissions(String... permissionNames)103   public void denyPermissions(String... permissionNames) {
104     getShadowInstrumentation().denyPermissions(permissionNames);
105   }
106 
107   /** Revoke the given permissions for the given process and user. */
denyPermissions(int pid, int uid, String... permissions)108   public void denyPermissions(int pid, int uid, String... permissions) {
109     getShadowInstrumentation().denyPermissions(pid, uid, permissions);
110   }
111 
getShadowInstrumentation()112   ShadowInstrumentation getShadowInstrumentation() {
113     ActivityThread activityThread = (ActivityThread) RuntimeEnvironment.getActivityThread();
114     return Shadow.extract(activityThread.getInstrumentation());
115   }
116 
117   /**
118    * Makes {@link Context#getSystemService(String)} return {@code null} for the given system service
119    * name, mimicking a device that doesn't have that system service.
120    */
removeSystemService(String name)121   public void removeSystemService(String name) {
122     ((ShadowContextImpl) Shadow.extract(realContextWrapper.getBaseContext()))
123         .removeSystemService(name);
124   }
125 }
126