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