1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertFalse; 6 import static org.junit.Assert.assertNull; 7 import static org.junit.Assert.assertSame; 8 import static org.junit.Assert.assertTrue; 9 10 import android.app.Activity; 11 import android.content.ComponentName; 12 import android.content.Context; 13 import android.content.Intent; 14 import android.content.pm.ActivityInfo; 15 import android.content.pm.PackageManager; 16 import android.net.Uri; 17 import android.os.Bundle; 18 import android.os.Parcel; 19 import android.os.Parcelable; 20 import androidx.test.core.app.ApplicationProvider; 21 import androidx.test.ext.junit.runners.AndroidJUnit4; 22 import java.io.Serializable; 23 import java.util.ArrayList; 24 import java.util.Arrays; 25 import java.util.Set; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 @RunWith(AndroidJUnit4.class) 30 public class ShadowIntentTest { 31 private static final String TEST_ACTIVITY_CLASS_NAME = "org.robolectric.shadows.TestActivity"; 32 33 @Test 34 public void resolveActivityInfo_shouldReturnActivityInfoForExistingActivity() { 35 Context context = ApplicationProvider.getApplicationContext(); 36 PackageManager packageManager = context.getPackageManager(); 37 38 Intent intent = new Intent(); 39 intent.setClassName(context, TEST_ACTIVITY_CLASS_NAME); 40 ActivityInfo activityInfo = intent.resolveActivityInfo(packageManager, PackageManager.GET_ACTIVITIES); 41 assertThat(activityInfo).isNotNull(); 42 } 43 44 @Test 45 public void testGetExtraReturnsNull_whenThereAreNoExtrasAdded() throws Exception { 46 Intent intent = new Intent(); 47 assertEquals(intent.getExtras(), null); 48 } 49 50 @Test 51 public void testStringExtra() throws Exception { 52 Intent intent = new Intent(); 53 assertSame(intent, intent.putExtra("foo", "bar")); 54 assertEquals("bar", intent.getExtras().get("foo")); 55 } 56 57 @Test 58 public void testCharSequenceExtra() throws Exception { 59 Intent intent = new Intent(); 60 CharSequence cs = new TestCharSequence("bar"); 61 assertSame(intent, intent.putExtra("foo", cs)); 62 assertSame(cs, intent.getExtras().get("foo")); 63 } 64 65 @Test 66 public void testIntExtra() throws Exception { 67 Intent intent = new Intent(); 68 assertSame(intent, intent.putExtra("foo", 2)); 69 assertEquals(2, intent.getExtras().get("foo")); 70 assertEquals(2, intent.getIntExtra("foo", -1)); 71 } 72 73 @Test 74 public void testDoubleExtra() throws Exception { 75 Intent intent = new Intent(); 76 assertSame(intent, intent.putExtra("foo", 2d)); 77 assertEquals(2d, intent.getExtras().get("foo")); 78 assertThat(intent.getDoubleExtra("foo", -1)).isEqualTo(2d); 79 } 80 81 @Test 82 public void testFloatExtra() throws Exception { 83 Intent intent = new Intent(); 84 assertSame(intent, intent.putExtra("foo", 2f)); 85 assertThat(intent.getExtras().get("foo")).isEqualTo(2f); 86 assertThat(intent.getFloatExtra("foo", -1)).isEqualTo(2f); 87 } 88 89 @Test 90 public void testIntArrayExtra() throws Exception { 91 Intent intent = new Intent(); 92 int[] array = new int[2]; 93 array[0] = 1; 94 array[1] = 2; 95 assertSame(intent, intent.putExtra("foo", array)); 96 assertEquals(1, intent.getIntArrayExtra("foo")[0]); 97 assertEquals(2, intent.getIntArrayExtra("foo")[1]); 98 } 99 100 @Test 101 public void testLongArrayExtra() throws Exception { 102 Intent intent = new Intent(); 103 long[] array = new long[2]; 104 array[0] = 1L; 105 array[1] = 2L; 106 assertSame(intent, intent.putExtra("foo", array)); 107 assertEquals(1L, intent.getLongArrayExtra("foo")[0]); 108 assertEquals(2L, intent.getLongArrayExtra("foo")[1]); 109 } 110 111 @Test 112 public void testSerializableExtra() throws Exception { 113 Intent intent = new Intent(); 114 TestSerializable serializable = new TestSerializable("some string"); 115 assertSame(intent, intent.putExtra("foo", serializable)); 116 assertEquals(serializable, intent.getExtras().get("foo")); 117 assertEquals(serializable, intent.getSerializableExtra("foo")); 118 } 119 120 @Test 121 public void testSerializableOfParcelableExtra() throws Exception { 122 Intent intent = new Intent(); 123 ArrayList<Parcelable> serializable = new ArrayList<>(); 124 serializable.add(new TestParcelable(12)); 125 assertSame(intent, intent.putExtra("foo", serializable)); 126 assertEquals(serializable, intent.getExtras().get("foo")); 127 assertEquals(serializable, intent.getSerializableExtra("foo")); 128 } 129 130 @Test 131 public void testParcelableExtra() throws Exception { 132 Intent intent = new Intent(); 133 Parcelable parcelable = new TestParcelable(44); 134 assertSame(intent, intent.putExtra("foo", parcelable)); 135 assertSame(parcelable, intent.getExtras().get("foo")); 136 assertSame(parcelable, intent.getParcelableExtra("foo")); 137 } 138 139 @Test 140 public void testParcelableArrayExtra() throws Exception { 141 Intent intent = new Intent(); 142 Parcelable parcelable = new TestParcelable(11); 143 intent.putExtra("foo", parcelable); 144 assertSame(null, intent.getParcelableArrayExtra("foo")); 145 Parcelable[] parcelables = {new TestParcelable(12), new TestParcelable(13)}; 146 assertSame(intent, intent.putExtra("bar", parcelables)); 147 assertSame(parcelables, intent.getParcelableArrayExtra("bar")); 148 } 149 150 @Test 151 public void testParcelableArrayListExtra() { 152 Intent intent = new Intent(); 153 Parcelable parcel1 = new TestParcelable(22); 154 Parcelable parcel2 = new TestParcelable(23); 155 ArrayList<Parcelable> parcels = new ArrayList<>(); 156 parcels.add(parcel1); 157 parcels.add(parcel2); 158 159 assertSame(intent, intent.putParcelableArrayListExtra("foo", parcels)); 160 assertSame(parcels, intent.getParcelableArrayListExtra("foo")); 161 assertSame(parcel1, intent.getParcelableArrayListExtra("foo").get(0)); 162 assertSame(parcel2, intent.getParcelableArrayListExtra("foo").get(1)); 163 assertSame(parcels, intent.getExtras().getParcelableArrayList("foo")); 164 } 165 166 @Test 167 public void testLongExtra() throws Exception { 168 Intent intent = new Intent(); 169 assertSame(intent, intent.putExtra("foo", 2L)); 170 assertEquals(2L, intent.getExtras().get("foo")); 171 assertEquals(2L, intent.getLongExtra("foo", -1)); 172 assertEquals(-1L, intent.getLongExtra("bar", -1)); 173 } 174 175 @Test 176 public void testBundleExtra() throws Exception { 177 Intent intent = new Intent(); 178 Bundle bundle = new Bundle(); 179 bundle.putInt("bar", 5); 180 assertSame(intent, intent.putExtra("foo", bundle)); 181 assertEquals(5, intent.getBundleExtra("foo").getInt("bar")); 182 } 183 184 @Test 185 public void testHasExtra() throws Exception { 186 Intent intent = new Intent(); 187 assertSame(intent, intent.putExtra("foo", "")); 188 assertTrue(intent.hasExtra("foo")); 189 assertFalse(intent.hasExtra("bar")); 190 } 191 192 @Test 193 public void testGetActionReturnsWhatWasSet() throws Exception { 194 Intent intent = new Intent(); 195 assertSame(intent, intent.setAction("foo")); 196 assertEquals("foo", intent.getAction()); 197 } 198 199 @Test 200 public void testSetData() throws Exception { 201 Intent intent = new Intent(); 202 Uri uri = Uri.parse("content://this/and/that"); 203 intent.setType("abc"); 204 assertSame(intent, intent.setData(uri)); 205 assertSame(uri, intent.getData()); 206 assertNull(intent.getType()); 207 } 208 209 @Test 210 public void testGetScheme() throws Exception { 211 Intent intent = new Intent(); 212 Uri uri = Uri.parse("http://robolectric.org"); 213 assertSame(intent, intent.setData(uri)); 214 assertSame(uri, intent.getData()); 215 assertEquals("http", intent.getScheme()); 216 } 217 218 @Test 219 public void testSetType() throws Exception { 220 Intent intent = new Intent(); 221 intent.setData(Uri.parse("content://this/and/that")); 222 assertSame(intent, intent.setType("def")); 223 assertNull(intent.getData()); 224 assertEquals("def", intent.getType()); 225 } 226 227 @Test 228 public void testSetDataAndType() throws Exception { 229 Intent intent = new Intent(); 230 Uri uri = Uri.parse("content://this/and/that"); 231 assertSame(intent, intent.setDataAndType(uri, "ghi")); 232 assertSame(uri, intent.getData()); 233 assertEquals("ghi", intent.getType()); 234 } 235 236 @Test 237 public void testSetClass() throws Exception { 238 Intent intent = new Intent(); 239 Class<? extends ShadowIntentTest> thisClass = getClass(); 240 Intent output = intent.setClass(ApplicationProvider.getApplicationContext(), thisClass); 241 242 assertSame(output, intent); 243 assertThat(intent.getComponent().getClassName()).isEqualTo(thisClass.getName()); 244 } 245 246 @Test 247 public void testSetClassName() throws Exception { 248 Intent intent = new Intent(); 249 Class<? extends ShadowIntentTest> thisClass = getClass(); 250 intent.setClassName("package.name", thisClass.getName()); 251 assertSame(thisClass.getName(), intent.getComponent().getClassName()); 252 assertEquals("package.name", intent.getComponent().getPackageName()); 253 assertSame(intent.getComponent().getClassName(), thisClass.getName()); 254 } 255 256 @Test 257 public void testSetClassThroughConstructor() throws Exception { 258 Intent intent = new Intent(ApplicationProvider.getApplicationContext(), getClass()); 259 assertThat(intent.getComponent().getClassName()).isEqualTo(getClass().getName()); 260 } 261 262 @Test 263 public void shouldSetFlags() throws Exception { 264 Intent intent = new Intent(); 265 Intent self = intent.setFlags(1234); 266 assertEquals(1234, intent.getFlags()); 267 assertSame(self, intent); 268 } 269 270 @Test 271 public void shouldAddFlags() throws Exception { 272 Intent intent = new Intent(); 273 Intent self = intent.addFlags(4); 274 self.addFlags(8); 275 assertEquals(12, intent.getFlags()); 276 assertSame(self, intent); 277 } 278 279 @Test 280 public void shouldSupportCategories() throws Exception { 281 Intent intent = new Intent(); 282 Intent self = intent.addCategory("category.name.1"); 283 intent.addCategory("category.name.2"); 284 285 assertTrue(intent.hasCategory("category.name.1")); 286 assertTrue(intent.hasCategory("category.name.2")); 287 288 Set<String> categories = intent.getCategories(); 289 assertTrue(categories.contains("category.name.1")); 290 assertTrue(categories.contains("category.name.2")); 291 292 intent.removeCategory("category.name.1"); 293 assertFalse(intent.hasCategory("category.name.1")); 294 assertTrue(intent.hasCategory("category.name.2")); 295 296 intent.removeCategory("category.name.2"); 297 assertFalse(intent.hasCategory("category.name.2")); 298 299 assertThat(intent.getCategories()).isNull(); 300 301 assertSame(self, intent); 302 } 303 304 @Test 305 public void shouldAddCategories() throws Exception { 306 Intent intent = new Intent(); 307 Intent self = intent.addCategory("foo"); 308 assertTrue(intent.getCategories().contains("foo")); 309 assertSame(self, intent); 310 } 311 312 @Test 313 public void shouldFillIn() throws Exception { 314 Intent intentA = new Intent(); 315 Intent intentB = new Intent(); 316 317 intentB.setAction("foo"); 318 Uri uri = Uri.parse("http://www.foo.com"); 319 intentB.setDataAndType(uri, "text/html"); 320 String category = "category"; 321 intentB.addCategory(category); 322 intentB.setPackage("com.foobar.app"); 323 ComponentName cn = new ComponentName("com.foobar.app", "fragmentActivity"); 324 intentB.setComponent(cn); 325 intentB.putExtra("FOO", 23); 326 327 int flags = Intent.FILL_IN_ACTION | 328 Intent.FILL_IN_DATA | 329 Intent.FILL_IN_CATEGORIES | 330 Intent.FILL_IN_PACKAGE | 331 Intent.FILL_IN_COMPONENT; 332 333 int result = intentA.fillIn(intentB, flags); 334 assertEquals("foo", intentA.getAction()); 335 assertSame(uri, intentA.getData()); 336 assertEquals("text/html", intentA.getType()); 337 assertTrue(intentA.getCategories().contains(category)); 338 assertEquals("com.foobar.app", intentA.getPackage()); 339 assertSame(cn, intentA.getComponent()); 340 assertEquals(23, intentA.getIntExtra("FOO", -1)); 341 assertEquals(result, flags); 342 } 343 344 @Test 345 public void createChooser_shouldWrapIntent() throws Exception { 346 Intent originalIntent = new Intent(Intent.ACTION_BATTERY_CHANGED, Uri.parse("foo://blah")); 347 Intent chooserIntent = Intent.createChooser(originalIntent, "The title"); 348 assertThat(chooserIntent.getAction()).isEqualTo(Intent.ACTION_CHOOSER); 349 assertThat(chooserIntent.getStringExtra(Intent.EXTRA_TITLE)).isEqualTo("The title"); 350 assertThat((Intent) chooserIntent.getParcelableExtra(Intent.EXTRA_INTENT)) 351 .isSameAs(originalIntent); 352 } 353 354 @Test 355 public void setUri_setsUri() throws Exception { 356 Intent intent = new Intent(); 357 intent.setData(Uri.parse("http://foo")); 358 assertThat(intent.getData()).isEqualTo(Uri.parse("http://foo")); 359 } 360 361 @Test 362 public void setUri_shouldReturnUriString() throws Exception { 363 Intent intent = new Intent(); 364 intent.setData(Uri.parse("http://foo")); 365 assertThat(intent.getDataString()).isEqualTo("http://foo"); 366 } 367 368 @Test 369 public void setUri_shouldReturnNullUriString() throws Exception { 370 Intent intent = new Intent(); 371 assertThat(intent.getDataString()).isNull(); 372 } 373 374 @Test 375 public void putStringArrayListExtra_addsListToExtras() { 376 Intent intent = new Intent(); 377 final ArrayList<String> strings = new ArrayList<>(Arrays.asList("hi", "there")); 378 379 intent.putStringArrayListExtra("KEY", strings); 380 assertThat(intent.getStringArrayListExtra("KEY")).isEqualTo(strings); 381 assertThat(intent.getExtras().getStringArrayList("KEY")).isEqualTo(strings); 382 } 383 384 @Test 385 public void putIntegerArrayListExtra_addsListToExtras() { 386 Intent intent = new Intent(); 387 final ArrayList<Integer> integers = new ArrayList<>(Arrays.asList(100, 200, 300)); 388 389 intent.putIntegerArrayListExtra("KEY", integers); 390 assertThat(intent.getIntegerArrayListExtra("KEY")).isEqualTo(integers); 391 assertThat(intent.getExtras().getIntegerArrayList("KEY")).isEqualTo(integers); 392 } 393 394 @Test 395 public void constructor_shouldSetComponentAndActionAndData() { 396 Intent intent = 397 new Intent( 398 "roboaction", 399 Uri.parse("http://www.robolectric.org"), 400 ApplicationProvider.getApplicationContext(), 401 Activity.class); 402 assertThat(intent.getComponent()).isEqualTo(new ComponentName("org.robolectric", "android.app.Activity")); 403 assertThat(intent.getAction()).isEqualTo("roboaction"); 404 assertThat(intent.getData()).isEqualTo(Uri.parse("http://www.robolectric.org")); 405 } 406 407 @Test 408 public void putExtra_shouldBeChainable() { 409 // Ensure that all putExtra methods return the Intent properly and can therefore be chained 410 // without causing NPE's 411 Intent intent = new Intent(); 412 413 assertThat(intent.putExtra("double array", new double[] { 0.0 })).isEqualTo(intent); 414 assertThat(intent.putExtra("int", 0)).isEqualTo(intent); 415 assertThat(intent.putExtra("CharSequence", new TestCharSequence("test"))).isEqualTo(intent); 416 assertThat(intent.putExtra("char", 'a')).isEqualTo(intent); 417 assertThat(intent.putExtra("Bundle", new Bundle())).isEqualTo(intent); 418 assertThat(intent.putExtra("Parcelable array", new Parcelable[] { new TestParcelable(0) })) 419 .isEqualTo(intent); 420 assertThat(intent.putExtra("Serializable", new TestSerializable("test"))).isEqualTo(intent); 421 assertThat(intent.putExtra("int array", new int[] { 0 })).isEqualTo(intent); 422 assertThat(intent.putExtra("float", 0f)).isEqualTo(intent); 423 assertThat(intent.putExtra("byte array", new byte[] { 0 })).isEqualTo(intent); 424 assertThat(intent.putExtra("long array", new long[] { 0L })).isEqualTo(intent); 425 assertThat(intent.putExtra("Parcelable", new TestParcelable(0))).isEqualTo(intent); 426 assertThat(intent.putExtra("float array", new float[] { 0f })).isEqualTo(intent); 427 assertThat(intent.putExtra("long", 0L)).isEqualTo(intent); 428 assertThat(intent.putExtra("String array", new String[] { "test" })).isEqualTo(intent); 429 assertThat(intent.putExtra("boolean", true)).isEqualTo(intent); 430 assertThat(intent.putExtra("boolean array", new boolean[] { true })).isEqualTo(intent); 431 assertThat(intent.putExtra("short", (short) 0)).isEqualTo(intent); 432 assertThat(intent.putExtra("double", 0.0)).isEqualTo(intent); 433 assertThat(intent.putExtra("short array", new short[] { 0 })).isEqualTo(intent); 434 assertThat(intent.putExtra("String", "test")).isEqualTo(intent); 435 assertThat(intent.putExtra("byte", (byte) 0)).isEqualTo(intent); 436 assertThat(intent.putExtra("char array", new char[] { 'a' })).isEqualTo(intent); 437 assertThat(intent.putExtra("CharSequence array", 438 new CharSequence[] { new TestCharSequence("test") })) 439 .isEqualTo(intent); 440 } 441 442 @Test 443 public void equals_shouldOnlyBeIdentity() { 444 assertThat(new Intent()).isNotEqualTo(new Intent()); 445 } 446 447 @Test 448 public void cloneFilter_shouldIncludeAction() { 449 Intent intent = new Intent("FOO"); 450 intent.cloneFilter(); 451 assertThat(intent.getAction()).isEqualTo("FOO"); 452 } 453 454 @Test 455 public void getExtra_shouldWorkAfterParcel() { 456 ComponentName componentName = new ComponentName("barcomponent", "compclass"); 457 Uri parsed = Uri.parse("https://foo.bar"); 458 Intent intent = new Intent(); 459 intent.putExtra("key", 123); 460 intent.setAction("Foo"); 461 intent.setComponent(componentName); 462 intent.setData(parsed); 463 Parcel parcel = Parcel.obtain(); 464 parcel.writeParcelable(intent, 0); 465 parcel.setDataPosition(0); 466 intent = parcel.readParcelable(getClass().getClassLoader()); 467 assertThat(intent.getIntExtra("key", 0)).isEqualTo(123); 468 assertThat(intent.getAction()).isEqualTo("Foo"); 469 assertThat(intent.getComponent()).isEqualTo(componentName); 470 assertThat(intent.getData()).isEqualTo(parsed); 471 } 472 473 private static class TestSerializable implements Serializable { 474 private String someValue; 475 476 public TestSerializable(String someValue) { 477 this.someValue = someValue; 478 } 479 480 @Override 481 public boolean equals(Object o) { 482 if (this == o) return true; 483 if (o == null || getClass() != o.getClass()) return false; 484 485 TestSerializable that = (TestSerializable) o; 486 487 if (someValue != null ? !someValue.equals(that.someValue) : that.someValue != null) return false; 488 489 return true; 490 } 491 492 @Override 493 public int hashCode() { 494 return someValue != null ? someValue.hashCode() : 0; 495 } 496 } 497 498 private static class TestCharSequence implements CharSequence { 499 String s; 500 501 public TestCharSequence(String s) { 502 this.s = s; 503 } 504 505 @Override 506 public char charAt(int index) { 507 return s.charAt(index); 508 } 509 510 @Override 511 public int length() { 512 return s.length(); 513 } 514 515 @Override 516 public CharSequence subSequence(int start, int end) { 517 return s.subSequence(start, end); 518 } 519 520 } 521 } 522