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.os.cts; 18 19 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.Parcel; 23 import android.os.ParcelFileDescriptor; 24 import android.os.Parcelable; 25 import android.test.AndroidTestCase; 26 import android.text.Spannable; 27 import android.text.SpannableString; 28 import android.text.style.ForegroundColorSpan; 29 import android.util.SparseArray; 30 31 import java.io.File; 32 import java.io.FileNotFoundException; 33 import java.util.ArrayList; 34 import java.util.Set; 35 36 public class BundleTest extends AndroidTestCase { 37 private static final boolean BOOLEANKEYVALUE = false; 38 39 private static final int INTKEYVALUE = 20; 40 41 private static final String INTKEY = "intkey"; 42 43 private static final String BOOLEANKEY = "booleankey"; 44 45 public static final String KEY = "Bruce Lee"; 46 47 private static final String KEY2 = "key2"; 48 49 private Spannable mSpannable; 50 51 private Bundle mBundle; 52 53 @Override 54 protected void setUp() throws Exception { 55 super.setUp(); 56 57 mBundle = new Bundle(); 58 mSpannable = new SpannableString("foo bar"); 59 mSpannable.setSpan(new ForegroundColorSpan(0x123456), 0, 3, 0); 60 } 61 62 public void testBundle() { 63 final Bundle b1 = new Bundle(); 64 assertTrue(b1.isEmpty()); 65 b1.putBoolean(KEY, true); 66 assertFalse(b1.isEmpty()); 67 68 final Bundle b2 = new Bundle(b1); 69 assertTrue(b2.getBoolean(KEY)); 70 71 new Bundle(1024); 72 new Bundle(getClass().getClassLoader()); 73 } 74 75 // first put sth into tested Bundle, it shouldn't be empty, then clear it and it should be empty 76 public void testClear() { 77 mBundle.putBoolean("android", true); 78 mBundle.putBoolean(KEY, true); 79 assertFalse(mBundle.isEmpty()); 80 mBundle.clear(); 81 assertTrue(mBundle.isEmpty()); 82 } 83 84 // first clone the tested Bundle, then compare the original Bundle with the 85 // cloned Bundle, they should equal 86 public void testClone() { 87 mBundle.putBoolean(BOOLEANKEY, BOOLEANKEYVALUE); 88 mBundle.putInt(INTKEY, INTKEYVALUE); 89 Bundle cloneBundle = (Bundle) mBundle.clone(); 90 assertEquals(mBundle.size(), cloneBundle.size()); 91 assertEquals(mBundle.getBoolean(BOOLEANKEY), cloneBundle.getBoolean(BOOLEANKEY)); 92 assertEquals(mBundle.getInt(INTKEY), cloneBundle.getInt(INTKEY)); 93 } 94 95 // containsKey would return false if nothing has been put into the Bundle, 96 // else containsKey would return true if any putXXX has been called before 97 public void testContainsKey() { 98 assertFalse(mBundle.containsKey(KEY)); 99 mBundle.putBoolean(KEY, true); 100 assertTrue(mBundle.containsKey(KEY)); 101 roundtrip(); 102 assertTrue(mBundle.containsKey(KEY)); 103 } 104 105 // get would return null if nothing has been put into the Bundle,else get 106 // would return the value set by putXXX 107 public void testGet() { 108 assertNull(mBundle.get(KEY)); 109 mBundle.putBoolean(KEY, true); 110 assertNotNull(mBundle.get(KEY)); 111 roundtrip(); 112 assertNotNull(mBundle.get(KEY)); 113 } 114 115 public void testGetBoolean1() { 116 assertFalse(mBundle.getBoolean(KEY)); 117 mBundle.putBoolean(KEY, true); 118 assertTrue(mBundle.getBoolean(KEY)); 119 roundtrip(); 120 assertTrue(mBundle.getBoolean(KEY)); 121 } 122 123 public void testGetBoolean2() { 124 assertTrue(mBundle.getBoolean(KEY, true)); 125 mBundle.putBoolean(KEY, false); 126 assertFalse(mBundle.getBoolean(KEY, true)); 127 roundtrip(); 128 assertFalse(mBundle.getBoolean(KEY, true)); 129 } 130 131 public void testGetBooleanArray() { 132 assertNull(mBundle.getBooleanArray(KEY)); 133 mBundle.putBooleanArray(KEY, new boolean[] { 134 true, false, true 135 }); 136 boolean[] booleanArray = mBundle.getBooleanArray(KEY); 137 assertNotNull(booleanArray); 138 assertEquals(3, booleanArray.length); 139 assertEquals(true, booleanArray[0]); 140 assertEquals(false, booleanArray[1]); 141 assertEquals(true, booleanArray[2]); 142 roundtrip(); 143 booleanArray = mBundle.getBooleanArray(KEY); 144 assertNotNull(booleanArray); 145 assertEquals(3, booleanArray.length); 146 assertEquals(true, booleanArray[0]); 147 assertEquals(false, booleanArray[1]); 148 assertEquals(true, booleanArray[2]); 149 } 150 151 public void testGetBundle() { 152 assertNull(mBundle.getBundle(KEY)); 153 final Bundle bundle = new Bundle(); 154 mBundle.putBundle(KEY, bundle); 155 assertTrue(bundle.equals(mBundle.getBundle(KEY))); 156 roundtrip(); 157 assertBundleEquals(bundle, mBundle.getBundle(KEY)); 158 } 159 160 public void testGetByte1() { 161 final byte b = 7; 162 163 assertEquals(0, mBundle.getByte(KEY)); 164 mBundle.putByte(KEY, b); 165 assertEquals(b, mBundle.getByte(KEY)); 166 roundtrip(); 167 assertEquals(b, mBundle.getByte(KEY)); 168 } 169 170 public void testGetByte2() { 171 final byte b1 = 6; 172 final byte b2 = 7; 173 174 assertEquals((Byte)b1, mBundle.getByte(KEY, b1)); 175 mBundle.putByte(KEY, b2); 176 assertEquals((Byte)b2, mBundle.getByte(KEY, b1)); 177 roundtrip(); 178 assertEquals((Byte)b2, mBundle.getByte(KEY, b1)); 179 } 180 181 public void testGetByteArray() { 182 assertNull(mBundle.getByteArray(KEY)); 183 mBundle.putByteArray(KEY, new byte[] { 184 1, 2, 3 185 }); 186 byte[] byteArray = mBundle.getByteArray(KEY); 187 assertNotNull(byteArray); 188 assertEquals(3, byteArray.length); 189 assertEquals(1, byteArray[0]); 190 assertEquals(2, byteArray[1]); 191 assertEquals(3, byteArray[2]); 192 roundtrip(); 193 byteArray = mBundle.getByteArray(KEY); 194 assertNotNull(byteArray); 195 assertEquals(3, byteArray.length); 196 assertEquals(1, byteArray[0]); 197 assertEquals(2, byteArray[1]); 198 assertEquals(3, byteArray[2]); 199 } 200 201 public void testGetChar1() { 202 final char c = 'l'; 203 204 assertEquals((char)0, mBundle.getChar(KEY)); 205 mBundle.putChar(KEY, c); 206 assertEquals(c, mBundle.getChar(KEY)); 207 roundtrip(); 208 assertEquals(c, mBundle.getChar(KEY)); 209 } 210 211 public void testGetChar2() { 212 final char c1 = 'l'; 213 final char c2 = 'i'; 214 215 assertEquals(c1, mBundle.getChar(KEY, c1)); 216 mBundle.putChar(KEY, c2); 217 assertEquals(c2, mBundle.getChar(KEY, c1)); 218 roundtrip(); 219 assertEquals(c2, mBundle.getChar(KEY, c1)); 220 } 221 222 public void testGetCharArray() { 223 assertNull(mBundle.getCharArray(KEY)); 224 mBundle.putCharArray(KEY, new char[] { 225 'h', 'i' 226 }); 227 char[] charArray = mBundle.getCharArray(KEY); 228 assertEquals('h', charArray[0]); 229 assertEquals('i', charArray[1]); 230 roundtrip(); 231 charArray = mBundle.getCharArray(KEY); 232 assertEquals('h', charArray[0]); 233 assertEquals('i', charArray[1]); 234 } 235 236 public void testGetCharSequence() { 237 final CharSequence cS = "Bruce Lee"; 238 239 assertNull(mBundle.getCharSequence(KEY)); 240 assertNull(mBundle.getCharSequence(KEY2)); 241 mBundle.putCharSequence(KEY, cS); 242 mBundle.putCharSequence(KEY2, mSpannable); 243 assertEquals(cS, mBundle.getCharSequence(KEY)); 244 assertSpannableEquals(mSpannable, mBundle.getCharSequence(KEY2)); 245 roundtrip(); 246 assertEquals(cS, mBundle.getCharSequence(KEY)); 247 assertSpannableEquals(mSpannable, mBundle.getCharSequence(KEY2)); 248 } 249 250 public void testGetCharSequenceArray() { 251 assertNull(mBundle.getCharSequenceArray(KEY)); 252 mBundle.putCharSequenceArray(KEY, new CharSequence[] { 253 "one", "two", "three", mSpannable 254 }); 255 CharSequence[] ret = mBundle.getCharSequenceArray(KEY); 256 assertEquals(4, ret.length); 257 assertEquals("one", ret[0]); 258 assertEquals("two", ret[1]); 259 assertEquals("three", ret[2]); 260 assertSpannableEquals(mSpannable, ret[3]); 261 roundtrip(); 262 ret = mBundle.getCharSequenceArray(KEY); 263 assertEquals(4, ret.length); 264 assertEquals("one", ret[0]); 265 assertEquals("two", ret[1]); 266 assertEquals("three", ret[2]); 267 assertSpannableEquals(mSpannable, ret[3]); 268 } 269 270 public void testGetCharSequenceArrayList() { 271 assertNull(mBundle.getCharSequenceArrayList(KEY)); 272 final ArrayList<CharSequence> list = new ArrayList<CharSequence>(); 273 list.add("one"); 274 list.add("two"); 275 list.add("three"); 276 list.add(mSpannable); 277 mBundle.putCharSequenceArrayList(KEY, list); 278 roundtrip(); 279 ArrayList<CharSequence> ret = mBundle.getCharSequenceArrayList(KEY); 280 assertEquals(4, ret.size()); 281 assertEquals("one", ret.get(0)); 282 assertEquals("two", ret.get(1)); 283 assertEquals("three", ret.get(2)); 284 assertSpannableEquals(mSpannable, ret.get(3)); 285 roundtrip(); 286 ret = mBundle.getCharSequenceArrayList(KEY); 287 assertEquals(4, ret.size()); 288 assertEquals("one", ret.get(0)); 289 assertEquals("two", ret.get(1)); 290 assertEquals("three", ret.get(2)); 291 assertSpannableEquals(mSpannable, ret.get(3)); 292 } 293 294 public void testGetDouble1() { 295 final double d = 10.07; 296 297 assertEquals(0.0, mBundle.getDouble(KEY)); 298 mBundle.putDouble(KEY, d); 299 assertEquals(d, mBundle.getDouble(KEY)); 300 roundtrip(); 301 assertEquals(d, mBundle.getDouble(KEY)); 302 } 303 304 public void testGetDouble2() { 305 final double d1 = 10.06; 306 final double d2 = 10.07; 307 308 assertEquals(d1, mBundle.getDouble(KEY, d1)); 309 mBundle.putDouble(KEY, d2); 310 assertEquals(d2, mBundle.getDouble(KEY, d1)); 311 roundtrip(); 312 assertEquals(d2, mBundle.getDouble(KEY, d1)); 313 } 314 315 public void testGetDoubleArray() { 316 assertNull(mBundle.getDoubleArray(KEY)); 317 mBundle.putDoubleArray(KEY, new double[] { 318 10.06, 10.07 319 }); 320 double[] doubleArray = mBundle.getDoubleArray(KEY); 321 assertEquals(10.06, doubleArray[0]); 322 assertEquals(10.07, doubleArray[1]); 323 roundtrip(); 324 doubleArray = mBundle.getDoubleArray(KEY); 325 assertEquals(10.06, doubleArray[0]); 326 assertEquals(10.07, doubleArray[1]); 327 } 328 329 public void testGetFloat1() { 330 final float f = 10.07f; 331 332 assertEquals(0.0f, mBundle.getFloat(KEY)); 333 mBundle.putFloat(KEY, f); 334 assertEquals(f, mBundle.getFloat(KEY)); 335 roundtrip(); 336 assertEquals(f, mBundle.getFloat(KEY)); 337 } 338 339 public void testGetFloat2() { 340 final float f1 = 10.06f; 341 final float f2 = 10.07f; 342 343 assertEquals(f1, mBundle.getFloat(KEY, f1)); 344 mBundle.putFloat(KEY, f2); 345 assertEquals(f2, mBundle.getFloat(KEY, f1)); 346 roundtrip(); 347 assertEquals(f2, mBundle.getFloat(KEY, f1)); 348 } 349 350 public void testGetFloatArray() { 351 assertNull(mBundle.getFloatArray(KEY)); 352 mBundle.putFloatArray(KEY, new float[] { 353 10.06f, 10.07f 354 }); 355 float[] floatArray = mBundle.getFloatArray(KEY); 356 assertEquals(10.06f, floatArray[0]); 357 assertEquals(10.07f, floatArray[1]); 358 roundtrip(); 359 floatArray = mBundle.getFloatArray(KEY); 360 assertEquals(10.06f, floatArray[0]); 361 assertEquals(10.07f, floatArray[1]); 362 } 363 364 public void testGetInt1() { 365 final int i = 1007; 366 367 assertEquals(0, mBundle.getInt(KEY)); 368 mBundle.putInt(KEY, i); 369 assertEquals(i, mBundle.getInt(KEY)); 370 roundtrip(); 371 assertEquals(i, mBundle.getInt(KEY)); 372 } 373 374 public void testGetInt2() { 375 final int i1 = 1006; 376 final int i2 = 1007; 377 378 assertEquals(i1, mBundle.getInt(KEY, i1)); 379 mBundle.putInt(KEY, i2); 380 assertEquals(i2, mBundle.getInt(KEY, i2)); 381 roundtrip(); 382 assertEquals(i2, mBundle.getInt(KEY, i2)); 383 } 384 385 public void testGetIntArray() { 386 assertNull(mBundle.getIntArray(KEY)); 387 mBundle.putIntArray(KEY, new int[] { 388 1006, 1007 389 }); 390 int[] intArray = mBundle.getIntArray(KEY); 391 assertEquals(1006, intArray[0]); 392 assertEquals(1007, intArray[1]); 393 roundtrip(); 394 intArray = mBundle.getIntArray(KEY); 395 assertEquals(1006, intArray[0]); 396 assertEquals(1007, intArray[1]); 397 } 398 399 // getIntegerArrayList should only return the IntegerArrayList set by putIntegerArrayLis 400 public void testGetIntegerArrayList() { 401 final int i1 = 1006; 402 final int i2 = 1007; 403 404 assertNull(mBundle.getIntegerArrayList(KEY)); 405 final ArrayList<Integer> arrayList = new ArrayList<Integer>(); 406 arrayList.add(i1); 407 arrayList.add(i2); 408 mBundle.putIntegerArrayList(KEY, arrayList); 409 ArrayList<Integer> retArrayList = mBundle.getIntegerArrayList(KEY); 410 assertNotNull(retArrayList); 411 assertEquals(2, retArrayList.size()); 412 assertEquals((Integer)i1, retArrayList.get(0)); 413 assertEquals((Integer)i2, retArrayList.get(1)); 414 roundtrip(); 415 retArrayList = mBundle.getIntegerArrayList(KEY); 416 assertNotNull(retArrayList); 417 assertEquals(2, retArrayList.size()); 418 assertEquals((Integer)i1, retArrayList.get(0)); 419 assertEquals((Integer)i2, retArrayList.get(1)); 420 } 421 422 public void testGetLong1() { 423 final long l = 1007; 424 425 assertEquals(0, mBundle.getLong(KEY)); 426 mBundle.putLong(KEY, l); 427 assertEquals(l, mBundle.getLong(KEY)); 428 roundtrip(); 429 assertEquals(l, mBundle.getLong(KEY)); 430 } 431 432 public void testGetLong2() { 433 final long l1 = 1006; 434 final long l2 = 1007; 435 436 assertEquals(l1, mBundle.getLong(KEY, l1)); 437 mBundle.putLong(KEY, l2); 438 assertEquals(l2, mBundle.getLong(KEY, l2)); 439 roundtrip(); 440 assertEquals(l2, mBundle.getLong(KEY, l2)); 441 } 442 443 public void testGetLongArray() { 444 assertNull(mBundle.getLongArray(KEY)); 445 mBundle.putLongArray(KEY, new long[] { 446 1006, 1007 447 }); 448 long[] longArray = mBundle.getLongArray(KEY); 449 assertEquals(1006, longArray[0]); 450 assertEquals(1007, longArray[1]); 451 roundtrip(); 452 longArray = mBundle.getLongArray(KEY); 453 assertEquals(1006, longArray[0]); 454 assertEquals(1007, longArray[1]); 455 } 456 457 public void testGetParcelable() { 458 assertNull(mBundle.getParcelable(KEY)); 459 final Bundle bundle = new Bundle(); 460 mBundle.putParcelable(KEY, bundle); 461 assertTrue(bundle.equals(mBundle.getParcelable(KEY))); 462 roundtrip(); 463 assertBundleEquals(bundle, (Bundle) mBundle.getParcelable(KEY)); 464 } 465 466 // getParcelableArray should only return the ParcelableArray set by putParcelableArray 467 public void testGetParcelableArray() { 468 assertNull(mBundle.getParcelableArray(KEY)); 469 final Bundle bundle1 = new Bundle(); 470 final Bundle bundle2 = new Bundle(); 471 mBundle.putParcelableArray(KEY, new Bundle[] { 472 bundle1, bundle2 473 }); 474 Parcelable[] parcelableArray = mBundle.getParcelableArray(KEY); 475 assertEquals(2, parcelableArray.length); 476 assertTrue(bundle1.equals(parcelableArray[0])); 477 assertTrue(bundle2.equals(parcelableArray[1])); 478 roundtrip(); 479 parcelableArray = mBundle.getParcelableArray(KEY); 480 assertEquals(2, parcelableArray.length); 481 assertBundleEquals(bundle1, (Bundle) parcelableArray[0]); 482 assertBundleEquals(bundle2, (Bundle) parcelableArray[1]); 483 } 484 485 // getParcelableArrayList should only return the parcelableArrayList set by putParcelableArrayList 486 public void testGetParcelableArrayList() { 487 assertNull(mBundle.getParcelableArrayList(KEY)); 488 final ArrayList<Parcelable> parcelableArrayList = new ArrayList<Parcelable>(); 489 final Bundle bundle1 = new Bundle(); 490 final Bundle bundle2 = new Bundle(); 491 parcelableArrayList.add(bundle1); 492 parcelableArrayList.add(bundle2); 493 mBundle.putParcelableArrayList(KEY, parcelableArrayList); 494 ArrayList<Parcelable> ret = mBundle.getParcelableArrayList(KEY); 495 assertEquals(2, ret.size()); 496 assertTrue(bundle1.equals(ret.get(0))); 497 assertTrue(bundle2.equals(ret.get(1))); 498 roundtrip(); 499 ret = mBundle.getParcelableArrayList(KEY); 500 assertEquals(2, ret.size()); 501 assertBundleEquals(bundle1, (Bundle) ret.get(0)); 502 assertBundleEquals(bundle2, (Bundle) ret.get(1)); 503 } 504 505 public void testGetSerializable() { 506 assertNull(mBundle.getSerializable(KEY)); 507 mBundle.putSerializable(KEY, "android"); 508 assertEquals("android", mBundle.getSerializable(KEY)); 509 roundtrip(); 510 assertEquals("android", mBundle.getSerializable(KEY)); 511 } 512 513 public void testGetShort1() { 514 final short s = 1007; 515 516 assertEquals(0, mBundle.getShort(KEY)); 517 mBundle.putShort(KEY, s); 518 assertEquals(s, mBundle.getShort(KEY)); 519 roundtrip(); 520 assertEquals(s, mBundle.getShort(KEY)); 521 } 522 523 public void testGetShort2() { 524 final short s1 = 1006; 525 final short s2 = 1007; 526 527 assertEquals(s1, mBundle.getShort(KEY, s1)); 528 mBundle.putShort(KEY, s2); 529 assertEquals(s2, mBundle.getShort(KEY, s1)); 530 roundtrip(); 531 assertEquals(s2, mBundle.getShort(KEY, s1)); 532 } 533 534 public void testGetShortArray() { 535 final short s1 = 1006; 536 final short s2 = 1007; 537 538 assertNull(mBundle.getShortArray(KEY)); 539 mBundle.putShortArray(KEY, new short[] { 540 s1, s2 541 }); 542 short[] shortArray = mBundle.getShortArray(KEY); 543 assertEquals(s1, shortArray[0]); 544 assertEquals(s2, shortArray[1]); 545 roundtrip(); 546 shortArray = mBundle.getShortArray(KEY); 547 assertEquals(s1, shortArray[0]); 548 assertEquals(s2, shortArray[1]); 549 } 550 551 // getSparseParcelableArray should only return the SparseArray<Parcelable> 552 // set by putSparseParcelableArray 553 public void testGetSparseParcelableArray() { 554 assertNull(mBundle.getSparseParcelableArray(KEY)); 555 final SparseArray<Parcelable> sparseArray = new SparseArray<Parcelable>(); 556 final Bundle bundle = new Bundle(); 557 final Intent intent = new Intent(); 558 sparseArray.put(1006, bundle); 559 sparseArray.put(1007, intent); 560 mBundle.putSparseParcelableArray(KEY, sparseArray); 561 SparseArray<Parcelable> ret = mBundle.getSparseParcelableArray(KEY); 562 assertEquals(2, ret.size()); 563 assertNull(ret.get(1008)); 564 assertTrue(bundle.equals(ret.get(1006))); 565 assertTrue(intent.equals(ret.get(1007))); 566 roundtrip(); 567 ret = mBundle.getSparseParcelableArray(KEY); 568 assertEquals(2, ret.size()); 569 assertNull(ret.get(1008)); 570 assertBundleEquals(bundle, (Bundle) ret.get(1006)); 571 assertIntentEquals(intent, (Intent) ret.get(1007)); 572 } 573 574 public void testGetString() { 575 assertNull(mBundle.getString(KEY)); 576 mBundle.putString(KEY, "android"); 577 assertEquals("android", mBundle.getString(KEY)); 578 roundtrip(); 579 assertEquals("android", mBundle.getString(KEY)); 580 } 581 582 public void testGetStringArray() { 583 assertNull(mBundle.getStringArray(KEY)); 584 mBundle.putStringArray(KEY, new String[] { 585 "one", "two", "three" 586 }); 587 String[] ret = mBundle.getStringArray(KEY); 588 assertEquals("one", ret[0]); 589 assertEquals("two", ret[1]); 590 assertEquals("three", ret[2]); 591 roundtrip(); 592 ret = mBundle.getStringArray(KEY); 593 assertEquals("one", ret[0]); 594 assertEquals("two", ret[1]); 595 assertEquals("three", ret[2]); 596 } 597 598 // getStringArrayList should only return the StringArrayList set by putStringArrayList 599 public void testGetStringArrayList() { 600 assertNull(mBundle.getStringArrayList(KEY)); 601 final ArrayList<String> stringArrayList = new ArrayList<String>(); 602 stringArrayList.add("one"); 603 stringArrayList.add("two"); 604 stringArrayList.add("three"); 605 mBundle.putStringArrayList(KEY, stringArrayList); 606 ArrayList<String> ret = mBundle.getStringArrayList(KEY); 607 assertEquals(3, ret.size()); 608 assertEquals("one", ret.get(0)); 609 assertEquals("two", ret.get(1)); 610 assertEquals("three", ret.get(2)); 611 roundtrip(); 612 ret = mBundle.getStringArrayList(KEY); 613 assertEquals(3, ret.size()); 614 assertEquals("one", ret.get(0)); 615 assertEquals("two", ret.get(1)); 616 assertEquals("three", ret.get(2)); 617 } 618 619 public void testKeySet() { 620 Set<String> setKey = mBundle.keySet(); 621 assertFalse(setKey.contains("one")); 622 assertFalse(setKey.contains("two")); 623 mBundle.putBoolean("one", true); 624 mBundle.putChar("two", 't'); 625 setKey = mBundle.keySet(); 626 assertEquals(2, setKey.size()); 627 assertTrue(setKey.contains("one")); 628 assertTrue(setKey.contains("two")); 629 assertFalse(setKey.contains("three")); 630 roundtrip(); 631 setKey = mBundle.keySet(); 632 assertEquals(2, setKey.size()); 633 assertTrue(setKey.contains("one")); 634 assertTrue(setKey.contains("two")); 635 assertFalse(setKey.contains("three")); 636 } 637 638 // same as hasFileDescriptors, the only difference is that describeContents 639 // return 0 if no fd and return 1 if has fd for the tested Bundle 640 641 public void testDescribeContents() { 642 assertTrue((mBundle.describeContents() 643 & Parcelable.CONTENTS_FILE_DESCRIPTOR) == 0); 644 645 final Parcel parcel = Parcel.obtain(); 646 try { 647 mBundle.putParcelable("foo", ParcelFileDescriptor.open( 648 new File("/system"), ParcelFileDescriptor.MODE_READ_ONLY)); 649 } catch (FileNotFoundException e) { 650 throw new RuntimeException("can't open /system", e); 651 } 652 assertTrue((mBundle.describeContents() 653 & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0); 654 mBundle.writeToParcel(parcel, 0); 655 mBundle.clear(); 656 assertTrue((mBundle.describeContents() 657 & Parcelable.CONTENTS_FILE_DESCRIPTOR) == 0); 658 parcel.setDataPosition(0); 659 mBundle.readFromParcel(parcel); 660 assertTrue((mBundle.describeContents() 661 & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0); 662 ParcelFileDescriptor pfd = (ParcelFileDescriptor)mBundle.getParcelable("foo"); 663 assertTrue((mBundle.describeContents() 664 & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0); 665 } 666 667 // case 1: The default bundle doesn't has FileDescriptor. 668 // case 2: The tested Bundle should has FileDescriptor 669 // if it read data from a Parcel object, which is created with a FileDescriptor. 670 // case 3: The tested Bundle should has FileDescriptor 671 // if put a Parcelable object, which is created with a FileDescriptor, into it. 672 public void testHasFileDescriptors() { 673 assertFalse(mBundle.hasFileDescriptors()); 674 675 final Parcel parcel = Parcel.obtain(); 676 assertFalse(parcel.hasFileDescriptors()); 677 try { 678 mBundle.putParcelable("foo", ParcelFileDescriptor.open( 679 new File("/system"), ParcelFileDescriptor.MODE_READ_ONLY)); 680 } catch (FileNotFoundException e) { 681 throw new RuntimeException("can't open /system", e); 682 } 683 assertTrue(mBundle.hasFileDescriptors()); 684 mBundle.writeToParcel(parcel, 0); 685 assertTrue(parcel.hasFileDescriptors()); 686 mBundle.clear(); 687 assertFalse(mBundle.hasFileDescriptors()); 688 parcel.setDataPosition(0); 689 mBundle.readFromParcel(parcel); 690 assertTrue(mBundle.hasFileDescriptors()); 691 ParcelFileDescriptor pfd = (ParcelFileDescriptor)mBundle.getParcelable("foo"); 692 assertTrue(mBundle.hasFileDescriptors()); 693 } 694 695 public void testSetClassLoader() { 696 mBundle.setClassLoader(new MockClassLoader()); 697 } 698 699 // Write the bundle(A) to a parcel(B), and then create a bundle(C) from B. 700 // C should be same as A. 701 public void testWriteToParcel() { 702 final String li = "Bruce Li"; 703 704 mBundle.putString(KEY, li); 705 final Parcel parcel = Parcel.obtain(); 706 mBundle.writeToParcel(parcel, 0); 707 parcel.setDataPosition(0); 708 final Bundle bundle = Bundle.CREATOR.createFromParcel(parcel); 709 assertEquals(li, bundle.getString(KEY)); 710 } 711 712 // test the size should be right after add/remove key-value pair of the Bundle. 713 public void testSize() { 714 assertEquals(0, mBundle.size()); 715 mBundle.putBoolean("one", true); 716 assertEquals(1, mBundle.size()); 717 718 mBundle.putBoolean("two", true); 719 assertEquals(2, mBundle.size()); 720 721 mBundle.putBoolean("three", true); 722 assertEquals(3, mBundle.size()); 723 724 mBundle.putBoolean("four", true); 725 mBundle.putBoolean("five", true); 726 assertEquals(5, mBundle.size()); 727 mBundle.remove("six"); 728 assertEquals(5, mBundle.size()); 729 730 mBundle.remove("one"); 731 assertEquals(4, mBundle.size()); 732 mBundle.remove("one"); 733 assertEquals(4, mBundle.size()); 734 735 mBundle.remove("two"); 736 assertEquals(3, mBundle.size()); 737 738 mBundle.remove("three"); 739 mBundle.remove("four"); 740 mBundle.remove("five"); 741 assertEquals(0, mBundle.size()); 742 } 743 744 // The return value of toString() should not be null. 745 public void testToString() { 746 assertNotNull(mBundle.toString()); 747 mBundle.putString("foo", "this test is so stupid"); 748 assertNotNull(mBundle.toString()); 749 } 750 751 // The tested Bundle should hold mappings from the given after putAll be invoked. 752 public void testPutAll() { 753 assertEquals(0, mBundle.size()); 754 755 final Bundle map = new Bundle(); 756 map.putBoolean(KEY, true); 757 assertEquals(1, map.size()); 758 mBundle.putAll(map); 759 assertEquals(1, mBundle.size()); 760 } 761 762 private void roundtrip() { 763 Parcel out = Parcel.obtain(); 764 mBundle.writeToParcel(out, 0); 765 Parcel in = roundtripParcel(out); 766 mBundle = in.readBundle(); 767 } 768 769 private Parcel roundtripParcel(Parcel out) { 770 byte[] buf = out.marshall(); 771 Parcel in = Parcel.obtain(); 772 in.unmarshall(buf, 0, buf.length); 773 in.setDataPosition(0); 774 return in; 775 } 776 777 private void assertBundleEquals(Bundle expected, Bundle observed) { 778 assertEquals(expected.size(), observed.size()); 779 for (String key : expected.keySet()) { 780 assertEquals(expected.get(key), observed.get(key)); 781 } 782 } 783 784 private void assertIntentEquals(Intent expected, Intent observed) { 785 assertEquals(expected.toUri(0), observed.toUri(0)); 786 } 787 788 private void assertSpannableEquals(Spannable expected, CharSequence observed) { 789 Spannable s = (Spannable) observed; 790 assertEquals(expected.toString(), observed.toString()); 791 Object[] expectedSpans = expected.getSpans(0, expected.length(), Object.class); 792 Object[] observedSpans = expected.getSpans(0, expected.length(), Object.class); 793 assertEquals(expectedSpans.length, observedSpans.length); 794 for (int i = 0; i < expectedSpans.length; i++) { 795 // Can't compare values of arbitrary objects 796 assertEquals(expectedSpans[i].getClass(), observedSpans[i].getClass()); 797 } 798 } 799 800 class MockClassLoader extends ClassLoader { 801 MockClassLoader() { 802 super(); 803 } 804 805 MockClassLoader(ClassLoader parent) { 806 super(parent); 807 } 808 } 809 } 810