1 package org.robolectric.shadows; 2 3 import static android.content.pm.PackageManager.PERMISSION_DENIED; 4 import static android.content.pm.PackageManager.PERMISSION_GRANTED; 5 import static org.assertj.core.api.Assertions.assertThat; 6 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertNotSame; 8 import static org.junit.Assert.assertTrue; 9 import static org.robolectric.Robolectric.buildActivity; 10 import static org.robolectric.Shadows.shadowOf; 11 12 import android.app.Activity; 13 import android.appwidget.AppWidgetProvider; 14 import android.content.BroadcastReceiver; 15 import android.content.Context; 16 import android.content.ContextWrapper; 17 import android.content.Intent; 18 import android.content.IntentFilter; 19 import android.content.SharedPreferences; 20 import android.content.pm.ApplicationInfo; 21 import android.os.Bundle; 22 import android.os.Handler; 23 import android.os.HandlerThread; 24 import android.os.Looper; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import com.google.common.util.concurrent.SettableFuture; 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.concurrent.atomic.AtomicReference; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.robolectric.R; 35 import org.robolectric.Robolectric; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 39 @RunWith(RobolectricTestRunner.class) 40 public class ShadowContextWrapperTest { 41 public ArrayList<String> transcript; 42 private ContextWrapper contextWrapper; 43 setUp()44 @Before public void setUp() throws Exception { 45 transcript = new ArrayList<>(); 46 contextWrapper = new ContextWrapper(RuntimeEnvironment.application); 47 } 48 49 @Test registerReceiver_shouldRegisterForAllIntentFilterActions()50 public void registerReceiver_shouldRegisterForAllIntentFilterActions() throws Exception { 51 BroadcastReceiver receiver = broadcastReceiver("Larry"); 52 contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz")); 53 54 contextWrapper.sendBroadcast(new Intent("foo")); 55 assertThat(transcript).containsExactly("Larry notified of foo"); 56 transcript.clear(); 57 58 contextWrapper.sendBroadcast(new Intent("womp")); 59 assertThat(transcript).isEmpty(); 60 61 contextWrapper.sendBroadcast(new Intent("baz")); 62 assertThat(transcript).containsExactly("Larry notified of baz"); 63 } 64 65 @Test sendBroadcast_shouldSendIntentToEveryInterestedReceiver()66 public void sendBroadcast_shouldSendIntentToEveryInterestedReceiver() throws Exception { 67 BroadcastReceiver larryReceiver = broadcastReceiver("Larry"); 68 contextWrapper.registerReceiver(larryReceiver, intentFilter("foo", "baz")); 69 70 BroadcastReceiver bobReceiver = broadcastReceiver("Bob"); 71 contextWrapper.registerReceiver(bobReceiver, intentFilter("foo")); 72 73 contextWrapper.sendBroadcast(new Intent("foo")); 74 assertThat(transcript).containsExactly("Larry notified of foo", "Bob notified of foo"); 75 transcript.clear(); 76 77 contextWrapper.sendBroadcast(new Intent("womp")); 78 assertThat(transcript).isEmpty(); 79 80 contextWrapper.sendBroadcast(new Intent("baz")); 81 assertThat(transcript).containsExactly("Larry notified of baz"); 82 } 83 84 @Test sendBroadcast_shouldOnlySendIntentWithMatchingReceiverPermission()85 public void sendBroadcast_shouldOnlySendIntentWithMatchingReceiverPermission() { 86 BroadcastReceiver receiver = broadcastReceiver("Larry"); 87 contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz"), "validPermission", null); 88 89 contextWrapper.sendBroadcast(new Intent("foo")); 90 assertThat(transcript).isEmpty(); 91 92 contextWrapper.sendBroadcast(new Intent("foo"), null); 93 assertThat(transcript).isEmpty(); 94 95 contextWrapper.sendBroadcast(new Intent("foo"), "wrongPermission"); 96 assertThat(transcript).isEmpty(); 97 98 contextWrapper.sendBroadcast(new Intent("foo"), "validPermission"); 99 assertThat(transcript).containsExactly("Larry notified of foo"); 100 transcript.clear(); 101 102 contextWrapper.sendBroadcast(new Intent("baz"), "validPermission"); 103 assertThat(transcript).containsExactly("Larry notified of baz"); 104 } 105 106 @Test sendBroadcast_shouldSendIntentUsingHandlerIfOneIsProvided()107 public void sendBroadcast_shouldSendIntentUsingHandlerIfOneIsProvided() { 108 HandlerThread handlerThread = new HandlerThread("test"); 109 handlerThread.start(); 110 111 Handler handler = new Handler(handlerThread.getLooper()); 112 assertNotSame(handler.getLooper(), Looper.getMainLooper()); 113 114 BroadcastReceiver receiver = broadcastReceiver("Larry"); 115 contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz"), null, handler); 116 117 assertThat(shadowOf(handler.getLooper()).getScheduler().size()).isEqualTo(0); 118 contextWrapper.sendBroadcast(new Intent("foo")); 119 assertThat(shadowOf(handler.getLooper()).getScheduler().size()).isEqualTo(1); 120 shadowOf(handlerThread.getLooper()).idle(); 121 assertThat(shadowOf(handler.getLooper()).getScheduler().size()).isEqualTo(0); 122 123 assertThat(transcript).containsExactly("Larry notified of foo"); 124 } 125 126 @Test sendOrderedBroadcast_shouldReturnValues()127 public void sendOrderedBroadcast_shouldReturnValues() throws Exception { 128 String action = "test"; 129 130 IntentFilter lowFilter = new IntentFilter(action); 131 lowFilter.setPriority(1); 132 BroadcastReceiver lowReceiver = broadcastReceiver("Low"); 133 contextWrapper.registerReceiver(lowReceiver, lowFilter); 134 135 IntentFilter highFilter = new IntentFilter(action); 136 highFilter.setPriority(2); 137 BroadcastReceiver highReceiver = broadcastReceiver("High"); 138 contextWrapper.registerReceiver(highReceiver, highFilter); 139 140 final FooReceiver resultReceiver = new FooReceiver(); 141 contextWrapper.sendOrderedBroadcast(new Intent(action), null, resultReceiver, null, 1, "initial", null); 142 assertThat(transcript).containsExactly("High notified of test", "Low notified of test"); 143 assertThat(resultReceiver.resultCode).isEqualTo(1); 144 } 145 146 private static final class FooReceiver extends BroadcastReceiver { 147 private int resultCode; 148 private SettableFuture<Void> settableFuture = SettableFuture.create(); 149 150 @Override onReceive(Context context, Intent intent)151 public void onReceive(Context context, Intent intent) { 152 resultCode = getResultCode(); 153 settableFuture.set(null); 154 } 155 } 156 157 @Test sendOrderedBroadcast_shouldExecuteSerially()158 public void sendOrderedBroadcast_shouldExecuteSerially() { 159 String action = "test"; 160 AtomicReference<BroadcastReceiver.PendingResult> midResult = new AtomicReference<>(); 161 162 IntentFilter lowFilter = new IntentFilter(action); 163 lowFilter.setPriority(1); 164 BroadcastReceiver lowReceiver = broadcastReceiver("Low"); 165 contextWrapper.registerReceiver(lowReceiver, lowFilter); 166 167 IntentFilter midFilter = new IntentFilter(action); 168 midFilter.setPriority(2); 169 AsyncReceiver midReceiver = new AsyncReceiver(midResult); 170 contextWrapper.registerReceiver(midReceiver, midFilter); 171 172 IntentFilter highFilter = new IntentFilter(action); 173 highFilter.setPriority(3); 174 BroadcastReceiver highReceiver = broadcastReceiver("High"); 175 contextWrapper.registerReceiver(highReceiver, highFilter); 176 177 contextWrapper.sendOrderedBroadcast(new Intent(action), null); 178 assertThat(transcript).containsExactly("High notified of test", "Mid notified of test"); 179 transcript.clear(); 180 assertThat(midResult.get()).isNotNull(); 181 midResult.get().finish(); 182 Robolectric.flushForegroundThreadScheduler(); 183 assertThat(transcript).containsExactly("Low notified of test"); 184 } 185 186 private class AsyncReceiver extends BroadcastReceiver { 187 private final AtomicReference<PendingResult> reference; 188 AsyncReceiver(AtomicReference<PendingResult> reference)189 private AsyncReceiver(AtomicReference<PendingResult> reference) { 190 this.reference = reference; 191 } 192 193 @Override onReceive(Context context, Intent intent)194 public void onReceive(Context context, Intent intent) { 195 reference.set(goAsync()); 196 transcript.add("Mid notified of " + intent.getAction()); 197 } 198 } 199 200 @Test sendOrderedBroadcast_shouldSendByPriority()201 public void sendOrderedBroadcast_shouldSendByPriority() throws Exception { 202 String action = "test"; 203 204 IntentFilter lowFilter = new IntentFilter(action); 205 lowFilter.setPriority(1); 206 BroadcastReceiver lowReceiver = broadcastReceiver("Low"); 207 contextWrapper.registerReceiver(lowReceiver, lowFilter); 208 209 IntentFilter highFilter = new IntentFilter(action); 210 highFilter.setPriority(2); 211 BroadcastReceiver highReceiver = broadcastReceiver("High"); 212 contextWrapper.registerReceiver(highReceiver, highFilter); 213 214 contextWrapper.sendOrderedBroadcast(new Intent(action), null); 215 assertThat(transcript).containsExactly("High notified of test", "Low notified of test"); 216 } 217 218 @Test orderedBroadcasts_shouldAbort()219 public void orderedBroadcasts_shouldAbort() throws Exception { 220 String action = "test"; 221 222 IntentFilter lowFilter = new IntentFilter(action); 223 lowFilter.setPriority(1); 224 BroadcastReceiver lowReceiver = broadcastReceiver("Low"); 225 contextWrapper.registerReceiver(lowReceiver, lowFilter); 226 227 IntentFilter highFilter = new IntentFilter(action); 228 highFilter.setPriority(2); 229 BroadcastReceiver highReceiver = new BroadcastReceiver() { 230 @Override 231 public void onReceive(Context context, Intent intent) { 232 transcript.add("High" + " notified of " + intent.getAction()); 233 abortBroadcast(); 234 } 235 }; 236 contextWrapper.registerReceiver(highReceiver, highFilter); 237 238 contextWrapper.sendOrderedBroadcast(new Intent(action), null); 239 assertThat(transcript).containsExactly("High notified of test"); 240 } 241 242 @Test unregisterReceiver_shouldUnregisterReceiver()243 public void unregisterReceiver_shouldUnregisterReceiver() throws Exception { 244 BroadcastReceiver receiver = broadcastReceiver("Larry"); 245 246 contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz")); 247 contextWrapper.unregisterReceiver(receiver); 248 249 contextWrapper.sendBroadcast(new Intent("foo")); 250 assertThat(transcript).isEmpty(); 251 } 252 253 @Test(expected = IllegalArgumentException.class) unregisterReceiver_shouldThrowExceptionWhenReceiverIsNotRegistered()254 public void unregisterReceiver_shouldThrowExceptionWhenReceiverIsNotRegistered() throws Exception { 255 contextWrapper.unregisterReceiver(new AppWidgetProvider()); 256 } 257 258 @Test broadcastReceivers_shouldBeSharedAcrossContextsPerApplicationContext()259 public void broadcastReceivers_shouldBeSharedAcrossContextsPerApplicationContext() throws Exception { 260 BroadcastReceiver receiver = broadcastReceiver("Larry"); 261 262 new ContextWrapper(RuntimeEnvironment.application).registerReceiver(receiver, intentFilter("foo", "baz")); 263 new ContextWrapper(RuntimeEnvironment.application).sendBroadcast(new Intent("foo")); 264 RuntimeEnvironment.application.sendBroadcast(new Intent("baz")); 265 assertThat(transcript).containsExactly("Larry notified of foo", "Larry notified of baz"); 266 267 new ContextWrapper(RuntimeEnvironment.application).unregisterReceiver(receiver); 268 } 269 270 @Test broadcasts_shouldBeLogged()271 public void broadcasts_shouldBeLogged() { 272 Intent broadcastIntent = new Intent("foo"); 273 contextWrapper.sendBroadcast(broadcastIntent); 274 275 List<Intent> broadcastIntents = shadowOf(contextWrapper).getBroadcastIntents(); 276 assertTrue(broadcastIntents.size() == 1); 277 assertEquals(broadcastIntent, broadcastIntents.get(0)); 278 } 279 280 @Test sendStickyBroadcast_shouldDeliverIntentToAllRegisteredReceivers()281 public void sendStickyBroadcast_shouldDeliverIntentToAllRegisteredReceivers() { 282 BroadcastReceiver receiver = broadcastReceiver("Larry"); 283 contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz")); 284 285 contextWrapper.sendStickyBroadcast(new Intent("foo")); 286 assertThat(transcript).containsExactly("Larry notified of foo"); 287 transcript.clear(); 288 289 contextWrapper.sendStickyBroadcast(new Intent("womp")); 290 assertThat(transcript).isEmpty(); 291 292 contextWrapper.sendStickyBroadcast(new Intent("baz")); 293 assertThat(transcript).containsExactly("Larry notified of baz"); 294 } 295 296 @Test sendStickyBroadcast_shouldStickSentIntent()297 public void sendStickyBroadcast_shouldStickSentIntent() { 298 contextWrapper.sendStickyBroadcast(new Intent("foo")); 299 assertThat(transcript).isEmpty(); 300 301 BroadcastReceiver receiver = broadcastReceiver("Larry"); 302 Intent sticker = contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz")); 303 assertThat(transcript).containsExactly("Larry notified of foo"); 304 assertThat(sticker).isNotNull(); 305 assertThat(sticker.getAction()).isEqualTo("foo"); 306 } 307 308 @Test afterSendStickyBroadcast_allSentIntentsShouldBeDeliveredToNewRegistrants()309 public void afterSendStickyBroadcast_allSentIntentsShouldBeDeliveredToNewRegistrants() { 310 contextWrapper.sendStickyBroadcast(new Intent("foo")); 311 contextWrapper.sendStickyBroadcast(new Intent("baz")); 312 assertThat(transcript).isEmpty(); 313 314 BroadcastReceiver receiver = broadcastReceiver("Larry"); 315 Intent sticker = contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz")); 316 assertThat(transcript).containsExactly("Larry notified of foo", "Larry notified of baz"); 317 /* 318 Note: we do not strictly test what is returned by the method in this case 319 because there no guaranties what particular Intent will be returned by Android system 320 */ 321 assertThat(sticker).isNotNull(); 322 } 323 324 @Test shouldReturnSameApplicationEveryTime()325 public void shouldReturnSameApplicationEveryTime() throws Exception { 326 Activity activity = new Activity(); 327 assertThat(activity.getApplication()).isSameAs(activity.getApplication()); 328 329 assertThat(activity.getApplication()).isSameAs(new Activity().getApplication()); 330 } 331 332 @Test shouldReturnSameApplicationContextEveryTime()333 public void shouldReturnSameApplicationContextEveryTime() throws Exception { 334 Activity activity = Robolectric.setupActivity(Activity.class); 335 assertThat(activity.getApplicationContext()).isSameAs(activity.getApplicationContext()); 336 337 assertThat(activity.getApplicationContext()).isSameAs(Robolectric.setupActivity(Activity.class).getApplicationContext()); 338 } 339 340 @Test shouldReturnApplicationContext_forViewContextInflatedWithApplicationContext()341 public void shouldReturnApplicationContext_forViewContextInflatedWithApplicationContext() throws Exception { 342 View view = LayoutInflater.from(RuntimeEnvironment.application).inflate(R.layout.custom_layout, null); 343 Context viewContext = new ContextWrapper(view.getContext()); 344 assertThat(viewContext.getApplicationContext()).isEqualTo(RuntimeEnvironment.application); 345 } 346 347 @Test shouldReturnSameContentResolverEveryTime()348 public void shouldReturnSameContentResolverEveryTime() throws Exception { 349 Activity activity = Robolectric.setupActivity(Activity.class); 350 assertThat(activity.getContentResolver()).isSameAs(activity.getContentResolver()); 351 352 assertThat(activity.getContentResolver()).isSameAs(Robolectric.setupActivity(Activity.class).getContentResolver()); 353 } 354 355 @Test shouldReturnSameLocationManagerEveryTime()356 public void shouldReturnSameLocationManagerEveryTime() throws Exception { 357 assertSameInstanceEveryTime(Context.LOCATION_SERVICE); 358 } 359 360 @Test shouldReturnSameWifiManagerEveryTime()361 public void shouldReturnSameWifiManagerEveryTime() throws Exception { 362 assertSameInstanceEveryTime(Context.WIFI_SERVICE); 363 } 364 365 @Test shouldReturnSameAlarmServiceEveryTime()366 public void shouldReturnSameAlarmServiceEveryTime() throws Exception { 367 assertSameInstanceEveryTime(Context.ALARM_SERVICE); 368 } 369 370 @Test checkPermissionsShouldReturnPermissionGrantedToAddedPermissions()371 public void checkPermissionsShouldReturnPermissionGrantedToAddedPermissions() throws Exception { 372 shadowOf(contextWrapper).grantPermissions("foo", "bar"); 373 assertThat(contextWrapper.checkPermission("foo", 0, 0)).isEqualTo(PERMISSION_GRANTED); 374 assertThat(contextWrapper.checkPermission("bar", 0, 0)).isEqualTo(PERMISSION_GRANTED); 375 assertThat(contextWrapper.checkPermission("baz", 0, 0)).isEqualTo(PERMISSION_DENIED); 376 } 377 assertSameInstanceEveryTime(String serviceName)378 private void assertSameInstanceEveryTime(String serviceName) { 379 Activity activity1 = buildActivity(Activity.class).create().get(); 380 Activity activity2 = buildActivity(Activity.class).create().get(); 381 assertThat(activity1.getSystemService(serviceName)).isSameAs(activity1.getSystemService(serviceName)); 382 assertThat(activity1.getSystemService(serviceName)).isSameAs(activity2.getSystemService(serviceName)); 383 } 384 385 @Test bindServiceDelegatesToShadowApplication()386 public void bindServiceDelegatesToShadowApplication() { 387 contextWrapper.bindService(new Intent("foo"), new TestService(), Context.BIND_AUTO_CREATE); 388 assertEquals("foo", shadowOf(RuntimeEnvironment.application).getNextStartedService().getAction()); 389 } 390 391 @Test startActivities_shouldStartAllActivities()392 public void startActivities_shouldStartAllActivities() { 393 final Intent view = new Intent(Intent.ACTION_VIEW); 394 final Intent pick = new Intent(Intent.ACTION_PICK); 395 contextWrapper.startActivities(new Intent[] {view, pick}); 396 397 assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(pick); 398 assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(view); 399 } 400 401 @Test startActivities_withBundle_shouldStartAllActivities()402 public void startActivities_withBundle_shouldStartAllActivities() { 403 final Intent view = new Intent(Intent.ACTION_VIEW); 404 final Intent pick = new Intent(Intent.ACTION_PICK); 405 contextWrapper.startActivities(new Intent[] {view, pick}, new Bundle()); 406 407 assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(pick); 408 assertThat(ShadowApplication.getInstance().getNextStartedActivity()).isEqualTo(view); 409 } 410 broadcastReceiver(final String name)411 private BroadcastReceiver broadcastReceiver(final String name) { 412 return new BroadcastReceiver() { 413 @Override public void onReceive(Context context, Intent intent) { 414 transcript.add(name + " notified of " + intent.getAction()); 415 } 416 }; 417 } 418 419 private IntentFilter intentFilter(String... actions) { 420 IntentFilter larryIntentFilter = new IntentFilter(); 421 for (String action : actions) { 422 larryIntentFilter.addAction(action); 423 } 424 return larryIntentFilter; 425 } 426 427 @Test 428 public void packageManagerShouldNotBeNullWhenWrappingAnApplication() { 429 assertThat(RuntimeEnvironment.application.getPackageManager()).isNotNull(); 430 } 431 432 @Test 433 public void checkCallingPermissionsShouldReturnPermissionGrantedToAddedPermissions() throws Exception { 434 shadowOf(contextWrapper).grantPermissions("foo", "bar"); 435 assertThat(contextWrapper.checkCallingPermission("foo")).isEqualTo(PERMISSION_GRANTED); 436 assertThat(contextWrapper.checkCallingPermission("bar")).isEqualTo(PERMISSION_GRANTED); 437 assertThat(contextWrapper.checkCallingPermission("baz")).isEqualTo(PERMISSION_DENIED); 438 } 439 440 @Test 441 public void checkCallingOrSelfPermissionsShouldReturnPermissionGrantedToAddedPermissions() throws Exception { 442 shadowOf(contextWrapper).grantPermissions("foo", "bar"); 443 assertThat(contextWrapper.checkCallingOrSelfPermission("foo")).isEqualTo(PERMISSION_GRANTED); 444 assertThat(contextWrapper.checkCallingOrSelfPermission("bar")).isEqualTo(PERMISSION_GRANTED); 445 assertThat(contextWrapper.checkCallingOrSelfPermission("baz")).isEqualTo(PERMISSION_DENIED); 446 } 447 448 @Test 449 public void checkCallingPermission_shouldReturnPermissionDeniedForRemovedPermissions() throws Exception { 450 shadowOf(contextWrapper).grantPermissions("foo", "bar"); 451 shadowOf(contextWrapper).denyPermissions("foo", "qux"); 452 assertThat(contextWrapper.checkCallingPermission("foo")).isEqualTo(PERMISSION_DENIED); 453 assertThat(contextWrapper.checkCallingPermission("bar")).isEqualTo(PERMISSION_GRANTED); 454 assertThat(contextWrapper.checkCallingPermission("baz")).isEqualTo(PERMISSION_DENIED); 455 assertThat(contextWrapper.checkCallingPermission("qux")).isEqualTo(PERMISSION_DENIED); 456 } 457 458 @Test 459 public void checkCallingOrSelfPermission_shouldReturnPermissionDeniedForRemovedPermissions() throws Exception { 460 shadowOf(contextWrapper).grantPermissions("foo", "bar"); 461 shadowOf(contextWrapper).denyPermissions("foo", "qux"); 462 assertThat(contextWrapper.checkCallingOrSelfPermission("foo")).isEqualTo(PERMISSION_DENIED); 463 assertThat(contextWrapper.checkCallingOrSelfPermission("bar")).isEqualTo(PERMISSION_GRANTED); 464 assertThat(contextWrapper.checkCallingOrSelfPermission("baz")).isEqualTo(PERMISSION_DENIED); 465 assertThat(contextWrapper.checkCallingOrSelfPermission("qux")).isEqualTo(PERMISSION_DENIED); 466 } 467 468 @Test 469 public void getSharedPreferencesShouldReturnSameInstanceWhenSameNameIsSupplied() { 470 final SharedPreferences pref1 = contextWrapper.getSharedPreferences("pref", Context.MODE_PRIVATE); 471 final SharedPreferences pref2 = contextWrapper.getSharedPreferences("pref", Context.MODE_PRIVATE); 472 473 assertThat(pref1).isSameAs(pref2); 474 } 475 476 @Test 477 public void getSharedPreferencesShouldReturnDifferentInstancesWhenDifferentNameIsSupplied() { 478 final SharedPreferences pref1 = contextWrapper.getSharedPreferences("pref1", Context.MODE_PRIVATE); 479 final SharedPreferences pref2 = contextWrapper.getSharedPreferences("pref2", Context.MODE_PRIVATE); 480 481 assertThat(pref1).isNotSameAs(pref2); 482 } 483 484 @Test 485 public void sendBroadcast_shouldOnlySendIntentWithTypeWhenReceiverMatchesType() 486 throws IntentFilter.MalformedMimeTypeException { 487 488 final BroadcastReceiver viewAllTypesReceiver = broadcastReceiver("ViewActionWithAnyTypeReceiver"); 489 final IntentFilter allTypesIntentFilter = intentFilter("view"); 490 allTypesIntentFilter.addDataType("*/*"); 491 contextWrapper.registerReceiver(viewAllTypesReceiver, allTypesIntentFilter); 492 493 final BroadcastReceiver imageReceiver = broadcastReceiver("ImageReceiver"); 494 final IntentFilter imageIntentFilter = intentFilter("view"); 495 imageIntentFilter.addDataType("img/*"); 496 contextWrapper.registerReceiver(imageReceiver, imageIntentFilter); 497 498 final BroadcastReceiver videoReceiver = broadcastReceiver("VideoReceiver"); 499 final IntentFilter videoIntentFilter = intentFilter("view"); 500 videoIntentFilter.addDataType("video/*"); 501 contextWrapper.registerReceiver(videoReceiver, videoIntentFilter); 502 503 final BroadcastReceiver viewReceiver = broadcastReceiver("ViewActionReceiver"); 504 final IntentFilter viewIntentFilter = intentFilter("view"); 505 contextWrapper.registerReceiver(viewReceiver, viewIntentFilter); 506 507 final Intent imageIntent = new Intent("view"); 508 imageIntent.setType("img/jpeg"); 509 contextWrapper.sendBroadcast(imageIntent); 510 511 final Intent videoIntent = new Intent("view"); 512 videoIntent.setType("video/mp4"); 513 contextWrapper.sendBroadcast(videoIntent); 514 515 assertThat(transcript).containsExactly( 516 "ViewActionWithAnyTypeReceiver notified of view", 517 "ImageReceiver notified of view", 518 "ViewActionWithAnyTypeReceiver notified of view", 519 "VideoReceiver notified of view"); 520 } 521 522 @Test 523 public void getApplicationInfo_shouldReturnApplicationInfoForApplicationPackage() { 524 final ApplicationInfo info = contextWrapper.getApplicationInfo(); 525 assertThat(info.packageName).isEqualTo("org.robolectric"); 526 } 527 } 528