1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.BroadcastReceiver; 4 import android.content.ComponentName; 5 import android.content.ContentResolver; 6 import android.content.Context; 7 import android.content.ContextWrapper; 8 import android.content.Intent; 9 import android.content.IntentFilter; 10 import android.content.ServiceConnection; 11 import android.content.SharedPreferences; 12 import android.content.pm.ApplicationInfo; 13 import android.content.pm.PackageManager; 14 import android.content.res.AssetManager; 15 import android.content.res.Resources; 16 import android.os.Looper; 17 import com.xtremelabs.robolectric.internal.Implementation; 18 import com.xtremelabs.robolectric.internal.Implements; 19 import com.xtremelabs.robolectric.internal.RealObject; 20 import com.xtremelabs.robolectric.tester.android.content.TestSharedPreferences; 21 22 import java.util.HashSet; 23 import java.util.List; 24 import java.util.Set; 25 26 import static android.content.pm.PackageManager.PERMISSION_DENIED; 27 import static android.content.pm.PackageManager.PERMISSION_GRANTED; 28 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 29 30 @SuppressWarnings({"UnusedDeclaration"}) 31 @Implements(ContextWrapper.class) 32 public class ShadowContextWrapper extends ShadowContext { 33 @RealObject private ContextWrapper realContextWrapper; 34 protected Context baseContext; 35 36 private PackageManager packageManager; 37 38 private String appName; 39 private String packageName; 40 private Set<String> grantedPermissions = new HashSet<String>(); 41 __constructor__(Context baseContext)42 public void __constructor__(Context baseContext) { 43 this.baseContext = baseContext; 44 } 45 46 @Implementation getApplicationContext()47 public Context getApplicationContext() { 48 return baseContext.getApplicationContext(); 49 } 50 51 @Implementation getTheme()52 public Resources.Theme getTheme() { 53 return getResources().newTheme(); 54 } 55 56 @Implementation getResources()57 public Resources getResources() { 58 return getApplicationContext().getResources(); 59 } 60 61 @Implementation getContentResolver()62 public ContentResolver getContentResolver() { 63 return getApplicationContext().getContentResolver(); 64 } 65 66 @Implementation getSystemService(String name)67 public Object getSystemService(String name) { 68 return getApplicationContext().getSystemService(name); 69 } 70 71 @Implementation sendBroadcast(Intent intent)72 public void sendBroadcast(Intent intent) { 73 getApplicationContext().sendBroadcast(intent); 74 } 75 getBroadcastIntents()76 public List<Intent> getBroadcastIntents() { 77 return ((ShadowApplication) shadowOf(getApplicationContext())).getBroadcastIntents(); 78 } 79 80 @Implementation checkPermission(java.lang.String permission, int pid, int uid)81 public int checkPermission(java.lang.String permission, int pid, int uid) { 82 return grantedPermissions.contains(permission) ? PERMISSION_GRANTED : PERMISSION_DENIED; 83 } 84 85 @Implementation registerReceiver(BroadcastReceiver receiver, IntentFilter filter)86 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 87 return ((ShadowApplication) shadowOf(getApplicationContext())).registerReceiverWithContext(receiver, filter, realContextWrapper); 88 } 89 90 @Implementation unregisterReceiver(BroadcastReceiver broadcastReceiver)91 public void unregisterReceiver(BroadcastReceiver broadcastReceiver) { 92 getApplicationContext().unregisterReceiver(broadcastReceiver); 93 } 94 95 @Implementation getPackageName()96 public String getPackageName() { 97 return realContextWrapper == getApplicationContext() ? packageName : getApplicationContext().getPackageName(); 98 } 99 100 @Implementation getApplicationInfo()101 public ApplicationInfo getApplicationInfo() { 102 ApplicationInfo appInfo = new ApplicationInfo(); 103 appInfo.name = appName; 104 appInfo.packageName = packageName; 105 appInfo.processName = packageName; 106 return appInfo; 107 } 108 109 /** 110 * Non-Android accessor to set the application name. 111 * 112 * @param name 113 */ setApplicationName(String name)114 public void setApplicationName(String name) { 115 appName = name; 116 } 117 118 /** 119 * Implements Android's {@code PackageManager}. 120 * 121 * @return a {@code RobolectricPackageManager} 122 */ 123 @Implementation getPackageManager()124 public PackageManager getPackageManager() { 125 return realContextWrapper == getApplicationContext() ? packageManager : getApplicationContext().getPackageManager(); 126 } 127 128 @Implementation startService(Intent service)129 public ComponentName startService(Intent service) { 130 return getApplicationContext().startService(service); 131 } 132 133 @Implementation stopService(Intent name)134 public boolean stopService(Intent name) { 135 return getApplicationContext().stopService(name); 136 } 137 138 @Implementation startActivity(Intent intent)139 public void startActivity(Intent intent) { 140 getApplicationContext().startActivity(intent); 141 } 142 143 @Implementation getSharedPreferences(String name, int mode)144 public SharedPreferences getSharedPreferences(String name, int mode) { 145 return new TestSharedPreferences(getShadowApplication().getSharedPreferenceMap(), name, mode); 146 } 147 148 @Implementation getAssets()149 public AssetManager getAssets() { 150 return getResources().getAssets(); 151 } 152 153 /** 154 * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the 155 * started activities stack. 156 * 157 * @return the next started {@code Intent} for an activity 158 */ getNextStartedActivity()159 public Intent getNextStartedActivity() { 160 return getShadowApplication().getNextStartedActivity(); 161 } 162 163 /** 164 * Non-Android accessor that delegates to the application to return (without consuming) the next {@code Intent} on 165 * the started activities stack. 166 * 167 * @return the next started {@code Intent} for an activity 168 */ peekNextStartedActivity()169 public Intent peekNextStartedActivity() { 170 return getShadowApplication().peekNextStartedActivity(); 171 } 172 173 /** 174 * Non-Android accessor that delegates to the application to consume and return the next {@code Intent} on the 175 * started services stack. 176 * 177 * @return the next started {@code Intent} for a service 178 */ getNextStartedService()179 public Intent getNextStartedService() { 180 return getShadowApplication().getNextStartedService(); 181 } 182 183 /** 184 * Non-android accessor that delefates to the application to clear the stack of started 185 * service intents. 186 */ clearStartedServices()187 public void clearStartedServices() { 188 getShadowApplication().clearStartedServices(); 189 } 190 191 /** 192 * Return (without consuming) the next {@code Intent} on the started services stack. 193 * 194 * @return the next started {@code Intent} for a service 195 */ peekNextStartedService()196 public Intent peekNextStartedService() { 197 return getShadowApplication().peekNextStartedService(); 198 } 199 200 /** 201 * Non-Android accessor that delegates to the application to return the next {@code Intent} to stop 202 * a service (irrespective of if the service was running) 203 * 204 * @return {@code Intent} for the next service requested to be stopped 205 */ getNextStoppedService()206 public Intent getNextStoppedService() { 207 return getShadowApplication().getNextStoppedService(); 208 } 209 210 /** 211 * Non-Android accessor that is used at start-up to set the package name 212 * 213 * @param packageName the package name 214 */ setPackageName(String packageName)215 public void setPackageName(String packageName) { 216 this.packageName = packageName; 217 } 218 219 /** 220 * Non-Android accessor that is used at start-up to set the packageManager = 221 * 222 * @param packageManager the package manager 223 */ setPackageManager(PackageManager packageManager)224 public void setPackageManager(PackageManager packageManager) { 225 this.packageManager = packageManager; 226 } 227 228 229 @Implementation getMainLooper()230 public Looper getMainLooper() { 231 return getShadowApplication().getMainLooper(); 232 } 233 234 @Implementation getBaseContext()235 public Context getBaseContext() { 236 return baseContext; 237 } 238 239 @Implementation attachBaseContext(Context context)240 public void attachBaseContext(Context context) { 241 baseContext = context; 242 } 243 getShadowApplication()244 private ShadowApplication getShadowApplication() { 245 return ((ShadowApplication) shadowOf(getApplicationContext())); 246 } 247 248 @Implementation bindService(Intent intent, final ServiceConnection serviceConnection, int i)249 public boolean bindService(Intent intent, final ServiceConnection serviceConnection, int i) { 250 return getShadowApplication().bindService(intent, serviceConnection, i); 251 } 252 253 /** 254 * Non-Android accessor that is used to grant permissions checked via 255 * {@link android.content.ContextWrapper#checkPermission(String, int, int)} 256 * 257 * @param permissionNames permission names that should be granted 258 */ grantPermissions(String... permissionNames)259 public void grantPermissions(String... permissionNames) { 260 for (String permissionName : permissionNames) { 261 grantedPermissions.add(permissionName); 262 } 263 } 264 } 265