1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.content.cts; 18 19 import org.xmlpull.v1.XmlPullParser; 20 import org.xmlpull.v1.XmlPullParserException; 21 22 import android.content.ComponentName; 23 import android.content.ContentResolver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.cts.util.XmlUtils; 27 import android.content.pm.ActivityInfo; 28 import android.content.pm.PackageManager; 29 import android.content.pm.PackageManager.NameNotFoundException; 30 import android.content.res.XmlResourceParser; 31 import android.net.Uri; 32 import android.os.Bundle; 33 import android.os.IBinder; 34 import android.os.Parcel; 35 import android.os.ServiceManager; 36 import android.platform.test.annotations.AppModeFull; 37 import android.provider.Contacts.People; 38 import android.test.AndroidTestCase; 39 import android.util.AttributeSet; 40 import android.util.Xml; 41 42 import com.android.content.cts.DummyParcelable; 43 44 import java.io.IOException; 45 import java.io.Serializable; 46 import java.net.URISyntaxException; 47 import java.util.ArrayList; 48 import java.util.Objects; 49 import java.util.Set; 50 51 public class IntentTest extends AndroidTestCase { 52 53 private Intent mIntent; 54 private static final String TEST_ACTION = "android.content.IntentTest_test"; 55 private static final Uri TEST_URI = People.CONTENT_URI; 56 private static final Uri ANOTHER_TEST_URI = People.CONTENT_FILTER_URI; 57 private static final String TEST_EXTRA_NAME = "testExtraName"; 58 private Context mContext; 59 private PackageManager mPm; 60 private ComponentName mComponentName; 61 private ComponentName mAnotherComponentName; 62 private static final String TEST_TYPE = "testType"; 63 private static final String ANOTHER_TEST_TYPE = "anotherTestType"; 64 private static final String TEST_IDENTIFIER = "testIdentifier"; 65 private static final String ANOTHER_TEST_IDENTIFIER = "anotherTestIdentifier"; 66 private static final String TEST_CATEGORY = "testCategory"; 67 private static final String ANOTHER_TEST_CATEGORY = "testAnotherCategory"; 68 private static final String TEST_PACKAGE = "android.content.cts"; 69 private static final String ANOTHER_TEST_PACKAGE = "android.database.cts"; 70 71 @Override setUp()72 protected void setUp() throws Exception { 73 super.setUp(); 74 mIntent = new Intent(); 75 mContext = getContext(); 76 mPm = mContext.getPackageManager(); 77 mComponentName = new ComponentName(mContext, MockActivity.class); 78 mAnotherComponentName = new ComponentName(mContext, "tmp"); 79 } 80 testConstructor()81 public void testConstructor() { 82 mIntent = new Intent(); 83 assertNotNull(mIntent); 84 85 Intent intent = new Intent(); 86 intent.setAction(TEST_ACTION); 87 88 mIntent = new Intent(intent); 89 assertNotNull(mIntent); 90 assertEquals(TEST_ACTION, mIntent.getAction()); 91 92 mIntent = new Intent(TEST_ACTION); 93 assertNotNull(mIntent); 94 assertEquals(TEST_ACTION, mIntent.getAction()); 95 96 mIntent = new Intent(TEST_ACTION, TEST_URI); 97 assertNotNull(mIntent); 98 assertEquals(TEST_ACTION, mIntent.getAction()); 99 assertEquals(TEST_URI, mIntent.getData()); 100 101 mIntent = new Intent(mContext, MockActivity.class); 102 assertNotNull(mIntent); 103 assertEquals(mComponentName, mIntent.getComponent()); 104 105 mIntent = new Intent(TEST_ACTION, TEST_URI, mContext, MockActivity.class); 106 assertNotNull(mIntent); 107 assertEquals(TEST_ACTION, mIntent.getAction()); 108 assertEquals(TEST_URI, mIntent.getData()); 109 assertEquals(mComponentName, mIntent.getComponent()); 110 } 111 testRemoveExtra()112 public void testRemoveExtra() { 113 mIntent = new Intent(); 114 mIntent.putExtra(TEST_EXTRA_NAME, "testvalue"); 115 assertNotNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 116 mIntent.removeExtra(TEST_EXTRA_NAME); 117 assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 118 } 119 testGetCharSequenceExtra()120 public void testGetCharSequenceExtra() { 121 final CharSequence expected = "CharSequencetest"; 122 mIntent.putExtra(TEST_EXTRA_NAME, expected); 123 assertEquals(expected, mIntent.getCharSequenceExtra(TEST_EXTRA_NAME)); 124 } 125 testReadFromParcel()126 public void testReadFromParcel() { 127 mIntent.setAction(TEST_ACTION); 128 mIntent.setData(TEST_URI); 129 mIntent.setType(TEST_TYPE); 130 mIntent.setIdentifier(TEST_IDENTIFIER); 131 mIntent.setFlags(0); 132 mIntent.setComponent(mComponentName); 133 mIntent.addCategory(TEST_CATEGORY); 134 final Parcel parcel = Parcel.obtain(); 135 mIntent.writeToParcel(parcel, 0); 136 parcel.setDataPosition(0); 137 final Intent target = new Intent(); 138 target.readFromParcel(parcel); 139 assertEquals(mIntent.getAction(), target.getAction()); 140 assertEquals(mIntent.getData(), target.getData()); 141 assertEquals(mIntent.getIdentifier(), target.getIdentifier()); 142 assertEquals(mIntent.getFlags(), target.getFlags()); 143 assertEquals(mIntent.getComponent(), target.getComponent()); 144 assertEquals(mIntent.getCategories(), target.getCategories()); 145 assertEquals(mIntent.toURI(), target.toURI()); 146 } 147 testGetParcelableArrayListExtra()148 public void testGetParcelableArrayListExtra() { 149 final ArrayList<Intent> expected = new ArrayList<Intent>(); 150 Intent intent = new Intent(TEST_ACTION); 151 expected.add(intent); 152 153 mIntent.putParcelableArrayListExtra(TEST_EXTRA_NAME, expected); 154 final ArrayList<Intent> target = mIntent.getParcelableArrayListExtra(TEST_EXTRA_NAME); 155 assertEquals(expected.size(), target.size()); 156 assertEquals(expected, target); 157 } 158 testFilterHashCode()159 public void testFilterHashCode() { 160 mIntent.filterHashCode(); 161 } 162 testGetCategories()163 public void testGetCategories() { 164 mIntent.addCategory(TEST_CATEGORY); 165 final Set<String> target = mIntent.getCategories(); 166 assertEquals(TEST_CATEGORY, target.toArray()[0]); 167 } 168 testGetScheme()169 public void testGetScheme() { 170 assertNull(mIntent.getScheme()); 171 mIntent.setData(TEST_URI); 172 assertEquals(TEST_URI.getScheme(), mIntent.getScheme()); 173 } 174 testGetIntegerArrayListExtra()175 public void testGetIntegerArrayListExtra() { 176 final ArrayList<Integer> expected = new ArrayList<Integer>(); 177 expected.add(0); 178 mIntent.putIntegerArrayListExtra(TEST_EXTRA_NAME, expected); 179 assertEquals(expected, mIntent.getIntegerArrayListExtra(TEST_EXTRA_NAME)); 180 } 181 testHasExtra()182 public void testHasExtra() { 183 mIntent = new Intent(); 184 assertFalse(mIntent.hasExtra(TEST_EXTRA_NAME)); 185 mIntent.putExtra(TEST_EXTRA_NAME, "test"); 186 assertTrue(mIntent.hasExtra(TEST_EXTRA_NAME)); 187 } 188 testGetIntArrayExtra()189 public void testGetIntArrayExtra() { 190 final int[] expected = { 1, 2, 3 }; 191 assertNull(mIntent.getIntArrayExtra(TEST_EXTRA_NAME)); 192 mIntent.putExtra(TEST_EXTRA_NAME, expected); 193 assertEquals(expected, mIntent.getIntArrayExtra(TEST_EXTRA_NAME)); 194 } 195 testSetClassName1()196 public void testSetClassName1() { 197 final Intent intent = mIntent.setClassName(mContext, MockActivity.class.getName()); 198 assertEquals(mComponentName, mIntent.getComponent()); 199 assertSame(mIntent, intent); 200 } 201 testSetClassName2()202 public void testSetClassName2() { 203 mIntent.setClassName(mContext.getPackageName(), MockActivity.class.getName()); 204 assertEquals(mComponentName, mIntent.getComponent()); 205 } 206 testGetIntExtra()207 public void testGetIntExtra() { 208 final int expected = 0; 209 mIntent = new Intent(); 210 assertEquals(expected, mIntent.getIntExtra(TEST_EXTRA_NAME, expected)); 211 mIntent.putExtra(TEST_EXTRA_NAME, 100); 212 assertEquals(100, mIntent.getIntExtra(TEST_EXTRA_NAME, 1)); 213 214 } 215 testPutIntegerArrayListExtra()216 public void testPutIntegerArrayListExtra() { 217 final ArrayList<Integer> expected = new ArrayList<Integer>(); 218 expected.add(0); 219 mIntent = new Intent(); 220 mIntent.putIntegerArrayListExtra(TEST_EXTRA_NAME, expected); 221 assertEquals(expected, mIntent.getIntegerArrayListExtra(TEST_EXTRA_NAME)); 222 } 223 testAccessType()224 public void testAccessType() { 225 mIntent.setType(TEST_TYPE); 226 assertEquals(TEST_TYPE, mIntent.getType()); 227 } 228 testGetBundleExtra()229 public void testGetBundleExtra() { 230 final Bundle expected = new Bundle(); 231 expected.putBoolean("testTrue", true); 232 mIntent.putExtras(expected); 233 mIntent.putExtra(TEST_EXTRA_NAME, expected); 234 235 assertEquals(expected, mIntent.getBundleExtra(TEST_EXTRA_NAME)); 236 } 237 testGetCharArrayExtra()238 public void testGetCharArrayExtra() { 239 final char[] expected = { 'a', 'b', 'c' }; 240 mIntent.putExtra(TEST_EXTRA_NAME, expected); 241 final char[] actual= mIntent.getCharArrayExtra(TEST_EXTRA_NAME); 242 assertEquals(expected.length, actual.length); 243 assertEquals(expected[0], actual[0]); 244 assertEquals(expected[1], actual[1]); 245 assertEquals(expected[2], actual[2]); 246 } 247 testGetDoubleArrayExtra()248 public void testGetDoubleArrayExtra() { 249 final double[] expected = { 1d, 2d }; 250 mIntent.putExtra(TEST_EXTRA_NAME, expected); 251 assertEquals(expected, mIntent.getDoubleArrayExtra(TEST_EXTRA_NAME)); 252 } 253 testPutStringArrayListExtra()254 public void testPutStringArrayListExtra() { 255 final ArrayList<String> expected = new ArrayList<String>(); 256 expected.add("testString"); 257 mIntent.putStringArrayListExtra(TEST_EXTRA_NAME, expected); 258 assertEquals(expected, mIntent.getStringArrayListExtra(TEST_EXTRA_NAME)); 259 } 260 testResolveType1()261 public void testResolveType1() { 262 final ContentResolver contentResolver = mContext.getContentResolver(); 263 assertNull(mIntent.resolveType(mContext)); 264 mIntent.setType(TEST_TYPE); 265 assertEquals(TEST_TYPE, mIntent.resolveType(mContext)); 266 mIntent.setType(null); 267 mIntent.setData(TEST_URI); 268 assertEquals(contentResolver.getType(TEST_URI), mIntent.resolveType(mContext)); 269 mIntent.setData(Uri.parse("test")); 270 assertNull(mIntent.resolveType(mContext)); 271 } 272 testResolveType2()273 public void testResolveType2() { 274 final ContentResolver contentResolver = mContext.getContentResolver(); 275 assertNull(mIntent.resolveType(contentResolver)); 276 mIntent.setType(TEST_TYPE); 277 assertEquals(TEST_TYPE, mIntent.resolveType(contentResolver)); 278 mIntent.setType(null); 279 mIntent.setData(TEST_URI); 280 assertEquals(contentResolver.getType(TEST_URI), mIntent.resolveType(contentResolver)); 281 mIntent.setData(Uri.parse("test")); 282 assertNull(mIntent.resolveType(contentResolver)); 283 } 284 testAccessComponent()285 public void testAccessComponent() { 286 mIntent.setComponent(mComponentName); 287 assertEquals(mComponentName, mIntent.getComponent()); 288 } 289 testGetDataString()290 public void testGetDataString() { 291 assertNull(mIntent.getDataString()); 292 mIntent.setData(TEST_URI); 293 assertEquals(TEST_URI.toString(), mIntent.getDataString()); 294 } 295 testGetIdentifer()296 public void testGetIdentifer() { 297 assertNull(mIntent.getIdentifier()); 298 mIntent.setIdentifier(TEST_IDENTIFIER); 299 assertEquals(TEST_IDENTIFIER, mIntent.getIdentifier()); 300 } 301 testHasCategory()302 public void testHasCategory() { 303 assertFalse(mIntent.hasCategory(TEST_CATEGORY)); 304 mIntent.addCategory(TEST_CATEGORY); 305 assertTrue(mIntent.hasCategory(TEST_CATEGORY)); 306 } 307 testGetLongArrayExtra()308 public void testGetLongArrayExtra() { 309 final long[] expected = { 1l, 2l, 3l }; 310 mIntent.putExtra(TEST_EXTRA_NAME, expected); 311 assertEquals(expected, mIntent.getLongArrayExtra(TEST_EXTRA_NAME)); 312 } 313 testParseIntent()314 public void testParseIntent() throws XmlPullParserException, IOException, 315 NameNotFoundException { 316 mIntent = null; 317 XmlResourceParser parser = null; 318 AttributeSet attrs = null; 319 ActivityInfo ai = null; 320 try { 321 mIntent = Intent.parseIntent(mContext.getResources(), parser, attrs); 322 fail("should thow exception!"); 323 } catch (NullPointerException e) { 324 // expected 325 } 326 327 ai = mContext.getPackageManager().getActivityInfo(mComponentName, 328 PackageManager.GET_META_DATA); 329 parser = ai.loadXmlMetaData(mContext.getPackageManager(), "android.app.intent"); 330 331 attrs = Xml.asAttributeSet(parser); 332 int type; 333 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 334 && type != XmlPullParser.START_TAG) { 335 } 336 337 String nodeName = parser.getName(); 338 if (!"alias".equals(nodeName)) { 339 throw new RuntimeException(); 340 } 341 342 int outerDepth = parser.getDepth(); 343 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 344 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 345 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 346 continue; 347 } 348 349 nodeName = parser.getName(); 350 if ("intent".equals(nodeName)) { 351 mIntent = Intent.parseIntent(mContext.getResources(), parser, attrs); 352 } else { 353 XmlUtils.skipCurrentTag(parser); 354 } 355 } 356 357 assertNotNull(mIntent); 358 assertEquals("android.intent.action.MAIN", mIntent.getAction()); 359 assertEquals(Uri.parse("http://www.google.com/"), mIntent.getData()); 360 assertEquals("something/something", mIntent.getType()); 361 assertEquals("testIdentifier", mIntent.getIdentifier()); 362 assertEquals(2, mIntent.getCategories().size()); 363 assertTrue(mIntent.hasCategory("some.category")); 364 assertTrue(mIntent.hasCategory("some.other.category")); 365 assertEquals(1, mIntent.getIntExtra("an_int", 0)); 366 assertEquals(2, mIntent.getIntExtra("another_int", 0)); 367 assertEquals(true, mIntent.getBooleanExtra("an_boolean", false)); 368 assertEquals(false, mIntent.getBooleanExtra("another_boolean", true)); 369 assertEquals(1.1f, mIntent.getFloatExtra("an_float", 0), Float.MIN_NORMAL); 370 assertEquals(2.2f, mIntent.getFloatExtra("another_float", 0), Float.MIN_NORMAL); 371 assertEquals("one", mIntent.getStringExtra("an_string")); 372 assertEquals("two", mIntent.getStringExtra("another_string")); 373 } 374 testSetClass()375 public void testSetClass() { 376 assertNull(mIntent.getComponent()); 377 mIntent.setClass(mContext, MockActivity.class); 378 assertEquals(mComponentName, mIntent.getComponent()); 379 } 380 testResolveTypeIfNeeded()381 public void testResolveTypeIfNeeded() { 382 ContentResolver contentResolver = mContext.getContentResolver(); 383 assertNull(mIntent.resolveTypeIfNeeded(contentResolver)); 384 mIntent.setType(TEST_TYPE); 385 assertEquals(TEST_TYPE, mIntent.resolveTypeIfNeeded(contentResolver)); 386 387 mIntent.setType(null); 388 mIntent.setComponent(mComponentName); 389 assertEquals(null, mIntent.resolveTypeIfNeeded(contentResolver)); 390 391 mIntent.setType(TEST_TYPE); 392 mIntent.setComponent(mComponentName); 393 assertEquals(TEST_TYPE, mIntent.resolveTypeIfNeeded(contentResolver)); 394 395 mIntent.setType(null); 396 mIntent.setData(TEST_URI); 397 assertNull(mIntent.resolveTypeIfNeeded(contentResolver)); 398 } 399 testPutExtra1()400 public void testPutExtra1() { 401 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 402 mIntent.putExtra(TEST_EXTRA_NAME, true); 403 assertTrue(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 404 mIntent.putExtra(TEST_EXTRA_NAME, false); 405 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 406 } 407 testPutExtra2()408 public void testPutExtra2() { 409 final byte expected = Byte.valueOf("1"); 410 mIntent.putExtra(TEST_EXTRA_NAME, expected); 411 assertEquals(expected, mIntent.getByteExtra(TEST_EXTRA_NAME, Byte.valueOf("1"))); 412 } 413 testPutExtra3()414 public void testPutExtra3() { 415 assertEquals('a', mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 416 final char expected = 'a'; 417 mIntent.putExtra(TEST_EXTRA_NAME, expected); 418 assertEquals(expected, mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 419 } 420 testPutExtra4()421 public void testPutExtra4() { 422 final Short expected = Short.valueOf("2"); 423 assertEquals(Short.valueOf("1").shortValue(), mIntent.getShortExtra( 424 TEST_EXTRA_NAME, Short.valueOf("1"))); 425 mIntent.putExtra(TEST_EXTRA_NAME, expected); 426 assertEquals(expected.shortValue(), mIntent.getShortExtra(TEST_EXTRA_NAME, Short.valueOf("1"))); 427 } 428 testPutExtra5()429 public void testPutExtra5() { 430 final int expected = 2; 431 assertEquals(1, mIntent.getIntExtra(TEST_EXTRA_NAME, 1)); 432 mIntent.putExtra(TEST_EXTRA_NAME, expected); 433 assertEquals(expected, mIntent.getIntExtra(TEST_EXTRA_NAME, 1)); 434 } 435 testPutExtra6()436 public void testPutExtra6() { 437 final long expected = 2l; 438 assertEquals(1l, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 439 mIntent.putExtra(TEST_EXTRA_NAME, expected); 440 assertEquals(expected, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 441 } 442 testPutExtra7()443 public void testPutExtra7() { 444 final float expected = 2f; 445 assertEquals(1f, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 446 mIntent.putExtra(TEST_EXTRA_NAME, expected); 447 assertEquals(expected, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 448 } 449 testPutExtra8()450 public void testPutExtra8() { 451 final double expected = 2d; 452 assertEquals(1d, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 453 mIntent.putExtra(TEST_EXTRA_NAME, expected); 454 assertEquals(expected, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 455 } 456 testPutExtra9()457 public void testPutExtra9() { 458 final String expected = "testString"; 459 assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 460 mIntent.putExtra(TEST_EXTRA_NAME, expected); 461 assertEquals(expected, mIntent.getStringExtra(TEST_EXTRA_NAME)); 462 } 463 testPutExtra10()464 public void testPutExtra10() { 465 final CharSequence expected = "testString"; 466 assertNull(mIntent.getCharSequenceExtra(TEST_EXTRA_NAME)); 467 mIntent.putExtra(TEST_EXTRA_NAME, expected); 468 assertEquals(expected, mIntent.getCharSequenceExtra(TEST_EXTRA_NAME)); 469 } 470 testPutExtra11()471 public void testPutExtra11() { 472 final Intent expected = new Intent(TEST_ACTION); 473 mIntent.putExtra(TEST_EXTRA_NAME, expected); 474 assertEquals(expected, mIntent.getParcelableExtra(TEST_EXTRA_NAME)); 475 } 476 testPutExtra12()477 public void testPutExtra12() { 478 final Intent[] expected = { new Intent(TEST_ACTION), new Intent(mContext, MockActivity.class) }; 479 mIntent.putExtra(TEST_EXTRA_NAME, expected); 480 assertEquals(expected, mIntent.getParcelableArrayExtra(TEST_EXTRA_NAME)); 481 } 482 testPutExtra13()483 public void testPutExtra13() { 484 final TestSerializable expected = new TestSerializable(); 485 expected.Name = "testName"; 486 mIntent.putExtra(TEST_EXTRA_NAME, expected); 487 assertEquals(expected, mIntent.getSerializableExtra(TEST_EXTRA_NAME)); 488 TestSerializable target = (TestSerializable) mIntent.getSerializableExtra(TEST_EXTRA_NAME); 489 assertEquals(expected.Name, target.Name); 490 } 491 testPutExtra14()492 public void testPutExtra14() { 493 final boolean[] expected = { true, true, false }; 494 mIntent.putExtra(TEST_EXTRA_NAME, expected); 495 assertEquals(expected, mIntent.getBooleanArrayExtra(TEST_EXTRA_NAME)); 496 } 497 testPutExtra15()498 public void testPutExtra15() { 499 final byte[] expected = TEST_ACTION.getBytes(); 500 mIntent.putExtra(TEST_EXTRA_NAME, expected); 501 assertEquals(expected, mIntent.getByteArrayExtra(TEST_EXTRA_NAME)); 502 } 503 testPutExtra16()504 public void testPutExtra16() { 505 final short[] expected = { 1, 2, 3 }; 506 mIntent.putExtra(TEST_EXTRA_NAME, expected); 507 assertEquals(expected, mIntent.getShortArrayExtra(TEST_EXTRA_NAME)); 508 } 509 testPutExtra17()510 public void testPutExtra17() { 511 final char[] expected = { '1', '2', '3' }; 512 mIntent.putExtra(TEST_EXTRA_NAME, expected); 513 assertEquals(expected, mIntent.getCharArrayExtra(TEST_EXTRA_NAME)); 514 } 515 testPutExtra18()516 public void testPutExtra18() { 517 final int[] expected = { 1, 2, 3 }; 518 mIntent.putExtra(TEST_EXTRA_NAME, expected); 519 assertEquals(expected, mIntent.getIntArrayExtra(TEST_EXTRA_NAME)); 520 } 521 testPutExtra19()522 public void testPutExtra19() { 523 final long[] expected = { 1l, 2l, 3l }; 524 mIntent.putExtra(TEST_EXTRA_NAME, expected); 525 assertEquals(expected, mIntent.getLongArrayExtra(TEST_EXTRA_NAME)); 526 } 527 testPutExtra20()528 public void testPutExtra20() { 529 final float[] expected = { 1f, 2f, 3f }; 530 mIntent.putExtra(TEST_EXTRA_NAME, expected); 531 assertEquals(expected, mIntent.getFloatArrayExtra(TEST_EXTRA_NAME)); 532 } 533 testPutExtra21()534 public void testPutExtra21() { 535 final double[] expected = { 1d, 2d, 3d }; 536 mIntent.putExtra(TEST_EXTRA_NAME, expected); 537 assertEquals(expected, mIntent.getDoubleArrayExtra(TEST_EXTRA_NAME)); 538 } 539 testPutExtra22()540 public void testPutExtra22() { 541 final String[] expected = { "1d", "2d", "3d" }; 542 mIntent.putExtra(TEST_EXTRA_NAME, expected); 543 assertEquals(expected, mIntent.getStringArrayExtra(TEST_EXTRA_NAME)); 544 } 545 testPutExtra23()546 public void testPutExtra23() { 547 final Bundle expected = new Bundle(); 548 expected.putString("key", "value"); 549 mIntent.putExtra(TEST_EXTRA_NAME, expected); 550 assertEquals(expected, mIntent.getBundleExtra(TEST_EXTRA_NAME)); 551 } 552 553 @SuppressWarnings("deprecation") testPutExtra24()554 public void testPutExtra24() { 555 final IBinder expected = ServiceManager.getService("activity"); 556 mIntent.putExtra(TEST_EXTRA_NAME, expected); 557 assertEquals(expected, mIntent.getIBinderExtra(TEST_EXTRA_NAME)); 558 } 559 testAddCategory()560 public void testAddCategory() { 561 assertFalse(mIntent.hasCategory(TEST_CATEGORY)); 562 mIntent.addCategory(TEST_CATEGORY); 563 assertTrue(mIntent.hasCategory(TEST_CATEGORY)); 564 } 565 testPutParcelableArrayListExtra()566 public void testPutParcelableArrayListExtra() { 567 ArrayList<Intent> expected = new ArrayList<Intent>(); 568 mIntent.putExtra(TEST_EXTRA_NAME, expected); 569 assertEquals(expected, mIntent.getParcelableArrayListExtra(TEST_EXTRA_NAME)); 570 } 571 testToString()572 public void testToString() { 573 assertNotNull(mIntent.toString()); 574 } 575 testAccessData()576 public void testAccessData() { 577 mIntent.setData(TEST_URI); 578 assertEquals(TEST_URI, mIntent.getData()); 579 } 580 testSetExtrasClassLoader()581 public void testSetExtrasClassLoader() { 582 } 583 testGetStringArrayListExtra()584 public void testGetStringArrayListExtra() { 585 final ArrayList<String> expected = new ArrayList<String>(); 586 expected.add("testString"); 587 mIntent.putStringArrayListExtra(TEST_EXTRA_NAME, expected); 588 assertEquals(expected, mIntent.getStringArrayListExtra(TEST_EXTRA_NAME)); 589 } 590 testGetCharSequenceArrayListExtra()591 public void testGetCharSequenceArrayListExtra() { 592 final ArrayList<CharSequence> expected = new ArrayList<CharSequence>(); 593 expected.add("testCharSequence"); 594 mIntent.putCharSequenceArrayListExtra(TEST_EXTRA_NAME, expected); 595 assertEquals(expected, mIntent.getCharSequenceArrayListExtra(TEST_EXTRA_NAME)); 596 } 597 testResolveActivityInfo()598 public void testResolveActivityInfo() throws NameNotFoundException { 599 final PackageManager pm = mContext.getPackageManager(); 600 assertEquals(null, mIntent.resolveActivityInfo(pm, 1)); 601 mIntent.setComponent(mComponentName); 602 ActivityInfo target = null; 603 604 target = pm.getActivityInfo(mComponentName, 1); 605 assertEquals(target.targetActivity, mIntent.resolveActivityInfo(pm, 1).targetActivity); 606 } 607 testGetParcelableExtra()608 public void testGetParcelableExtra() { 609 final Intent expected = new Intent(TEST_ACTION); 610 mIntent.putExtra(TEST_EXTRA_NAME, expected); 611 assertEquals(expected, mIntent.getParcelableExtra(TEST_EXTRA_NAME)); 612 } 613 testAccessAction()614 public void testAccessAction() { 615 mIntent.setAction(TEST_ACTION); 616 assertEquals(TEST_ACTION, mIntent.getAction()); 617 } 618 testAddFlags()619 public void testAddFlags() { 620 final int flag = 1; 621 int expected = 0; 622 mIntent.addFlags(flag); 623 expected |= flag; 624 assertEquals(expected, mIntent.getFlags()); 625 } 626 testDescribeContents()627 public void testDescribeContents() { 628 final int expected = 0; 629 assertEquals(expected, mIntent.describeContents()); 630 mIntent.putExtra(TEST_EXTRA_NAME, "test"); 631 assertEquals(mIntent.getExtras().describeContents(), mIntent.describeContents()); 632 } 633 testGetShortExtra()634 public void testGetShortExtra() { 635 636 final Short expected = Short.valueOf("2"); 637 assertEquals(Short.valueOf("1").shortValue(), mIntent.getShortExtra( 638 TEST_EXTRA_NAME, Short.valueOf("1"))); 639 mIntent.putExtra(TEST_EXTRA_NAME, expected); 640 assertEquals(expected.shortValue(), mIntent.getShortExtra(TEST_EXTRA_NAME, Short.valueOf("1"))); 641 } 642 testClone()643 public void testClone() { 644 mIntent.setAction(TEST_ACTION); 645 mIntent.setClass(mContext, MockActivity.class); 646 mIntent.setComponent(mComponentName); 647 mIntent.setDataAndType(TEST_URI, TEST_TYPE); 648 mIntent.setIdentifier(TEST_IDENTIFIER); 649 mIntent.addCategory(TEST_CATEGORY); 650 final String key = "testkey"; 651 final String excepted = "testValue"; 652 mIntent.putExtra(key, excepted); 653 Intent actual = (Intent) mIntent.clone(); 654 assertEquals(mComponentName, actual.getComponent()); 655 assertEquals(TEST_ACTION, actual.getAction()); 656 assertEquals(mComponentName, actual.getComponent()); 657 assertEquals(TEST_URI, actual.getData()); 658 assertEquals(TEST_TYPE, actual.getType()); 659 assertEquals(TEST_IDENTIFIER, actual.getIdentifier()); 660 assertEquals(TEST_CATEGORY, (String) (actual.getCategories().toArray()[0])); 661 assertEquals(excepted, actual.getStringExtra(key)); 662 } 663 testGetDoubleExtra()664 public void testGetDoubleExtra() { 665 final double expected = 2d; 666 assertEquals(1d, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 667 mIntent.putExtra(TEST_EXTRA_NAME, expected); 668 assertEquals(expected, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 669 } 670 testCloneFilter()671 public void testCloneFilter() { 672 mIntent.setAction(TEST_ACTION); 673 mIntent.setClass(mContext, MockActivity.class); 674 mIntent.setComponent(mComponentName); 675 mIntent.setDataAndType(TEST_URI, TEST_TYPE); 676 mIntent.setIdentifier(TEST_IDENTIFIER); 677 mIntent.addCategory(TEST_CATEGORY); 678 final String key = "testkey"; 679 mIntent.putExtra(key, "testValue"); 680 Intent actual = mIntent.cloneFilter(); 681 assertEquals(mComponentName, actual.getComponent()); 682 assertEquals(TEST_ACTION, actual.getAction()); 683 assertEquals(mComponentName, actual.getComponent()); 684 assertEquals(TEST_URI, actual.getData()); 685 assertEquals(TEST_TYPE, actual.getType()); 686 assertEquals(TEST_IDENTIFIER, actual.getIdentifier()); 687 assertEquals(TEST_CATEGORY, (String) (actual.getCategories().toArray()[0])); 688 assertNull(actual.getStringExtra(key)); 689 } 690 testGetIntentOld()691 public void testGetIntentOld() throws URISyntaxException { 692 String uri = "test"; 693 mIntent = Intent.getIntentOld(uri); 694 assertEquals(Intent.ACTION_VIEW, mIntent.getAction()); 695 696 mIntent = null; 697 try { 698 uri = "test#action(test)categories(test)type(mtype)launchFlags(test)extras(test"; 699 mIntent = Intent.getIntentOld(uri); 700 fail("should throw URISyntaxException."); 701 } catch (Exception e) { 702 // expected 703 } 704 705 final String compnent = 706 "component(" + mContext.getPackageName() + "!" + MockActivity.class.getName() + ")"; 707 uri = "testdata#action(test)categories(test!test2)type(mtype)launchFlags(5)" + compnent 708 + "extras(Stest=testString!btestbyte=1!" 709 + "Btestboolean=true!ctestchar=a!dtestdouble=1d!" 710 + "itestint=1!ltestlong=1!stestshort=1!ftestfloat=1f)"; 711 mIntent = Intent.getIntentOld(uri); 712 assertEquals("test", mIntent.getAction()); 713 assertEquals("testdata", mIntent.getData().toString()); 714 assertEquals(mComponentName, mIntent.getComponent()); 715 assertEquals("test", (String) (mIntent.getCategories().toArray()[0])); 716 assertEquals("mtype", mIntent.getType()); 717 assertEquals(4, mIntent.getFlags()); 718 assertEquals("testString", mIntent.getStringExtra("test")); 719 assertTrue(mIntent.getBooleanExtra("testboolean", false)); 720 final byte b = 1; 721 final byte defaulttByte = 2; 722 assertEquals(b, mIntent.getByteExtra("testbyte", defaulttByte)); 723 assertEquals('a', mIntent.getCharExtra("testchar", 'b')); 724 final float testFloat = 1f; 725 assertEquals(testFloat, mIntent.getFloatExtra("testfloat", 2f)); 726 final double testDouble = 1d; 727 assertEquals(testDouble, mIntent.getDoubleExtra("testdouble", 2d)); 728 729 final long testLong = 1; 730 assertEquals(testLong, mIntent.getLongExtra("testlong", 2l)); 731 732 final short testShort = 1; 733 final short defaultShort = 2; 734 assertEquals(testShort, mIntent.getShortExtra("testshort", defaultShort)); 735 assertEquals(1, mIntent.getIntExtra("testint", 2)); 736 } 737 testGetParcelableArrayExtra()738 public void testGetParcelableArrayExtra() { 739 final Intent[] expected = { new Intent(TEST_ACTION), new Intent(mContext, MockActivity.class) }; 740 mIntent.putExtra(TEST_EXTRA_NAME, expected); 741 assertEquals(expected, mIntent.getParcelableArrayExtra(TEST_EXTRA_NAME)); 742 } 743 testResolveActivityEmpty()744 public void testResolveActivityEmpty() { 745 final Intent emptyIntent = new Intent(); 746 747 // Empty intent shouldn't resolve to anything 748 final ComponentName target = emptyIntent.resolveActivity(mPm); 749 assertNull(target); 750 } 751 testResolveActivitySingleMatch()752 public void testResolveActivitySingleMatch() { 753 final Intent intent = new Intent("android.content.cts.action.TEST_ACTION"); 754 intent.addCategory("android.content.cts.category.TEST_CATEGORY"); 755 756 // Should only have one activity responding to narrow category 757 final ComponentName target = intent.resolveActivity(mPm); 758 assertEquals("android.content.cts", target.getPackageName()); 759 assertEquals("android.content.cts.MockActivity", target.getClassName()); 760 } 761 testResolveActivityShortcutMatch()762 public void testResolveActivityShortcutMatch() { 763 final Intent intent = new Intent("android.content.cts.action.TEST_ACTION"); 764 intent.setComponent( 765 new ComponentName("android.content.cts", "android.content.cts.MockActivity2")); 766 767 // Multiple activities match, but we asked for explicit component 768 final ComponentName target = intent.resolveActivity(mPm); 769 assertEquals("android.content.cts", target.getPackageName()); 770 assertEquals("android.content.cts.MockActivity2", target.getClassName()); 771 } 772 773 @AppModeFull testResolveActivityMultipleMatch()774 public void testResolveActivityMultipleMatch() { 775 final Intent intent = new Intent("android.content.cts.action.TEST_ACTION"); 776 777 // Should have multiple activities, resulting in resolver dialog 778 final ComponentName target = intent.resolveActivity(mPm); 779 final String pkgName = target.getPackageName(); 780 assertFalse("android.content.cts".equals(pkgName)); 781 782 // Whoever they are must be able to set preferred activities 783 if (!"android".equals(pkgName)) { 784 if (mPm.checkPermission(android.Manifest.permission.SET_PREFERRED_APPLICATIONS, pkgName) 785 != PackageManager.PERMISSION_GRANTED) { 786 fail("Resolved target " + target 787 + " doesn't have SET_PREFERRED_APPLICATIONS permission"); 788 } 789 } 790 } 791 testGetCharExtra()792 public void testGetCharExtra() { 793 assertEquals('a', mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 794 final char expected = 'b'; 795 mIntent.putExtra(TEST_EXTRA_NAME, expected); 796 assertEquals(expected, mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 797 } 798 testGetIntent()799 public void testGetIntent() throws URISyntaxException { 800 mIntent = Intent.getIntent("test#"); 801 assertEquals(Intent.ACTION_VIEW, mIntent.getAction()); 802 803 try { 804 String uri = "#Intent;action=android.content.IntentTest_test;" 805 + "category=testCategory;type=testtype;identifier=testident;launchFlags=0x1;" 806 + "component=com.android/.app.MockActivity;K.testExtraName=1;end"; 807 mIntent = Intent.getIntent(uri); 808 fail("should throw URISyntaxException."); 809 } catch (Exception e) { 810 // expected 811 } 812 mIntent = new Intent(); 813 814 String uri = mIntent.toURI(); 815 Intent target = Intent.getIntent(uri); 816 assertEquals(Intent.ACTION_VIEW, target.getAction()); 817 818 mIntent.setAction(TEST_ACTION); 819 uri = mIntent.toURI(); 820 target = Intent.getIntent(uri); 821 assertEquals(TEST_ACTION, target.getAction()); 822 823 mIntent.setData(TEST_URI); 824 uri = mIntent.toURI(); 825 target = Intent.getIntent(uri); 826 assertEquals(TEST_URI, target.getData()); 827 828 mIntent.setComponent(mComponentName); 829 uri = mIntent.toURI(); 830 target = Intent.getIntent(uri); 831 assertEquals(mComponentName, target.getComponent()); 832 833 mIntent.addCategory(TEST_CATEGORY); 834 uri = mIntent.toURI(); 835 target = Intent.getIntent(uri); 836 assertEquals(1, target.getCategories().size()); 837 assertEquals(TEST_CATEGORY, (String) (target.getCategories().toArray()[0])); 838 839 mIntent.setType(TEST_TYPE); 840 uri = mIntent.toURI(); 841 target = Intent.getIntent(uri); 842 assertEquals(TEST_TYPE, target.getType()); 843 844 mIntent.setIdentifier(TEST_IDENTIFIER); 845 uri = mIntent.toURI(); 846 target = Intent.getIntent(uri); 847 assertEquals(TEST_IDENTIFIER, target.getIdentifier()); 848 849 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT 850 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION 851 | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION 852 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); 853 uri = mIntent.toURI(); 854 target = Intent.getIntent(uri); 855 assertEquals(Intent.FLAG_ACTIVITY_NEW_DOCUMENT, target.getFlags()); 856 857 String stringValue = "testString"; 858 mIntent.putExtra(TEST_EXTRA_NAME, stringValue); 859 uri = mIntent.toURI(); 860 target = Intent.getIntent(uri); 861 assertEquals(stringValue, target.getStringExtra(TEST_EXTRA_NAME)); 862 863 mIntent.putExtra(TEST_EXTRA_NAME, true); 864 uri = mIntent.toURI(); 865 target = Intent.getIntent(uri); 866 assertTrue(target.getBooleanExtra(TEST_EXTRA_NAME, false)); 867 868 final byte b = 1; 869 mIntent.putExtra(TEST_EXTRA_NAME, b); 870 871 uri = mIntent.toURI(); 872 target = Intent.getIntent(uri); 873 final byte defaulttByte = 2; 874 assertEquals(b, target.getByteExtra(TEST_EXTRA_NAME, defaulttByte)); 875 876 final char testChar = 'a'; 877 mIntent.putExtra(TEST_EXTRA_NAME, testChar); 878 uri = mIntent.toURI(); 879 target = Intent.getIntent(uri); 880 assertEquals(testChar, target.getCharExtra(TEST_EXTRA_NAME, 'b')); 881 882 final double testDouble = 1; 883 mIntent.putExtra(TEST_EXTRA_NAME, testDouble); 884 uri = mIntent.toURI(); 885 target = Intent.getIntent(uri); 886 assertEquals(testDouble, target.getDoubleExtra(TEST_EXTRA_NAME, 2)); 887 888 final int testInt = 1; 889 mIntent.putExtra(TEST_EXTRA_NAME, testInt); 890 uri = mIntent.toURI(); 891 target = Intent.getIntent(uri); 892 assertEquals(testInt, target.getIntExtra(TEST_EXTRA_NAME, 2)); 893 894 final long testLong = 1l; 895 mIntent.putExtra(TEST_EXTRA_NAME, testLong); 896 uri = mIntent.toURI(); 897 target = Intent.getIntent(uri); 898 assertEquals(testLong, target.getLongExtra(TEST_EXTRA_NAME, 2l)); 899 900 final short testShort = 1; 901 final short defaultShort = 2; 902 mIntent.putExtra(TEST_EXTRA_NAME, testShort); 903 uri = mIntent.toURI(); 904 target = Intent.getIntent(uri); 905 assertEquals(testShort, target.getShortExtra(TEST_EXTRA_NAME, defaultShort)); 906 } 907 testToURI()908 public void testToURI() { 909 mIntent = new Intent(); 910 assertEquals("", mIntent.toURI()); 911 912 mIntent.setData(TEST_URI); 913 assertTrue(mIntent.toURI().indexOf(TEST_URI.toString()) != -1); 914 915 mIntent.setAction(TEST_ACTION); 916 assertTrue(mIntent.toURI().indexOf("action=" + TEST_ACTION) != -1); 917 918 mIntent.addCategory(TEST_CATEGORY); 919 assertTrue(mIntent.toURI().indexOf("category=") != -1); 920 921 mIntent.setType(TEST_TYPE); 922 assertTrue(mIntent.toURI().indexOf("type=" + TEST_TYPE) != -1); 923 924 mIntent.setIdentifier(TEST_IDENTIFIER); 925 assertTrue(mIntent.toURI().indexOf("identifier=" + TEST_IDENTIFIER) != -1); 926 927 mIntent.setFlags(1); 928 assertFalse(mIntent.toURI().indexOf("launchFlags=" + Integer.toHexString(1)) != -1); 929 930 mIntent.setComponent(mComponentName); 931 assertTrue(mIntent.toURI().indexOf( 932 "component=" + mComponentName.flattenToShortString()) != -1); 933 934 final String stringValue = "testString"; 935 mIntent.putExtra(TEST_EXTRA_NAME, stringValue); 936 937 assertTrue(mIntent.toURI().indexOf(getString("S", TEST_EXTRA_NAME, stringValue)) != -1); 938 939 mIntent.putExtra(TEST_EXTRA_NAME, true); 940 941 assertTrue(mIntent.toURI().indexOf(getString("B", TEST_EXTRA_NAME, true)) != -1); 942 943 final byte b = 1; 944 mIntent.putExtra(TEST_EXTRA_NAME, b); 945 assertTrue(mIntent.toURI().indexOf(getString("b", TEST_EXTRA_NAME, b)) != -1); 946 947 final Character testChar = 'a'; 948 mIntent.putExtra(TEST_EXTRA_NAME, testChar); 949 950 assertTrue(mIntent.toURI().indexOf(getString("c", TEST_EXTRA_NAME, testChar)) != -1); 951 952 final double testDouble = 1; 953 mIntent.putExtra(TEST_EXTRA_NAME, testDouble); 954 assertTrue(mIntent.toURI().indexOf(getString("d", TEST_EXTRA_NAME, testDouble)) != -1); 955 956 final int testInt = 1; 957 mIntent.putExtra(TEST_EXTRA_NAME, testInt); 958 assertTrue(mIntent.toURI().indexOf(getString("i", TEST_EXTRA_NAME, testInt)) != -1); 959 960 final long testLong = 1l; 961 mIntent.putExtra(TEST_EXTRA_NAME, testLong); 962 assertTrue(mIntent.toURI().indexOf(getString("l", TEST_EXTRA_NAME, testLong)) != -1); 963 final short testShort = 1; 964 mIntent.putExtra(TEST_EXTRA_NAME, testShort); 965 assertTrue(mIntent.toURI().indexOf(getString("s", TEST_EXTRA_NAME, testShort)) != -1); 966 assertTrue(mIntent.toURI().indexOf("end") != -1); 967 } 968 getString(String entryType, String key, Object value)969 private String getString(String entryType, String key, Object value) { 970 StringBuilder uri = new StringBuilder(); 971 uri.append(entryType); 972 uri.append('.'); 973 uri.append(Uri.encode(key)); 974 uri.append('='); 975 uri.append(Uri.encode(value.toString())); 976 return uri.toString(); 977 } 978 makeSelector(Intent baseIntent, Intent selectorIntent)979 static Intent makeSelector(Intent baseIntent, Intent selectorIntent) { 980 baseIntent.setSelector(selectorIntent); 981 return baseIntent; 982 } 983 testUris()984 public void testUris() { 985 checkIntentUri( 986 "intent:#Intent;action=android.test.FOO;end", 987 null, 988 new Intent().setAction("android.test.FOO")); 989 checkIntentUri( 990 "intent:#Intent;category=android.test.FOO;end", 991 null, 992 new Intent().setAction(Intent.ACTION_VIEW).addCategory("android.test.FOO")); 993 checkIntentUri( 994 "intent:#Intent;action=android.test.FOO;launchFlags=0x20;end", 995 null, 996 new Intent().setAction("android.test.FOO").setFlags(0x20)); 997 checkIntentUri( 998 "intent://www.example.com/blah#Intent;scheme=http;end", 999 null, 1000 new Intent().setAction(Intent.ACTION_VIEW) 1001 .setData(Uri.parse("http://www.example.com/blah"))); 1002 checkIntentUri( 1003 "intent://www.example.com/blah#Intent;scheme=http;component=com.exfoo/com.argh.Bar;end", 1004 null, 1005 new Intent().setAction(Intent.ACTION_VIEW) 1006 .setData(Uri.parse("http://www.example.com/blah")) 1007 .setComponent(new ComponentName("com.exfoo", "com.argh.Bar"))); 1008 checkIntentUri( 1009 "intent://www.example.com/blah#fragment#Intent;scheme=http;end", 1010 null, 1011 new Intent().setAction(Intent.ACTION_VIEW) 1012 .setData(Uri.parse("http://www.example.com/blah#fragment"))); 1013 checkIntentUri( 1014 "intent://www.example.com/blah#Intent;scheme=http;action=android.test.foo;end", 1015 null, 1016 new Intent().setAction("android.test.foo") 1017 .setData(Uri.parse("http://www.example.com/blah"))); 1018 checkIntentUri( 1019 "intent:foo#Intent;scheme=mailto;type=image/foo;end", 1020 null, 1021 new Intent().setAction(Intent.ACTION_VIEW) 1022 .setDataAndType(Uri.parse("mailto:foo"), "image/foo")); 1023 checkIntentUri( 1024 "intent:foo#Intent;scheme=mailto;S.string=text;end", 1025 null, 1026 new Intent().setAction(Intent.ACTION_VIEW) 1027 .setData(Uri.parse("mailto:foo")) 1028 .putExtra("string", "text")); 1029 checkIntentUri( 1030 "intent:#Intent;action=android.test.FOO;S.string=text;end", 1031 null, 1032 new Intent().setAction("android.test.FOO").putExtra("string", "text")); 1033 checkIntentUri( 1034 "intent:foo#Intent;scheme=mailto;i.int=1000;end", 1035 null, 1036 new Intent().setAction(Intent.ACTION_VIEW) 1037 .setData(Uri.parse("mailto:foo")) 1038 .putExtra("int", 1000)); 1039 checkIntentUri( 1040 "intent:foo#Intent;scheme=mailto;l.long=1000;end", 1041 null, 1042 new Intent().setAction(Intent.ACTION_VIEW) 1043 .setData(Uri.parse("mailto:foo")) 1044 .putExtra("long", (long) 1000)); 1045 checkIntentUri( 1046 "intent:foo#Intent;scheme=mailto;B.boolean=true;end", 1047 null, 1048 new Intent().setAction(Intent.ACTION_VIEW) 1049 .setData(Uri.parse("mailto:foo")) 1050 .putExtra("boolean", true)); 1051 checkIntentUri( 1052 "intent:foo#Intent;scheme=mailto;f.float=10.4;end", 1053 null, 1054 new Intent().setAction(Intent.ACTION_VIEW) 1055 .setData(Uri.parse("mailto:foo")) 1056 .putExtra("float", 10.4f)); 1057 checkIntentUri( 1058 "intent:foo#Intent;scheme=mailto;d.double=10.4;end", 1059 null, 1060 new Intent().setAction(Intent.ACTION_VIEW) 1061 .setData(Uri.parse("mailto:foo")) 1062 .putExtra("double", (double) 10.4)); 1063 checkIntentUri( 1064 "intent:#Intent;S.string=text;i.int=1000;l.long=1000;B.boolean=true;f.float=10.4;end", 1065 null, 1066 new Intent().setAction(Intent.ACTION_VIEW).putExtra("string", "text") 1067 .putExtra("int", 1000).putExtra("long", (long) 1000) 1068 .putExtra("boolean", true).putExtra("float", 10.4f)); 1069 checkIntentUri( 1070 "intent:foo#Intent;scheme=mailto;SEL;scheme=foobar;action=android.test.FOO;end", 1071 null, 1072 makeSelector(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("mailto:foo")), 1073 new Intent("android.test.FOO").setData(Uri.parse("foobar:")))); 1074 checkIntentUri( 1075 "intent:foo#Intent;scheme=mailto;SEL;action=android.test.FOO;package=com.myapp;end", 1076 null, 1077 makeSelector(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("mailto:foo")), 1078 new Intent("android.test.FOO").setPackage("com.myapp"))); 1079 checkIntentUri( 1080 "intent:foo#Intent;scheme=mailto;SEL;action=android.test.FOO;component=com.exfoo/com.argh.Bar;end", 1081 null, 1082 makeSelector(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("mailto:foo")), 1083 new Intent("android.test.FOO") 1084 .setComponent(new ComponentName("com.exfoo", "com.argh.Bar")))); 1085 1086 checkIntentUri( 1087 "intent:#Intent;action=android.test.FOO;package=com.myapp;end", 1088 "android-app://com.myapp#Intent;action=android.test.FOO;end", 1089 new Intent().setAction("android.test.FOO").setPackage("com.myapp")); 1090 checkIntentUri( 1091 "intent:#Intent;action=android.intent.action.MAIN;package=com.myapp;end", 1092 "android-app://com.myapp", 1093 new Intent().setAction(Intent.ACTION_MAIN).setPackage("com.myapp")); 1094 checkIntentUri( 1095 "intent:#Intent;package=com.myapp;end", 1096 "android-app://com.myapp#Intent;action=android.intent.action.VIEW;end", 1097 new Intent().setAction(Intent.ACTION_VIEW).setPackage("com.myapp")); 1098 checkIntentUri( 1099 "intent:#Intent;category=android.test.FOO;package=com.myapp;end", 1100 "android-app://com.myapp#Intent;action=android.intent.action.VIEW;category=android.test.FOO;end", 1101 new Intent().setAction(Intent.ACTION_VIEW).addCategory("android.test.FOO") 1102 .setPackage("com.myapp")); 1103 checkIntentUri( 1104 "intent:#Intent;action=android.test.FOO;launchFlags=0x20;package=com.myapp;end", 1105 "android-app://com.myapp#Intent;action=android.test.FOO;launchFlags=0x20;end", 1106 new Intent().setAction("android.test.FOO").setFlags(0x20) 1107 .setPackage("com.myapp")); 1108 checkIntentUri( 1109 "intent://www.example.com/blah#Intent;scheme=http;package=com.myapp;end", 1110 "android-app://com.myapp/http/www.example.com/blah", 1111 new Intent().setAction(Intent.ACTION_VIEW) 1112 .setData(Uri.parse("http://www.example.com/blah")) 1113 .setPackage("com.myapp")); 1114 checkIntentUri( 1115 "intent://www.example.com/blah#Intent;scheme=http;package=com.myapp;component=com.exfoo/com.argh.Bar;end", 1116 "android-app://com.myapp/http/www.example.com/blah#Intent;component=com.exfoo/com.argh.Bar;end", 1117 new Intent().setAction(Intent.ACTION_VIEW) 1118 .setData(Uri.parse("http://www.example.com/blah")) 1119 .setComponent(new ComponentName("com.exfoo", "com.argh.Bar")) 1120 .setPackage("com.myapp")); 1121 checkIntentUri( 1122 "intent://www.example.com/blah#fragment#Intent;scheme=http;package=com.myapp;end", 1123 "android-app://com.myapp/http/www.example.com/blah#fragment", 1124 new Intent().setAction(Intent.ACTION_VIEW) 1125 .setData(Uri.parse("http://www.example.com/blah#fragment")) 1126 .setPackage("com.myapp")); 1127 checkIntentUri( 1128 "intent://www.example.com/blah#fragment#Intent;scheme=http;action=android.test.FOO;package=com.myapp;end", 1129 "android-app://com.myapp/http/www.example.com/blah#fragment#Intent;action=android.test.FOO;end", 1130 new Intent().setAction("android.test.FOO") 1131 .setData(Uri.parse("http://www.example.com/blah#fragment")) 1132 .setPackage("com.myapp")); 1133 checkIntentUri( 1134 "intent://www.example.com/blah#Intent;scheme=http;package=com.myapp;end", 1135 "android-app://com.myapp/http/www.example.com/blah", 1136 new Intent().setAction(Intent.ACTION_VIEW) 1137 .setData(Uri.parse("http://www.example.com/blah")) 1138 .setPackage("com.myapp")); 1139 checkIntentUri( 1140 "intent:#Intent;scheme=mailto;type=image/foo;package=com.myapp;end", 1141 "android-app://com.myapp/mailto#Intent;type=image/foo;end", 1142 new Intent().setAction(Intent.ACTION_VIEW) 1143 .setDataAndType(Uri.parse("mailto:"), "image/foo") 1144 .setPackage("com.myapp")); 1145 checkIntentUri( 1146 "intent:#Intent;scheme=mailto;package=com.myapp;S.string=text;end", 1147 "android-app://com.myapp/mailto#Intent;S.string=text;end", 1148 new Intent().setAction(Intent.ACTION_VIEW).putExtra("string", "text") 1149 .setData(Uri.parse("mailto:")).setPackage("com.myapp")); 1150 checkIntentUri( 1151 "intent:#Intent;action=android.test.FOO;package=com.myapp;S.string=text;end", 1152 "android-app://com.myapp#Intent;action=android.test.FOO;S.string=text;end", 1153 new Intent().setAction("android.test.FOO").putExtra("string", "text") 1154 .setPackage("com.myapp")); 1155 checkIntentUri( 1156 "intent:#Intent;scheme=mailto;package=com.myapp;i.int=1000;end", 1157 "android-app://com.myapp/mailto#Intent;i.int=1000;end", 1158 new Intent().setAction(Intent.ACTION_VIEW).putExtra("int", 1000) 1159 .setData(Uri.parse("mailto:")).setPackage("com.myapp")); 1160 checkIntentUri( 1161 "intent:#Intent;scheme=mailto;package=com.myapp;l.long=1000;end", 1162 "android-app://com.myapp/mailto#Intent;l.long=1000;end", 1163 new Intent().setAction(Intent.ACTION_VIEW).putExtra("long", (long) 1000) 1164 .setData(Uri.parse("mailto:")).setPackage("com.myapp")); 1165 checkIntentUri( 1166 "intent:#Intent;scheme=mailto;package=com.myapp;B.boolean=true;end", 1167 "android-app://com.myapp/mailto#Intent;B.boolean=true;end", 1168 new Intent().setAction(Intent.ACTION_VIEW).putExtra("boolean", true) 1169 .setData(Uri.parse("mailto:")).setPackage("com.myapp")); 1170 checkIntentUri( 1171 "intent:#Intent;scheme=mailto;package=com.myapp;f.float=10.4;end", 1172 "android-app://com.myapp/mailto#Intent;f.float=10.4;end", 1173 new Intent().setAction(Intent.ACTION_VIEW).putExtra("float", 10.4f) 1174 .setData(Uri.parse("mailto:")).setPackage("com.myapp")); 1175 checkIntentUri( 1176 "intent:#Intent;scheme=mailto;package=com.myapp;d.double=10.4;end", 1177 "android-app://com.myapp/mailto#Intent;d.double=10.4;end", 1178 new Intent().setAction(Intent.ACTION_VIEW).putExtra("double", (double) 10.4) 1179 .setData(Uri.parse("mailto:")).setPackage("com.myapp")); 1180 checkIntentUri( 1181 "intent:#Intent;package=com.myapp;S.string=text;i.int=1000;l.long=1000;B.boolean=true;f.float=10.4;end", 1182 "android-app://com.myapp#Intent;action=android.intent.action.VIEW;S.string=text;i.int=1000;l.long=1000;B.boolean=true;f.float=10.4;end", 1183 new Intent().setAction(Intent.ACTION_VIEW).putExtra("string", "text") 1184 .putExtra("int", 1000).putExtra("long", (long) 1000) 1185 .putExtra("boolean", true).putExtra("float", 10.4f) 1186 .setPackage("com.myapp")); 1187 checkIntentUri( 1188 "intent://example.org/db?123#Intent;scheme=z39.50r;end", 1189 null, 1190 new Intent().setAction(Intent.ACTION_VIEW) 1191 .setData(Uri.parse("z39.50r://example.org/db?123"))); 1192 } 1193 compareIntents(Intent expected, Intent actual)1194 private boolean compareIntents(Intent expected, Intent actual) { 1195 if (!Objects.equals(expected.getAction(), actual.getAction())) { 1196 return false; 1197 } 1198 if (!Objects.equals(expected.getData(), actual.getData())) { 1199 return false; 1200 } 1201 if (!Objects.equals(expected.getType(), actual.getType())) { 1202 return false; 1203 } 1204 if (!Objects.equals(expected.getIdentifier(), actual.getIdentifier())) { 1205 return false; 1206 } 1207 if (!Objects.equals(expected.getPackage(), actual.getPackage())) { 1208 return false; 1209 } 1210 if (!Objects.equals(expected.getComponent(), actual.getComponent())) { 1211 return false; 1212 } 1213 if (expected.getFlags() != actual.getFlags()) { 1214 return false; 1215 } 1216 Set<String> expectedCat = expected.getCategories(); 1217 Set<String> actualCat = actual.getCategories(); 1218 if (expectedCat != actualCat) { 1219 if (expectedCat == null || actualCat == null) { 1220 return false; 1221 } 1222 for (String cat : expectedCat) { 1223 if (!actual.hasCategory(cat)) { 1224 return false; 1225 } 1226 } 1227 for (String cat : actualCat) { 1228 if (!expected.hasCategory(cat)) { 1229 return false; 1230 } 1231 } 1232 } 1233 Bundle extras1 = expected.getExtras(); 1234 Bundle extras2 = actual.getExtras(); 1235 if (extras1 != extras2) { 1236 if (extras1 == null || extras2 == null) { 1237 return false; 1238 } 1239 for (String key : extras1.keySet()) { 1240 if (!Objects.equals(extras1.get(key), extras2.get(key))) { 1241 return false; 1242 } 1243 } 1244 for (String key : extras2.keySet()) { 1245 if (!Objects.equals(extras1.get(key), extras2.get(key))) { 1246 return false; 1247 } 1248 } 1249 } 1250 return true; 1251 } 1252 assertEqualsIntent(String msg, Intent expected, Intent actual)1253 private void assertEqualsIntent(String msg, Intent expected, Intent actual) { 1254 if (!compareIntents(expected, actual)) { 1255 failNotEquals(msg, expected, actual); 1256 } 1257 Intent expectedSel = expected.getSelector(); 1258 Intent actualSel = actual.getSelector(); 1259 if (expectedSel != actualSel) { 1260 if (expectedSel == null || actualSel == null) { 1261 failNotEquals(msg, expected, actual); 1262 } 1263 if (!compareIntents(expectedSel, actualSel)) { 1264 failNotEquals(msg, expected, actual); 1265 } 1266 } 1267 } 1268 checkIntentUri(String intentSchemeUri, String androidAppSchemeUri, Intent intent)1269 private void checkIntentUri(String intentSchemeUri, String androidAppSchemeUri, Intent intent) { 1270 if (intentSchemeUri != null) { 1271 try { 1272 Intent genIntent = Intent.parseUri(intentSchemeUri, 0); 1273 assertEqualsIntent("Implicitly converting " + intentSchemeUri + " to Intent", 1274 intent, genIntent); 1275 genIntent = Intent.parseUri(intentSchemeUri, Intent.URI_INTENT_SCHEME); 1276 assertEqualsIntent("Explicitly converting " + intentSchemeUri + " to Intent", 1277 intent, genIntent); 1278 } catch (URISyntaxException e) { 1279 fail("Failure parsing " + intentSchemeUri + ": " + e); 1280 } 1281 String genUri = intent.toUri(Intent.URI_INTENT_SCHEME); 1282 assertEquals("Converting " + intent + " to intent: uri", 1283 intentSchemeUri, genUri); 1284 } 1285 if (androidAppSchemeUri != null) { 1286 try { 1287 Intent genIntent = Intent.parseUri(androidAppSchemeUri, 0); 1288 assertEqualsIntent("Implicitly converting " + androidAppSchemeUri + " to Intent", 1289 intent, genIntent); 1290 genIntent = Intent.parseUri(intentSchemeUri, Intent.URI_ANDROID_APP_SCHEME); 1291 assertEqualsIntent("Explicitly converting " + androidAppSchemeUri + " to Intent", 1292 intent, genIntent); 1293 } catch (URISyntaxException e) { 1294 fail("Failure parsing " + androidAppSchemeUri + ": " + e); 1295 } 1296 String genUri = intent.toUri(Intent.URI_ANDROID_APP_SCHEME); 1297 assertEquals("Converting " + intent + " to android-app: uri", 1298 androidAppSchemeUri, genUri); 1299 } 1300 } 1301 testAccessFlags()1302 public void testAccessFlags() { 1303 int expected = 1; 1304 mIntent.setFlags(expected); 1305 assertEquals(expected, mIntent.getFlags()); 1306 } 1307 testCreateChooser()1308 public void testCreateChooser() { 1309 Intent target = Intent.createChooser(mIntent, null); 1310 assertEquals(Intent.ACTION_CHOOSER, target.getAction()); 1311 Intent returnIntent = (Intent) target.getParcelableExtra(Intent.EXTRA_INTENT); 1312 assertEquals(mIntent.toString(), returnIntent.toString()); 1313 assertEquals(mIntent.toURI(), returnIntent.toURI()); 1314 assertNull(returnIntent.getStringExtra(Intent.EXTRA_INTENT)); 1315 final String title = "title String"; 1316 target = Intent.createChooser(mIntent, title); 1317 assertEquals(title, target.getStringExtra(Intent.EXTRA_TITLE)); 1318 assertNotNull(target.resolveActivity(mPm)); 1319 } 1320 testGetFloatArrayExtra()1321 public void testGetFloatArrayExtra() { 1322 final float[] expected = { 1f, 2f, 3f }; 1323 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1324 assertEquals(expected, mIntent.getFloatArrayExtra(TEST_EXTRA_NAME)); 1325 } 1326 testSetDataAndType()1327 public void testSetDataAndType() { 1328 mIntent.setDataAndType(TEST_URI, TEST_TYPE); 1329 assertEquals(TEST_URI, mIntent.getData()); 1330 assertEquals(TEST_TYPE, mIntent.getType()); 1331 } 1332 testSetData()1333 public void testSetData() { 1334 mIntent.setData(TEST_URI); 1335 assertEquals(TEST_URI, mIntent.getData()); 1336 assertNull(mIntent.getType()); 1337 1338 mIntent.setType(TEST_TYPE); 1339 mIntent.setData(TEST_URI); 1340 assertEquals(TEST_URI, mIntent.getData()); 1341 assertNull(mIntent.getType()); 1342 } 1343 testSetType()1344 public void testSetType() { 1345 mIntent.setType(TEST_TYPE); 1346 assertEquals(TEST_TYPE, mIntent.getType()); 1347 assertNull(mIntent.getData()); 1348 1349 mIntent.setData(TEST_URI); 1350 mIntent.setType(TEST_TYPE); 1351 assertEquals(TEST_TYPE, mIntent.getType()); 1352 assertNull(mIntent.getData()); 1353 } 1354 testGetStringExtra()1355 public void testGetStringExtra() { 1356 final String expected = "testString"; 1357 assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 1358 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1359 assertEquals(expected, mIntent.getStringExtra(TEST_EXTRA_NAME)); 1360 } 1361 1362 /** 1363 * Test that fillIn has no effect when no fields are set. 1364 */ testFillIn_blank()1365 public void testFillIn_blank() { 1366 Intent sourceIntent = new Intent(); 1367 Intent destIntent = new Intent(); 1368 assertEquals(0, destIntent.fillIn(sourceIntent, Intent.FILL_IN_ACTION)); 1369 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1370 assertNull(destIntent.getAction()); 1371 } 1372 1373 /** 1374 * Test that fillIn copies the action field. 1375 */ testFillIn_action()1376 public void testFillIn_action() { 1377 Intent sourceIntent = new Intent(); 1378 Intent destIntent = new Intent(); 1379 // test action copied when null 1380 sourceIntent.setAction(TEST_ACTION); 1381 assertEquals(Intent.FILL_IN_ACTION, destIntent.fillIn(sourceIntent, 0)); 1382 assertEquals(TEST_ACTION, destIntent.getAction()); 1383 } 1384 1385 /** 1386 * Test that fillIn does not copy action when its already set in target Intent. 1387 */ testFillIn_actionSet()1388 public void testFillIn_actionSet() { 1389 Intent sourceIntent = new Intent(); 1390 Intent destIntent = new Intent(); 1391 final String newAction = "foo"; 1392 sourceIntent = new Intent(); 1393 sourceIntent.setAction(newAction); 1394 destIntent.setAction(TEST_ACTION); 1395 1396 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1397 assertEquals(TEST_ACTION, destIntent.getAction()); 1398 } 1399 1400 /** 1401 * Test that fillIn copies action when {@link Intent#FILL_IN_ACTION} flag is set. 1402 */ testFillIn_actionOverride()1403 public void testFillIn_actionOverride() { 1404 Intent sourceIntent = new Intent(); 1405 Intent destIntent = new Intent(); 1406 final String newAction = "foo"; 1407 sourceIntent = new Intent(); 1408 sourceIntent.setAction(newAction); 1409 destIntent.setAction(TEST_ACTION); 1410 1411 assertEquals(Intent.FILL_IN_ACTION, destIntent.fillIn(sourceIntent, Intent.FILL_IN_ACTION)); 1412 assertEquals(newAction, destIntent.getAction()); 1413 } 1414 1415 /** 1416 * Test that fillIn copies data. 1417 */ testFillIn_data()1418 public void testFillIn_data() { 1419 Intent sourceIntent = new Intent(); 1420 Intent destIntent = new Intent(); 1421 sourceIntent.setData(TEST_URI); 1422 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, 0)); 1423 assertEquals(TEST_URI, destIntent.getData()); 1424 } 1425 1426 /** 1427 * Test that fillIn does not copy data when already its already set in target Intent. 1428 */ testFillIn_dataSet()1429 public void testFillIn_dataSet() { 1430 Intent sourceIntent = new Intent(); 1431 Intent destIntent = new Intent(); 1432 sourceIntent.setData(TEST_URI); 1433 destIntent.setData(ANOTHER_TEST_URI); 1434 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1435 assertEquals(ANOTHER_TEST_URI, destIntent.getData()); 1436 } 1437 1438 /** 1439 * Test that fillIn overrides data when {@link Intent#FILL_IN_DATA} flag is set. 1440 */ testFillIn_dataOverride()1441 public void testFillIn_dataOverride() { 1442 Intent sourceIntent = new Intent(); 1443 Intent destIntent = new Intent(); 1444 sourceIntent.setData(TEST_URI); 1445 destIntent.setData(ANOTHER_TEST_URI); 1446 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, Intent.FILL_IN_DATA)); 1447 assertEquals(TEST_URI, destIntent.getData()); 1448 } 1449 1450 /** 1451 * Test that fillIn copies data type. 1452 */ testFillIn_dataType()1453 public void testFillIn_dataType() { 1454 Intent sourceIntent = new Intent(); 1455 Intent destIntent = new Intent(); 1456 sourceIntent.setType(TEST_TYPE); 1457 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, 0)); 1458 assertEquals(TEST_TYPE, destIntent.getType()); 1459 } 1460 1461 /** 1462 * Test that fillIn does not copy data type when already its already set in target Intent. 1463 */ testFillIn_dataTypeSet()1464 public void testFillIn_dataTypeSet() { 1465 Intent sourceIntent = new Intent(); 1466 Intent destIntent = new Intent(); 1467 sourceIntent.setType(TEST_TYPE); 1468 destIntent.setType(ANOTHER_TEST_TYPE); 1469 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1470 assertEquals(ANOTHER_TEST_TYPE, destIntent.getType()); 1471 } 1472 1473 /** 1474 * Test that fillIn overrides data type when {@link Intent#FILL_IN_DATA} flag is set. 1475 */ testFillIn_dataTypeOverride()1476 public void testFillIn_dataTypeOverride() { 1477 Intent sourceIntent = new Intent(); 1478 Intent destIntent = new Intent(); 1479 sourceIntent.setType(TEST_TYPE); 1480 destIntent.setType(ANOTHER_TEST_TYPE); 1481 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, Intent.FILL_IN_DATA)); 1482 assertEquals(TEST_TYPE, destIntent.getType()); 1483 } 1484 1485 /** 1486 * Test that fillIn copies identifier. 1487 */ testFillIn_identifier()1488 public void testFillIn_identifier() { 1489 Intent sourceIntent = new Intent(); 1490 Intent destIntent = new Intent(); 1491 sourceIntent.setIdentifier(TEST_IDENTIFIER); 1492 assertEquals(Intent.FILL_IN_IDENTIFIER, destIntent.fillIn(sourceIntent, 0)); 1493 assertEquals(TEST_IDENTIFIER, destIntent.getIdentifier()); 1494 } 1495 1496 /** 1497 * Test that fillIn does not copy identifier when already its already set in target Intent. 1498 */ testFillIn_identifierSet()1499 public void testFillIn_identifierSet() { 1500 Intent sourceIntent = new Intent(); 1501 Intent destIntent = new Intent(); 1502 sourceIntent.setIdentifier(TEST_IDENTIFIER); 1503 destIntent.setIdentifier(ANOTHER_TEST_IDENTIFIER); 1504 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1505 assertEquals(ANOTHER_TEST_IDENTIFIER, destIntent.getIdentifier()); 1506 } 1507 1508 /** 1509 * Test that fillIn overrides identifier when {@link Intent#FILL_IN_IDENTIFIER} flag is set. 1510 */ testFillIn_identifierOverride()1511 public void testFillIn_identifierOverride() { 1512 Intent sourceIntent = new Intent(); 1513 Intent destIntent = new Intent(); 1514 sourceIntent.setIdentifier(TEST_IDENTIFIER); 1515 destIntent.setIdentifier(TEST_IDENTIFIER); 1516 assertEquals(Intent.FILL_IN_IDENTIFIER, destIntent.fillIn(sourceIntent, 1517 Intent.FILL_IN_IDENTIFIER)); 1518 assertEquals(TEST_IDENTIFIER, destIntent.getIdentifier()); 1519 } 1520 1521 /** 1522 * Test component is not copied by fillIn method when {@link Intent#FILL_IN_COMPONENT} flag is 1523 * not set. 1524 */ testFillIn_componentNoCopy()1525 public void testFillIn_componentNoCopy() { 1526 Intent sourceIntent = new Intent(); 1527 Intent destIntent = new Intent(); 1528 sourceIntent.setComponent(mComponentName); 1529 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1530 assertEquals(null, destIntent.getComponent()); 1531 } 1532 1533 /** 1534 * Test that fillIn copies component when {@link Intent#FILL_IN_COMPONENT} flag is set. 1535 */ testFillIn_componentOverride()1536 public void testFillIn_componentOverride() { 1537 Intent sourceIntent = new Intent(); 1538 Intent destIntent = new Intent(); 1539 sourceIntent.setComponent(mComponentName); 1540 destIntent.setComponent(mAnotherComponentName); 1541 assertEquals(Intent.FILL_IN_COMPONENT, destIntent.fillIn(sourceIntent, 1542 Intent.FILL_IN_COMPONENT)); 1543 assertEquals(mComponentName, destIntent.getComponent()); 1544 } 1545 1546 /** 1547 * Test that fillIn copies categories. 1548 */ testFillIn_category()1549 public void testFillIn_category() { 1550 Intent sourceIntent = new Intent(); 1551 Intent destIntent = new Intent(); 1552 // add two categories to ensure all categories are copied 1553 sourceIntent.addCategory(TEST_CATEGORY); 1554 sourceIntent.addCategory(ANOTHER_TEST_CATEGORY); 1555 assertEquals(Intent.FILL_IN_CATEGORIES, destIntent.fillIn(sourceIntent, 0)); 1556 assertEquals(2, destIntent.getCategories().size()); 1557 assertTrue(destIntent.getCategories().contains(TEST_CATEGORY)); 1558 assertTrue(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY)); 1559 } 1560 1561 /** 1562 * Test fillIn does not copy categories by default when already set. 1563 */ testFillIn_categorySet()1564 public void testFillIn_categorySet() { 1565 Intent sourceIntent = new Intent(); 1566 Intent destIntent = new Intent(); 1567 sourceIntent = new Intent(); 1568 sourceIntent.addCategory(TEST_CATEGORY); 1569 destIntent.addCategory(ANOTHER_TEST_CATEGORY); 1570 1571 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1572 assertEquals(1, destIntent.getCategories().size()); 1573 assertTrue(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY)); 1574 assertFalse(destIntent.getCategories().contains(TEST_CATEGORY)); 1575 } 1576 1577 /** 1578 * Test that fillIn adds categories when {@link Intent#FILL_IN_CATEGORIES} flag is set. 1579 */ testFillIn_categoryOverride()1580 public void testFillIn_categoryOverride() { 1581 Intent sourceIntent = new Intent(); 1582 Intent destIntent = new Intent(); 1583 sourceIntent = new Intent(); 1584 sourceIntent.addCategory(TEST_CATEGORY); 1585 destIntent.addCategory(ANOTHER_TEST_CATEGORY); 1586 1587 assertEquals(Intent.FILL_IN_CATEGORIES, destIntent.fillIn(sourceIntent, Intent.FILL_IN_CATEGORIES)); 1588 assertEquals(1, destIntent.getCategories().size()); 1589 assertFalse(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY)); 1590 assertTrue(destIntent.getCategories().contains(TEST_CATEGORY)); 1591 } 1592 1593 /** 1594 * Test fillIn copies package. 1595 */ testFillIn_package()1596 public void testFillIn_package() { 1597 Intent sourceIntent = new Intent(); 1598 Intent destIntent = new Intent(); 1599 sourceIntent.setPackage(TEST_PACKAGE); 1600 assertEquals(Intent.FILL_IN_PACKAGE, destIntent.fillIn(sourceIntent, 0)); 1601 assertEquals(TEST_PACKAGE, destIntent.getPackage()); 1602 } 1603 1604 /** 1605 * Test fillIn does not copy package by default when already set. 1606 */ testFillIn_packageSet()1607 public void testFillIn_packageSet() { 1608 Intent sourceIntent = new Intent(); 1609 Intent destIntent = new Intent(); 1610 sourceIntent.setPackage(TEST_PACKAGE); 1611 destIntent.setPackage(ANOTHER_TEST_PACKAGE); 1612 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1613 assertEquals(ANOTHER_TEST_PACKAGE, destIntent.getPackage()); 1614 } 1615 1616 /** 1617 * Test that fillIn overrides package when {@link Intent#FILL_IN_PACKAGE} flag is set. 1618 */ testFillIn_packageOverride()1619 public void testFillIn_packageOverride() { 1620 Intent sourceIntent = new Intent(); 1621 Intent destIntent = new Intent(); 1622 sourceIntent.setPackage(TEST_PACKAGE); 1623 destIntent.setPackage(ANOTHER_TEST_PACKAGE); 1624 assertEquals(Intent.FILL_IN_PACKAGE, destIntent.fillIn(sourceIntent, Intent.FILL_IN_PACKAGE)); 1625 assertEquals(TEST_PACKAGE, destIntent.getPackage()); 1626 } 1627 1628 /** 1629 * Test that fillIn copies extras. 1630 */ testFillIn_extras()1631 public void testFillIn_extras() { 1632 Intent sourceIntent = new Intent(); 1633 Intent destIntent = new Intent(); 1634 final Bundle bundle = new Bundle(); 1635 bundle.putBoolean(TEST_EXTRA_NAME, true); 1636 sourceIntent.putExtras(bundle); 1637 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1638 assertTrue(destIntent.getExtras().getBoolean(TEST_EXTRA_NAME)); 1639 } 1640 testGetExtras()1641 public void testGetExtras() { 1642 assertNull(mIntent.getExtras()); 1643 final String expected = "testString"; 1644 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1645 assertNotNull(mIntent.getExtras()); 1646 assertEquals(expected, mIntent.getExtras().getString(TEST_EXTRA_NAME)); 1647 } 1648 testGetBooleanExtra()1649 public void testGetBooleanExtra() { 1650 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 1651 mIntent.putExtra(TEST_EXTRA_NAME, true); 1652 assertTrue(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 1653 mIntent.putExtra(TEST_EXTRA_NAME, false); 1654 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 1655 } 1656 testGetFloatExtra()1657 public void testGetFloatExtra() { 1658 float expected = 2f; 1659 assertEquals(1f, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 1660 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1661 assertEquals(expected, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 1662 } 1663 testGetShortArrayExtra()1664 public void testGetShortArrayExtra() { 1665 final short[] expected = { 1, 2, 3 }; 1666 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1667 assertEquals(expected, mIntent.getShortArrayExtra(TEST_EXTRA_NAME)); 1668 } 1669 testGetStringArrayExtra()1670 public void testGetStringArrayExtra() { 1671 final String[] expected = { "1d", "2d", "3d" }; 1672 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1673 assertEquals(expected, mIntent.getStringArrayExtra(TEST_EXTRA_NAME)); 1674 } 1675 testGetCharSequenceArrayExtra()1676 public void testGetCharSequenceArrayExtra() { 1677 final String[] expected = { "1d", "2d", "3d" }; 1678 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1679 assertEquals(expected, mIntent.getCharSequenceArrayExtra(TEST_EXTRA_NAME)); 1680 } 1681 testGetByteArrayExtra()1682 public void testGetByteArrayExtra() { 1683 final byte[] expected = TEST_ACTION.getBytes(); 1684 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1685 assertEquals(expected, mIntent.getByteArrayExtra(TEST_EXTRA_NAME)); 1686 } 1687 testHasFileDescriptors()1688 public void testHasFileDescriptors() { 1689 Bundle bundle = mIntent.getExtras(); 1690 assertEquals(bundle != null && bundle.hasFileDescriptors(), mIntent.hasFileDescriptors()); 1691 final byte[] expected = TEST_ACTION.getBytes(); 1692 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1693 bundle = mIntent.getExtras(); 1694 assertEquals(bundle != null && bundle.hasFileDescriptors(), mIntent.hasFileDescriptors()); 1695 } 1696 testGetBooleanArrayExtra()1697 public void testGetBooleanArrayExtra() { 1698 final boolean[] expected = { true, true, false }; 1699 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1700 assertEquals(expected, mIntent.getBooleanArrayExtra(TEST_EXTRA_NAME)); 1701 } 1702 testGetLongExtra()1703 public void testGetLongExtra() { 1704 final long expected = 2l; 1705 assertEquals(1l, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 1706 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1707 assertEquals(expected, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 1708 } 1709 testRemoveCategory()1710 public void testRemoveCategory() { 1711 assertNull(mIntent.getCategories()); 1712 mIntent.addCategory(TEST_CATEGORY); 1713 assertNotNull(mIntent.getCategories()); 1714 assertEquals(1, mIntent.getCategories().size()); 1715 assertEquals(TEST_CATEGORY, (String) (mIntent.getCategories().toArray()[0])); 1716 mIntent.removeCategory(TEST_CATEGORY); 1717 assertFalse(mIntent.hasCategory(TEST_CATEGORY)); 1718 } 1719 testFilterEquals()1720 public void testFilterEquals() { 1721 assertFalse(mIntent.filterEquals(null)); 1722 1723 Intent target = new Intent(); 1724 assertTrue(mIntent.filterEquals(target)); 1725 1726 target.setAction(TEST_ACTION); 1727 assertFalse(mIntent.filterEquals(target)); 1728 mIntent.setAction(TEST_ACTION + "test"); 1729 assertFalse(mIntent.filterEquals(target)); 1730 mIntent.setAction(null); 1731 assertFalse(mIntent.filterEquals(target)); 1732 mIntent.setAction(TEST_ACTION); 1733 assertTrue(mIntent.filterEquals(target)); 1734 1735 target.setData(TEST_URI); 1736 assertFalse(mIntent.filterEquals(target)); 1737 mIntent.setData(Uri.parse("myURI")); 1738 assertFalse(mIntent.filterEquals(target)); 1739 mIntent.setData(null); 1740 assertFalse(mIntent.filterEquals(target)); 1741 mIntent.setData(TEST_URI); 1742 assertTrue(mIntent.filterEquals(target)); 1743 1744 target.setType(TEST_TYPE); 1745 assertFalse(mIntent.filterEquals(target)); 1746 mIntent.setType(TEST_TYPE + "test"); 1747 assertFalse(mIntent.filterEquals(target)); 1748 mIntent.setType(null); 1749 assertFalse(mIntent.filterEquals(target)); 1750 mIntent.setType(TEST_TYPE); 1751 assertTrue(mIntent.filterEquals(target)); 1752 1753 target.setIdentifier(TEST_IDENTIFIER); 1754 assertFalse(mIntent.filterEquals(target)); 1755 mIntent.setIdentifier(TEST_IDENTIFIER + "test"); 1756 assertFalse(mIntent.filterEquals(target)); 1757 mIntent.setIdentifier(null); 1758 assertFalse(mIntent.filterEquals(target)); 1759 mIntent.setIdentifier(TEST_IDENTIFIER); 1760 assertTrue(mIntent.filterEquals(target)); 1761 1762 target.setComponent(mComponentName); 1763 assertFalse(mIntent.filterEquals(target)); 1764 mIntent.setComponent(new ComponentName(mContext, MockReceiver.class)); 1765 assertFalse(mIntent.filterEquals(target)); 1766 mIntent.setComponent(null); 1767 assertFalse(mIntent.filterEquals(target)); 1768 mIntent.setComponent(mComponentName); 1769 assertTrue(mIntent.filterEquals(target)); 1770 1771 target.addCategory(TEST_CATEGORY); 1772 assertFalse(mIntent.filterEquals(target)); 1773 mIntent.addCategory(TEST_CATEGORY + "test"); 1774 assertFalse(mIntent.filterEquals(target)); 1775 mIntent.addCategory(TEST_CATEGORY); 1776 assertFalse(mIntent.filterEquals(target)); 1777 } 1778 testPutExtras1()1779 public void testPutExtras1() { 1780 final Intent intent = new Intent(); 1781 mIntent.putExtras(intent); 1782 assertEquals(intent.getExtras(), mIntent.getExtras()); 1783 intent.putExtra("test2", true); 1784 mIntent.putExtras(intent); 1785 assertEquals(intent.getExtras().toString(), mIntent.getExtras().toString()); 1786 } 1787 testPutExtras2()1788 public void testPutExtras2() { 1789 final Bundle bundle = new Bundle(); 1790 mIntent.putExtras(bundle); 1791 assertEquals(0, mIntent.getExtras().size()); 1792 String expected = "testString"; 1793 bundle.putString(TEST_EXTRA_NAME, expected); 1794 mIntent.putExtras(bundle); 1795 assertEquals(1, mIntent.getExtras().size()); 1796 assertEquals(expected, mIntent.getExtras().getString(TEST_EXTRA_NAME)); 1797 mIntent.putExtra(TEST_EXTRA_NAME, bundle); 1798 assertEquals(bundle, mIntent.getBundleExtra(TEST_EXTRA_NAME)); 1799 } 1800 testGetByteExtra()1801 public void testGetByteExtra() { 1802 final byte expected = Byte.valueOf("1"); 1803 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1804 assertEquals(expected, mIntent.getByteExtra(TEST_EXTRA_NAME, Byte.valueOf("1"))); 1805 } 1806 testGetSerializableExtra()1807 public void testGetSerializableExtra() { 1808 TestSerializable expected = new TestSerializable(); 1809 expected.Name = "testName"; 1810 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1811 assertEquals(expected, mIntent.getSerializableExtra(TEST_EXTRA_NAME)); 1812 TestSerializable target = (TestSerializable) mIntent.getSerializableExtra(TEST_EXTRA_NAME); 1813 assertEquals(expected.Name, target.Name); 1814 } 1815 testReplaceExtras()1816 public void testReplaceExtras() { 1817 Bundle extras = new Bundle(); 1818 String bundleKey = "testKey"; 1819 String bundleValue = "testValue"; 1820 extras.putString(bundleKey, bundleValue); 1821 1822 Intent intent = mIntent.replaceExtras(extras); 1823 assertSame(mIntent, intent); 1824 String actualValue = intent.getExtras().getString(bundleKey); 1825 assertEquals(bundleValue, actualValue); 1826 1827 Intent src = new Intent(); 1828 String intentName = "srcName"; 1829 String intentValue = "srcValue"; 1830 src.putExtra(intentName, intentValue); 1831 1832 intent = mIntent.replaceExtras(src); 1833 assertSame(mIntent, intent); 1834 actualValue = intent.getExtras().getString(intentName); 1835 assertEquals(intentValue, actualValue); 1836 } 1837 testNormalizeMimeType()1838 public void testNormalizeMimeType() { 1839 assertEquals(null, Intent.normalizeMimeType(null)); 1840 assertEquals("text/plain", Intent.normalizeMimeType("text/plain; charset=UTF-8")); 1841 assertEquals("text/x-vcard", Intent.normalizeMimeType("text/x-vCard")); 1842 assertEquals("foo/bar", Intent.normalizeMimeType(" foo/bar ")); 1843 } 1844 testRemoveUnsafeExtras()1845 public void testRemoveUnsafeExtras() { 1846 final Intent intent = new Intent(); 1847 final DummyParcelable dummyParcelable = new DummyParcelable(); 1848 intent.removeUnsafeExtras(); 1849 assertNull(intent.getExtras()); 1850 1851 // Check that removeUnsafeExtras keeps the same bundle if no changes are made. 1852 Bundle origExtras = new Bundle(); 1853 origExtras.putString("foo", "bar"); 1854 intent.replaceExtras(origExtras); 1855 intent.removeUnsafeExtras(); 1856 Bundle newExtras = intent.getExtras(); 1857 assertEquals(1, newExtras.size()); 1858 assertEquals("bar", newExtras.get("foo")); 1859 1860 // Check that removeUnsafeExtras will strip non-framework parcelables without modifying 1861 // the original extras bundle. 1862 origExtras.putParcelable("baddy", dummyParcelable); 1863 intent.replaceExtras(origExtras); 1864 intent.removeUnsafeExtras(); 1865 newExtras = intent.getExtras(); 1866 assertEquals(1, newExtras.size()); 1867 assertEquals("bar", newExtras.get("foo")); 1868 assertEquals(2, origExtras.size()); 1869 assertEquals("bar", origExtras.get("foo")); 1870 assertSame(dummyParcelable, origExtras.get("baddy")); 1871 1872 // Check that nested bad values will be stripped. 1873 Bundle origSubExtras = new Bundle(); 1874 origSubExtras.putParcelable("baddy", dummyParcelable); 1875 origExtras.putBundle("baddy", origSubExtras); 1876 intent.replaceExtras(origExtras); 1877 intent.removeUnsafeExtras(); 1878 newExtras = intent.getExtras(); 1879 assertEquals(2, newExtras.size()); 1880 assertEquals("bar", newExtras.get("foo")); 1881 Bundle newSubExtras = newExtras.getBundle("baddy"); 1882 assertNotSame(origSubExtras, newSubExtras); 1883 assertEquals(0, newSubExtras.size()); 1884 assertEquals(1, origSubExtras.size()); 1885 assertSame(dummyParcelable, origSubExtras.get("baddy")); 1886 } 1887 1888 private static class TestSerializable implements Serializable { 1889 static final long serialVersionUID = 1l; 1890 public String Name; 1891 } 1892 } 1893