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 import dalvik.annotation.TestLevel; 20 import dalvik.annotation.TestTargetClass; 21 import dalvik.annotation.TestTargetNew; 22 import dalvik.annotation.TestTargets; 23 import dalvik.annotation.ToBeFixed; 24 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.os.Parcel; 28 import android.os.Parcelable; 29 import android.os.ParcelFileDescriptor; 30 import android.test.AndroidTestCase; 31 import android.util.SparseArray; 32 33 import java.io.File; 34 import java.io.FileNotFoundException; 35 import java.util.ArrayList; 36 import java.util.Set; 37 38 @TestTargetClass(Bundle.class) 39 public class BundleTest extends AndroidTestCase { 40 private static final boolean BOOLEANKEYVALUE = false; 41 42 private static final int INTKEYVALUE = 20; 43 44 private static final String INTKEY = "intkey"; 45 46 private static final String BOOLEANKEY = "booleankey"; 47 48 public static final String KEY = "Bruce Lee"; 49 50 private Bundle mBundle; 51 52 @Override setUp()53 protected void setUp() throws Exception { 54 super.setUp(); 55 56 mBundle = new Bundle(); 57 } 58 59 @TestTargets({ 60 @TestTargetNew( 61 level = TestLevel.COMPLETE, 62 notes = "test all constructors", 63 method = "Bundle", 64 args = {} 65 ), 66 @TestTargetNew( 67 level = TestLevel.COMPLETE, 68 notes = "test all constructors", 69 method = "Bundle", 70 args = {android.os.Bundle.class} 71 ), 72 @TestTargetNew( 73 level = TestLevel.COMPLETE, 74 notes = "test all constructors", 75 method = "Bundle", 76 args = {java.lang.ClassLoader.class} 77 ), 78 @TestTargetNew( 79 level = TestLevel.COMPLETE, 80 notes = "test all constructors", 81 method = "Bundle", 82 args = {int.class} 83 ) 84 }) testBundle()85 public void testBundle() { 86 final Bundle b1 = new Bundle(); 87 assertTrue(b1.isEmpty()); 88 b1.putBoolean(KEY, true); 89 assertFalse(b1.isEmpty()); 90 91 final Bundle b2 = new Bundle(b1); 92 assertTrue(b2.getBoolean(KEY)); 93 94 new Bundle(1024); 95 new Bundle(getClass().getClassLoader()); 96 } 97 98 // first put sth into tested Bundle, it shouldn't be empty, then clear it and it should be empty 99 @TestTargets({ 100 @TestTargetNew( 101 level = TestLevel.COMPLETE, 102 method = "clear", 103 args = {} 104 ), 105 @TestTargetNew( 106 level = TestLevel.COMPLETE, 107 method = "isEmpty", 108 args = {} 109 ) 110 }) testClear()111 public void testClear() { 112 mBundle.putBoolean("android", true); 113 mBundle.putBoolean(KEY, true); 114 assertFalse(mBundle.isEmpty()); 115 mBundle.clear(); 116 assertTrue(mBundle.isEmpty()); 117 } 118 119 // first clone the tested Bundle, then compare the original Bundle with the 120 // cloned Bundle, they should equal 121 @TestTargetNew( 122 level = TestLevel.COMPLETE, 123 method = "clone", 124 args = {} 125 ) testClone()126 public void testClone() { 127 mBundle.putBoolean(BOOLEANKEY, BOOLEANKEYVALUE); 128 mBundle.putInt(INTKEY, INTKEYVALUE); 129 Bundle cloneBundle = (Bundle) mBundle.clone(); 130 assertEquals(mBundle.size(), cloneBundle.size()); 131 assertEquals(mBundle.getBoolean(BOOLEANKEY), cloneBundle.getBoolean(BOOLEANKEY)); 132 assertEquals(mBundle.getInt(INTKEY), cloneBundle.getInt(INTKEY)); 133 } 134 135 // containsKey would return false if nothing has been put into the Bundle, 136 // else containsKey would return true if any putXXX has been called before 137 @TestTargetNew( 138 level = TestLevel.COMPLETE, 139 method = "containsKey", 140 args = {java.lang.String.class} 141 ) testContainsKey()142 public void testContainsKey() { 143 assertFalse(mBundle.containsKey(KEY)); 144 mBundle.putBoolean(KEY, true); 145 assertTrue(mBundle.containsKey(KEY)); 146 } 147 148 // get would return null if nothing has been put into the Bundle,else get 149 // would return the value set by putXXX 150 @TestTargetNew( 151 level = TestLevel.COMPLETE, 152 method = "get", 153 args = {java.lang.String.class} 154 ) testGet()155 public void testGet() { 156 assertNull(mBundle.get(KEY)); 157 mBundle.putBoolean(KEY, true); 158 assertNotNull(mBundle.get(KEY)); 159 } 160 161 @TestTargets({ 162 @TestTargetNew( 163 level = TestLevel.COMPLETE, 164 notes = "getBoolean should only return the Boolean set by putBoolean", 165 method = "getBoolean", 166 args = {java.lang.String.class} 167 ), 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 notes = "getBoolean should only return the Boolean set by putBoolean", 171 method = "putBoolean", 172 args = {java.lang.String.class, boolean.class} 173 ) 174 }) testGetBoolean1()175 public void testGetBoolean1() { 176 assertFalse(mBundle.getBoolean(KEY)); 177 mBundle.putBoolean(KEY, true); 178 assertTrue(mBundle.getBoolean(KEY)); 179 } 180 181 @TestTargets({ 182 @TestTargetNew( 183 level = TestLevel.COMPLETE, 184 notes = "getBoolean should only return the Boolean set by putBoolean", 185 method = "getBoolean", 186 args = {java.lang.String.class, boolean.class} 187 ), 188 @TestTargetNew( 189 level = TestLevel.COMPLETE, 190 notes = "getBoolean should only return the Boolean set by putBoolean", 191 method = "putBoolean", 192 args = {java.lang.String.class, boolean.class} 193 ) 194 }) testGetBoolean2()195 public void testGetBoolean2() { 196 assertTrue(mBundle.getBoolean(KEY, true)); 197 mBundle.putBoolean(KEY, false); 198 assertFalse(mBundle.getBoolean(KEY, true)); 199 } 200 201 @TestTargets({ 202 @TestTargetNew( 203 level = TestLevel.COMPLETE, 204 notes = "getBooleanArray should only return the BooleanArray set by putBooleanArray", 205 method = "getBooleanArray", 206 args = {java.lang.String.class} 207 ), 208 @TestTargetNew( 209 level = TestLevel.COMPLETE, 210 notes = "getBooleanArray should only return the BooleanArray set by putBooleanArray", 211 method = "putBooleanArray", 212 args = {java.lang.String.class, boolean[].class} 213 ) 214 }) testGetBooleanArray()215 public void testGetBooleanArray() { 216 assertNull(mBundle.getBooleanArray(KEY)); 217 mBundle.putBooleanArray(KEY, new boolean[] { 218 true, false, true 219 }); 220 final boolean[] booleanArray = mBundle.getBooleanArray(KEY); 221 assertNotNull(booleanArray); 222 assertEquals(3, booleanArray.length); 223 assertEquals(true, booleanArray[0]); 224 assertEquals(false, booleanArray[1]); 225 assertEquals(true, booleanArray[2]); 226 } 227 228 @TestTargets({ 229 @TestTargetNew( 230 level = TestLevel.COMPLETE, 231 notes = "getBundle should only return the Bundle set by putBundle", 232 method = "getBundle", 233 args = {java.lang.String.class} 234 ), 235 @TestTargetNew( 236 level = TestLevel.COMPLETE, 237 notes = "getBundle should only return the Bundle set by putBundle", 238 method = "putBundle", 239 args = {java.lang.String.class, android.os.Bundle.class} 240 ) 241 }) testGetBundle()242 public void testGetBundle() { 243 assertNull(mBundle.getBundle(KEY)); 244 final Bundle bundle = new Bundle(); 245 mBundle.putBundle(KEY, bundle); 246 assertTrue(bundle.equals(mBundle.getBundle(KEY))); 247 } 248 249 @TestTargets({ 250 @TestTargetNew( 251 level = TestLevel.COMPLETE, 252 notes = "getByte should only return the Byte set by putByte", 253 method = "getByte", 254 args = {java.lang.String.class} 255 ), 256 @TestTargetNew( 257 level = TestLevel.COMPLETE, 258 notes = "getByte should only return the Byte set by putByte", 259 method = "putByte", 260 args = {java.lang.String.class, byte.class} 261 ) 262 }) testGetByte1()263 public void testGetByte1() { 264 final byte b = 7; 265 266 assertEquals(0, mBundle.getByte(KEY)); 267 mBundle.putByte(KEY, b); 268 assertEquals(b, mBundle.getByte(KEY)); 269 } 270 271 @TestTargets({ 272 @TestTargetNew( 273 level = TestLevel.COMPLETE, 274 notes = "getByte should only return the Byte set by putByte", 275 method = "getByte", 276 args = {java.lang.String.class, byte.class} 277 ), 278 @TestTargetNew( 279 level = TestLevel.COMPLETE, 280 notes = "getByte should only return the Byte set by putByte", 281 method = "putByte", 282 args = {java.lang.String.class, byte.class} 283 ) 284 }) testGetByte2()285 public void testGetByte2() { 286 final byte b1 = 6; 287 final byte b2 = 7; 288 289 assertEquals((Byte)b1, mBundle.getByte(KEY, b1)); 290 mBundle.putByte(KEY, b2); 291 assertEquals((Byte)b2, mBundle.getByte(KEY, b1)); 292 } 293 294 @TestTargets({ 295 @TestTargetNew( 296 level = TestLevel.COMPLETE, 297 notes = "getByteArray should only return the ByteArray set by putByteArray", 298 method = "getByteArray", 299 args = {java.lang.String.class} 300 ), 301 @TestTargetNew( 302 level = TestLevel.COMPLETE, 303 notes = "getByteArray should only return the ByteArray set by putByteArray", 304 method = "putByteArray", 305 args = {java.lang.String.class, byte[].class} 306 ) 307 }) testGetByteArray()308 public void testGetByteArray() { 309 assertNull(mBundle.getByteArray(KEY)); 310 mBundle.putByteArray(KEY, new byte[] { 311 1, 2, 3 312 }); 313 final byte[] byteArray = mBundle.getByteArray(KEY); 314 assertNotNull(byteArray); 315 assertEquals(3, byteArray.length); 316 assertEquals(1, byteArray[0]); 317 assertEquals(2, byteArray[1]); 318 assertEquals(3, byteArray[2]); 319 } 320 321 @TestTargets({ 322 @TestTargetNew( 323 level = TestLevel.COMPLETE, 324 notes = "getChar should only return the Char set by putChar", 325 method = "getChar", 326 args = {java.lang.String.class} 327 ), 328 @TestTargetNew( 329 level = TestLevel.COMPLETE, 330 notes = "getChar should only return the Char set by putChar", 331 method = "putChar", 332 args = {java.lang.String.class, char.class} 333 ) 334 }) testGetChar1()335 public void testGetChar1() { 336 final char c = 'l'; 337 338 assertEquals((char)0, mBundle.getChar(KEY)); 339 mBundle.putChar(KEY, c); 340 assertEquals(c, mBundle.getChar(KEY)); 341 } 342 343 @TestTargets({ 344 @TestTargetNew( 345 level = TestLevel.COMPLETE, 346 notes = "getChar should only return the Char set by putChar", 347 method = "getChar", 348 args = {java.lang.String.class, char.class} 349 ), 350 @TestTargetNew( 351 level = TestLevel.COMPLETE, 352 notes = "getChar should only return the Char set by putChar", 353 method = "putChar", 354 args = {java.lang.String.class, char.class} 355 ) 356 }) testGetChar2()357 public void testGetChar2() { 358 final char c1 = 'l'; 359 final char c2 = 'i'; 360 361 assertEquals(c1, mBundle.getChar(KEY, c1)); 362 mBundle.putChar(KEY, c2); 363 assertEquals(c2, mBundle.getChar(KEY, c1)); 364 } 365 366 @TestTargets({ 367 @TestTargetNew( 368 level = TestLevel.COMPLETE, 369 notes = "getCharArray should only return the CharArray set by putCharArray", 370 method = "getCharArray", 371 args = {java.lang.String.class} 372 ), 373 @TestTargetNew( 374 level = TestLevel.COMPLETE, 375 notes = "getCharArray should only return the CharArray set by putCharArray", 376 method = "putCharArray", 377 args = {java.lang.String.class, char[].class} 378 ) 379 }) testGetCharArray()380 public void testGetCharArray() { 381 assertNull(mBundle.getCharArray(KEY)); 382 mBundle.putCharArray(KEY, new char[] { 383 'h', 'i' 384 }); 385 final char[] charArray = mBundle.getCharArray(KEY); 386 assertEquals('h', charArray[0]); 387 assertEquals('i', charArray[1]); 388 } 389 390 @TestTargets({ 391 @TestTargetNew( 392 level = TestLevel.COMPLETE, 393 notes = "getCharSequence should only return the CharSequence set by putCharSequence", 394 method = "getCharSequence", 395 args = {java.lang.String.class} 396 ), 397 @TestTargetNew( 398 level = TestLevel.COMPLETE, 399 notes = "getCharSequence should only return the CharSequence set by putCharSequence", 400 method = "putCharSequence", 401 args = {java.lang.String.class, java.lang.CharSequence.class} 402 ) 403 }) testGetCharSequence()404 public void testGetCharSequence() { 405 final CharSequence cS = "Bruce Lee"; 406 407 assertNull(mBundle.getCharSequence(KEY)); 408 mBundle.putCharSequence(KEY, cS); 409 assertEquals(cS, mBundle.getCharSequence(KEY)); 410 } 411 412 @TestTargets({ 413 @TestTargetNew( 414 level = TestLevel.COMPLETE, 415 notes = "getDouble should return the Double set by putDouble", 416 method = "getDouble", 417 args = {java.lang.String.class} 418 ), 419 @TestTargetNew( 420 level = TestLevel.COMPLETE, 421 notes = "getDouble should return the Double set by putDouble", 422 method = "putDouble", 423 args = {java.lang.String.class, double.class} 424 ) 425 }) testGetDouble1()426 public void testGetDouble1() { 427 final double d = 10.07; 428 429 assertEquals(0.0, mBundle.getDouble(KEY)); 430 mBundle.putDouble(KEY, d); 431 assertEquals(d, mBundle.getDouble(KEY)); 432 } 433 434 @TestTargets({ 435 @TestTargetNew( 436 level = TestLevel.COMPLETE, 437 notes = "getDouble should return the Double set by putDouble", 438 method = "getDouble", 439 args = {java.lang.String.class, double.class} 440 ), 441 @TestTargetNew( 442 level = TestLevel.COMPLETE, 443 notes = "getDouble should return the Double set by putDouble", 444 method = "putDouble", 445 args = {java.lang.String.class, double.class} 446 ) 447 }) testGetDouble2()448 public void testGetDouble2() { 449 final double d1 = 10.06; 450 final double d2 = 10.07; 451 452 assertEquals(d1, mBundle.getDouble(KEY, d1)); 453 mBundle.putDouble(KEY, d2); 454 assertEquals(d2, mBundle.getDouble(KEY, d1)); 455 } 456 457 @TestTargets({ 458 @TestTargetNew( 459 level = TestLevel.COMPLETE, 460 notes = "getDoubleArray should only return the DoubleArray set by putDoubleArray", 461 method = "getDoubleArray", 462 args = {java.lang.String.class} 463 ), 464 @TestTargetNew( 465 level = TestLevel.COMPLETE, 466 notes = "getDoubleArray should only return the DoubleArray set by putDoubleArray", 467 method = "putDoubleArray", 468 args = {java.lang.String.class, double[].class} 469 ) 470 }) testGetDoubleArray()471 public void testGetDoubleArray() { 472 assertNull(mBundle.getDoubleArray(KEY)); 473 mBundle.putDoubleArray(KEY, new double[] { 474 10.06, 10.07 475 }); 476 final double[] doubleArray = mBundle.getDoubleArray(KEY); 477 assertEquals(10.06, doubleArray[0]); 478 assertEquals(10.07, doubleArray[1]); 479 } 480 481 @TestTargets({ 482 @TestTargetNew( 483 level = TestLevel.COMPLETE, 484 notes = "getFloat should only return the Float set by putFloat", 485 method = "getFloat", 486 args = {java.lang.String.class} 487 ), 488 @TestTargetNew( 489 level = TestLevel.COMPLETE, 490 notes = "getFloat should only return the Float set by putFloat", 491 method = "putFloat", 492 args = {java.lang.String.class, float.class} 493 ) 494 }) testGetFloat1()495 public void testGetFloat1() { 496 final float f = 10.07f; 497 498 assertEquals(0.0f, mBundle.getFloat(KEY)); 499 mBundle.putFloat(KEY, f); 500 assertEquals(f, mBundle.getFloat(KEY)); 501 } 502 503 @TestTargets({ 504 @TestTargetNew( 505 level = TestLevel.COMPLETE, 506 notes = "getFloat should only return the Float set by putFloat", 507 method = "getFloat", 508 args = {java.lang.String.class, float.class} 509 ), 510 @TestTargetNew( 511 level = TestLevel.COMPLETE, 512 notes = "getFloat should only return the Float set by putFloat", 513 method = "putFloat", 514 args = {java.lang.String.class, float.class} 515 ) 516 }) testGetFloat2()517 public void testGetFloat2() { 518 final float f1 = 10.06f; 519 final float f2 = 10.07f; 520 521 assertEquals(f1, mBundle.getFloat(KEY, f1)); 522 mBundle.putFloat(KEY, f2); 523 assertEquals(f2, mBundle.getFloat(KEY, f1)); 524 } 525 526 @TestTargets({ 527 @TestTargetNew( 528 level = TestLevel.COMPLETE, 529 notes = "getFloatArray should only return the FloatArray set by putFloatArray", 530 method = "getFloatArray", 531 args = {java.lang.String.class} 532 ), 533 @TestTargetNew( 534 level = TestLevel.COMPLETE, 535 notes = "getFloatArray should only return the FloatArray set by putFloatArray", 536 method = "putFloatArray", 537 args = {java.lang.String.class, float[].class} 538 ) 539 }) testGetFloatArray()540 public void testGetFloatArray() { 541 assertNull(mBundle.getFloatArray(KEY)); 542 mBundle.putFloatArray(KEY, new float[] { 543 10.06f, 10.07f 544 }); 545 final float[] floatArray = mBundle.getFloatArray(KEY); 546 assertEquals(10.06f, floatArray[0]); 547 assertEquals(10.07f, floatArray[1]); 548 } 549 550 @TestTargets({ 551 @TestTargetNew( 552 level = TestLevel.COMPLETE, 553 notes = "getInt should only return the Int set by putInt", 554 method = "getInt", 555 args = {java.lang.String.class} 556 ), 557 @TestTargetNew( 558 level = TestLevel.COMPLETE, 559 notes = "getInt should only return the Int set by putInt", 560 method = "putInt", 561 args = {java.lang.String.class, int.class} 562 ) 563 }) testGetInt1()564 public void testGetInt1() { 565 final int i = 1007; 566 567 assertEquals(0, mBundle.getInt(KEY)); 568 mBundle.putInt(KEY, i); 569 assertEquals(i, mBundle.getInt(KEY)); 570 } 571 572 @TestTargets({ 573 @TestTargetNew( 574 level = TestLevel.COMPLETE, 575 notes = "getInt should only return the Int set by putInt", 576 method = "getInt", 577 args = {java.lang.String.class, int.class} 578 ), 579 @TestTargetNew( 580 level = TestLevel.COMPLETE, 581 notes = "getInt should only return the Int set by putInt", 582 method = "putInt", 583 args = {java.lang.String.class, int.class} 584 ) 585 }) testGetInt2()586 public void testGetInt2() { 587 final int i1 = 1006; 588 final int i2 = 1007; 589 590 assertEquals(i1, mBundle.getInt(KEY, i1)); 591 mBundle.putInt(KEY, i2); 592 assertEquals(i2, mBundle.getInt(KEY, i2)); 593 } 594 595 @TestTargets({ 596 @TestTargetNew( 597 level = TestLevel.COMPLETE, 598 notes = "getIntArray should only return the IntArray set by putIntArray", 599 method = "getIntArray", 600 args = {java.lang.String.class} 601 ), 602 @TestTargetNew( 603 level = TestLevel.COMPLETE, 604 notes = "getIntArray should only return the IntArray set by putIntArray", 605 method = "putIntArray", 606 args = {java.lang.String.class, int[].class} 607 ) 608 }) testGetIntArray()609 public void testGetIntArray() { 610 assertNull(mBundle.getIntArray(KEY)); 611 mBundle.putIntArray(KEY, new int[] { 612 1006, 1007 613 }); 614 final int[] intArray = mBundle.getIntArray(KEY); 615 assertEquals(1006, intArray[0]); 616 assertEquals(1007, intArray[1]); 617 } 618 619 // getIntegerArrayList should only return the IntegerArrayList set by putIntegerArrayLis 620 @TestTargets({ 621 @TestTargetNew( 622 level = TestLevel.COMPLETE, 623 method = "getIntegerArrayList", 624 args = {java.lang.String.class} 625 ), 626 @TestTargetNew( 627 level = TestLevel.COMPLETE, 628 method = "putIntegerArrayList", 629 args = {java.lang.String.class, java.util.ArrayList.class} 630 ) 631 }) testGetIntegerArrayList()632 public void testGetIntegerArrayList() { 633 final int i1 = 1006; 634 final int i2 = 1007; 635 636 assertNull(mBundle.getIntegerArrayList(KEY)); 637 final ArrayList<Integer> arrayList = new ArrayList<Integer>(); 638 arrayList.add(i1); 639 arrayList.add(i2); 640 mBundle.putIntegerArrayList(KEY, arrayList); 641 final ArrayList<Integer> retArrayList = mBundle.getIntegerArrayList(KEY); 642 assertNotNull(retArrayList); 643 assertEquals(2, retArrayList.size()); 644 assertEquals((Integer)i1, retArrayList.get(0)); 645 assertEquals((Integer)i2, retArrayList.get(1)); 646 } 647 648 @TestTargets({ 649 @TestTargetNew( 650 level = TestLevel.COMPLETE, 651 notes = "getLong should only return the Long set by putLong", 652 method = "getLong", 653 args = {java.lang.String.class} 654 ), 655 @TestTargetNew( 656 level = TestLevel.COMPLETE, 657 notes = "getLong should only return the Long set by putLong", 658 method = "putLong", 659 args = {java.lang.String.class, long.class} 660 ) 661 }) testGetLong1()662 public void testGetLong1() { 663 final long l = 1007; 664 665 assertEquals(0, mBundle.getLong(KEY)); 666 mBundle.putLong(KEY, l); 667 assertEquals(l, mBundle.getLong(KEY)); 668 } 669 670 @TestTargets({ 671 @TestTargetNew( 672 level = TestLevel.COMPLETE, 673 notes = "getLong should only return the Long set by putLong", 674 method = "getLong", 675 args = {java.lang.String.class, long.class} 676 ), 677 @TestTargetNew( 678 level = TestLevel.COMPLETE, 679 notes = "getLong should only return the Long set by putLong", 680 method = "putLong", 681 args = {java.lang.String.class, long.class} 682 ) 683 }) testGetLong2()684 public void testGetLong2() { 685 final long l1 = 1006; 686 final long l2 = 1007; 687 688 assertEquals(l1, mBundle.getLong(KEY, l1)); 689 mBundle.putLong(KEY, l2); 690 assertEquals(l2, mBundle.getLong(KEY, l2)); 691 } 692 693 @TestTargets({ 694 @TestTargetNew( 695 level = TestLevel.COMPLETE, 696 notes = "getLongArray should only return the LongArray set by putLongArray", 697 method = "getLongArray", 698 args = {java.lang.String.class} 699 ), 700 @TestTargetNew( 701 level = TestLevel.COMPLETE, 702 notes = "getLongArray should only return the LongArray set by putLongArray", 703 method = "putLongArray", 704 args = {java.lang.String.class, long[].class} 705 ) 706 }) testGetLongArray()707 public void testGetLongArray() { 708 assertNull(mBundle.getLongArray(KEY)); 709 mBundle.putLongArray(KEY, new long[] { 710 1006, 1007 711 }); 712 final long[] longArray = mBundle.getLongArray(KEY); 713 assertEquals(1006, longArray[0]); 714 assertEquals(1007, longArray[1]); 715 } 716 717 @TestTargets({ 718 @TestTargetNew( 719 level = TestLevel.COMPLETE, 720 notes = "getParcelable should only return the Parcelable set by putParcelable", 721 method = "getParcelable", 722 args = {java.lang.String.class} 723 ), 724 @TestTargetNew( 725 level = TestLevel.COMPLETE, 726 notes = "getParcelable should only return the Parcelable set by putParcelable", 727 method = "putParcelable", 728 args = {java.lang.String.class, android.os.Parcelable.class} 729 ) 730 }) testGetParcelable()731 public void testGetParcelable() { 732 assertNull(mBundle.getParcelable(KEY)); 733 final Bundle bundle = new Bundle(); 734 mBundle.putParcelable(KEY, bundle); 735 assertTrue(bundle.equals(mBundle.getParcelable(KEY))); 736 } 737 738 // getParcelableArray should only return the ParcelableArray set by putParcelableArray 739 @TestTargets({ 740 @TestTargetNew( 741 level = TestLevel.COMPLETE, 742 method = "getParcelableArray", 743 args = {java.lang.String.class} 744 ), 745 @TestTargetNew( 746 level = TestLevel.COMPLETE, 747 method = "putParcelableArray", 748 args = {java.lang.String.class, android.os.Parcelable[].class} 749 ) 750 }) testGetParcelableArray()751 public void testGetParcelableArray() { 752 assertNull(mBundle.getParcelableArray(KEY)); 753 final Bundle bundle1 = new Bundle(); 754 final Bundle bundle2 = new Bundle(); 755 mBundle.putParcelableArray(KEY, new Bundle[] { 756 bundle1, bundle2 757 }); 758 final Parcelable[] parcelableArray = mBundle.getParcelableArray(KEY); 759 assertEquals(2, parcelableArray.length); 760 assertTrue(bundle1.equals(parcelableArray[0])); 761 assertTrue(bundle2.equals(parcelableArray[1])); 762 } 763 764 // getParcelableArrayList should only return the parcelableArrayList set by putParcelableArrayList 765 @TestTargets({ 766 @TestTargetNew( 767 level = TestLevel.COMPLETE, 768 method = "getParcelableArrayList", 769 args = {java.lang.String.class} 770 ), 771 @TestTargetNew( 772 level = TestLevel.COMPLETE, 773 method = "putParcelableArrayList", 774 args = {java.lang.String.class, java.util.ArrayList.class} 775 ) 776 }) testGetParcelableArrayList()777 public void testGetParcelableArrayList() { 778 assertNull(mBundle.getParcelableArrayList(KEY)); 779 final ArrayList<Parcelable> parcelableArrayList = new ArrayList<Parcelable>(); 780 final Bundle bundle1 = new Bundle(); 781 final Bundle bundle2 = new Bundle(); 782 parcelableArrayList.add(bundle1); 783 parcelableArrayList.add(bundle2); 784 mBundle.putParcelableArrayList(KEY, parcelableArrayList); 785 final ArrayList<Parcelable> ret = mBundle.getParcelableArrayList(KEY); 786 assertEquals(2, ret.size()); 787 assertTrue(bundle1.equals(ret.get(0))); 788 assertTrue(bundle2.equals(ret.get(1))); 789 } 790 791 @TestTargets({ 792 @TestTargetNew( 793 level = TestLevel.COMPLETE, 794 notes = "getSerializable should only return the Serializable set by putSerializable", 795 method = "getSerializable", 796 args = {java.lang.String.class} 797 ), 798 @TestTargetNew( 799 level = TestLevel.COMPLETE, 800 notes = "getSerializable should only return the Serializable set by putSerializable", 801 method = "putSerializable", 802 args = {java.lang.String.class, java.io.Serializable.class} 803 ) 804 }) testGetSerializable()805 public void testGetSerializable() { 806 assertNull(mBundle.getSerializable(KEY)); 807 mBundle.putSerializable(KEY, "android"); 808 assertEquals("android", mBundle.getSerializable(KEY)); 809 } 810 811 @TestTargets({ 812 @TestTargetNew( 813 level = TestLevel.COMPLETE, 814 notes = "getShort should only return the Short set by putShort", 815 method = "getShort", 816 args = {java.lang.String.class} 817 ), 818 @TestTargetNew( 819 level = TestLevel.COMPLETE, 820 notes = "getShort should only return the Short set by putShort", 821 method = "putShort", 822 args = {java.lang.String.class, short.class} 823 ) 824 }) testGetShort1()825 public void testGetShort1() { 826 final short s = 1007; 827 828 assertEquals(0, mBundle.getShort(KEY)); 829 mBundle.putShort(KEY, s); 830 assertEquals(s, mBundle.getShort(KEY)); 831 } 832 833 @TestTargets({ 834 @TestTargetNew( 835 level = TestLevel.COMPLETE, 836 notes = "getShort should only return the Short set by putShort", 837 method = "getShort", 838 args = {java.lang.String.class, short.class} 839 ), 840 @TestTargetNew( 841 level = TestLevel.COMPLETE, 842 notes = "getShort should only return the Short set by putShort", 843 method = "putShort", 844 args = {java.lang.String.class, short.class} 845 ) 846 }) testGetShort2()847 public void testGetShort2() { 848 final short s1 = 1006; 849 final short s2 = 1007; 850 851 assertEquals(s1, mBundle.getShort(KEY, s1)); 852 mBundle.putShort(KEY, s2); 853 assertEquals(s2, mBundle.getShort(KEY, s1)); 854 } 855 856 @TestTargets({ 857 @TestTargetNew( 858 level = TestLevel.COMPLETE, 859 notes = "getShortArray should only return the ShortArray set by putShortArray", 860 method = "getShortArray", 861 args = {java.lang.String.class} 862 ), 863 @TestTargetNew( 864 level = TestLevel.COMPLETE, 865 notes = "getShortArray should only return the ShortArray set by putShortArray", 866 method = "putShortArray", 867 args = {java.lang.String.class, short[].class} 868 ) 869 }) testGetShortArray()870 public void testGetShortArray() { 871 final short s1 = 1006; 872 final short s2 = 1007; 873 874 assertNull(mBundle.getShortArray(KEY)); 875 mBundle.putShortArray(KEY, new short[] { 876 s1, s2 877 }); 878 final short[] shortArray = mBundle.getShortArray(KEY); 879 assertEquals(s1, shortArray[0]); 880 assertEquals(s2, shortArray[1]); 881 } 882 883 // getSparseParcelableArray should only return the SparseArray<Parcelable> 884 // set by putSparseParcelableArray 885 @TestTargets({ 886 @TestTargetNew( 887 level = TestLevel.COMPLETE, 888 method = "getSparseParcelableArray", 889 args = {java.lang.String.class} 890 ), 891 @TestTargetNew( 892 level = TestLevel.COMPLETE, 893 method = "putSparseParcelableArray", 894 args = {java.lang.String.class, android.util.SparseArray.class} 895 ) 896 }) testGetSparseParcelableArray()897 public void testGetSparseParcelableArray() { 898 assertNull(mBundle.getSparseParcelableArray(KEY)); 899 final SparseArray<Parcelable> sparseArray = new SparseArray<Parcelable>(); 900 final Bundle bundle = new Bundle(); 901 final Intent intent = new Intent(); 902 sparseArray.put(1006, bundle); 903 sparseArray.put(1007, intent); 904 mBundle.putSparseParcelableArray(KEY, sparseArray); 905 final SparseArray<Parcelable> ret = mBundle.getSparseParcelableArray(KEY); 906 assertEquals(2, ret.size()); 907 assertNull(ret.get(1008)); 908 assertTrue(bundle.equals(ret.get(1006))); 909 assertTrue(intent.equals(ret.get(1007))); 910 } 911 912 @TestTargets({ 913 @TestTargetNew( 914 level = TestLevel.COMPLETE, 915 notes = "getString should only return the String set by putString", 916 method = "getString", 917 args = {java.lang.String.class} 918 ), 919 @TestTargetNew( 920 level = TestLevel.COMPLETE, 921 notes = "getString should only return the String set by putString", 922 method = "putString", 923 args = {java.lang.String.class, java.lang.String.class} 924 ) 925 }) testGetString()926 public void testGetString() { 927 assertNull(mBundle.getString(KEY)); 928 mBundle.putString(KEY, "android"); 929 assertEquals("android", mBundle.getString(KEY)); 930 } 931 932 @TestTargets({ 933 @TestTargetNew( 934 level = TestLevel.COMPLETE, 935 notes = "getStringArray should only return the StringArray set by putStringArray", 936 method = "getStringArray", 937 args = {java.lang.String.class} 938 ), 939 @TestTargetNew( 940 level = TestLevel.COMPLETE, 941 notes = "getStringArray should only return the StringArray set by putStringArray", 942 method = "putStringArray", 943 args = {java.lang.String.class, java.lang.String[].class} 944 ) 945 }) testGetStringArray()946 public void testGetStringArray() { 947 assertNull(mBundle.getStringArray(KEY)); 948 mBundle.putStringArray(KEY, new String[] { 949 "one", "two", "three" 950 }); 951 final String[] ret = mBundle.getStringArray(KEY); 952 assertEquals("one", ret[0]); 953 assertEquals("two", ret[1]); 954 assertEquals("three", ret[2]); 955 } 956 957 // getStringArrayList should only return the StringArrayList set by putStringArrayList 958 @TestTargets({ 959 @TestTargetNew( 960 level = TestLevel.COMPLETE, 961 method = "getStringArrayList", 962 args = {java.lang.String.class} 963 ), 964 @TestTargetNew( 965 level = TestLevel.COMPLETE, 966 method = "putStringArrayList", 967 args = {java.lang.String.class, java.util.ArrayList.class} 968 ) 969 }) testGetStringArrayList()970 public void testGetStringArrayList() { 971 assertNull(mBundle.getStringArrayList(KEY)); 972 final ArrayList<String> stringArrayList = new ArrayList<String>(); 973 stringArrayList.add("one"); 974 stringArrayList.add("two"); 975 stringArrayList.add("three"); 976 mBundle.putStringArrayList(KEY, stringArrayList); 977 final ArrayList<String> ret = mBundle.getStringArrayList(KEY); 978 assertEquals(3, ret.size()); 979 assertEquals("one", ret.get(0)); 980 assertEquals("two", ret.get(1)); 981 assertEquals("three", ret.get(2)); 982 } 983 984 @TestTargetNew( 985 level = TestLevel.COMPLETE, 986 notes = "keySet should contains all keys that have been set", 987 method = "keySet", 988 args = {} 989 ) testKeySet()990 public void testKeySet() { 991 Set<String> setKey = mBundle.keySet(); 992 assertFalse(setKey.contains("one")); 993 assertFalse(setKey.contains("two")); 994 mBundle.putBoolean("one", true); 995 mBundle.putChar("two", 't'); 996 setKey = mBundle.keySet(); 997 assertEquals(2, setKey.size()); 998 assertTrue(setKey.contains("one")); 999 assertTrue(setKey.contains("two")); 1000 assertFalse(setKey.contains("three")); 1001 } 1002 1003 // same as hasFileDescriptors, the only difference is that describeContents 1004 // return 0 if no fd and return 1 if has fd for the tested Bundle 1005 @TestTargetNew( 1006 level = TestLevel.COMPLETE, 1007 method = "describeContents", 1008 args = {} 1009 ) 1010 testDescribeContents()1011 public void testDescribeContents() { 1012 assertTrue((mBundle.describeContents() 1013 & Parcelable.CONTENTS_FILE_DESCRIPTOR) == 0); 1014 1015 final Parcel parcel = Parcel.obtain(); 1016 try { 1017 mBundle.putParcelable("foo", ParcelFileDescriptor.open( 1018 new File("/system"), ParcelFileDescriptor.MODE_READ_ONLY)); 1019 } catch (FileNotFoundException e) { 1020 throw new RuntimeException("can't open /system", e); 1021 } 1022 assertTrue((mBundle.describeContents() 1023 & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0); 1024 mBundle.writeToParcel(parcel, 0); 1025 mBundle.clear(); 1026 assertTrue((mBundle.describeContents() 1027 & Parcelable.CONTENTS_FILE_DESCRIPTOR) == 0); 1028 parcel.setDataPosition(0); 1029 mBundle.readFromParcel(parcel); 1030 assertTrue((mBundle.describeContents() 1031 & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0); 1032 ParcelFileDescriptor pfd = (ParcelFileDescriptor)mBundle.getParcelable("foo"); 1033 assertTrue((mBundle.describeContents() 1034 & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0); 1035 } 1036 1037 // case 1: The default bundle doesn't has FileDescriptor. 1038 // case 2: The tested Bundle should has FileDescriptor 1039 // if it read data from a Parcel object, which is created with a FileDescriptor. 1040 // case 3: The tested Bundle should has FileDescriptor 1041 // if put a Parcelable object, which is created with a FileDescriptor, into it. 1042 @TestTargetNew( 1043 level = TestLevel.COMPLETE, 1044 method = "hasFileDescriptors", 1045 args = {} 1046 ) testHasFileDescriptors()1047 public void testHasFileDescriptors() { 1048 assertFalse(mBundle.hasFileDescriptors()); 1049 1050 final Parcel parcel = Parcel.obtain(); 1051 assertFalse(parcel.hasFileDescriptors()); 1052 try { 1053 mBundle.putParcelable("foo", ParcelFileDescriptor.open( 1054 new File("/system"), ParcelFileDescriptor.MODE_READ_ONLY)); 1055 } catch (FileNotFoundException e) { 1056 throw new RuntimeException("can't open /system", e); 1057 } 1058 assertTrue(mBundle.hasFileDescriptors()); 1059 mBundle.writeToParcel(parcel, 0); 1060 assertTrue(parcel.hasFileDescriptors()); 1061 mBundle.clear(); 1062 assertFalse(mBundle.hasFileDescriptors()); 1063 parcel.setDataPosition(0); 1064 mBundle.readFromParcel(parcel); 1065 assertTrue(mBundle.hasFileDescriptors()); 1066 ParcelFileDescriptor pfd = (ParcelFileDescriptor)mBundle.getParcelable("foo"); 1067 assertTrue(mBundle.hasFileDescriptors()); 1068 } 1069 1070 @TestTargetNew( 1071 level = TestLevel.COMPLETE, 1072 method = "setClassLoader", 1073 args = {java.lang.ClassLoader.class} 1074 ) 1075 @ToBeFixed(bug = "", explanation = "this method only set a private member and there is " 1076 + "no way to check whether the set value is right or not ") testSetClassLoader()1077 public void testSetClassLoader() { 1078 mBundle.setClassLoader(new MockClassLoader()); 1079 } 1080 1081 // Write the bundle(A) to a parcel(B), and then create a bundle(C) from B. 1082 // C should be same as A. 1083 @TestTargetNew( 1084 level = TestLevel.COMPLETE, 1085 method = "writeToParcel", 1086 args = {android.os.Parcel.class, int.class} 1087 ) testWriteToParcel()1088 public void testWriteToParcel() { 1089 final String li = "Bruce Li"; 1090 1091 mBundle.putString(KEY, li); 1092 final Parcel parcel = Parcel.obtain(); 1093 mBundle.writeToParcel(parcel, 0); 1094 parcel.setDataPosition(0); 1095 final Bundle bundle = Bundle.CREATOR.createFromParcel(parcel); 1096 assertEquals(li, bundle.getString(KEY)); 1097 } 1098 1099 // test the size should be right after add/remove key-value pair of the Bundle. 1100 @TestTargets({ 1101 @TestTargetNew( 1102 level = TestLevel.COMPLETE, 1103 method = "size", 1104 args = {} 1105 ), 1106 @TestTargetNew( 1107 level = TestLevel.COMPLETE, 1108 method = "remove", 1109 args = {java.lang.String.class} 1110 ) 1111 }) testSize()1112 public void testSize() { 1113 assertEquals(0, mBundle.size()); 1114 mBundle.putBoolean("one", true); 1115 assertEquals(1, mBundle.size()); 1116 1117 mBundle.putBoolean("two", true); 1118 assertEquals(2, mBundle.size()); 1119 1120 mBundle.putBoolean("three", true); 1121 assertEquals(3, mBundle.size()); 1122 1123 mBundle.putBoolean("four", true); 1124 mBundle.putBoolean("five", true); 1125 assertEquals(5, mBundle.size()); 1126 mBundle.remove("six"); 1127 assertEquals(5, mBundle.size()); 1128 1129 mBundle.remove("one"); 1130 assertEquals(4, mBundle.size()); 1131 mBundle.remove("one"); 1132 assertEquals(4, mBundle.size()); 1133 1134 mBundle.remove("two"); 1135 assertEquals(3, mBundle.size()); 1136 1137 mBundle.remove("three"); 1138 mBundle.remove("four"); 1139 mBundle.remove("five"); 1140 assertEquals(0, mBundle.size()); 1141 } 1142 1143 // The return value of toString() should not be null. 1144 @TestTargets({ 1145 @TestTargetNew( 1146 level = TestLevel.COMPLETE, 1147 method = "toString", 1148 args = {} 1149 ), 1150 @TestTargetNew( 1151 level = TestLevel.COMPLETE, 1152 method = "readFromParcel", 1153 args = {android.os.Parcel.class} 1154 ) 1155 }) testToString()1156 public void testToString() { 1157 assertNotNull(mBundle.toString()); 1158 mBundle.putString("foo", "this test is so stupid"); 1159 assertNotNull(mBundle.toString()); 1160 } 1161 1162 // The tested Bundle should hold mappings from the given after putAll be invoked. 1163 @TestTargetNew( 1164 level = TestLevel.COMPLETE, 1165 method = "putAll", 1166 args = {android.os.Bundle.class} 1167 ) testPutAll()1168 public void testPutAll() { 1169 assertEquals(0, mBundle.size()); 1170 1171 final Bundle map = new Bundle(); 1172 map.putBoolean(KEY, true); 1173 assertEquals(1, map.size()); 1174 mBundle.putAll(map); 1175 assertEquals(1, mBundle.size()); 1176 } 1177 1178 class MockClassLoader extends ClassLoader { MockClassLoader()1179 MockClassLoader() { 1180 super(); 1181 } 1182 MockClassLoader(ClassLoader parent)1183 MockClassLoader(ClassLoader parent) { 1184 super(parent); 1185 } 1186 } 1187 } 1188