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