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 com.android.internal.app.ResolverActivity; 20 import com.android.internal.util.XmlUtils; 21 22 23 import org.xmlpull.v1.XmlPullParser; 24 import org.xmlpull.v1.XmlPullParserException; 25 26 import android.app.cts.MockActivity; 27 import android.app.cts.MockReceiver; 28 import android.content.ComponentName; 29 import android.content.ContentResolver; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.content.pm.ActivityInfo; 33 import android.content.pm.PackageManager; 34 import android.content.pm.PackageManager.NameNotFoundException; 35 import android.content.res.XmlResourceParser; 36 import android.net.Uri; 37 import android.os.Bundle; 38 import android.os.IBinder; 39 import android.os.Parcel; 40 import android.os.ServiceManager; 41 import android.provider.Contacts.People; 42 import android.test.AndroidTestCase; 43 import android.util.AttributeSet; 44 import android.util.Xml; 45 46 import java.io.IOException; 47 import java.io.Serializable; 48 import java.net.URISyntaxException; 49 import java.util.ArrayList; 50 import java.util.Set; 51 52 public class IntentTest extends AndroidTestCase { 53 54 private Intent mIntent; 55 private static final String TEST_ACTION = "android.content.IntentTest_test"; 56 private static final Uri TEST_URI = People.CONTENT_URI; 57 private static final Uri ANOTHER_TEST_URI = People.CONTENT_FILTER_URI; 58 private static final String TEST_EXTRA_NAME = "testExtraName"; 59 private Context mContext; 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_CATEGORY = "testCategory"; 65 private static final String ANOTHER_TEST_CATEGORY = "testAnotherCategory"; 66 private static final String TEST_PACKAGE = "android.content.cts"; 67 private static final String ANOTHER_TEST_PACKAGE = "android.database.cts"; 68 69 @Override 70 protected void setUp() throws Exception { 71 super.setUp(); 72 mIntent = new Intent(); 73 mContext = getContext(); 74 mComponentName = new ComponentName(mContext, MockActivity.class); 75 mAnotherComponentName = new ComponentName(mContext, "tmp"); 76 } 77 78 public void testConstructor() { 79 mIntent = new Intent(); 80 assertNotNull(mIntent); 81 82 Intent intent = new Intent(); 83 intent.setAction(TEST_ACTION); 84 85 mIntent = new Intent(intent); 86 assertNotNull(mIntent); 87 assertEquals(TEST_ACTION, mIntent.getAction()); 88 89 mIntent = new Intent(TEST_ACTION); 90 assertNotNull(mIntent); 91 assertEquals(TEST_ACTION, mIntent.getAction()); 92 93 mIntent = new Intent(TEST_ACTION, TEST_URI); 94 assertNotNull(mIntent); 95 assertEquals(TEST_ACTION, mIntent.getAction()); 96 assertEquals(TEST_URI, mIntent.getData()); 97 98 mIntent = new Intent(mContext, MockActivity.class); 99 assertNotNull(mIntent); 100 assertEquals(mComponentName, mIntent.getComponent()); 101 102 mIntent = new Intent(TEST_ACTION, TEST_URI, mContext, MockActivity.class); 103 assertNotNull(mIntent); 104 assertEquals(TEST_ACTION, mIntent.getAction()); 105 assertEquals(TEST_URI, mIntent.getData()); 106 assertEquals(mComponentName, mIntent.getComponent()); 107 } 108 109 public void testRemoveExtra() { 110 mIntent = new Intent(); 111 mIntent.putExtra(TEST_EXTRA_NAME, "testvalue"); 112 assertNotNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 113 mIntent.removeExtra(TEST_EXTRA_NAME); 114 assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 115 } 116 117 public void testGetCharSequenceExtra() { 118 final CharSequence expected = "CharSequencetest"; 119 mIntent.putExtra(TEST_EXTRA_NAME, expected); 120 assertEquals(expected, mIntent.getCharSequenceExtra(TEST_EXTRA_NAME)); 121 } 122 123 public void testReadFromParcel() { 124 mIntent.setAction(TEST_ACTION); 125 mIntent.setData(TEST_URI); 126 mIntent.setType(TEST_TYPE); 127 mIntent.setFlags(0); 128 mIntent.setComponent(mComponentName); 129 mIntent.addCategory(TEST_CATEGORY); 130 final Parcel parcel = Parcel.obtain(); 131 mIntent.writeToParcel(parcel, 0); 132 parcel.setDataPosition(0); 133 final Intent target = new Intent(); 134 target.readFromParcel(parcel); 135 assertEquals(mIntent.getAction(), target.getAction()); 136 assertEquals(mIntent.getData(), target.getData()); 137 assertEquals(mIntent.getFlags(), target.getFlags()); 138 assertEquals(mIntent.getComponent(), target.getComponent()); 139 assertEquals(mIntent.getCategories(), target.getCategories()); 140 assertEquals(mIntent.toURI(), target.toURI()); 141 } 142 143 public void testGetParcelableArrayListExtra() { 144 final ArrayList<Intent> expected = new ArrayList<Intent>(); 145 Intent intent = new Intent(TEST_ACTION); 146 expected.add(intent); 147 148 mIntent.putParcelableArrayListExtra(TEST_EXTRA_NAME, expected); 149 final ArrayList<Intent> target = mIntent.getParcelableArrayListExtra(TEST_EXTRA_NAME); 150 assertEquals(expected.size(), target.size()); 151 assertEquals(expected, target); 152 } 153 154 public void testFilterHashCode() { 155 mIntent.filterHashCode(); 156 } 157 158 public void testGetCategories() { 159 mIntent.addCategory(TEST_CATEGORY); 160 final Set<String> target = mIntent.getCategories(); 161 assertEquals(TEST_CATEGORY, target.toArray()[0]); 162 } 163 164 public void testGetScheme() { 165 assertNull(mIntent.getScheme()); 166 mIntent.setData(TEST_URI); 167 assertEquals(TEST_URI.getScheme(), mIntent.getScheme()); 168 } 169 170 public void testGetIntegerArrayListExtra() { 171 final ArrayList<Integer> expected = new ArrayList<Integer>(); 172 expected.add(0); 173 mIntent.putIntegerArrayListExtra(TEST_EXTRA_NAME, expected); 174 assertEquals(expected, mIntent.getIntegerArrayListExtra(TEST_EXTRA_NAME)); 175 } 176 177 public void testHasExtra() { 178 mIntent = new Intent(); 179 assertFalse(mIntent.hasExtra(TEST_EXTRA_NAME)); 180 mIntent.putExtra(TEST_EXTRA_NAME, "test"); 181 assertTrue(mIntent.hasExtra(TEST_EXTRA_NAME)); 182 } 183 184 public void testGetIntArrayExtra() { 185 final int[] expected = { 1, 2, 3 }; 186 assertNull(mIntent.getIntArrayExtra(TEST_EXTRA_NAME)); 187 mIntent.putExtra(TEST_EXTRA_NAME, expected); 188 assertEquals(expected, mIntent.getIntArrayExtra(TEST_EXTRA_NAME)); 189 } 190 191 public void testSetClassName1() { 192 final Intent intent = mIntent.setClassName(mContext, MockActivity.class.getName()); 193 assertEquals(mComponentName, mIntent.getComponent()); 194 assertSame(mIntent, intent); 195 } 196 197 public void testSetClassName2() { 198 mIntent.setClassName(mContext.getPackageName(), MockActivity.class.getName()); 199 assertEquals(mComponentName, mIntent.getComponent()); 200 } 201 202 public void testGetIntExtra() { 203 final int expected = 0; 204 mIntent = new Intent(); 205 assertEquals(expected, mIntent.getIntExtra(TEST_EXTRA_NAME, expected)); 206 mIntent.putExtra(TEST_EXTRA_NAME, 100); 207 assertEquals(100, mIntent.getIntExtra(TEST_EXTRA_NAME, 1)); 208 209 } 210 211 public void testPutIntegerArrayListExtra() { 212 final ArrayList<Integer> expected = new ArrayList<Integer>(); 213 expected.add(0); 214 mIntent = new Intent(); 215 mIntent.putIntegerArrayListExtra(TEST_EXTRA_NAME, expected); 216 assertEquals(expected, mIntent.getIntegerArrayListExtra(TEST_EXTRA_NAME)); 217 } 218 219 public void testAccessType() { 220 mIntent.setType(TEST_TYPE); 221 assertEquals(TEST_TYPE, mIntent.getType()); 222 } 223 224 public void testGetBundleExtra() { 225 final Bundle expected = new Bundle(); 226 expected.putBoolean("testTrue", true); 227 mIntent.putExtras(expected); 228 mIntent.putExtra(TEST_EXTRA_NAME, expected); 229 230 assertEquals(expected, mIntent.getBundleExtra(TEST_EXTRA_NAME)); 231 } 232 233 public void testGetCharArrayExtra() { 234 final char[] expected = { 'a', 'b', 'c' }; 235 mIntent.putExtra(TEST_EXTRA_NAME, expected); 236 final char[] actual= mIntent.getCharArrayExtra(TEST_EXTRA_NAME); 237 assertEquals(expected.length, actual.length); 238 assertEquals(expected[0], actual[0]); 239 assertEquals(expected[1], actual[1]); 240 assertEquals(expected[2], actual[2]); 241 } 242 243 public void testGetDoubleArrayExtra() { 244 final double[] expected = { 1d, 2d }; 245 mIntent.putExtra(TEST_EXTRA_NAME, expected); 246 assertEquals(expected, mIntent.getDoubleArrayExtra(TEST_EXTRA_NAME)); 247 } 248 249 public void testPutStringArrayListExtra() { 250 final ArrayList<String> expected = new ArrayList<String>(); 251 expected.add("testString"); 252 mIntent.putStringArrayListExtra(TEST_EXTRA_NAME, expected); 253 assertEquals(expected, mIntent.getStringArrayListExtra(TEST_EXTRA_NAME)); 254 } 255 256 public void testResolveType1() { 257 final ContentResolver contentResolver = mContext.getContentResolver(); 258 assertNull(mIntent.resolveType(mContext)); 259 mIntent.setType(TEST_TYPE); 260 assertEquals(TEST_TYPE, mIntent.resolveType(mContext)); 261 mIntent.setType(null); 262 mIntent.setData(TEST_URI); 263 assertEquals(contentResolver.getType(TEST_URI), mIntent.resolveType(mContext)); 264 mIntent.setData(Uri.parse("test")); 265 assertNull(mIntent.resolveType(mContext)); 266 } 267 268 public void testResolveType2() { 269 final ContentResolver contentResolver = mContext.getContentResolver(); 270 assertNull(mIntent.resolveType(contentResolver)); 271 mIntent.setType(TEST_TYPE); 272 assertEquals(TEST_TYPE, mIntent.resolveType(contentResolver)); 273 mIntent.setType(null); 274 mIntent.setData(TEST_URI); 275 assertEquals(contentResolver.getType(TEST_URI), mIntent.resolveType(contentResolver)); 276 mIntent.setData(Uri.parse("test")); 277 assertNull(mIntent.resolveType(contentResolver)); 278 } 279 280 public void testAccessComponent() { 281 mIntent.setComponent(mComponentName); 282 assertEquals(mComponentName, mIntent.getComponent()); 283 } 284 285 public void testGetDataString() { 286 assertNull(mIntent.getDataString()); 287 mIntent.setData(TEST_URI); 288 assertEquals(TEST_URI.toString(), mIntent.getDataString()); 289 } 290 291 public void testHasCategory() { 292 assertFalse(mIntent.hasCategory(TEST_CATEGORY)); 293 mIntent.addCategory(TEST_CATEGORY); 294 assertTrue(mIntent.hasCategory(TEST_CATEGORY)); 295 } 296 297 public void testGetLongArrayExtra() { 298 final long[] expected = { 1l, 2l, 3l }; 299 mIntent.putExtra(TEST_EXTRA_NAME, expected); 300 assertEquals(expected, mIntent.getLongArrayExtra(TEST_EXTRA_NAME)); 301 } 302 303 public void testParseIntent() throws XmlPullParserException, IOException, 304 NameNotFoundException { 305 mIntent = null; 306 XmlResourceParser parser = null; 307 AttributeSet attrs = null; 308 ActivityInfo ai = null; 309 try { 310 mIntent = Intent.parseIntent(mContext.getResources(), parser, attrs); 311 fail("should thow exception!"); 312 } catch (NullPointerException e) { 313 // expected 314 } 315 316 ai = mContext.getPackageManager().getActivityInfo(mComponentName, 317 PackageManager.GET_META_DATA); 318 parser = ai.loadXmlMetaData(mContext.getPackageManager(), "android.app.alias"); 319 320 attrs = Xml.asAttributeSet(parser); 321 int type; 322 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 323 && type != XmlPullParser.START_TAG) { 324 } 325 326 String nodeName = parser.getName(); 327 if (!"alias".equals(nodeName)) { 328 throw new RuntimeException(); 329 } 330 331 int outerDepth = parser.getDepth(); 332 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 333 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { 334 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { 335 continue; 336 } 337 338 nodeName = parser.getName(); 339 if ("intent".equals(nodeName)) { 340 mIntent = Intent.parseIntent(mContext.getResources(), parser, attrs); 341 } else { 342 XmlUtils.skipCurrentTag(parser); 343 } 344 } 345 346 assertNotNull(mIntent); 347 assertEquals("android.intent.action.MAIN", mIntent.getAction()); 348 assertEquals(Uri.parse("http://www.google.com/"), mIntent.getData()); 349 } 350 351 public void testSetClass() { 352 assertNull(mIntent.getComponent()); 353 mIntent.setClass(mContext, MockActivity.class); 354 assertEquals(mComponentName, mIntent.getComponent()); 355 } 356 357 public void testResolveTypeIfNeeded() { 358 ContentResolver contentResolver = mContext.getContentResolver(); 359 assertNull(mIntent.resolveTypeIfNeeded(contentResolver)); 360 mIntent.setType(TEST_TYPE); 361 assertEquals(TEST_TYPE, mIntent.resolveTypeIfNeeded(contentResolver)); 362 363 mIntent.setType(null); 364 mIntent.setComponent(mComponentName); 365 assertEquals(null, mIntent.resolveTypeIfNeeded(contentResolver)); 366 367 mIntent.setType(TEST_TYPE); 368 mIntent.setComponent(mComponentName); 369 assertEquals(TEST_TYPE, mIntent.resolveTypeIfNeeded(contentResolver)); 370 371 mIntent.setType(null); 372 mIntent.setData(TEST_URI); 373 assertNull(mIntent.resolveTypeIfNeeded(contentResolver)); 374 } 375 376 public void testPutExtra1() { 377 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 378 mIntent.putExtra(TEST_EXTRA_NAME, true); 379 assertTrue(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 380 mIntent.putExtra(TEST_EXTRA_NAME, false); 381 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 382 } 383 384 public void testPutExtra2() { 385 final byte expected = Byte.valueOf("1"); 386 mIntent.putExtra(TEST_EXTRA_NAME, expected); 387 assertEquals(expected, mIntent.getByteExtra(TEST_EXTRA_NAME, Byte.valueOf("1"))); 388 } 389 390 public void testPutExtra3() { 391 assertEquals('a', mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 392 final char expected = 'a'; 393 mIntent.putExtra(TEST_EXTRA_NAME, expected); 394 assertEquals(expected, mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 395 } 396 397 public void testPutExtra4() { 398 final Short expected = Short.valueOf("2"); 399 assertEquals(Short.valueOf("1").shortValue(), mIntent.getShortExtra( 400 TEST_EXTRA_NAME, Short.valueOf("1"))); 401 mIntent.putExtra(TEST_EXTRA_NAME, expected); 402 assertEquals(expected.shortValue(), mIntent.getShortExtra(TEST_EXTRA_NAME, Short.valueOf("1"))); 403 } 404 405 public void testPutExtra5() { 406 final int expected = 2; 407 assertEquals(1, mIntent.getIntExtra(TEST_EXTRA_NAME, 1)); 408 mIntent.putExtra(TEST_EXTRA_NAME, expected); 409 assertEquals(expected, mIntent.getIntExtra(TEST_EXTRA_NAME, 1)); 410 } 411 412 public void testPutExtra6() { 413 final long expected = 2l; 414 assertEquals(1l, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 415 mIntent.putExtra(TEST_EXTRA_NAME, expected); 416 assertEquals(expected, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 417 } 418 419 public void testPutExtra7() { 420 final float expected = 2f; 421 assertEquals(1f, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 422 mIntent.putExtra(TEST_EXTRA_NAME, expected); 423 assertEquals(expected, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 424 } 425 426 public void testPutExtra8() { 427 final double expected = 2d; 428 assertEquals(1d, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 429 mIntent.putExtra(TEST_EXTRA_NAME, expected); 430 assertEquals(expected, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 431 } 432 433 public void testPutExtra9() { 434 final String expected = "testString"; 435 assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 436 mIntent.putExtra(TEST_EXTRA_NAME, expected); 437 assertEquals(expected, mIntent.getStringExtra(TEST_EXTRA_NAME)); 438 } 439 440 public void testPutExtra10() { 441 final CharSequence expected = "testString"; 442 assertNull(mIntent.getCharSequenceExtra(TEST_EXTRA_NAME)); 443 mIntent.putExtra(TEST_EXTRA_NAME, expected); 444 assertEquals(expected, mIntent.getCharSequenceExtra(TEST_EXTRA_NAME)); 445 } 446 447 public void testPutExtra11() { 448 final Intent expected = new Intent(TEST_ACTION); 449 mIntent.putExtra(TEST_EXTRA_NAME, expected); 450 assertEquals(expected, mIntent.getParcelableExtra(TEST_EXTRA_NAME)); 451 } 452 453 public void testPutExtra12() { 454 final Intent[] expected = { new Intent(TEST_ACTION), new Intent(mContext, MockActivity.class) }; 455 mIntent.putExtra(TEST_EXTRA_NAME, expected); 456 assertEquals(expected, mIntent.getParcelableArrayExtra(TEST_EXTRA_NAME)); 457 } 458 459 public void testPutExtra13() { 460 final TestSerializable expected = new TestSerializable(); 461 expected.Name = "testName"; 462 mIntent.putExtra(TEST_EXTRA_NAME, expected); 463 assertEquals(expected, mIntent.getSerializableExtra(TEST_EXTRA_NAME)); 464 TestSerializable target = (TestSerializable) mIntent.getSerializableExtra(TEST_EXTRA_NAME); 465 assertEquals(expected.Name, target.Name); 466 } 467 468 public void testPutExtra14() { 469 final boolean[] expected = { true, true, false }; 470 mIntent.putExtra(TEST_EXTRA_NAME, expected); 471 assertEquals(expected, mIntent.getBooleanArrayExtra(TEST_EXTRA_NAME)); 472 } 473 474 public void testPutExtra15() { 475 final byte[] expected = TEST_ACTION.getBytes(); 476 mIntent.putExtra(TEST_EXTRA_NAME, expected); 477 assertEquals(expected, mIntent.getByteArrayExtra(TEST_EXTRA_NAME)); 478 } 479 480 public void testPutExtra16() { 481 final short[] expected = { 1, 2, 3 }; 482 mIntent.putExtra(TEST_EXTRA_NAME, expected); 483 assertEquals(expected, mIntent.getShortArrayExtra(TEST_EXTRA_NAME)); 484 } 485 486 public void testPutExtra17() { 487 final char[] expected = { '1', '2', '3' }; 488 mIntent.putExtra(TEST_EXTRA_NAME, expected); 489 assertEquals(expected, mIntent.getCharArrayExtra(TEST_EXTRA_NAME)); 490 } 491 492 public void testPutExtra18() { 493 final int[] expected = { 1, 2, 3 }; 494 mIntent.putExtra(TEST_EXTRA_NAME, expected); 495 assertEquals(expected, mIntent.getIntArrayExtra(TEST_EXTRA_NAME)); 496 } 497 498 public void testPutExtra19() { 499 final long[] expected = { 1l, 2l, 3l }; 500 mIntent.putExtra(TEST_EXTRA_NAME, expected); 501 assertEquals(expected, mIntent.getLongArrayExtra(TEST_EXTRA_NAME)); 502 } 503 504 public void testPutExtra20() { 505 final float[] expected = { 1f, 2f, 3f }; 506 mIntent.putExtra(TEST_EXTRA_NAME, expected); 507 assertEquals(expected, mIntent.getFloatArrayExtra(TEST_EXTRA_NAME)); 508 } 509 510 public void testPutExtra21() { 511 final double[] expected = { 1d, 2d, 3d }; 512 mIntent.putExtra(TEST_EXTRA_NAME, expected); 513 assertEquals(expected, mIntent.getDoubleArrayExtra(TEST_EXTRA_NAME)); 514 } 515 516 public void testPutExtra22() { 517 final String[] expected = { "1d", "2d", "3d" }; 518 mIntent.putExtra(TEST_EXTRA_NAME, expected); 519 assertEquals(expected, mIntent.getStringArrayExtra(TEST_EXTRA_NAME)); 520 } 521 522 public void testPutExtra23() { 523 final Bundle expected = new Bundle(); 524 expected.putString("key", "value"); 525 mIntent.putExtra(TEST_EXTRA_NAME, expected); 526 assertEquals(expected, mIntent.getBundleExtra(TEST_EXTRA_NAME)); 527 } 528 529 @SuppressWarnings("deprecation") 530 public void testPutExtra24() { 531 final IBinder expected = ServiceManager.getService("activity"); 532 mIntent.putExtra(TEST_EXTRA_NAME, expected); 533 assertEquals(expected, mIntent.getIBinderExtra(TEST_EXTRA_NAME)); 534 } 535 536 public void testAddCategory() { 537 assertFalse(mIntent.hasCategory(TEST_CATEGORY)); 538 mIntent.addCategory(TEST_CATEGORY); 539 assertTrue(mIntent.hasCategory(TEST_CATEGORY)); 540 } 541 542 public void testPutParcelableArrayListExtra() { 543 ArrayList<Intent> expected = new ArrayList<Intent>(); 544 mIntent.putExtra(TEST_EXTRA_NAME, expected); 545 assertEquals(expected, mIntent.getParcelableArrayListExtra(TEST_EXTRA_NAME)); 546 } 547 548 public void testToString() { 549 assertNotNull(mIntent.toString()); 550 } 551 552 public void testAccessData() { 553 mIntent.setData(TEST_URI); 554 assertEquals(TEST_URI, mIntent.getData()); 555 } 556 557 public void testSetExtrasClassLoader() { 558 } 559 560 public void testGetStringArrayListExtra() { 561 final ArrayList<String> expected = new ArrayList<String>(); 562 expected.add("testString"); 563 mIntent.putStringArrayListExtra(TEST_EXTRA_NAME, expected); 564 assertEquals(expected, mIntent.getStringArrayListExtra(TEST_EXTRA_NAME)); 565 } 566 567 public void testGetCharSequenceArrayListExtra() { 568 final ArrayList<CharSequence> expected = new ArrayList<CharSequence>(); 569 expected.add("testCharSequence"); 570 mIntent.putCharSequenceArrayListExtra(TEST_EXTRA_NAME, expected); 571 assertEquals(expected, mIntent.getCharSequenceArrayListExtra(TEST_EXTRA_NAME)); 572 } 573 574 public void testResolveActivityInfo() throws NameNotFoundException { 575 final PackageManager pm = mContext.getPackageManager(); 576 assertEquals(null, mIntent.resolveActivityInfo(pm, 1)); 577 mIntent.setComponent(mComponentName); 578 ActivityInfo target = null; 579 580 target = pm.getActivityInfo(mComponentName, 1); 581 assertEquals(target.targetActivity, mIntent.resolveActivityInfo(pm, 1).targetActivity); 582 } 583 584 public void testGetParcelableExtra() { 585 final Intent expected = new Intent(TEST_ACTION); 586 mIntent.putExtra(TEST_EXTRA_NAME, expected); 587 assertEquals(expected, mIntent.getParcelableExtra(TEST_EXTRA_NAME)); 588 } 589 590 public void testAccessAction() { 591 mIntent.setAction(TEST_ACTION); 592 assertEquals(TEST_ACTION, mIntent.getAction()); 593 } 594 595 public void testAddFlags() { 596 final int flag = 1; 597 int expected = 0; 598 mIntent.addFlags(flag); 599 expected |= flag; 600 assertEquals(expected, mIntent.getFlags()); 601 } 602 603 public void testDescribeContents() { 604 final int expected = 0; 605 assertEquals(expected, mIntent.describeContents()); 606 mIntent.putExtra(TEST_EXTRA_NAME, "test"); 607 assertEquals(mIntent.getExtras().describeContents(), mIntent.describeContents()); 608 } 609 610 public void testGetShortExtra() { 611 612 final Short expected = Short.valueOf("2"); 613 assertEquals(Short.valueOf("1").shortValue(), mIntent.getShortExtra( 614 TEST_EXTRA_NAME, Short.valueOf("1"))); 615 mIntent.putExtra(TEST_EXTRA_NAME, expected); 616 assertEquals(expected.shortValue(), mIntent.getShortExtra(TEST_EXTRA_NAME, Short.valueOf("1"))); 617 } 618 619 public void testClone() { 620 mIntent.setAction(TEST_ACTION); 621 mIntent.setClass(mContext, MockActivity.class); 622 mIntent.setComponent(mComponentName); 623 mIntent.setDataAndType(TEST_URI, TEST_TYPE); 624 mIntent.addCategory(TEST_CATEGORY); 625 final String key = "testkey"; 626 final String excepted = "testValue"; 627 mIntent.putExtra(key, excepted); 628 Intent actual = (Intent) mIntent.clone(); 629 assertEquals(mComponentName, actual.getComponent()); 630 assertEquals(TEST_ACTION, actual.getAction()); 631 assertEquals(mComponentName, actual.getComponent()); 632 assertEquals(TEST_URI, actual.getData()); 633 assertEquals(TEST_TYPE, actual.getType()); 634 assertEquals(TEST_CATEGORY, (String) (actual.getCategories().toArray()[0])); 635 assertEquals(excepted, actual.getStringExtra(key)); 636 } 637 638 public void testGetDoubleExtra() { 639 final double expected = 2d; 640 assertEquals(1d, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 641 mIntent.putExtra(TEST_EXTRA_NAME, expected); 642 assertEquals(expected, mIntent.getDoubleExtra(TEST_EXTRA_NAME, 1d)); 643 } 644 645 public void testCloneFilter() { 646 mIntent.setAction(TEST_ACTION); 647 mIntent.setClass(mContext, MockActivity.class); 648 mIntent.setComponent(mComponentName); 649 mIntent.setDataAndType(TEST_URI, TEST_TYPE); 650 mIntent.addCategory(TEST_CATEGORY); 651 final String key = "testkey"; 652 mIntent.putExtra(key, "testValue"); 653 Intent actual = mIntent.cloneFilter(); 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_CATEGORY, (String) (actual.getCategories().toArray()[0])); 660 assertNull(actual.getStringExtra(key)); 661 } 662 663 public void testGetIntentOld() throws URISyntaxException { 664 String uri = "test"; 665 mIntent = Intent.getIntentOld(uri); 666 assertEquals(Intent.ACTION_VIEW, mIntent.getAction()); 667 668 mIntent = null; 669 try { 670 uri = "test#action(test)categories(test)type(mtype)launchFlags(test)extras(test"; 671 mIntent = Intent.getIntentOld(uri); 672 fail("should throw URISyntaxException."); 673 } catch (Exception e) { 674 // expected 675 } 676 677 final String compnent = 678 "component(" + mContext.getPackageName() + "!" + MockActivity.class.getName() + ")"; 679 uri = "testdata#action(test)categories(test!test2)type(mtype)launchFlags(1)" + compnent 680 + "extras(Stest=testString!btestbyte=1!" 681 + "Btestboolean=true!ctestchar=a!dtestdouble=1d!" 682 + "itestint=1!ltestlong=1!stestshort=1!ftestfloat=1f)"; 683 mIntent = Intent.getIntentOld(uri); 684 assertEquals("test", mIntent.getAction()); 685 assertEquals("testdata", mIntent.getData().toString()); 686 assertEquals(mComponentName, mIntent.getComponent()); 687 assertEquals("test", (String) (mIntent.getCategories().toArray()[0])); 688 assertEquals("mtype", mIntent.getType()); 689 assertEquals(1, mIntent.getFlags()); 690 assertEquals("testString", mIntent.getStringExtra("test")); 691 assertTrue(mIntent.getBooleanExtra("testboolean", false)); 692 final byte b = 1; 693 final byte defaulttByte = 2; 694 assertEquals(b, mIntent.getByteExtra("testbyte", defaulttByte)); 695 assertEquals('a', mIntent.getCharExtra("testchar", 'b')); 696 final float testFloat = 1f; 697 assertEquals(testFloat, mIntent.getFloatExtra("testfloat", 2f)); 698 final double testDouble = 1d; 699 assertEquals(testDouble, mIntent.getDoubleExtra("testdouble", 2d)); 700 701 final long testLong = 1; 702 assertEquals(testLong, mIntent.getLongExtra("testlong", 2l)); 703 704 final short testShort = 1; 705 final short defaultShort = 2; 706 assertEquals(testShort, mIntent.getShortExtra("testshort", defaultShort)); 707 assertEquals(1, mIntent.getIntExtra("testint", 2)); 708 } 709 710 public void testGetParcelableArrayExtra() { 711 final Intent[] expected = { new Intent(TEST_ACTION), new Intent(mContext, MockActivity.class) }; 712 mIntent.putExtra(TEST_EXTRA_NAME, expected); 713 assertEquals(expected, mIntent.getParcelableArrayExtra(TEST_EXTRA_NAME)); 714 } 715 716 public void testResolveActivity() { 717 final PackageManager pm = mContext.getPackageManager(); 718 719 ComponentName target = mIntent.resolveActivity(pm); 720 assertNull(target); 721 722 mIntent.setComponent(mComponentName); 723 target = mIntent.resolveActivity(pm); 724 assertEquals(mComponentName, target); 725 726 mIntent.setComponent(null); 727 mIntent.setData(TEST_URI); 728 target = mIntent.resolveActivity(pm); 729 assertEquals(ResolverActivity.class.getName(), target.getClassName()); 730 assertEquals("android", target.getPackageName()); 731 732 mIntent.setComponent(null); 733 mIntent.setAction(TEST_TYPE); 734 target = mIntent.resolveActivity(pm); 735 assertNull(target); 736 } 737 738 public void testGetCharExtra() { 739 assertEquals('a', mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 740 final char expected = 'b'; 741 mIntent.putExtra(TEST_EXTRA_NAME, expected); 742 assertEquals(expected, mIntent.getCharExtra(TEST_EXTRA_NAME, 'a')); 743 } 744 745 public void testGetIntent() throws URISyntaxException { 746 mIntent = Intent.getIntent("test#"); 747 assertEquals(Intent.ACTION_VIEW, mIntent.getAction()); 748 749 try { 750 String uri = "#Intent;action=android.content.IntentTest_test;" 751 + "category=testCategory;type=testtype;launchFlags=0x1;" 752 + "component=com.android/.app.MockActivity;K.testExtraName=1;end"; 753 mIntent = Intent.getIntent(uri); 754 fail("should throw URISyntaxException."); 755 } catch (Exception e) { 756 // expected 757 } 758 mIntent = new Intent(); 759 760 String uri = mIntent.toURI(); 761 Intent target = Intent.getIntent(uri); 762 assertEquals(Intent.ACTION_VIEW, target.getAction()); 763 764 mIntent.setAction(TEST_ACTION); 765 uri = mIntent.toURI(); 766 target = Intent.getIntent(uri); 767 assertEquals(TEST_ACTION, target.getAction()); 768 769 mIntent.setData(TEST_URI); 770 uri = mIntent.toURI(); 771 target = Intent.getIntent(uri); 772 assertEquals(TEST_URI, target.getData()); 773 774 mIntent.setComponent(mComponentName); 775 uri = mIntent.toURI(); 776 target = Intent.getIntent(uri); 777 assertEquals(mComponentName, target.getComponent()); 778 779 mIntent.addCategory(TEST_CATEGORY); 780 uri = mIntent.toURI(); 781 target = Intent.getIntent(uri); 782 assertEquals(1, target.getCategories().size()); 783 assertEquals(TEST_CATEGORY, (String) (target.getCategories().toArray()[0])); 784 785 mIntent.setType(TEST_TYPE); 786 uri = mIntent.toURI(); 787 target = Intent.getIntent(uri); 788 assertEquals(TEST_TYPE, target.getType()); 789 790 mIntent.setFlags(1); 791 uri = mIntent.toURI(); 792 target = Intent.getIntent(uri); 793 assertEquals(1, target.getFlags()); 794 795 String stringValue = "testString"; 796 mIntent.putExtra(TEST_EXTRA_NAME, stringValue); 797 uri = mIntent.toURI(); 798 target = Intent.getIntent(uri); 799 assertEquals(stringValue, target.getStringExtra(TEST_EXTRA_NAME)); 800 801 mIntent.putExtra(TEST_EXTRA_NAME, true); 802 uri = mIntent.toURI(); 803 target = Intent.getIntent(uri); 804 assertTrue(target.getBooleanExtra(TEST_EXTRA_NAME, false)); 805 806 final byte b = 1; 807 mIntent.putExtra(TEST_EXTRA_NAME, b); 808 809 uri = mIntent.toURI(); 810 target = Intent.getIntent(uri); 811 final byte defaulttByte = 2; 812 assertEquals(b, target.getByteExtra(TEST_EXTRA_NAME, defaulttByte)); 813 814 final char testChar = 'a'; 815 mIntent.putExtra(TEST_EXTRA_NAME, testChar); 816 uri = mIntent.toURI(); 817 target = Intent.getIntent(uri); 818 assertEquals(testChar, target.getCharExtra(TEST_EXTRA_NAME, 'b')); 819 820 final double testDouble = 1; 821 mIntent.putExtra(TEST_EXTRA_NAME, testDouble); 822 uri = mIntent.toURI(); 823 target = Intent.getIntent(uri); 824 assertEquals(testDouble, target.getDoubleExtra(TEST_EXTRA_NAME, 2)); 825 826 final int testInt = 1; 827 mIntent.putExtra(TEST_EXTRA_NAME, testInt); 828 uri = mIntent.toURI(); 829 target = Intent.getIntent(uri); 830 assertEquals(testInt, target.getIntExtra(TEST_EXTRA_NAME, 2)); 831 832 final long testLong = 1l; 833 mIntent.putExtra(TEST_EXTRA_NAME, testLong); 834 uri = mIntent.toURI(); 835 target = Intent.getIntent(uri); 836 assertEquals(testLong, target.getLongExtra(TEST_EXTRA_NAME, 2l)); 837 838 final short testShort = 1; 839 final short defaultShort = 2; 840 mIntent.putExtra(TEST_EXTRA_NAME, testShort); 841 uri = mIntent.toURI(); 842 target = Intent.getIntent(uri); 843 assertEquals(testShort, target.getShortExtra(TEST_EXTRA_NAME, defaultShort)); 844 } 845 846 public void testToURI() { 847 mIntent.setFlags(0); 848 assertEquals("#Intent;end", mIntent.toURI()); 849 850 mIntent.setData(TEST_URI); 851 assertTrue(mIntent.toURI().indexOf(TEST_URI.toString()) != -1); 852 853 mIntent.setAction(TEST_ACTION); 854 assertTrue(mIntent.toURI().indexOf("action=" + TEST_ACTION) != -1); 855 856 mIntent.addCategory(TEST_CATEGORY); 857 assertTrue(mIntent.toURI().indexOf("category=") != -1); 858 859 mIntent.setType(TEST_TYPE); 860 861 assertTrue(mIntent.toURI().indexOf("type=" + TEST_TYPE) != -1); 862 863 mIntent.setFlags(1); 864 assertFalse(mIntent.toURI().indexOf("launchFlags=" + Integer.toHexString(1)) != -1); 865 866 mIntent.setComponent(mComponentName); 867 assertTrue(mIntent.toURI().indexOf( 868 "component=" + mComponentName.flattenToShortString()) != -1); 869 870 final String stringValue = "testString"; 871 mIntent.putExtra(TEST_EXTRA_NAME, stringValue); 872 873 assertTrue(mIntent.toURI().indexOf(getString("S", TEST_EXTRA_NAME, stringValue)) != -1); 874 875 mIntent.putExtra(TEST_EXTRA_NAME, true); 876 877 assertTrue(mIntent.toURI().indexOf(getString("B", TEST_EXTRA_NAME, true)) != -1); 878 879 final byte b = 1; 880 mIntent.putExtra(TEST_EXTRA_NAME, b); 881 assertTrue(mIntent.toURI().indexOf(getString("b", TEST_EXTRA_NAME, b)) != -1); 882 883 final Character testChar = 'a'; 884 mIntent.putExtra(TEST_EXTRA_NAME, testChar); 885 886 assertTrue(mIntent.toURI().indexOf(getString("c", TEST_EXTRA_NAME, testChar)) != -1); 887 888 final double testDouble = 1; 889 mIntent.putExtra(TEST_EXTRA_NAME, testDouble); 890 assertTrue(mIntent.toURI().indexOf(getString("d", TEST_EXTRA_NAME, testDouble)) != -1); 891 892 final int testInt = 1; 893 mIntent.putExtra(TEST_EXTRA_NAME, testInt); 894 assertTrue(mIntent.toURI().indexOf(getString("i", TEST_EXTRA_NAME, testInt)) != -1); 895 896 final long testLong = 1l; 897 mIntent.putExtra(TEST_EXTRA_NAME, testLong); 898 assertTrue(mIntent.toURI().indexOf(getString("l", TEST_EXTRA_NAME, testLong)) != -1); 899 final short testShort = 1; 900 mIntent.putExtra(TEST_EXTRA_NAME, testShort); 901 assertTrue(mIntent.toURI().indexOf(getString("s", TEST_EXTRA_NAME, testShort)) != -1); 902 assertTrue(mIntent.toURI().indexOf("end") != -1); 903 } 904 905 private String getString(String entryType, String key, Object value) { 906 StringBuilder uri = new StringBuilder(); 907 uri.append(entryType); 908 uri.append('.'); 909 uri.append(Uri.encode(key)); 910 uri.append('='); 911 uri.append(Uri.encode(value.toString())); 912 return uri.toString(); 913 } 914 915 public void testAccessFlags() { 916 int expected = 1; 917 mIntent.setFlags(expected); 918 assertEquals(expected, mIntent.getFlags()); 919 } 920 921 public void testCreateChooser() { 922 Intent target = Intent.createChooser(mIntent, null); 923 assertEquals(Intent.ACTION_CHOOSER, target.getAction()); 924 Intent returnIntent = (Intent) target.getParcelableExtra(Intent.EXTRA_INTENT); 925 assertEquals(mIntent.toString(), returnIntent.toString()); 926 assertEquals(mIntent.toURI(), returnIntent.toURI()); 927 assertNull(returnIntent.getStringExtra(Intent.EXTRA_INTENT)); 928 final String title = "title String"; 929 target = Intent.createChooser(mIntent, title); 930 assertEquals(title, target.getStringExtra(Intent.EXTRA_TITLE)); 931 } 932 933 public void testGetFloatArrayExtra() { 934 final float[] expected = { 1f, 2f, 3f }; 935 mIntent.putExtra(TEST_EXTRA_NAME, expected); 936 assertEquals(expected, mIntent.getFloatArrayExtra(TEST_EXTRA_NAME)); 937 } 938 939 public void testSetDataAndType() { 940 mIntent.setDataAndType(TEST_URI, TEST_TYPE); 941 assertEquals(TEST_URI, mIntent.getData()); 942 assertEquals(TEST_TYPE, mIntent.getType()); 943 } 944 945 public void testSetData() { 946 mIntent.setData(TEST_URI); 947 assertEquals(TEST_URI, mIntent.getData()); 948 assertNull(mIntent.getType()); 949 950 mIntent.setType(TEST_TYPE); 951 mIntent.setData(TEST_URI); 952 assertEquals(TEST_URI, mIntent.getData()); 953 assertNull(mIntent.getType()); 954 } 955 956 public void testSetType() { 957 mIntent.setType(TEST_TYPE); 958 assertEquals(TEST_TYPE, mIntent.getType()); 959 assertNull(mIntent.getData()); 960 961 mIntent.setData(TEST_URI); 962 mIntent.setType(TEST_TYPE); 963 assertEquals(TEST_TYPE, mIntent.getType()); 964 assertNull(mIntent.getData()); 965 } 966 967 public void testGetStringExtra() { 968 final String expected = "testString"; 969 assertNull(mIntent.getStringExtra(TEST_EXTRA_NAME)); 970 mIntent.putExtra(TEST_EXTRA_NAME, expected); 971 assertEquals(expected, mIntent.getStringExtra(TEST_EXTRA_NAME)); 972 } 973 974 /** 975 * Test that fillIn has no effect when no fields are set. 976 */ 977 public void testFillIn_blank() { 978 Intent sourceIntent = new Intent(); 979 Intent destIntent = new Intent(); 980 assertEquals(0, destIntent.fillIn(sourceIntent, Intent.FILL_IN_ACTION)); 981 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 982 assertNull(destIntent.getAction()); 983 } 984 985 /** 986 * Test that fillIn copies the action field. 987 */ 988 public void testFillIn_action() { 989 Intent sourceIntent = new Intent(); 990 Intent destIntent = new Intent(); 991 // test action copied when null 992 sourceIntent.setAction(TEST_ACTION); 993 assertEquals(Intent.FILL_IN_ACTION, destIntent.fillIn(sourceIntent, 0)); 994 assertEquals(TEST_ACTION, destIntent.getAction()); 995 } 996 997 /** 998 * Test that fillIn does not copy action when its already set in target Intent. 999 */ 1000 public void testFillIn_actionSet() { 1001 Intent sourceIntent = new Intent(); 1002 Intent destIntent = new Intent(); 1003 final String newAction = "foo"; 1004 sourceIntent = new Intent(); 1005 sourceIntent.setAction(newAction); 1006 destIntent.setAction(TEST_ACTION); 1007 1008 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1009 assertEquals(TEST_ACTION, destIntent.getAction()); 1010 } 1011 1012 /** 1013 * Test that fillIn copies action when {@link Intent#FILL_IN_ACTION} flag is set. 1014 */ 1015 public void testFillIn_actionOverride() { 1016 Intent sourceIntent = new Intent(); 1017 Intent destIntent = new Intent(); 1018 final String newAction = "foo"; 1019 sourceIntent = new Intent(); 1020 sourceIntent.setAction(newAction); 1021 destIntent.setAction(TEST_ACTION); 1022 1023 assertEquals(Intent.FILL_IN_ACTION, destIntent.fillIn(sourceIntent, Intent.FILL_IN_ACTION)); 1024 assertEquals(newAction, destIntent.getAction()); 1025 } 1026 1027 /** 1028 * Test that fillIn copies data. 1029 */ 1030 public void testFillIn_data() { 1031 Intent sourceIntent = new Intent(); 1032 Intent destIntent = new Intent(); 1033 sourceIntent.setData(TEST_URI); 1034 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, 0)); 1035 assertEquals(TEST_URI, destIntent.getData()); 1036 } 1037 1038 /** 1039 * Test that fillIn does not copy data when already its already set in target Intent. 1040 */ 1041 public void testFillIn_dataSet() { 1042 Intent sourceIntent = new Intent(); 1043 Intent destIntent = new Intent(); 1044 sourceIntent.setData(TEST_URI); 1045 destIntent.setData(ANOTHER_TEST_URI); 1046 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1047 assertEquals(ANOTHER_TEST_URI, destIntent.getData()); 1048 } 1049 1050 /** 1051 * Test that fillIn overrides data when {@link Intent#FILL_IN_DATA} flag is set. 1052 */ 1053 public void testFillIn_dataOverride() { 1054 Intent sourceIntent = new Intent(); 1055 Intent destIntent = new Intent(); 1056 sourceIntent.setData(TEST_URI); 1057 destIntent.setData(ANOTHER_TEST_URI); 1058 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, Intent.FILL_IN_DATA)); 1059 assertEquals(TEST_URI, destIntent.getData()); 1060 } 1061 1062 /** 1063 * Test that fillIn copies data type. 1064 */ 1065 public void testFillIn_dataType() { 1066 Intent sourceIntent = new Intent(); 1067 Intent destIntent = new Intent(); 1068 sourceIntent.setType(TEST_TYPE); 1069 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, 0)); 1070 assertEquals(TEST_TYPE, destIntent.getType()); 1071 } 1072 1073 /** 1074 * Test that fillIn does not copy data type when already its already set in target Intent. 1075 */ 1076 public void testFillIn_dataTypeSet() { 1077 Intent sourceIntent = new Intent(); 1078 Intent destIntent = new Intent(); 1079 sourceIntent.setType(TEST_TYPE); 1080 destIntent.setType(ANOTHER_TEST_TYPE); 1081 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1082 assertEquals(ANOTHER_TEST_TYPE, destIntent.getType()); 1083 } 1084 1085 /** 1086 * Test that fillIn overrides data type when {@link Intent#FILL_IN_DATA} flag is set. 1087 */ 1088 public void testFillIn_dataTypeOverride() { 1089 Intent sourceIntent = new Intent(); 1090 Intent destIntent = new Intent(); 1091 sourceIntent.setType(TEST_TYPE); 1092 destIntent.setType(ANOTHER_TEST_TYPE); 1093 assertEquals(Intent.FILL_IN_DATA, destIntent.fillIn(sourceIntent, Intent.FILL_IN_DATA)); 1094 assertEquals(TEST_TYPE, destIntent.getType()); 1095 } 1096 1097 /** 1098 * Test component is not copied by fillIn method when {@link Intent#FILL_IN_COMPONENT} flag is 1099 * not set. 1100 */ 1101 public void testFillIn_componentNoCopy() { 1102 Intent sourceIntent = new Intent(); 1103 Intent destIntent = new Intent(); 1104 sourceIntent.setComponent(mComponentName); 1105 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1106 assertEquals(null, destIntent.getComponent()); 1107 } 1108 1109 /** 1110 * Test that fillIn copies component when {@link Intent#FILL_IN_COMPONENT} flag is set. 1111 */ 1112 public void testFillIn_componentOverride() { 1113 Intent sourceIntent = new Intent(); 1114 Intent destIntent = new Intent(); 1115 sourceIntent.setComponent(mComponentName); 1116 destIntent.setComponent(mAnotherComponentName); 1117 assertEquals(Intent.FILL_IN_COMPONENT, destIntent.fillIn(sourceIntent, 1118 Intent.FILL_IN_COMPONENT)); 1119 assertEquals(mComponentName, destIntent.getComponent()); 1120 } 1121 1122 /** 1123 * Test that fillIn copies categories. 1124 */ 1125 public void testFillIn_category() { 1126 Intent sourceIntent = new Intent(); 1127 Intent destIntent = new Intent(); 1128 // add two categories to ensure all categories are copied 1129 sourceIntent.addCategory(TEST_CATEGORY); 1130 sourceIntent.addCategory(ANOTHER_TEST_CATEGORY); 1131 assertEquals(Intent.FILL_IN_CATEGORIES, destIntent.fillIn(sourceIntent, 0)); 1132 assertEquals(2, destIntent.getCategories().size()); 1133 assertTrue(destIntent.getCategories().contains(TEST_CATEGORY)); 1134 assertTrue(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY)); 1135 } 1136 1137 /** 1138 * Test fillIn does not copy categories by default when already set. 1139 */ 1140 public void testFillIn_categorySet() { 1141 Intent sourceIntent = new Intent(); 1142 Intent destIntent = new Intent(); 1143 sourceIntent = new Intent(); 1144 sourceIntent.addCategory(TEST_CATEGORY); 1145 destIntent.addCategory(ANOTHER_TEST_CATEGORY); 1146 1147 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1148 assertEquals(1, destIntent.getCategories().size()); 1149 assertTrue(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY)); 1150 assertFalse(destIntent.getCategories().contains(TEST_CATEGORY)); 1151 } 1152 1153 /** 1154 * Test that fillIn adds categories when {@link Intent#FILL_IN_CATEGORIES} flag is set. 1155 */ 1156 public void testFillIn_categoryOverride() { 1157 Intent sourceIntent = new Intent(); 1158 Intent destIntent = new Intent(); 1159 sourceIntent = new Intent(); 1160 sourceIntent.addCategory(TEST_CATEGORY); 1161 destIntent.addCategory(ANOTHER_TEST_CATEGORY); 1162 1163 assertEquals(Intent.FILL_IN_CATEGORIES, destIntent.fillIn(sourceIntent, Intent.FILL_IN_CATEGORIES)); 1164 assertEquals(1, destIntent.getCategories().size()); 1165 assertFalse(destIntent.getCategories().contains(ANOTHER_TEST_CATEGORY)); 1166 assertTrue(destIntent.getCategories().contains(TEST_CATEGORY)); 1167 } 1168 1169 /** 1170 * Test fillIn copies package. 1171 */ 1172 public void testFillIn_package() { 1173 Intent sourceIntent = new Intent(); 1174 Intent destIntent = new Intent(); 1175 sourceIntent.setPackage(TEST_PACKAGE); 1176 assertEquals(Intent.FILL_IN_PACKAGE, destIntent.fillIn(sourceIntent, 0)); 1177 assertEquals(TEST_PACKAGE, destIntent.getPackage()); 1178 } 1179 1180 /** 1181 * Test fillIn does not copy package by default when already set. 1182 */ 1183 public void testFillIn_packageSet() { 1184 Intent sourceIntent = new Intent(); 1185 Intent destIntent = new Intent(); 1186 sourceIntent.setPackage(TEST_PACKAGE); 1187 destIntent.setPackage(ANOTHER_TEST_PACKAGE); 1188 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1189 assertEquals(ANOTHER_TEST_PACKAGE, destIntent.getPackage()); 1190 } 1191 1192 /** 1193 * Test that fillIn overrides package when {@link Intent#FILL_IN_PACKAGE} flag is set. 1194 */ 1195 public void testFillIn_packageOverride() { 1196 Intent sourceIntent = new Intent(); 1197 Intent destIntent = new Intent(); 1198 sourceIntent.setPackage(TEST_PACKAGE); 1199 destIntent.setPackage(ANOTHER_TEST_PACKAGE); 1200 assertEquals(Intent.FILL_IN_PACKAGE, destIntent.fillIn(sourceIntent, Intent.FILL_IN_PACKAGE)); 1201 assertEquals(TEST_PACKAGE, destIntent.getPackage()); 1202 } 1203 1204 /** 1205 * Test that fillIn copies extras. 1206 */ 1207 public void testFillIn_extras() { 1208 Intent sourceIntent = new Intent(); 1209 Intent destIntent = new Intent(); 1210 final Bundle bundle = new Bundle(); 1211 bundle.putBoolean(TEST_EXTRA_NAME, true); 1212 sourceIntent.putExtras(bundle); 1213 assertEquals(0, destIntent.fillIn(sourceIntent, 0)); 1214 assertTrue(destIntent.getExtras().getBoolean(TEST_EXTRA_NAME)); 1215 } 1216 1217 public void testGetExtras() { 1218 assertNull(mIntent.getExtras()); 1219 final String expected = "testString"; 1220 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1221 assertNotNull(mIntent.getExtras()); 1222 assertEquals(expected, mIntent.getExtras().getString(TEST_EXTRA_NAME)); 1223 } 1224 1225 public void testGetBooleanExtra() { 1226 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 1227 mIntent.putExtra(TEST_EXTRA_NAME, true); 1228 assertTrue(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 1229 mIntent.putExtra(TEST_EXTRA_NAME, false); 1230 assertFalse(mIntent.getBooleanExtra(TEST_EXTRA_NAME, false)); 1231 } 1232 1233 public void testGetFloatExtra() { 1234 float expected = 2f; 1235 assertEquals(1f, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 1236 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1237 assertEquals(expected, mIntent.getFloatExtra(TEST_EXTRA_NAME, 1f)); 1238 } 1239 1240 public void testGetShortArrayExtra() { 1241 final short[] expected = { 1, 2, 3 }; 1242 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1243 assertEquals(expected, mIntent.getShortArrayExtra(TEST_EXTRA_NAME)); 1244 } 1245 1246 public void testGetStringArrayExtra() { 1247 final String[] expected = { "1d", "2d", "3d" }; 1248 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1249 assertEquals(expected, mIntent.getStringArrayExtra(TEST_EXTRA_NAME)); 1250 } 1251 1252 public void testGetCharSequenceArrayExtra() { 1253 final String[] expected = { "1d", "2d", "3d" }; 1254 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1255 assertEquals(expected, mIntent.getCharSequenceArrayExtra(TEST_EXTRA_NAME)); 1256 } 1257 1258 public void testGetByteArrayExtra() { 1259 final byte[] expected = TEST_ACTION.getBytes(); 1260 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1261 assertEquals(expected, mIntent.getByteArrayExtra(TEST_EXTRA_NAME)); 1262 } 1263 1264 public void testHasFileDescriptors() { 1265 Bundle bundle = mIntent.getExtras(); 1266 assertEquals(bundle != null && bundle.hasFileDescriptors(), mIntent.hasFileDescriptors()); 1267 final byte[] expected = TEST_ACTION.getBytes(); 1268 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1269 bundle = mIntent.getExtras(); 1270 assertEquals(bundle != null && bundle.hasFileDescriptors(), mIntent.hasFileDescriptors()); 1271 } 1272 1273 public void testGetBooleanArrayExtra() { 1274 final boolean[] expected = { true, true, false }; 1275 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1276 assertEquals(expected, mIntent.getBooleanArrayExtra(TEST_EXTRA_NAME)); 1277 } 1278 1279 public void testGetLongExtra() { 1280 final long expected = 2l; 1281 assertEquals(1l, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 1282 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1283 assertEquals(expected, mIntent.getLongExtra(TEST_EXTRA_NAME, 1l)); 1284 } 1285 1286 public void testRemoveCategory() { 1287 assertNull(mIntent.getCategories()); 1288 mIntent.addCategory(TEST_CATEGORY); 1289 assertNotNull(mIntent.getCategories()); 1290 assertEquals(1, mIntent.getCategories().size()); 1291 assertEquals(TEST_CATEGORY, (String) (mIntent.getCategories().toArray()[0])); 1292 mIntent.removeCategory(TEST_CATEGORY); 1293 assertFalse(mIntent.hasCategory(TEST_CATEGORY)); 1294 } 1295 1296 public void testFilterEquals() { 1297 assertFalse(mIntent.filterEquals(null)); 1298 1299 Intent target = new Intent(); 1300 assertTrue(mIntent.filterEquals(target)); 1301 1302 target.setAction(TEST_ACTION); 1303 assertFalse(mIntent.filterEquals(target)); 1304 mIntent.setAction(TEST_ACTION + "test"); 1305 assertFalse(mIntent.filterEquals(target)); 1306 mIntent.setAction(null); 1307 assertFalse(mIntent.filterEquals(target)); 1308 mIntent.setAction(TEST_ACTION); 1309 assertTrue(mIntent.filterEquals(target)); 1310 1311 target.setData(TEST_URI); 1312 assertFalse(mIntent.filterEquals(target)); 1313 mIntent.setData(Uri.parse("myURI")); 1314 assertFalse(mIntent.filterEquals(target)); 1315 mIntent.setData(null); 1316 assertFalse(mIntent.filterEquals(target)); 1317 mIntent.setData(TEST_URI); 1318 assertTrue(mIntent.filterEquals(target)); 1319 1320 target.setType(TEST_TYPE); 1321 assertFalse(mIntent.filterEquals(target)); 1322 mIntent.setType(TEST_TYPE + "test"); 1323 assertFalse(mIntent.filterEquals(target)); 1324 mIntent.setType(null); 1325 assertFalse(mIntent.filterEquals(target)); 1326 mIntent.setType(TEST_TYPE); 1327 assertTrue(mIntent.filterEquals(target)); 1328 1329 target.setComponent(mComponentName); 1330 assertFalse(mIntent.filterEquals(target)); 1331 mIntent.setComponent(new ComponentName(mContext, MockReceiver.class)); 1332 assertFalse(mIntent.filterEquals(target)); 1333 mIntent.setComponent(null); 1334 assertFalse(mIntent.filterEquals(target)); 1335 mIntent.setComponent(mComponentName); 1336 assertTrue(mIntent.filterEquals(target)); 1337 1338 target.addCategory(TEST_CATEGORY); 1339 assertFalse(mIntent.filterEquals(target)); 1340 mIntent.addCategory(TEST_CATEGORY + "test"); 1341 assertFalse(mIntent.filterEquals(target)); 1342 mIntent.addCategory(TEST_CATEGORY); 1343 assertFalse(mIntent.filterEquals(target)); 1344 } 1345 1346 public void testPutExtras1() { 1347 final Intent intent = new Intent(); 1348 mIntent.putExtras(intent); 1349 assertEquals(intent.getExtras(), mIntent.getExtras()); 1350 intent.putExtra("test2", true); 1351 mIntent.putExtras(intent); 1352 assertEquals(intent.getExtras().toString(), mIntent.getExtras().toString()); 1353 } 1354 1355 public void testPutExtras2() { 1356 final Bundle bundle = new Bundle(); 1357 mIntent.putExtras(bundle); 1358 assertEquals(0, mIntent.getExtras().size()); 1359 String expected = "testString"; 1360 bundle.putString(TEST_EXTRA_NAME, expected); 1361 mIntent.putExtras(bundle); 1362 assertEquals(1, mIntent.getExtras().size()); 1363 assertEquals(expected, mIntent.getExtras().getString(TEST_EXTRA_NAME)); 1364 mIntent.putExtra(TEST_EXTRA_NAME, bundle); 1365 assertEquals(bundle, mIntent.getBundleExtra(TEST_EXTRA_NAME)); 1366 } 1367 1368 public void testGetByteExtra() { 1369 final byte expected = Byte.valueOf("1"); 1370 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1371 assertEquals(expected, mIntent.getByteExtra(TEST_EXTRA_NAME, Byte.valueOf("1"))); 1372 } 1373 1374 public void testGetSerializableExtra() { 1375 TestSerializable expected = new TestSerializable(); 1376 expected.Name = "testName"; 1377 mIntent.putExtra(TEST_EXTRA_NAME, expected); 1378 assertEquals(expected, mIntent.getSerializableExtra(TEST_EXTRA_NAME)); 1379 TestSerializable target = (TestSerializable) mIntent.getSerializableExtra(TEST_EXTRA_NAME); 1380 assertEquals(expected.Name, target.Name); 1381 } 1382 1383 public void testReplaceExtras() { 1384 Bundle extras = new Bundle(); 1385 String bundleKey = "testKey"; 1386 String bundleValue = "testValue"; 1387 extras.putString(bundleKey, bundleValue); 1388 1389 Intent intent = mIntent.replaceExtras(extras); 1390 assertSame(mIntent, intent); 1391 String actualValue = intent.getExtras().getString(bundleKey); 1392 assertEquals(bundleValue, actualValue); 1393 1394 Intent src = new Intent(); 1395 String intentName = "srcName"; 1396 String intentValue = "srcValue"; 1397 src.putExtra(intentName, intentValue); 1398 1399 intent = mIntent.replaceExtras(src); 1400 assertSame(mIntent, intent); 1401 actualValue = intent.getExtras().getString(intentName); 1402 assertEquals(intentValue, actualValue); 1403 } 1404 1405 public void testNormalizeMimeType() { 1406 assertEquals(null, Intent.normalizeMimeType(null)); 1407 assertEquals("text/plain", Intent.normalizeMimeType("text/plain; charset=UTF-8")); 1408 assertEquals("text/x-vcard", Intent.normalizeMimeType("text/x-vCard")); 1409 assertEquals("foo/bar", Intent.normalizeMimeType(" foo/bar ")); 1410 } 1411 1412 private static class TestSerializable implements Serializable { 1413 static final long serialVersionUID = 1l; 1414 public String Name; 1415 } 1416 } 1417