1 /* 2 * Copyright (C) 2009 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 java.io.FileDescriptor; 20 import java.io.Serializable; 21 import java.util.ArrayList; 22 import java.util.HashMap; 23 24 import android.content.pm.Signature; 25 import android.os.BadParcelableException; 26 import android.os.Binder; 27 import android.os.Bundle; 28 import android.os.IBinder; 29 import android.os.IInterface; 30 import android.os.Parcel; 31 import android.os.ParcelFileDescriptor; 32 import android.os.Parcelable; 33 import android.test.AndroidTestCase; 34 import android.util.Log; 35 import android.util.SparseArray; 36 import android.util.SparseBooleanArray; 37 38 public class ParcelTest extends AndroidTestCase { 39 40 public void testObtain() { 41 Parcel p1 = Parcel.obtain(); 42 assertNotNull(p1); 43 Parcel p2 = Parcel.obtain(); 44 assertNotNull(p2); 45 Parcel p3 = Parcel.obtain(); 46 assertNotNull(p3); 47 Parcel p4 = Parcel.obtain(); 48 assertNotNull(p4); 49 Parcel p5 = Parcel.obtain(); 50 assertNotNull(p5); 51 Parcel p6 = Parcel.obtain(); 52 assertNotNull(p6); 53 Parcel p7 = Parcel.obtain(); 54 assertNotNull(p7); 55 56 p1.recycle(); 57 p2.recycle(); 58 p3.recycle(); 59 p4.recycle(); 60 p5.recycle(); 61 p6.recycle(); 62 p7.recycle(); 63 } 64 65 public void testAppendFrom() { 66 Parcel p; 67 Parcel p2; 68 int d1; 69 int d2; 70 71 p = Parcel.obtain(); 72 d1 = p.dataPosition(); 73 p.writeInt(7); 74 p.writeInt(5); 75 d2 = p.dataPosition(); 76 p2 = Parcel.obtain(); 77 p2.appendFrom(p, d1, d2 - d1); 78 p2.setDataPosition(0); 79 assertEquals(7, p2.readInt()); 80 assertEquals(5, p2.readInt()); 81 p2.recycle(); 82 p.recycle(); 83 } 84 85 public void testDataAvail() { 86 Parcel p; 87 88 p = Parcel.obtain(); 89 p.writeInt(7); // size 4 90 p.writeInt(5); // size 4 91 p.writeLong(7L); // size 8 92 p.writeString("7L"); // size 12 93 p.setDataPosition(0); 94 assertEquals(p.dataSize(), p.dataAvail()); 95 p.readInt(); 96 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 97 p.readInt(); 98 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 99 p.readLong(); 100 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 101 p.readString(); 102 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 103 p.recycle(); 104 } 105 106 public void testDataCapacity() { 107 Parcel p; 108 109 p = Parcel.obtain(); 110 assertEquals(0, p.dataCapacity()); 111 p.writeInt(7); // size 4 112 int dC1 = p.dataCapacity(); 113 p.writeDouble(2.19); 114 int dC2 = p.dataCapacity(); 115 assertTrue(dC2 > dC1); 116 p.recycle(); 117 } 118 119 public void testSetDataCapacity() { 120 Parcel p; 121 122 p = Parcel.obtain(); 123 assertEquals(0, p.dataCapacity()); 124 p.setDataCapacity(2); 125 assertEquals(2, p.dataCapacity()); 126 p.setDataCapacity(1); 127 assertEquals(2, p.dataCapacity()); 128 p.setDataCapacity(3); 129 assertEquals(3, p.dataCapacity()); 130 p.recycle(); 131 } 132 133 public void testDataPosition() { 134 Parcel p; 135 136 p = Parcel.obtain(); 137 assertEquals(0, p.dataPosition()); 138 p.writeInt(7); // size 4 139 int dP1 = p.dataPosition(); 140 p.writeLong(7L); // size 8 141 int dP2 = p.dataPosition(); 142 assertTrue(dP2 > dP1); 143 p.recycle(); 144 } 145 146 public void testSetDataPosition() { 147 Parcel p; 148 149 p = Parcel.obtain(); 150 assertEquals(0, p.dataSize()); 151 assertEquals(0, p.dataPosition()); 152 p.setDataPosition(4); 153 assertEquals(4, p.dataPosition()); 154 p.setDataPosition(7); 155 assertEquals(7, p.dataPosition()); 156 p.setDataPosition(0); 157 p.writeInt(7); 158 assertEquals(4, p.dataSize()); 159 p.setDataPosition(4); 160 assertEquals(4, p.dataPosition()); 161 p.setDataPosition(7); 162 assertEquals(7, p.dataPosition()); 163 p.recycle(); 164 } 165 166 public void testDataSize() { 167 Parcel p; 168 169 p = Parcel.obtain(); 170 assertEquals(0, p.dataSize()); 171 p.writeInt(7); // size 4 172 assertEquals(4, p.dataSize()); 173 p.writeInt(5); // size 4 174 assertEquals(8, p.dataSize()); 175 p.writeLong(7L); // size 8 176 assertEquals(16, p.dataSize()); 177 p.recycle(); 178 } 179 180 public void testSetDataSize() { 181 Parcel p; 182 183 p = Parcel.obtain(); 184 assertEquals(0, p.dataSize()); 185 p.setDataSize(5); 186 assertEquals(5, p.dataSize()); 187 p.setDataSize(3); 188 assertEquals(3, p.dataSize()); 189 190 p.writeInt(3); 191 assertEquals(4, p.dataSize()); 192 p.setDataSize(5); 193 assertEquals(5, p.dataSize()); 194 p.setDataSize(3); 195 assertEquals(3, p.dataSize()); 196 p.recycle(); 197 } 198 199 public void testEnforceInterface() { 200 Parcel p; 201 String s = "IBinder interface token"; 202 203 p = Parcel.obtain(); 204 p.writeInterfaceToken(s); 205 p.setDataPosition(0); 206 try { 207 p.enforceInterface(""); 208 fail("Should throw an SecurityException"); 209 } catch (SecurityException e) { 210 //expected 211 } 212 p.recycle(); 213 214 p = Parcel.obtain(); 215 p.writeInterfaceToken(s); 216 p.setDataPosition(0); 217 p.enforceInterface(s); 218 p.recycle(); 219 } 220 221 public void testMarshall() { 222 final byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 223 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 224 225 Parcel p1 = Parcel.obtain(); 226 p1.writeByteArray(c); 227 p1.setDataPosition(0); 228 byte[] d1 = p1.marshall(); 229 230 Parcel p2 = Parcel.obtain(); 231 p2.unmarshall(d1, 0, d1.length); 232 p2.setDataPosition(0); 233 byte[] d2 = new byte[c.length]; 234 p2.readByteArray(d2); 235 236 for (int i = 0; i < c.length; i++) { 237 assertEquals(c[i], d2[i]); 238 } 239 240 p1.recycle(); 241 p2.recycle(); 242 } 243 244 @SuppressWarnings("unchecked") 245 public void testReadValue() { 246 Parcel p; 247 MockClassLoader mcl = new MockClassLoader(); 248 249 // test null 250 p = Parcel.obtain(); 251 p.writeValue(null); 252 p.setDataPosition(0); 253 assertNull(p.readValue(mcl)); 254 p.recycle(); 255 256 // test String 257 p = Parcel.obtain(); 258 p.writeValue("String"); 259 p.setDataPosition(0); 260 assertEquals("String", p.readValue(mcl)); 261 p.recycle(); 262 263 // test Integer 264 p = Parcel.obtain(); 265 p.writeValue(Integer.MAX_VALUE); 266 p.setDataPosition(0); 267 assertEquals(Integer.MAX_VALUE, p.readValue(mcl)); 268 p.recycle(); 269 270 // test Map 271 HashMap map = new HashMap(); 272 HashMap map2; 273 map.put("string", "String"); 274 map.put("int", Integer.MAX_VALUE); 275 map.put("boolean", true); 276 p = Parcel.obtain(); 277 p.writeValue(map); 278 p.setDataPosition(0); 279 map2 = (HashMap) p.readValue(mcl); 280 assertNotNull(map2); 281 assertEquals(map.size(), map2.size()); 282 assertEquals("String", map.get("string")); 283 assertEquals(Integer.MAX_VALUE, map.get("int")); 284 assertEquals(true, map.get("boolean")); 285 p.recycle(); 286 287 // test Bundle 288 Bundle bundle = new Bundle(); 289 bundle.putBoolean("boolean", true); 290 bundle.putInt("int", Integer.MAX_VALUE); 291 bundle.putString("string", "String"); 292 Bundle bundle2; 293 p = Parcel.obtain(); 294 p.writeValue(bundle); 295 p.setDataPosition(0); 296 bundle2 = (Bundle) p.readValue(mcl); 297 assertNotNull(bundle2); 298 assertEquals(true, bundle2.getBoolean("boolean")); 299 assertEquals(Integer.MAX_VALUE, bundle2.getInt("int")); 300 assertEquals("String", bundle2.getString("string")); 301 p.recycle(); 302 303 // test Parcelable 304 final String signatureString = "1234567890abcdef"; 305 Signature s = new Signature(signatureString); 306 p = Parcel.obtain(); 307 p.writeValue(s); 308 p.setDataPosition(0); 309 assertEquals(s, p.readValue(mcl)); 310 p.recycle(); 311 312 // test Short 313 p = Parcel.obtain(); 314 p.writeValue(Short.MAX_VALUE); 315 p.setDataPosition(0); 316 assertEquals(Short.MAX_VALUE, p.readValue(mcl)); 317 p.recycle(); 318 319 // test Long 320 p = Parcel.obtain(); 321 p.writeValue(Long.MAX_VALUE); 322 p.setDataPosition(0); 323 assertEquals(Long.MAX_VALUE, p.readValue(mcl)); 324 p.recycle(); 325 326 // test Float 327 p = Parcel.obtain(); 328 p.writeValue(Float.MAX_VALUE); 329 p.setDataPosition(0); 330 assertEquals(Float.MAX_VALUE, p.readValue(mcl)); 331 p.recycle(); 332 333 // test Double 334 p = Parcel.obtain(); 335 p.writeValue(Double.MAX_VALUE); 336 p.setDataPosition(0); 337 assertEquals(Double.MAX_VALUE, p.readValue(mcl)); 338 p.recycle(); 339 340 // test Boolean 341 p = Parcel.obtain(); 342 p.writeValue(true); 343 p.writeValue(false); 344 p.setDataPosition(0); 345 assertTrue((Boolean) p.readValue(mcl)); 346 assertFalse((Boolean) p.readValue(mcl)); 347 p.recycle(); 348 349 // test CharSequence 350 p = Parcel.obtain(); 351 p.writeValue((CharSequence) "CharSequence"); 352 p.setDataPosition(0); 353 assertEquals("CharSequence", p.readValue(mcl)); 354 p.recycle(); 355 356 // test List 357 ArrayList arrayList2 = new ArrayList(); 358 arrayList2.add(Integer.MAX_VALUE); 359 arrayList2.add(true); 360 arrayList2.add(Long.MAX_VALUE); 361 ArrayList arrayList = new ArrayList(); 362 p = Parcel.obtain(); 363 p.writeValue(arrayList2); 364 p.setDataPosition(0); 365 assertEquals(0, arrayList.size()); 366 arrayList = (ArrayList) p.readValue(mcl); 367 assertEquals(3, arrayList.size()); 368 for (int i = 0; i < arrayList.size(); i++) { 369 assertEquals(arrayList.get(i), arrayList2.get(i)); 370 } 371 p.recycle(); 372 373 // test SparseArray 374 SparseArray<Object> sparseArray = new SparseArray<Object>(); 375 sparseArray.put(3, "String"); 376 sparseArray.put(2, Long.MAX_VALUE); 377 sparseArray.put(4, Float.MAX_VALUE); 378 sparseArray.put(0, Integer.MAX_VALUE); 379 sparseArray.put(1, true); 380 sparseArray.put(10, true); 381 SparseArray<Object> sparseArray2; 382 p = Parcel.obtain(); 383 p.writeValue(sparseArray); 384 p.setDataPosition(0); 385 sparseArray2 = (SparseArray<Object>) p.readValue(mcl); 386 assertNotNull(sparseArray2); 387 assertEquals(sparseArray.size(), sparseArray2.size()); 388 assertEquals(sparseArray.get(0), sparseArray2.get(0)); 389 assertEquals(sparseArray.get(1), sparseArray2.get(1)); 390 assertEquals(sparseArray.get(2), sparseArray2.get(2)); 391 assertEquals(sparseArray.get(3), sparseArray2.get(3)); 392 assertEquals(sparseArray.get(4), sparseArray2.get(4)); 393 assertEquals(sparseArray.get(10), sparseArray2.get(10)); 394 p.recycle(); 395 396 // test boolean[] 397 boolean[] booleanArray = {true, false, true, false}; 398 boolean[] booleanArray2 = new boolean[booleanArray.length]; 399 p = Parcel.obtain(); 400 p.writeValue(booleanArray); 401 p.setDataPosition(0); 402 booleanArray2 = (boolean[]) p.readValue(mcl); 403 for (int i = 0; i < booleanArray.length; i++) { 404 assertEquals(booleanArray[i], booleanArray2[i]); 405 } 406 p.recycle(); 407 408 // test byte[] 409 byte[] byteArray = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 410 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 411 byte[] byteArray2 = new byte[byteArray.length]; 412 p = Parcel.obtain(); 413 p.writeValue(byteArray); 414 p.setDataPosition(0); 415 byteArray2 = (byte[]) p.readValue(mcl); 416 for (int i = 0; i < byteArray.length; i++) { 417 assertEquals(byteArray[i], byteArray2[i]); 418 } 419 p.recycle(); 420 421 // test string[] 422 String[] stringArray = {"", 423 "a", 424 "Hello, Android!", 425 "A long string that is used to test the api readStringArray(),"}; 426 String[] stringArray2 = new String[stringArray.length]; 427 p = Parcel.obtain(); 428 p.writeValue(stringArray); 429 p.setDataPosition(0); 430 stringArray2 = (String[]) p.readValue(mcl); 431 for (int i = 0; i < stringArray.length; i++) { 432 assertEquals(stringArray[i], stringArray2[i]); 433 } 434 p.recycle(); 435 436 // test IBinder 437 Binder binder; 438 Binder binder2 = new Binder(); 439 p = Parcel.obtain(); 440 p.writeValue(binder2); 441 p.setDataPosition(0); 442 binder = (Binder) p.readValue(mcl); 443 assertEquals(binder2, binder); 444 p.recycle(); 445 446 // test Parcelable[] 447 Signature[] signatures = {new Signature("1234"), 448 new Signature("ABCD"), 449 new Signature("abcd")}; 450 Parcelable[] signatures2; 451 p = Parcel.obtain(); 452 p.writeValue(signatures); 453 p.setDataPosition(0); 454 signatures2 = (Parcelable[]) p.readValue(mcl); 455 for (int i = 0; i < signatures.length; i++) { 456 assertEquals(signatures[i], signatures2[i]); 457 } 458 p.recycle(); 459 460 // test Object 461 Object[] objects = new Object[5]; 462 objects[0] = Integer.MAX_VALUE; 463 objects[1] = true; 464 objects[2] = Long.MAX_VALUE; 465 objects[3] = "String"; 466 objects[4] = Float.MAX_VALUE; 467 Object[] objects2; 468 p = Parcel.obtain(); 469 p.writeValue(objects); 470 p.setDataPosition(0); 471 objects2 = (Object[]) p.readValue(mcl); 472 assertNotNull(objects2); 473 for (int i = 0; i < objects2.length; i++) { 474 assertEquals(objects[i], objects2[i]); 475 } 476 p.recycle(); 477 478 // test int[] 479 int[] intArray = {111, 11, 1, 0, -1, -11, -111}; 480 int[] intArray2 = new int[intArray.length]; 481 p = Parcel.obtain(); 482 p.writeValue(intArray); 483 p.setDataPosition(0); 484 intArray2= (int[]) p.readValue(mcl); 485 assertNotNull(intArray2); 486 for (int i = 0; i < intArray2.length; i++) { 487 assertEquals(intArray[i], intArray2[i]); 488 } 489 p.recycle(); 490 491 // test long[] 492 long[] longArray = {111L, 11L, 1L, 0L, -1L, -11L, -111L}; 493 long[] longArray2 = new long[longArray.length]; 494 p = Parcel.obtain(); 495 p.writeValue(longArray); 496 p.setDataPosition(0); 497 longArray2= (long[]) p.readValue(mcl); 498 assertNotNull(longArray2); 499 for (int i = 0; i < longArray2.length; i++) { 500 assertEquals(longArray[i], longArray2[i]); 501 } 502 p.recycle(); 503 504 // test byte 505 p = Parcel.obtain(); 506 p.writeValue(Byte.MAX_VALUE); 507 p.setDataPosition(0); 508 assertEquals(Byte.MAX_VALUE, p.readValue(mcl)); 509 p.recycle(); 510 511 // test Serializable 512 p = Parcel.obtain(); 513 p.writeValue((Serializable) "Serializable"); 514 p.setDataPosition(0); 515 assertEquals("Serializable", p.readValue(mcl)); 516 p.recycle(); 517 } 518 519 public void testReadByte() { 520 Parcel p; 521 522 p = Parcel.obtain(); 523 p.writeByte((byte) 0); 524 p.setDataPosition(0); 525 assertEquals((byte) 0, p.readByte()); 526 p.recycle(); 527 528 p = Parcel.obtain(); 529 p.writeByte((byte) 1); 530 p.setDataPosition(0); 531 assertEquals((byte) 1, p.readByte()); 532 p.recycle(); 533 534 p = Parcel.obtain(); 535 p.writeByte((byte) -1); 536 p.setDataPosition(0); 537 assertEquals((byte) -1, p.readByte()); 538 p.recycle(); 539 540 p = Parcel.obtain(); 541 p.writeByte(Byte.MAX_VALUE); 542 p.setDataPosition(0); 543 assertEquals(Byte.MAX_VALUE, p.readByte()); 544 p.recycle(); 545 546 p = Parcel.obtain(); 547 p.writeByte(Byte.MIN_VALUE); 548 p.setDataPosition(0); 549 assertEquals(Byte.MIN_VALUE, p.readByte()); 550 p.recycle(); 551 552 p = Parcel.obtain(); 553 p.writeByte(Byte.MAX_VALUE); 554 p.writeByte((byte) 11); 555 p.writeByte((byte) 1); 556 p.writeByte((byte) 0); 557 p.writeByte((byte) -1); 558 p.writeByte((byte) -11); 559 p.writeByte(Byte.MIN_VALUE); 560 p.setDataPosition(0); 561 assertEquals(Byte.MAX_VALUE, p.readByte()); 562 assertEquals((byte) 11, p.readByte()); 563 assertEquals((byte) 1, p.readByte()); 564 assertEquals((byte) 0, p.readByte()); 565 assertEquals((byte) -1, p.readByte()); 566 assertEquals((byte) -11, p.readByte()); 567 assertEquals(Byte.MIN_VALUE, p.readByte()); 568 p.recycle(); 569 } 570 571 public void testReadByteArray() { 572 Parcel p; 573 574 byte[] a = {(byte) 21}; 575 byte[] b = new byte[a.length]; 576 577 byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 578 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 579 byte[] d = new byte[c.length]; 580 581 // test write null 582 p = Parcel.obtain(); 583 p.writeByteArray(null); 584 p.setDataPosition(0); 585 try { 586 p.readByteArray(null); 587 fail("Should throw a RuntimeException"); 588 } catch (RuntimeException e) { 589 //expected 590 } 591 592 p.setDataPosition(0); 593 try { 594 p.readByteArray(b); 595 fail("Should throw a RuntimeException"); 596 } catch (RuntimeException e) { 597 //expected 598 } 599 p.recycle(); 600 601 // test write byte array with length: 1 602 p = Parcel.obtain(); 603 p.writeByteArray(a); 604 p.setDataPosition(0); 605 try { 606 p.readByteArray(d); 607 fail("Should throw a RuntimeException"); 608 } catch (RuntimeException e) { 609 //expected 610 } 611 612 p.setDataPosition(0); 613 p.readByteArray(b); 614 for (int i = 0; i < a.length; i++) { 615 assertEquals(a[i], b[i]); 616 } 617 p.recycle(); 618 619 // test write byte array with length: 9 620 p = Parcel.obtain(); 621 p.writeByteArray(c); 622 p.setDataPosition(0); 623 try { 624 p.readByteArray(b); 625 fail("Should throw a RuntimeException"); 626 } catch (RuntimeException e) { 627 //expected 628 } 629 630 p.setDataPosition(0); 631 p.readByteArray(d); 632 for (int i = 0; i < c.length; i++) { 633 assertEquals(c[i], d[i]); 634 } 635 p.recycle(); 636 637 // Test array bounds checks (null already checked above). 638 p = Parcel.obtain(); 639 try { 640 p.writeByteArray(c, -1, 1); // Negative offset. 641 fail(); 642 } catch (RuntimeException expected) { 643 } 644 try { 645 p.writeByteArray(c, 0, -1); // Negative count. 646 fail(); 647 } catch (RuntimeException expected) { 648 } 649 try { 650 p.writeByteArray(c, c.length + 1, 1); // High offset. 651 fail(); 652 } catch (RuntimeException expected) { 653 } 654 try { 655 p.writeByteArray(c, 0, c.length + 1); // High count. 656 fail(); 657 } catch (RuntimeException expected) { 658 } 659 p.recycle(); 660 } 661 662 public void testWriteByteArray() { 663 Parcel p; 664 665 byte[] a = {(byte) 21}; 666 byte[] b = new byte[a.length]; 667 668 byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 669 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 670 byte[] d = new byte[c.length - 2]; 671 672 // test write null 673 p = Parcel.obtain(); 674 p.writeByteArray(null, 0, 2); 675 p.setDataPosition(0); 676 try { 677 p.readByteArray(null); 678 fail("Should throw a RuntimeException"); 679 } catch (RuntimeException e) { 680 //expected 681 } 682 683 p.setDataPosition(0); 684 try { 685 p.readByteArray(b); 686 fail("Should throw a RuntimeException"); 687 } catch (RuntimeException e) { 688 //expected 689 } 690 p.recycle(); 691 692 // test with wrong offset and length 693 p = Parcel.obtain(); 694 try { 695 p.writeByteArray(a, 0, 2); 696 fail("Should throw a ArrayIndexOutOfBoundsException"); 697 } catch (ArrayIndexOutOfBoundsException e) { 698 //expected 699 } 700 p.recycle(); 701 702 p = Parcel.obtain(); 703 try { 704 p.writeByteArray(a, -1, 1); 705 fail("Should throw a ArrayIndexOutOfBoundsException"); 706 } catch (ArrayIndexOutOfBoundsException e) { 707 //expected 708 } 709 p.recycle(); 710 711 p = Parcel.obtain(); 712 try { 713 p.writeByteArray(a, 0, -1); 714 fail("Should throw a ArrayIndexOutOfBoundsException"); 715 } catch (ArrayIndexOutOfBoundsException e) { 716 //expected 717 } 718 p.recycle(); 719 720 // test write byte array with length: 1 721 p = Parcel.obtain(); 722 p.writeByteArray(a, 0 , 1); 723 p.setDataPosition(0); 724 try { 725 p.readByteArray(d); 726 fail("Should throw a RuntimeException"); 727 } catch (RuntimeException e) { 728 //expected 729 } 730 731 p.setDataPosition(0); 732 p.readByteArray(b); 733 for (int i = 0; i < a.length; i++) { 734 assertEquals(a[i], b[i]); 735 } 736 p.recycle(); 737 738 // test write byte array with offset: 1, length: 7 739 p = Parcel.obtain(); 740 p.writeByteArray(c, 1, 7); 741 p.setDataPosition(0); 742 try { 743 p.readByteArray(b); 744 fail("Should throw a RuntimeException"); 745 } catch (RuntimeException e) { 746 //expected 747 } 748 749 d = new byte[c.length - 2]; 750 p.setDataPosition(0); 751 p.readByteArray(d); 752 for (int i = 0; i < d.length; i++) { 753 Log.d("Trace", "i=" + i + " d[i]=" + d[i]); 754 } 755 for (int i = 0; i < 7; i++) { 756 assertEquals(c[i + 1], d[i]); 757 } 758 p.recycle(); 759 } 760 761 public void testCreateByteArray() { 762 Parcel p; 763 764 byte[] a = {(byte) 21}; 765 byte[] b; 766 767 byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 768 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 769 byte[] d; 770 771 byte[] e = {}; 772 byte[] f; 773 774 // test write null 775 p = Parcel.obtain(); 776 p.writeByteArray(null); 777 p.setDataPosition(0); 778 b = p.createByteArray(); 779 assertNull(b); 780 p.recycle(); 781 782 // test write byte array with length: 0 783 p = Parcel.obtain(); 784 p.writeByteArray(e); 785 p.setDataPosition(0); 786 f = p.createByteArray(); 787 assertNotNull(f); 788 assertEquals(0, f.length); 789 p.recycle(); 790 791 // test write byte array with length: 1 792 p = Parcel.obtain(); 793 p.writeByteArray(a); 794 p.setDataPosition(0); 795 b = p.createByteArray(); 796 assertNotNull(b); 797 for (int i = 0; i < a.length; i++) { 798 assertEquals(a[i], b[i]); 799 } 800 p.recycle(); 801 802 // test write byte array with length: 9 803 p = Parcel.obtain(); 804 p.writeByteArray(c); 805 p.setDataPosition(0); 806 d = p.createByteArray(); 807 assertNotNull(d); 808 for (int i = 0; i < c.length; i++) { 809 assertEquals(c[i], d[i]); 810 } 811 p.recycle(); 812 } 813 814 public void testReadCharArray() { 815 Parcel p; 816 817 char[] a = {'a'}; 818 char[] b = new char[a.length]; 819 820 char[] c = {'a', Character.MAX_VALUE, Character.MIN_VALUE, Character.MAX_SURROGATE, Character.MIN_SURROGATE, 821 Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE, 822 Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE}; 823 char[] d = new char[c.length]; 824 825 // test write null 826 p = Parcel.obtain(); 827 p.writeCharArray(null); 828 p.setDataPosition(0); 829 try { 830 p.readCharArray(null); 831 fail("Should throw a RuntimeException"); 832 } catch (RuntimeException e) { 833 //expected 834 } 835 836 p.setDataPosition(0); 837 try { 838 p.readCharArray(b); 839 fail("Should throw a RuntimeException"); 840 } catch (RuntimeException e) { 841 //expected 842 } 843 p.recycle(); 844 845 // test write char array with length: 1 846 p = Parcel.obtain(); 847 p.writeCharArray(a); 848 p.setDataPosition(0); 849 try { 850 p.readCharArray(d); 851 fail("Should throw a RuntimeException"); 852 } catch (RuntimeException e) { 853 //expected 854 } 855 856 p.setDataPosition(0); 857 p.readCharArray(b); 858 for (int i = 0; i < a.length; i++) { 859 assertEquals(a[i], b[i]); 860 } 861 p.recycle(); 862 863 // test write char array with length: 9 864 p = Parcel.obtain(); 865 p.writeCharArray(c); 866 p.setDataPosition(0); 867 try { 868 p.readCharArray(b); 869 fail("Should throw a RuntimeException"); 870 } catch (RuntimeException e) { 871 //expected 872 } 873 874 p.setDataPosition(0); 875 p.readCharArray(d); 876 for (int i = 0; i < c.length; i++) { 877 assertEquals(c[i], d[i]); 878 } 879 p.recycle(); 880 } 881 882 public void testCreateCharArray() { 883 Parcel p; 884 885 char[] a = {'a'}; 886 char[] b; 887 888 char[] c = {'a', Character.MAX_VALUE, Character.MIN_VALUE, Character.MAX_SURROGATE, Character.MIN_SURROGATE, 889 Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE, 890 Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE}; 891 char[] d; 892 893 char[] e = {}; 894 char[] f; 895 896 // test write null 897 p = Parcel.obtain(); 898 p.writeCharArray(null); 899 p.setDataPosition(0); 900 b = p.createCharArray(); 901 assertNull(b); 902 p.recycle(); 903 904 // test write char array with length: 1 905 p = Parcel.obtain(); 906 p.writeCharArray(e); 907 p.setDataPosition(0); 908 f = p.createCharArray(); 909 assertNotNull(e); 910 assertEquals(0, f.length); 911 p.recycle(); 912 913 // test write char array with length: 1 914 p = Parcel.obtain(); 915 p.writeCharArray(a); 916 p.setDataPosition(0); 917 b = p.createCharArray(); 918 assertNotNull(b); 919 for (int i = 0; i < a.length; i++) { 920 assertEquals(a[i], b[i]); 921 } 922 p.recycle(); 923 924 // test write char array with length: 9 925 p = Parcel.obtain(); 926 p.writeCharArray(c); 927 p.setDataPosition(0); 928 d = p.createCharArray(); 929 assertNotNull(d); 930 for (int i = 0; i < c.length; i++) { 931 assertEquals(c[i], d[i]); 932 } 933 p.recycle(); 934 } 935 936 public void testReadInt() { 937 Parcel p; 938 939 p = Parcel.obtain(); 940 p.writeInt(0); 941 p.setDataPosition(0); 942 assertEquals(0, p.readInt()); 943 p.recycle(); 944 945 p = Parcel.obtain(); 946 p.writeInt(1); 947 p.setDataPosition(0); 948 assertEquals(1, p.readInt()); 949 p.recycle(); 950 951 p = Parcel.obtain(); 952 p.writeInt(-1); 953 p.setDataPosition(0); 954 assertEquals(-1, p.readInt()); 955 p.recycle(); 956 957 p = Parcel.obtain(); 958 p.writeInt(Integer.MAX_VALUE); 959 p.setDataPosition(0); 960 assertEquals(Integer.MAX_VALUE, p.readInt()); 961 p.recycle(); 962 963 p = Parcel.obtain(); 964 p.writeInt(Integer.MIN_VALUE); 965 p.setDataPosition(0); 966 assertEquals(Integer.MIN_VALUE, p.readInt()); 967 p.recycle(); 968 969 p = Parcel.obtain(); 970 p.writeInt(Integer.MAX_VALUE); 971 p.writeInt(11); 972 p.writeInt(1); 973 p.writeInt(0); 974 p.writeInt(-1); 975 p.writeInt(-11); 976 p.writeInt(Integer.MIN_VALUE); 977 p.setDataPosition(0); 978 assertEquals(Integer.MAX_VALUE, p.readInt()); 979 assertEquals(11, p.readInt()); 980 assertEquals(1, p.readInt()); 981 assertEquals(0, p.readInt()); 982 assertEquals(-1, p.readInt()); 983 assertEquals(-11, p.readInt()); 984 assertEquals(Integer.MIN_VALUE, p.readInt()); 985 p.recycle(); 986 } 987 988 public void testReadIntArray() { 989 Parcel p; 990 991 int[] a = {21}; 992 int[] b = new int[a.length]; 993 994 int[] c = {Integer.MAX_VALUE, 111, 11, 1, 0, -1, -11, -111, Integer.MIN_VALUE}; 995 int[] d = new int[c.length]; 996 997 // test write null 998 p = Parcel.obtain(); 999 p.writeIntArray(null); 1000 p.setDataPosition(0); 1001 try { 1002 p.readIntArray(null); 1003 fail("Should throw a RuntimeException"); 1004 } catch (RuntimeException e) { 1005 //expected 1006 } 1007 1008 p.setDataPosition(0); 1009 try { 1010 p.readIntArray(b); 1011 fail("Should throw a RuntimeException"); 1012 } catch (RuntimeException e) { 1013 //expected 1014 } 1015 p.recycle(); 1016 1017 // test write int array with length: 1 1018 p = Parcel.obtain(); 1019 p.writeIntArray(a); 1020 p.setDataPosition(0); 1021 try { 1022 p.readIntArray(d); 1023 fail("Should throw a RuntimeException"); 1024 } catch (RuntimeException e) { 1025 //expected 1026 } 1027 1028 p.setDataPosition(0); 1029 p.readIntArray(b); 1030 for (int i = 0; i < a.length; i++) { 1031 assertEquals(a[i], b[i]); 1032 } 1033 p.recycle(); 1034 1035 // test write int array with length: 9 1036 p = Parcel.obtain(); 1037 p.writeIntArray(c); 1038 p.setDataPosition(0); 1039 try { 1040 p.readIntArray(b); 1041 fail("Should throw a RuntimeException"); 1042 } catch (RuntimeException e) { 1043 //expected 1044 } 1045 1046 p.setDataPosition(0); 1047 p.readIntArray(d); 1048 for (int i = 0; i < c.length; i++) { 1049 assertEquals(c[i], d[i]); 1050 } 1051 p.recycle(); 1052 } 1053 1054 public void testCreateIntArray() { 1055 Parcel p; 1056 1057 int[] a = {21}; 1058 int[] b; 1059 1060 int[] c = {Integer.MAX_VALUE, 111, 11, 1, 0, -1, -11, -111, Integer.MIN_VALUE}; 1061 int[] d; 1062 1063 int[] e = {}; 1064 int[] f; 1065 1066 // test write null 1067 p = Parcel.obtain(); 1068 p.writeIntArray(null); 1069 p.setDataPosition(0); 1070 b = p.createIntArray(); 1071 assertNull(b); 1072 p.recycle(); 1073 1074 // test write int array with length: 0 1075 p = Parcel.obtain(); 1076 p.writeIntArray(e); 1077 p.setDataPosition(0); 1078 f = p.createIntArray(); 1079 assertNotNull(e); 1080 assertEquals(0, f.length); 1081 p.recycle(); 1082 1083 // test write int array with length: 1 1084 p = Parcel.obtain(); 1085 p.writeIntArray(a); 1086 p.setDataPosition(0); 1087 b = p.createIntArray(); 1088 assertNotNull(b); 1089 for (int i = 0; i < a.length; i++) { 1090 assertEquals(a[i], b[i]); 1091 } 1092 p.recycle(); 1093 1094 // test write int array with length: 9 1095 p = Parcel.obtain(); 1096 p.writeIntArray(c); 1097 p.setDataPosition(0); 1098 d = p.createIntArray(); 1099 assertNotNull(d); 1100 for (int i = 0; i < c.length; i++) { 1101 assertEquals(c[i], d[i]); 1102 } 1103 p.recycle(); 1104 } 1105 1106 public void testReadLong() { 1107 Parcel p; 1108 1109 p = Parcel.obtain(); 1110 p.writeLong(0L); 1111 p.setDataPosition(0); 1112 assertEquals(0, p.readLong()); 1113 p.recycle(); 1114 1115 p = Parcel.obtain(); 1116 p.writeLong(1L); 1117 p.setDataPosition(0); 1118 assertEquals(1, p.readLong()); 1119 p.recycle(); 1120 1121 p = Parcel.obtain(); 1122 p.writeLong(-1L); 1123 p.setDataPosition(0); 1124 assertEquals(-1L, p.readLong()); 1125 p.recycle(); 1126 1127 p = Parcel.obtain(); 1128 p.writeLong(Long.MAX_VALUE); 1129 p.writeLong(11L); 1130 p.writeLong(1L); 1131 p.writeLong(0L); 1132 p.writeLong(-1L); 1133 p.writeLong(-11L); 1134 p.writeLong(Long.MIN_VALUE); 1135 p.setDataPosition(0); 1136 assertEquals(Long.MAX_VALUE, p.readLong()); 1137 assertEquals(11L, p.readLong()); 1138 assertEquals(1L, p.readLong()); 1139 assertEquals(0L, p.readLong()); 1140 assertEquals(-1L, p.readLong()); 1141 assertEquals(-11L, p.readLong()); 1142 assertEquals(Long.MIN_VALUE, p.readLong()); 1143 p.recycle(); 1144 } 1145 1146 public void testReadLongArray() { 1147 Parcel p; 1148 1149 long[] a = {21L}; 1150 long[] b = new long[a.length]; 1151 1152 long[] c = {Long.MAX_VALUE, 111L, 11L, 1L, 0L, -1L, -11L, -111L, Long.MIN_VALUE}; 1153 long[] d = new long[c.length]; 1154 1155 // test write null 1156 p = Parcel.obtain(); 1157 p.writeLongArray(null); 1158 p.setDataPosition(0); 1159 try { 1160 p.readLongArray(null); 1161 fail("Should throw a RuntimeException"); 1162 } catch (RuntimeException e) { 1163 //expected 1164 } 1165 1166 p.setDataPosition(0); 1167 try { 1168 p.readLongArray(b); 1169 fail("Should throw a RuntimeException"); 1170 } catch (RuntimeException e) { 1171 //expected 1172 } 1173 p.recycle(); 1174 1175 // test write long array with length: 1 1176 p = Parcel.obtain(); 1177 p.writeLongArray(a); 1178 p.setDataPosition(0); 1179 try { 1180 p.readLongArray(d); 1181 fail("Should throw a RuntimeException"); 1182 } catch (RuntimeException e) { 1183 //expected 1184 } 1185 1186 p.setDataPosition(0); 1187 p.readLongArray(b); 1188 for (int i = 0; i < a.length; i++) { 1189 assertEquals(a[i], b[i]); 1190 } 1191 p.recycle(); 1192 1193 // test write long array with length: 9 1194 p = Parcel.obtain(); 1195 p.writeLongArray(c); 1196 p.setDataPosition(0); 1197 try { 1198 p.readLongArray(b); 1199 fail("Should throw a RuntimeException"); 1200 } catch (RuntimeException e) { 1201 //expected 1202 } 1203 1204 p.setDataPosition(0); 1205 p.readLongArray(d); 1206 for (int i = 0; i < c.length; i++) { 1207 assertEquals(c[i], d[i]); 1208 } 1209 p.recycle(); 1210 } 1211 1212 public void testCreateLongArray() { 1213 Parcel p; 1214 1215 long[] a = {21L}; 1216 long[] b; 1217 1218 long[] c = {Long.MAX_VALUE, 111L, 11L, 1L, 0L, -1L, -11L, -111L, Long.MIN_VALUE}; 1219 long[] d; 1220 1221 long[] e = {}; 1222 long[] f; 1223 1224 // test write null 1225 p = Parcel.obtain(); 1226 p.writeLongArray(null); 1227 p.setDataPosition(0); 1228 b = p.createLongArray(); 1229 assertNull(b); 1230 p.recycle(); 1231 1232 // test write long array with length: 0 1233 p = Parcel.obtain(); 1234 p.writeLongArray(e); 1235 p.setDataPosition(0); 1236 f = p.createLongArray(); 1237 assertNotNull(e); 1238 assertEquals(0, f.length); 1239 p.recycle(); 1240 1241 // test write long array with length: 1 1242 p = Parcel.obtain(); 1243 p.writeLongArray(a); 1244 p.setDataPosition(0); 1245 b = p.createLongArray(); 1246 assertNotNull(b); 1247 for (int i = 0; i < a.length; i++) { 1248 assertEquals(a[i], b[i]); 1249 } 1250 p.recycle(); 1251 1252 // test write long array with length: 9 1253 p = Parcel.obtain(); 1254 p.writeLongArray(c); 1255 p.setDataPosition(0); 1256 d = p.createLongArray(); 1257 assertNotNull(d); 1258 for (int i = 0; i < c.length; i++) { 1259 assertEquals(c[i], d[i]); 1260 } 1261 p.recycle(); 1262 } 1263 1264 public void testReadFloat() { 1265 Parcel p; 1266 1267 p = Parcel.obtain(); 1268 p.writeFloat(.0f); 1269 p.setDataPosition(0); 1270 assertEquals(.0f, p.readFloat()); 1271 p.recycle(); 1272 1273 p = Parcel.obtain(); 1274 p.writeFloat(0.1f); 1275 p.setDataPosition(0); 1276 assertEquals(0.1f, p.readFloat()); 1277 p.recycle(); 1278 1279 p = Parcel.obtain(); 1280 p.writeFloat(-1.1f); 1281 p.setDataPosition(0); 1282 assertEquals(-1.1f, p.readFloat()); 1283 p.recycle(); 1284 1285 p = Parcel.obtain(); 1286 p.writeFloat(Float.MAX_VALUE); 1287 p.setDataPosition(0); 1288 assertEquals(Float.MAX_VALUE, p.readFloat()); 1289 p.recycle(); 1290 1291 p = Parcel.obtain(); 1292 p.writeFloat(Float.MIN_VALUE); 1293 p.setDataPosition(0); 1294 assertEquals(Float.MIN_VALUE, p.readFloat()); 1295 p.recycle(); 1296 1297 p = Parcel.obtain(); 1298 p.writeFloat(Float.MAX_VALUE); 1299 p.writeFloat(1.1f); 1300 p.writeFloat(0.1f); 1301 p.writeFloat(.0f); 1302 p.writeFloat(-0.1f); 1303 p.writeFloat(-1.1f); 1304 p.writeFloat(Float.MIN_VALUE); 1305 p.setDataPosition(0); 1306 assertEquals(Float.MAX_VALUE, p.readFloat()); 1307 assertEquals(1.1f, p.readFloat()); 1308 assertEquals(0.1f, p.readFloat()); 1309 assertEquals(.0f, p.readFloat()); 1310 assertEquals(-0.1f, p.readFloat()); 1311 assertEquals(-1.1f, p.readFloat()); 1312 assertEquals(Float.MIN_VALUE, p.readFloat()); 1313 p.recycle(); 1314 } 1315 1316 public void testReadFloatArray() { 1317 Parcel p; 1318 1319 float[] a = {2.1f}; 1320 float[] b = new float[a.length]; 1321 1322 float[] c = {Float.MAX_VALUE, 11.1f, 1.1f, 0.1f, .0f, -0.1f, -1.1f, -11.1f, Float.MIN_VALUE}; 1323 float[] d = new float[c.length]; 1324 1325 // test write null 1326 p = Parcel.obtain(); 1327 p.writeFloatArray(null); 1328 p.setDataPosition(0); 1329 try { 1330 p.readFloatArray(null); 1331 fail("Should throw a RuntimeException"); 1332 } catch (RuntimeException e) { 1333 //expected 1334 } 1335 1336 p.setDataPosition(0); 1337 try { 1338 p.readFloatArray(b); 1339 fail("Should throw a RuntimeException"); 1340 } catch (RuntimeException e) { 1341 //expected 1342 } 1343 p.recycle(); 1344 1345 // test write float array with length: 1 1346 p = Parcel.obtain(); 1347 p.writeFloatArray(a); 1348 p.setDataPosition(0); 1349 try { 1350 p.readFloatArray(d); 1351 fail("Should throw a RuntimeException"); 1352 } catch (RuntimeException e) { 1353 //expected 1354 } 1355 1356 p.setDataPosition(0); 1357 p.readFloatArray(b); 1358 for (int i = 0; i < a.length; i++) { 1359 assertEquals(a[i], b[i]); 1360 } 1361 p.recycle(); 1362 1363 // test write float array with length: 9 1364 p = Parcel.obtain(); 1365 p.writeFloatArray(c); 1366 p.setDataPosition(0); 1367 try { 1368 p.readFloatArray(b); 1369 fail("Should throw a RuntimeException"); 1370 } catch (RuntimeException e) { 1371 //expected 1372 } 1373 1374 p.setDataPosition(0); 1375 p.readFloatArray(d); 1376 for (int i = 0; i < c.length; i++) { 1377 assertEquals(c[i], d[i]); 1378 } 1379 p.recycle(); 1380 } 1381 1382 public void testCreateFloatArray() { 1383 Parcel p; 1384 1385 float[] a = {2.1f}; 1386 float[] b; 1387 1388 float[] c = {Float.MAX_VALUE, 11.1f, 1.1f, 0.1f, .0f, -0.1f, -1.1f, -11.1f, Float.MIN_VALUE}; 1389 float[] d; 1390 1391 float[] e = {}; 1392 float[] f; 1393 1394 // test write null 1395 p = Parcel.obtain(); 1396 p.writeFloatArray(null); 1397 p.setDataPosition(0); 1398 b = p.createFloatArray(); 1399 assertNull(b); 1400 p.recycle(); 1401 1402 // test write float array with length: 0 1403 p = Parcel.obtain(); 1404 p.writeFloatArray(e); 1405 p.setDataPosition(0); 1406 f = p.createFloatArray(); 1407 assertNotNull(f); 1408 assertEquals(0, f.length); 1409 p.recycle(); 1410 1411 // test write float array with length: 1 1412 p = Parcel.obtain(); 1413 p.writeFloatArray(a); 1414 p.setDataPosition(0); 1415 b = p.createFloatArray(); 1416 assertNotNull(b); 1417 for (int i = 0; i < a.length; i++) { 1418 assertEquals(a[i], b[i]); 1419 } 1420 p.recycle(); 1421 1422 // test write float array with length: 9 1423 p = Parcel.obtain(); 1424 p.writeFloatArray(c); 1425 p.setDataPosition(0); 1426 d = p.createFloatArray(); 1427 assertNotNull(d); 1428 for (int i = 0; i < c.length; i++) { 1429 assertEquals(c[i], d[i]); 1430 } 1431 p.recycle(); 1432 } 1433 1434 public void testReadDouble() { 1435 Parcel p; 1436 1437 p = Parcel.obtain(); 1438 p.writeDouble(.0d); 1439 p.setDataPosition(0); 1440 assertEquals(.0d, p.readDouble()); 1441 p.recycle(); 1442 1443 p = Parcel.obtain(); 1444 p.writeDouble(0.1d); 1445 p.setDataPosition(0); 1446 assertEquals(0.1d, p.readDouble()); 1447 p.recycle(); 1448 1449 p = Parcel.obtain(); 1450 p.writeDouble(-1.1d); 1451 p.setDataPosition(0); 1452 assertEquals(-1.1d, p.readDouble()); 1453 p.recycle(); 1454 1455 p = Parcel.obtain(); 1456 p.writeDouble(Double.MAX_VALUE); 1457 p.setDataPosition(0); 1458 assertEquals(Double.MAX_VALUE, p.readDouble()); 1459 p.recycle(); 1460 1461 p = Parcel.obtain(); 1462 p.writeDouble(Double.MIN_VALUE); 1463 p.setDataPosition(0); 1464 assertEquals(Double.MIN_VALUE, p.readDouble()); 1465 p.recycle(); 1466 1467 p = Parcel.obtain(); 1468 p.writeDouble(Double.MAX_VALUE); 1469 p.writeDouble(1.1d); 1470 p.writeDouble(0.1d); 1471 p.writeDouble(.0d); 1472 p.writeDouble(-0.1d); 1473 p.writeDouble(-1.1d); 1474 p.writeDouble(Double.MIN_VALUE); 1475 p.setDataPosition(0); 1476 assertEquals(Double.MAX_VALUE, p.readDouble()); 1477 assertEquals(1.1d, p.readDouble()); 1478 assertEquals(0.1d, p.readDouble()); 1479 assertEquals(.0d, p.readDouble()); 1480 assertEquals(-0.1d, p.readDouble()); 1481 assertEquals(-1.1d, p.readDouble()); 1482 assertEquals(Double.MIN_VALUE, p.readDouble()); 1483 p.recycle(); 1484 } 1485 1486 public void testReadDoubleArray() { 1487 Parcel p; 1488 1489 double[] a = {2.1d}; 1490 double[] b = new double[a.length]; 1491 1492 double[] c = {Double.MAX_VALUE, 11.1d, 1.1d, 0.1d, .0d, -0.1d, -1.1d, -11.1d, Double.MIN_VALUE}; 1493 double[] d = new double[c.length]; 1494 1495 // test write null 1496 p = Parcel.obtain(); 1497 p.writeDoubleArray(null); 1498 p.setDataPosition(0); 1499 try { 1500 p.readDoubleArray(null); 1501 fail("Should throw a RuntimeException"); 1502 } catch (RuntimeException e) { 1503 //expected 1504 } 1505 1506 p.setDataPosition(0); 1507 try { 1508 p.readDoubleArray(b); 1509 fail("Should throw a RuntimeException"); 1510 } catch (RuntimeException e) { 1511 //expected 1512 } 1513 p.recycle(); 1514 1515 // test write double array with length: 1 1516 p = Parcel.obtain(); 1517 p.writeDoubleArray(a); 1518 p.setDataPosition(0); 1519 try { 1520 p.readDoubleArray(d); 1521 fail("Should throw a RuntimeException"); 1522 } catch (RuntimeException e) { 1523 //expected 1524 } 1525 1526 p.setDataPosition(0); 1527 p.readDoubleArray(b); 1528 for (int i = 0; i < a.length; i++) { 1529 assertEquals(a[i], b[i]); 1530 } 1531 p.recycle(); 1532 1533 // test write double array with length: 9 1534 p = Parcel.obtain(); 1535 p.writeDoubleArray(c); 1536 p.setDataPosition(0); 1537 try { 1538 p.readDoubleArray(b); 1539 fail("Should throw a RuntimeException"); 1540 } catch (RuntimeException e) { 1541 //expected 1542 } 1543 1544 p.setDataPosition(0); 1545 p.readDoubleArray(d); 1546 for (int i = 0; i < c.length; i++) { 1547 assertEquals(c[i], d[i]); 1548 } 1549 p.recycle(); 1550 } 1551 1552 public void testCreateDoubleArray() { 1553 Parcel p; 1554 1555 double[] a = {2.1d}; 1556 double[] b; 1557 1558 double[] c = { 1559 Double.MAX_VALUE, 11.1d, 1.1d, 0.1d, .0d, -0.1d, -1.1d, -11.1d, Double.MIN_VALUE 1560 }; 1561 double[] d; 1562 1563 double[] e = {}; 1564 double[] f; 1565 1566 // test write null 1567 p = Parcel.obtain(); 1568 p.writeDoubleArray(null); 1569 p.setDataPosition(0); 1570 b = p.createDoubleArray(); 1571 assertNull(b); 1572 p.recycle(); 1573 1574 // test write double array with length: 0 1575 p = Parcel.obtain(); 1576 p.writeDoubleArray(e); 1577 p.setDataPosition(0); 1578 f = p.createDoubleArray(); 1579 assertNotNull(f); 1580 assertEquals(0, f.length); 1581 p.recycle(); 1582 1583 // test write double array with length: 1 1584 p = Parcel.obtain(); 1585 p.writeDoubleArray(a); 1586 p.setDataPosition(0); 1587 b = p.createDoubleArray(); 1588 assertNotNull(b); 1589 for (int i = 0; i < a.length; i++) { 1590 assertEquals(a[i], b[i]); 1591 } 1592 p.recycle(); 1593 1594 // test write double array with length: 9 1595 p = Parcel.obtain(); 1596 p.writeDoubleArray(c); 1597 p.setDataPosition(0); 1598 d = p.createDoubleArray(); 1599 assertNotNull(d); 1600 for (int i = 0; i < c.length; i++) { 1601 assertEquals(c[i], d[i]); 1602 } 1603 p.recycle(); 1604 } 1605 1606 public void testReadBooleanArray() { 1607 Parcel p; 1608 1609 boolean[] a = {true}; 1610 boolean[] b = new boolean[a.length]; 1611 1612 boolean[] c = {true, false, true, false}; 1613 boolean[] d = new boolean[c.length]; 1614 1615 // test write null 1616 p = Parcel.obtain(); 1617 p.writeBooleanArray(null); 1618 p.setDataPosition(0); 1619 try { 1620 p.readIntArray(null); 1621 fail("Should throw a RuntimeException"); 1622 } catch (RuntimeException e) { 1623 //expected 1624 } 1625 1626 p.setDataPosition(0); 1627 try { 1628 p.readBooleanArray(b); 1629 fail("Should throw a RuntimeException"); 1630 } catch (RuntimeException e) { 1631 //expected 1632 } 1633 p.recycle(); 1634 1635 // test write boolean array with length: 1 1636 p = Parcel.obtain(); 1637 p.writeBooleanArray(a); 1638 p.setDataPosition(0); 1639 try { 1640 p.readBooleanArray(d); 1641 fail("Should throw a RuntimeException"); 1642 } catch (RuntimeException e) { 1643 //expected 1644 } 1645 1646 p.setDataPosition(0); 1647 p.readBooleanArray(b); 1648 for (int i = 0; i < a.length; i++) { 1649 assertEquals(a[i], b[i]); 1650 } 1651 p.recycle(); 1652 1653 // test write boolean array with length: 4 1654 p = Parcel.obtain(); 1655 p.writeBooleanArray(c); 1656 p.setDataPosition(0); 1657 try { 1658 p.readBooleanArray(b); 1659 fail("Should throw a RuntimeException"); 1660 } catch (RuntimeException e) { 1661 //expected 1662 } 1663 1664 p.setDataPosition(0); 1665 p.readBooleanArray(d); 1666 for (int i = 0; i < c.length; i++) { 1667 assertEquals(c[i], d[i]); 1668 } 1669 p.recycle(); 1670 } 1671 1672 public void testCreateBooleanArray() { 1673 Parcel p; 1674 1675 boolean[] a = {true}; 1676 boolean[] b; 1677 1678 boolean[] c = {true, false, true, false}; 1679 boolean[] d; 1680 1681 boolean[] e = {}; 1682 boolean[] f; 1683 1684 // test write null 1685 p = Parcel.obtain(); 1686 p.writeBooleanArray(null); 1687 p.setDataPosition(0); 1688 b = p.createBooleanArray(); 1689 assertNull(b); 1690 p.recycle(); 1691 1692 // test write boolean array with length: 0 1693 p = Parcel.obtain(); 1694 p.writeBooleanArray(e); 1695 p.setDataPosition(0); 1696 f = p.createBooleanArray(); 1697 assertNotNull(f); 1698 assertEquals(0, f.length); 1699 p.recycle(); 1700 1701 // test write boolean array with length: 1 1702 p = Parcel.obtain(); 1703 p.writeBooleanArray(a); 1704 1705 p.setDataPosition(0); 1706 b = p.createBooleanArray(); 1707 assertNotNull(b); 1708 for (int i = 0; i < a.length; i++) { 1709 assertEquals(a[i], b[i]); 1710 } 1711 p.recycle(); 1712 1713 // test write boolean array with length: 4 1714 p = Parcel.obtain(); 1715 p.writeBooleanArray(c); 1716 p.setDataPosition(0); 1717 d = p.createBooleanArray(); 1718 assertNotNull(d); 1719 for (int i = 0; i < c.length; i++) { 1720 assertEquals(c[i], d[i]); 1721 } 1722 p.recycle(); 1723 } 1724 1725 public void testReadString() { 1726 Parcel p; 1727 final String string = "Hello, Android!"; 1728 1729 // test write null 1730 p = Parcel.obtain(); 1731 p.writeString(null); 1732 p.setDataPosition(0); 1733 assertNull(p.readString()); 1734 p.recycle(); 1735 1736 p = Parcel.obtain(); 1737 p.writeString(""); 1738 p.setDataPosition(0); 1739 assertEquals("", p.readString()); 1740 p.recycle(); 1741 1742 p = Parcel.obtain(); 1743 p.writeString("a"); 1744 p.setDataPosition(0); 1745 assertEquals("a", p.readString()); 1746 p.recycle(); 1747 1748 p = Parcel.obtain(); 1749 p.writeString(string); 1750 p.setDataPosition(0); 1751 assertEquals(string, p.readString()); 1752 p.recycle(); 1753 1754 p = Parcel.obtain(); 1755 p.writeString(string); 1756 p.writeString("a"); 1757 p.writeString(""); 1758 p.setDataPosition(0); 1759 assertEquals(string, p.readString()); 1760 assertEquals("a", p.readString()); 1761 assertEquals("", p.readString()); 1762 p.recycle(); 1763 } 1764 1765 public void testReadStringArray() { 1766 Parcel p; 1767 1768 String[] a = {"21"}; 1769 String[] b = new String[a.length]; 1770 1771 String[] c = {"", 1772 "a", 1773 "Hello, Android!", 1774 "A long string that is used to test the api readStringArray(),"}; 1775 String[] d = new String[c.length]; 1776 1777 // test write null 1778 p = Parcel.obtain(); 1779 p.writeStringArray(null); 1780 p.setDataPosition(0); 1781 try { 1782 p.readStringArray(null); 1783 fail("Should throw a RuntimeException"); 1784 } catch (RuntimeException e) { 1785 //expected 1786 } 1787 1788 p.setDataPosition(0); 1789 try { 1790 p.readStringArray(b); 1791 fail("Should throw a RuntimeException"); 1792 } catch (RuntimeException e) { 1793 //expected 1794 } 1795 p.recycle(); 1796 1797 // test write String array with length: 1 1798 p = Parcel.obtain(); 1799 p.writeStringArray(a); 1800 p.setDataPosition(0); 1801 try { 1802 p.readStringArray(d); 1803 fail("Should throw a RuntimeException"); 1804 } catch (RuntimeException e) { 1805 //expected 1806 } 1807 1808 p.setDataPosition(0); 1809 p.readStringArray(b); 1810 for (int i = 0; i < a.length; i++) { 1811 assertEquals(a[i], b[i]); 1812 } 1813 p.recycle(); 1814 1815 // test write String array with length: 9 1816 p = Parcel.obtain(); 1817 p.writeStringArray(c); 1818 p.setDataPosition(0); 1819 try { 1820 p.readStringArray(b); 1821 fail("Should throw a RuntimeException"); 1822 } catch (RuntimeException e) { 1823 //expected 1824 } 1825 1826 p.setDataPosition(0); 1827 p.readStringArray(d); 1828 for (int i = 0; i < c.length; i++) { 1829 assertEquals(c[i], d[i]); 1830 } 1831 p.recycle(); 1832 } 1833 1834 public void testCreateStringArray() { 1835 Parcel p; 1836 1837 String[] a = {"21"}; 1838 String[] b; 1839 1840 String[] c = {"", 1841 "a", 1842 "Hello, Android!", 1843 "A long string that is used to test the api readStringArray(),"}; 1844 String[] d; 1845 1846 String[] e = {}; 1847 String[] f; 1848 1849 // test write null 1850 p = Parcel.obtain(); 1851 p.writeStringArray(null); 1852 p.setDataPosition(0); 1853 b = p.createStringArray(); 1854 assertNull(b); 1855 p.recycle(); 1856 1857 // test write String array with length: 0 1858 p = Parcel.obtain(); 1859 p.writeStringArray(e); 1860 p.setDataPosition(0); 1861 f = p.createStringArray(); 1862 assertNotNull(e); 1863 assertEquals(0, f.length); 1864 p.recycle(); 1865 1866 // test write String array with length: 1 1867 p = Parcel.obtain(); 1868 p.writeStringArray(a); 1869 p.setDataPosition(0); 1870 b = p.createStringArray(); 1871 assertNotNull(b); 1872 for (int i = 0; i < a.length; i++) { 1873 assertEquals(a[i], b[i]); 1874 } 1875 p.recycle(); 1876 1877 // test write String array with length: 9 1878 p = Parcel.obtain(); 1879 p.writeStringArray(c); 1880 p.setDataPosition(0); 1881 d = p.createStringArray(); 1882 assertNotNull(d); 1883 for (int i = 0; i < c.length; i++) { 1884 assertEquals(c[i], d[i]); 1885 } 1886 p.recycle(); 1887 } 1888 1889 public void testReadStringList() { 1890 Parcel p; 1891 1892 ArrayList<String> a = new ArrayList<String>(); 1893 a.add("21"); 1894 ArrayList<String> b = new ArrayList<String>(); 1895 1896 ArrayList<String> c = new ArrayList<String>(); 1897 c.add(""); 1898 c.add("a"); 1899 c.add("Hello, Android!"); 1900 c.add("A long string that is used to test the api readStringList(),"); 1901 ArrayList<String> d = new ArrayList<String>(); 1902 1903 // test write null 1904 p = Parcel.obtain(); 1905 p.writeStringList(null); 1906 p.setDataPosition(0); 1907 try { 1908 p.readStringList(null); 1909 fail("Should throw a RuntimeException"); 1910 } catch (RuntimeException e) { 1911 //expected 1912 } 1913 1914 p.setDataPosition(0); 1915 p.readStringList(b); 1916 assertTrue(0 == b.size()); 1917 p.recycle(); 1918 1919 // test write String array with length: 1 1920 p = Parcel.obtain(); 1921 p.writeStringList(a); 1922 p.setDataPosition(0); 1923 assertTrue(c.size() > a.size()); 1924 p.readStringList(c); 1925 assertTrue(c.size() == a.size()); 1926 assertEquals(a, c); 1927 1928 p.setDataPosition(0); 1929 assertTrue(0 == b.size() && 0 != a.size()); 1930 p.readStringList(b); 1931 assertEquals(a, b); 1932 p.recycle(); 1933 1934 c = new ArrayList<String>(); 1935 c.add(""); 1936 c.add("a"); 1937 c.add("Hello, Android!"); 1938 c.add("A long string that is used to test the api readStringList(),"); 1939 // test write String array with length: 4 1940 p = Parcel.obtain(); 1941 p.writeStringList(c); 1942 p.setDataPosition(0); 1943 1944 assertTrue(b.size() < c.size()); 1945 p.readStringList(b); 1946 assertTrue(b.size() == c.size()); 1947 assertEquals(c, b); 1948 1949 p.setDataPosition(0); 1950 assertTrue(d.size() < c.size()); 1951 p.readStringList(d); 1952 assertEquals(c, d); 1953 p.recycle(); 1954 } 1955 1956 public void testCreateStringArrayList() { 1957 Parcel p; 1958 1959 ArrayList<String> a = new ArrayList<String>(); 1960 a.add("21"); 1961 ArrayList<String> b; 1962 1963 ArrayList<String> c = new ArrayList<String>(); 1964 c.add(""); 1965 c.add("a"); 1966 c.add("Hello, Android!"); 1967 c.add("A long string that is used to test the api readStringList(),"); 1968 ArrayList<String> d; 1969 1970 ArrayList<String> e = new ArrayList<String>(); 1971 ArrayList<String> f = null; 1972 1973 // test write null 1974 p = Parcel.obtain(); 1975 p.writeStringList(null); 1976 p.setDataPosition(0); 1977 b = p.createStringArrayList(); 1978 assertNull(b); 1979 p.recycle(); 1980 1981 // test write String array with length: 0 1982 p = Parcel.obtain(); 1983 p.writeStringList(e); 1984 p.setDataPosition(0); 1985 assertNull(f); 1986 f = p.createStringArrayList(); 1987 assertNotNull(f); 1988 p.recycle(); 1989 1990 // test write String array with length: 1 1991 p = Parcel.obtain(); 1992 p.writeStringList(a); 1993 p.setDataPosition(0); 1994 b = p.createStringArrayList(); 1995 assertEquals(a, b); 1996 p.recycle(); 1997 1998 // test write String array with length: 4 1999 p = Parcel.obtain(); 2000 p.writeStringList(c); 2001 p.setDataPosition(0); 2002 d = p.createStringArrayList(); 2003 assertEquals(c, d); 2004 p.recycle(); 2005 } 2006 2007 public void testReadSerializable() { 2008 Parcel p; 2009 2010 // test write null 2011 p = Parcel.obtain(); 2012 p.writeSerializable(null); 2013 p.setDataPosition(0); 2014 assertNull(p.readSerializable()); 2015 p.recycle(); 2016 2017 p = Parcel.obtain(); 2018 p.writeSerializable("Hello, Android!"); 2019 p.setDataPosition(0); 2020 assertEquals("Hello, Android!", p.readSerializable()); 2021 p.recycle(); 2022 } 2023 2024 public void testReadParcelable() { 2025 Parcel p; 2026 MockClassLoader mcl = new MockClassLoader(); 2027 final String signatureString = "1234567890abcdef"; 2028 Signature s = new Signature(signatureString); 2029 2030 // test write null 2031 p = Parcel.obtain(); 2032 p.writeParcelable(null, 0); 2033 p.setDataPosition(0); 2034 assertNull(p.readParcelable(mcl)); 2035 p.recycle(); 2036 2037 p = Parcel.obtain(); 2038 p.writeParcelable(s, 0); 2039 p.setDataPosition(0); 2040 assertEquals(s, p.readParcelable(mcl)); 2041 p.recycle(); 2042 } 2043 2044 public void testReadParcelableArray() { 2045 Parcel p; 2046 MockClassLoader mcl = new MockClassLoader(); 2047 Signature[] s = {new Signature("1234"), 2048 new Signature("ABCD"), 2049 new Signature("abcd")}; 2050 2051 Signature[] s2 = {new Signature("1234"), 2052 null, 2053 new Signature("abcd")}; 2054 Parcelable[] s3; 2055 2056 // test write null 2057 p = Parcel.obtain(); 2058 p.writeParcelableArray(null, 0); 2059 p.setDataPosition(0); 2060 assertNull(p.readParcelableArray(mcl)); 2061 p.recycle(); 2062 2063 p = Parcel.obtain(); 2064 p.writeParcelableArray(s, 0); 2065 p.setDataPosition(0); 2066 s3 = p.readParcelableArray(mcl); 2067 for (int i = 0; i < s.length; i++) { 2068 assertEquals(s[i], s3[i]); 2069 } 2070 p.recycle(); 2071 2072 p = Parcel.obtain(); 2073 p.writeParcelableArray(s2, 0); 2074 p.setDataPosition(0); 2075 s3 = p.readParcelableArray(mcl); 2076 for (int i = 0; i < s2.length; i++) { 2077 assertEquals(s2[i], s3[i]); 2078 } 2079 p.recycle(); 2080 } 2081 2082 public void testReadTypedArray() { 2083 Parcel p; 2084 Signature[] s = {new Signature("1234"), 2085 new Signature("ABCD"), 2086 new Signature("abcd")}; 2087 2088 Signature[] s2 = {new Signature("1234"), 2089 null, 2090 new Signature("abcd")}; 2091 Signature[] s3 = new Signature[3]; 2092 Signature[] s4 = new Signature[4]; 2093 2094 // test write null 2095 p = Parcel.obtain(); 2096 p.writeTypedArray(null, 0); 2097 p.setDataPosition(0); 2098 try { 2099 p.readTypedArray(s3, Signature.CREATOR); 2100 fail("should throw a RuntimeException"); 2101 } catch (RuntimeException e) { 2102 //expected 2103 } 2104 2105 p.setDataPosition(0); 2106 try { 2107 p.readTypedArray(null, Signature.CREATOR); 2108 fail("should throw a RuntimeException"); 2109 } catch (RuntimeException e) { 2110 //expected 2111 } 2112 p.recycle(); 2113 2114 // test write not null 2115 p = Parcel.obtain(); 2116 p.writeTypedArray(s, 0); 2117 p.setDataPosition(0); 2118 p.readTypedArray(s3, Signature.CREATOR); 2119 for (int i = 0; i < s.length; i++) { 2120 assertEquals(s[i], s3[i]); 2121 } 2122 2123 p.setDataPosition(0); 2124 try { 2125 p.readTypedArray(null, Signature.CREATOR); 2126 fail("should throw a RuntimeException"); 2127 } catch (RuntimeException e) { 2128 //expected 2129 } 2130 2131 p.setDataPosition(0); 2132 try { 2133 p.readTypedArray(s4, Signature.CREATOR); 2134 fail("should throw a RuntimeException"); 2135 } catch (RuntimeException e) { 2136 //expected 2137 } 2138 p.recycle(); 2139 2140 s3 = new Signature[s2.length]; 2141 p = Parcel.obtain(); 2142 p.writeTypedArray(s2, 0); 2143 p.setDataPosition(0); 2144 p.readTypedArray(s3, Signature.CREATOR); 2145 for (int i = 0; i < s.length; i++) { 2146 assertEquals(s2[i], s3[i]); 2147 } 2148 p.recycle(); 2149 } 2150 2151 public void testReadTypedArray2() { 2152 Parcel p; 2153 Signature[] s = { 2154 new Signature("1234"), new Signature("ABCD"), new Signature("abcd") 2155 }; 2156 2157 Signature[] s2 = { 2158 new Signature("1234"), null, new Signature("abcd") 2159 }; 2160 Signature[] s3 = { 2161 null, null, null 2162 }; 2163 2164 // test write null 2165 p = Parcel.obtain(); 2166 p.writeTypedArray(null, 0); 2167 p.setDataPosition(0); 2168 p.recycle(); 2169 2170 // test write not null 2171 p = Parcel.obtain(); 2172 p.writeTypedArray(s, 0); 2173 p.setDataPosition(0); 2174 p.readTypedArray(s3, Signature.CREATOR); 2175 for (int i = 0; i < s.length; i++) { 2176 assertEquals(s[i], s3[i]); 2177 } 2178 p.recycle(); 2179 2180 p = Parcel.obtain(); 2181 p.writeTypedArray(s2, 0); 2182 p.setDataPosition(0); 2183 p.readTypedArray(s3, Signature.CREATOR); 2184 for (int i = 0; i < s.length; i++) { 2185 assertEquals(s2[i], s3[i]); 2186 } 2187 p.recycle(); 2188 } 2189 2190 public void testCreateTypedArray() { 2191 Parcel p; 2192 Signature[] s = {new Signature("1234"), 2193 new Signature("ABCD"), 2194 new Signature("abcd")}; 2195 2196 Signature[] s2 = {new Signature("1234"), 2197 null, 2198 new Signature("abcd")}; 2199 Signature[] s3; 2200 2201 // test write null 2202 p = Parcel.obtain(); 2203 p.writeTypedArray(null, 0); 2204 p.setDataPosition(0); 2205 assertNull(p.createTypedArray(Signature.CREATOR)); 2206 p.recycle(); 2207 2208 // test write not null 2209 p = Parcel.obtain(); 2210 p.writeTypedArray(s, 0); 2211 p.setDataPosition(0); 2212 s3 = p.createTypedArray(Signature.CREATOR); 2213 for (int i = 0; i < s.length; i++) { 2214 assertEquals(s[i], s3[i]); 2215 } 2216 p.recycle(); 2217 2218 p = Parcel.obtain(); 2219 p.writeTypedArray(s2, 0); 2220 p.setDataPosition(0); 2221 s3 = p.createTypedArray(Signature.CREATOR); 2222 for (int i = 0; i < s.length; i++) { 2223 assertEquals(s2[i], s3[i]); 2224 } 2225 p.recycle(); 2226 } 2227 2228 public void testReadTypedList() { 2229 Parcel p; 2230 ArrayList<Signature> s = new ArrayList<Signature>(); 2231 s.add(new Signature("1234")); 2232 s.add(new Signature("ABCD")); 2233 s.add(new Signature("abcd")); 2234 2235 ArrayList<Signature> s2 = new ArrayList<Signature>(); 2236 s2.add(new Signature("1234")); 2237 s2.add(null); 2238 2239 ArrayList<Signature> s3 = new ArrayList<Signature>(); 2240 2241 // test write null 2242 p = Parcel.obtain(); 2243 p.writeTypedList(null); 2244 p.setDataPosition(0); 2245 p.readTypedList(s3, Signature.CREATOR); 2246 assertEquals(0, s3.size()); 2247 2248 p.setDataPosition(0); 2249 try { 2250 p.readTypedList(null, Signature.CREATOR); 2251 fail("should throw a RuntimeException"); 2252 } catch (RuntimeException e) { 2253 //expected 2254 } 2255 p.recycle(); 2256 2257 // test write not null 2258 p = Parcel.obtain(); 2259 p.writeTypedList(s); 2260 p.setDataPosition(0); 2261 p.readTypedList(s3, Signature.CREATOR); 2262 for (int i = 0; i < s.size(); i++) { 2263 assertEquals(s.get(i), s3.get(i)); 2264 } 2265 2266 p.setDataPosition(0); 2267 try { 2268 p.readTypedList(null, Signature.CREATOR); 2269 fail("should throw a RuntimeException"); 2270 } catch (RuntimeException e) { 2271 //expected 2272 } 2273 2274 p.setDataPosition(0); 2275 p.readTypedList(s2, Signature.CREATOR); 2276 assertEquals(s.size(), s2.size()); 2277 for (int i = 0; i < s.size(); i++) { 2278 assertEquals(s.get(i), s2.get(i)); 2279 } 2280 p.recycle(); 2281 2282 s2 = new ArrayList<Signature>(); 2283 s2.add(new Signature("1234")); 2284 s2.add(null); 2285 p = Parcel.obtain(); 2286 p.writeTypedList(s2); 2287 p.setDataPosition(0); 2288 p.readTypedList(s3, Signature.CREATOR); 2289 assertEquals(s3.size(), s2.size()); 2290 for (int i = 0; i < s2.size(); i++) { 2291 assertEquals(s2.get(i), s3.get(i)); 2292 } 2293 p.recycle(); 2294 } 2295 2296 public void testCreateTypedArrayList() { 2297 Parcel p; 2298 ArrayList<Signature> s = new ArrayList<Signature>(); 2299 s.add(new Signature("1234")); 2300 s.add(new Signature("ABCD")); 2301 s.add(new Signature("abcd")); 2302 2303 ArrayList<Signature> s2 = new ArrayList<Signature>(); 2304 s2.add(new Signature("1234")); 2305 s2.add(null); 2306 2307 ArrayList<Signature> s3; 2308 2309 // test write null 2310 p = Parcel.obtain(); 2311 p.writeTypedList(null); 2312 p.setDataPosition(0); 2313 assertNull(p.createTypedArrayList(Signature.CREATOR)); 2314 p.recycle(); 2315 2316 // test write not null 2317 p = Parcel.obtain(); 2318 p.writeTypedList(s); 2319 p.setDataPosition(0); 2320 s3 = p.createTypedArrayList(Signature.CREATOR); 2321 for (int i = 0; i < s.size(); i++) { 2322 assertEquals(s.get(i), s3.get(i)); 2323 } 2324 2325 p = Parcel.obtain(); 2326 p.writeTypedList(s2); 2327 p.setDataPosition(0); 2328 s3 = p.createTypedArrayList(Signature.CREATOR); 2329 assertEquals(s3.size(), s2.size()); 2330 for (int i = 0; i < s2.size(); i++) { 2331 assertEquals(s2.get(i), s3.get(i)); 2332 } 2333 p.recycle(); 2334 } 2335 2336 public void testReadException() { 2337 } 2338 2339 public void testReadException2() { 2340 Parcel p = Parcel.obtain(); 2341 String msg = "testReadException2"; 2342 2343 p.writeException(new SecurityException(msg)); 2344 p.setDataPosition(0); 2345 try { 2346 p.readException(); 2347 fail("Should throw a SecurityException"); 2348 } catch (SecurityException e) { 2349 assertEquals(msg, e.getMessage()); 2350 } 2351 2352 p.setDataPosition(0); 2353 p.writeException(new BadParcelableException(msg)); 2354 p.setDataPosition(0); 2355 try { 2356 p.readException(); 2357 fail("Should throw a BadParcelableException"); 2358 } catch (BadParcelableException e) { 2359 assertEquals(msg, e.getMessage()); 2360 } 2361 2362 p.setDataPosition(0); 2363 p.writeException(new IllegalArgumentException(msg)); 2364 p.setDataPosition(0); 2365 try { 2366 p.readException(); 2367 fail("Should throw an IllegalArgumentException"); 2368 } catch (IllegalArgumentException e) { 2369 assertEquals(msg, e.getMessage()); 2370 } 2371 2372 p.setDataPosition(0); 2373 p.writeException(new NullPointerException(msg)); 2374 p.setDataPosition(0); 2375 try { 2376 p.readException(); 2377 fail("Should throw a NullPointerException"); 2378 } catch (NullPointerException e) { 2379 assertEquals(msg, e.getMessage()); 2380 } 2381 2382 p.setDataPosition(0); 2383 p.writeException(new IllegalStateException(msg)); 2384 p.setDataPosition(0); 2385 try { 2386 p.readException(); 2387 fail("Should throw an IllegalStateException"); 2388 } catch (IllegalStateException e) { 2389 assertEquals(msg, e.getMessage()); 2390 } 2391 2392 p.setDataPosition(0); 2393 try { 2394 p.writeException(new RuntimeException()); 2395 fail("Should throw an IllegalStateException"); 2396 } catch (RuntimeException e) { 2397 //expected 2398 } 2399 p.recycle(); 2400 } 2401 2402 public void testWriteNoException() { 2403 Parcel p = Parcel.obtain(); 2404 p.writeNoException(); 2405 p.setDataPosition(0); 2406 p.readException(); 2407 p.recycle(); 2408 } 2409 2410 public void testWriteFileDescriptor() { 2411 Parcel p; 2412 FileDescriptor fIn = FileDescriptor.in; 2413 ParcelFileDescriptor pfd; 2414 2415 p = Parcel.obtain(); 2416 pfd = p.readFileDescriptor(); 2417 assertNull(pfd); 2418 p.recycle(); 2419 2420 p = Parcel.obtain(); 2421 p.writeFileDescriptor(fIn); 2422 p.setDataPosition(0); 2423 pfd = p.readFileDescriptor(); 2424 assertNotNull(pfd); 2425 assertNotNull(pfd.getFileDescriptor()); 2426 p.recycle(); 2427 } 2428 2429 public void testHasFileDescriptor() { 2430 Parcel p; 2431 FileDescriptor fIn = FileDescriptor.in; 2432 2433 p = Parcel.obtain(); 2434 p.writeFileDescriptor(fIn); 2435 p.setDataPosition(0); 2436 assertTrue(p.hasFileDescriptors()); 2437 p.recycle(); 2438 2439 p = Parcel.obtain(); 2440 p.writeInt(111); 2441 p.setDataPosition(0); 2442 assertFalse(p.hasFileDescriptors()); 2443 p.recycle(); 2444 } 2445 2446 public void testReadBundle() { 2447 Bundle bundle = new Bundle(); 2448 bundle.putBoolean("boolean", true); 2449 bundle.putInt("int", Integer.MAX_VALUE); 2450 bundle.putString("string", "String"); 2451 2452 Bundle bundle2; 2453 Parcel p; 2454 2455 // test null 2456 p = Parcel.obtain(); 2457 p.writeBundle(null); 2458 p.setDataPosition(0); 2459 bundle2 = p.readBundle(); 2460 assertNull(bundle2); 2461 p.recycle(); 2462 2463 // test not null 2464 bundle2 = null; 2465 p = Parcel.obtain(); 2466 p.writeBundle(bundle); 2467 p.setDataPosition(0); 2468 bundle2 = p.readBundle(); 2469 assertNotNull(bundle2); 2470 assertEquals(true, bundle2.getBoolean("boolean")); 2471 assertEquals(Integer.MAX_VALUE, bundle2.getInt("int")); 2472 assertEquals("String", bundle2.getString("string")); 2473 p.recycle(); 2474 2475 bundle2 = null; 2476 Parcel a = Parcel.obtain(); 2477 bundle2 = new Bundle(); 2478 bundle2.putString("foo", "test"); 2479 a.writeBundle(bundle2); 2480 a.setDataPosition(0); 2481 bundle.readFromParcel(a); 2482 p = Parcel.obtain(); 2483 p.setDataPosition(0); 2484 p.writeBundle(bundle); 2485 p.setDataPosition(0); 2486 bundle2 = p.readBundle(); 2487 assertNotNull(bundle2); 2488 assertFalse(true == bundle2.getBoolean("boolean")); 2489 assertFalse(Integer.MAX_VALUE == bundle2.getInt("int")); 2490 assertFalse("String".equals( bundle2.getString("string"))); 2491 a.recycle(); 2492 p.recycle(); 2493 } 2494 2495 public void testReadBundle2() { 2496 Bundle b = new Bundle(); 2497 b.putBoolean("boolean", true); 2498 b.putInt("int", Integer.MAX_VALUE); 2499 b.putString("string", "String"); 2500 2501 Bundle u; 2502 Parcel p; 2503 MockClassLoader m = new MockClassLoader(); 2504 2505 p = Parcel.obtain(); 2506 p.writeBundle(null); 2507 p.setDataPosition(0); 2508 u = p.readBundle(m); 2509 assertNull(u); 2510 p.recycle(); 2511 2512 u = null; 2513 p = Parcel.obtain(); 2514 p.writeBundle(b); 2515 p.setDataPosition(0); 2516 u = p.readBundle(m); 2517 assertNotNull(u); 2518 assertEquals(true, b.getBoolean("boolean")); 2519 assertEquals(Integer.MAX_VALUE, b.getInt("int")); 2520 assertEquals("String", b.getString("string")); 2521 p.recycle(); 2522 } 2523 2524 public void testWriteArray() { 2525 Parcel p; 2526 MockClassLoader mcl = new MockClassLoader(); 2527 2528 p = Parcel.obtain(); 2529 p.writeArray(null); 2530 p.setDataPosition(0); 2531 assertNull(p.readArray(mcl)); 2532 p.recycle(); 2533 2534 Object[] objects = new Object[5]; 2535 objects[0] = Integer.MAX_VALUE; 2536 objects[1] = true; 2537 objects[2] = Long.MAX_VALUE; 2538 objects[3] = "String"; 2539 objects[4] = Float.MAX_VALUE; 2540 Object[] objects2; 2541 2542 p = Parcel.obtain(); 2543 p.writeArray(objects); 2544 p.setDataPosition(0); 2545 objects2 = p.readArray(mcl); 2546 assertNotNull(objects2); 2547 for (int i = 0; i < objects2.length; i++) { 2548 assertEquals(objects[i], objects2[i]); 2549 } 2550 p.recycle(); 2551 } 2552 2553 public void testReadArrayList() { 2554 Parcel p; 2555 MockClassLoader mcl = new MockClassLoader(); 2556 2557 p = Parcel.obtain(); 2558 p.writeArray(null); 2559 p.setDataPosition(0); 2560 assertNull(p.readArrayList(mcl)); 2561 p.recycle(); 2562 2563 Object[] objects = new Object[5]; 2564 objects[0] = Integer.MAX_VALUE; 2565 objects[1] = true; 2566 objects[2] = Long.MAX_VALUE; 2567 objects[3] = "String"; 2568 objects[4] = Float.MAX_VALUE; 2569 ArrayList<?> objects2; 2570 2571 p = Parcel.obtain(); 2572 p.writeArray(objects); 2573 p.setDataPosition(0); 2574 objects2 = p.readArrayList(mcl); 2575 assertNotNull(objects2); 2576 for (int i = 0; i < objects2.size(); i++) { 2577 assertEquals(objects[i], objects2.get(i)); 2578 } 2579 p.recycle(); 2580 } 2581 2582 @SuppressWarnings("unchecked") 2583 public void testWriteSparseArray() { 2584 Parcel p; 2585 MockClassLoader mcl = new MockClassLoader(); 2586 2587 p = Parcel.obtain(); 2588 p.writeSparseArray(null); 2589 p.setDataPosition(0); 2590 assertNull(p.readSparseArray(mcl)); 2591 p.recycle(); 2592 2593 SparseArray<Object> sparseArray = new SparseArray<Object>(); 2594 sparseArray.put(3, "String"); 2595 sparseArray.put(2, Long.MAX_VALUE); 2596 sparseArray.put(4, Float.MAX_VALUE); 2597 sparseArray.put(0, Integer.MAX_VALUE); 2598 sparseArray.put(1, true); 2599 sparseArray.put(10, true); 2600 SparseArray<Object> sparseArray2; 2601 2602 p = Parcel.obtain(); 2603 p.writeSparseArray(sparseArray); 2604 p.setDataPosition(0); 2605 sparseArray2 = p.readSparseArray(mcl); 2606 assertNotNull(sparseArray2); 2607 assertEquals(sparseArray.size(), sparseArray2.size()); 2608 assertEquals(sparseArray.get(0), sparseArray2.get(0)); 2609 assertEquals(sparseArray.get(1), sparseArray2.get(1)); 2610 assertEquals(sparseArray.get(2), sparseArray2.get(2)); 2611 assertEquals(sparseArray.get(3), sparseArray2.get(3)); 2612 assertEquals(sparseArray.get(4), sparseArray2.get(4)); 2613 assertEquals(sparseArray.get(10), sparseArray2.get(10)); 2614 p.recycle(); 2615 } 2616 2617 public void testWriteSparseBooleanArray() { 2618 Parcel p; 2619 2620 p = Parcel.obtain(); 2621 p.writeSparseArray(null); 2622 p.setDataPosition(0); 2623 assertNull(p.readSparseBooleanArray()); 2624 p.recycle(); 2625 2626 SparseBooleanArray sparseBooleanArray = new SparseBooleanArray(); 2627 sparseBooleanArray.put(3, true); 2628 sparseBooleanArray.put(2, false); 2629 sparseBooleanArray.put(4, false); 2630 sparseBooleanArray.put(0, true); 2631 sparseBooleanArray.put(1, true); 2632 sparseBooleanArray.put(10, true); 2633 SparseBooleanArray sparseBoolanArray2; 2634 2635 p = Parcel.obtain(); 2636 p.writeSparseBooleanArray(sparseBooleanArray); 2637 p.setDataPosition(0); 2638 sparseBoolanArray2 = p.readSparseBooleanArray(); 2639 assertNotNull(sparseBoolanArray2); 2640 assertEquals(sparseBooleanArray.size(), sparseBoolanArray2.size()); 2641 assertEquals(sparseBooleanArray.get(0), sparseBoolanArray2.get(0)); 2642 assertEquals(sparseBooleanArray.get(1), sparseBoolanArray2.get(1)); 2643 assertEquals(sparseBooleanArray.get(2), sparseBoolanArray2.get(2)); 2644 assertEquals(sparseBooleanArray.get(3), sparseBoolanArray2.get(3)); 2645 assertEquals(sparseBooleanArray.get(4), sparseBoolanArray2.get(4)); 2646 assertEquals(sparseBooleanArray.get(10), sparseBoolanArray2.get(10)); 2647 p.recycle(); 2648 } 2649 2650 public void testWriteStrongBinder() { 2651 Parcel p; 2652 Binder binder; 2653 Binder binder2 = new Binder(); 2654 2655 p = Parcel.obtain(); 2656 p.writeStrongBinder(null); 2657 p.setDataPosition(0); 2658 assertNull(p.readStrongBinder()); 2659 p.recycle(); 2660 2661 p = Parcel.obtain(); 2662 p.writeStrongBinder(binder2); 2663 p.setDataPosition(0); 2664 binder = (Binder) p.readStrongBinder(); 2665 assertEquals(binder2, binder); 2666 p.recycle(); 2667 } 2668 2669 public void testWriteStrongInterface() { 2670 Parcel p; 2671 MockIInterface mockInterface = new MockIInterface(); 2672 MockIInterface mockIInterface2 = new MockIInterface(); 2673 2674 p = Parcel.obtain(); 2675 p.writeStrongInterface(null); 2676 p.setDataPosition(0); 2677 assertNull(p.readStrongBinder()); 2678 p.recycle(); 2679 2680 p = Parcel.obtain(); 2681 p.writeStrongInterface(mockInterface); 2682 p.setDataPosition(0); 2683 mockIInterface2.binder = (Binder) p.readStrongBinder(); 2684 assertEquals(mockInterface.binder, mockIInterface2.binder); 2685 p.recycle(); 2686 } 2687 2688 public void testWriteBinderArray() { 2689 Parcel p; 2690 IBinder[] ibinder2 = {new Binder(), new Binder()}; 2691 IBinder[] ibinder3 = new IBinder[2]; 2692 IBinder[] ibinder4 = new IBinder[3]; 2693 2694 p = Parcel.obtain(); 2695 p.writeBinderArray(null); 2696 p.setDataPosition(0); 2697 try { 2698 p.readBinderArray(null); 2699 fail("Should throw a RuntimeException"); 2700 } catch (RuntimeException e) { 2701 //expected 2702 } 2703 2704 p.setDataPosition(0); 2705 try { 2706 p.readBinderArray(ibinder3); 2707 fail("Should throw a RuntimeException"); 2708 } catch (RuntimeException e) { 2709 //expected 2710 } 2711 2712 p.setDataPosition(0); 2713 try { 2714 p.readBinderArray(ibinder2); 2715 fail("Should throw a RuntimeException"); 2716 } catch (RuntimeException e) { 2717 //expected 2718 } 2719 p.recycle(); 2720 2721 p = Parcel.obtain(); 2722 p.writeBinderArray(ibinder2); 2723 p.setDataPosition(0); 2724 try { 2725 p.readBinderArray(null); 2726 fail("Should throw a RuntimeException"); 2727 } catch (RuntimeException e) { 2728 //expected 2729 } 2730 2731 p.setDataPosition(0); 2732 try { 2733 p.readBinderArray(ibinder4); 2734 fail("Should throw a RuntimeException"); 2735 } catch (RuntimeException e) { 2736 //expected 2737 } 2738 2739 p.setDataPosition(0); 2740 p.readBinderArray(ibinder3); 2741 assertNotNull(ibinder3); 2742 for (int i = 0; i < ibinder3.length; i++) { 2743 assertNotNull(ibinder3[i]); 2744 assertEquals(ibinder2[i], ibinder3[i]); 2745 } 2746 p.recycle(); 2747 } 2748 2749 public void testCreateBinderArray() { 2750 Parcel p; 2751 IBinder[] ibinder = {}; 2752 IBinder[] ibinder2 = {new Binder(), new Binder()}; 2753 IBinder[] ibinder3; 2754 IBinder[] ibinder4; 2755 2756 p = Parcel.obtain(); 2757 p.writeBinderArray(null); 2758 p.setDataPosition(0); 2759 ibinder3 = p.createBinderArray(); 2760 assertNull(ibinder3); 2761 p.recycle(); 2762 2763 p = Parcel.obtain(); 2764 p.writeBinderArray(ibinder); 2765 p.setDataPosition(0); 2766 ibinder4 = p.createBinderArray(); 2767 assertNotNull(ibinder4); 2768 assertEquals(0, ibinder4.length); 2769 p.recycle(); 2770 2771 p = Parcel.obtain(); 2772 p.writeBinderArray(ibinder2); 2773 p.setDataPosition(0); 2774 ibinder3 = p.createBinderArray(); 2775 assertNotNull(ibinder3); 2776 for (int i = 0; i < ibinder3.length; i++) { 2777 assertNotNull(ibinder3[i]); 2778 assertEquals(ibinder2[i], ibinder3[i]); 2779 } 2780 p.recycle(); 2781 } 2782 2783 public void testWriteBinderList() { 2784 Parcel p; 2785 ArrayList<IBinder> arrayList = new ArrayList<IBinder>(); 2786 ArrayList<IBinder> arrayList2 = new ArrayList<IBinder>(); 2787 arrayList2.add(new Binder()); 2788 arrayList2.add(new Binder()); 2789 ArrayList<IBinder> arrayList3 = new ArrayList<IBinder>(); 2790 arrayList3.add(new Binder()); 2791 arrayList3.add(new Binder()); 2792 arrayList3.add(new Binder()); 2793 2794 p = Parcel.obtain(); 2795 p.writeBinderList(null); 2796 p.setDataPosition(0); 2797 try { 2798 p.readBinderList(null); 2799 fail("Should throw a RuntimeException"); 2800 } catch (RuntimeException e) { 2801 //expected 2802 } 2803 p.setDataPosition(0); 2804 assertEquals(0, arrayList.size()); 2805 p.readBinderList(arrayList); 2806 assertEquals(0, arrayList.size()); 2807 p.recycle(); 2808 2809 p = Parcel.obtain(); 2810 p.writeBinderList(arrayList2); 2811 p.setDataPosition(0); 2812 assertEquals(0, arrayList.size()); 2813 p.readBinderList(arrayList); 2814 assertEquals(2, arrayList.size()); 2815 assertEquals(arrayList2, arrayList); 2816 p.recycle(); 2817 2818 p = Parcel.obtain(); 2819 p.writeBinderList(arrayList2); 2820 p.setDataPosition(0); 2821 assertEquals(3, arrayList3.size()); 2822 p.readBinderList(arrayList3); 2823 assertEquals(2, arrayList3.size()); 2824 assertEquals(arrayList2, arrayList3); 2825 p.recycle(); 2826 } 2827 2828 public void testCreateBinderArrayList() { 2829 Parcel p; 2830 ArrayList<IBinder> arrayList = new ArrayList<IBinder>(); 2831 ArrayList<IBinder> arrayList2 = new ArrayList<IBinder>(); 2832 arrayList2.add(new Binder()); 2833 arrayList2.add(new Binder()); 2834 ArrayList<IBinder> arrayList3; 2835 ArrayList<IBinder> arrayList4; 2836 2837 p = Parcel.obtain(); 2838 p.writeBinderList(null); 2839 p.setDataPosition(0); 2840 arrayList3 = p.createBinderArrayList(); 2841 assertNull(arrayList3); 2842 p.recycle(); 2843 2844 p = Parcel.obtain(); 2845 p.writeBinderList(arrayList); 2846 p.setDataPosition(0); 2847 arrayList3 = p.createBinderArrayList(); 2848 assertNotNull(arrayList3); 2849 assertEquals(0, arrayList3.size()); 2850 p.recycle(); 2851 2852 p = Parcel.obtain(); 2853 p.writeBinderList(arrayList2); 2854 p.setDataPosition(0); 2855 arrayList4 = p.createBinderArrayList(); 2856 assertNotNull(arrayList4); 2857 assertEquals(arrayList2, arrayList4); 2858 p.recycle(); 2859 } 2860 2861 @SuppressWarnings("unchecked") 2862 public void testWriteMap() { 2863 Parcel p; 2864 MockClassLoader mcl = new MockClassLoader(); 2865 HashMap map = new HashMap(); 2866 HashMap map2 = new HashMap(); 2867 2868 p = Parcel.obtain(); 2869 p.writeMap(null); 2870 p.setDataPosition(0); 2871 assertEquals(0, map2.size()); 2872 p.readMap(map2, mcl); 2873 assertEquals(0, map2.size()); 2874 p.recycle(); 2875 2876 map.put("string", "String"); 2877 map.put("int", Integer.MAX_VALUE); 2878 map.put("boolean", true); 2879 p = Parcel.obtain(); 2880 p.writeMap(map); 2881 p.setDataPosition(0); 2882 assertEquals(0, map2.size()); 2883 p.readMap(map2, mcl); 2884 assertEquals(3, map2.size()); 2885 assertEquals("String", map.get("string")); 2886 assertEquals(Integer.MAX_VALUE, map.get("int")); 2887 assertEquals(true, map.get("boolean")); 2888 p.recycle(); 2889 } 2890 2891 @SuppressWarnings("unchecked") 2892 public void testReadHashMap() { 2893 Parcel p; 2894 MockClassLoader mcl = new MockClassLoader(); 2895 HashMap map = new HashMap(); 2896 HashMap map2; 2897 2898 p = Parcel.obtain(); 2899 p.writeMap(null); 2900 p.setDataPosition(0); 2901 map2 = p.readHashMap(mcl); 2902 assertNull(map2); 2903 p.recycle(); 2904 2905 map.put("string", "String"); 2906 map.put("int", Integer.MAX_VALUE); 2907 map.put("boolean", true); 2908 map2 = null; 2909 p = Parcel.obtain(); 2910 p.writeMap(map); 2911 p.setDataPosition(0); 2912 map2 = p.readHashMap(mcl); 2913 assertNotNull(map2); 2914 assertEquals(3, map2.size()); 2915 assertEquals("String", map.get("string")); 2916 assertEquals(Integer.MAX_VALUE, map.get("int")); 2917 assertEquals(true, map.get("boolean")); 2918 p.recycle(); 2919 } 2920 2921 @SuppressWarnings("unchecked") 2922 public void testReadList() { 2923 Parcel p; 2924 MockClassLoader mcl = new MockClassLoader(); 2925 ArrayList arrayList = new ArrayList(); 2926 2927 p = Parcel.obtain(); 2928 p.writeList(null); 2929 p.setDataPosition(0); 2930 assertEquals(0, arrayList.size()); 2931 p.readList(arrayList, mcl); 2932 assertEquals(0, arrayList.size()); 2933 p.recycle(); 2934 2935 ArrayList arrayList2 = new ArrayList(); 2936 arrayList2.add(Integer.MAX_VALUE); 2937 arrayList2.add(true); 2938 arrayList2.add(Long.MAX_VALUE); 2939 arrayList2.add("String"); 2940 arrayList2.add(Float.MAX_VALUE); 2941 2942 p = Parcel.obtain(); 2943 p.writeList(arrayList2); 2944 p.setDataPosition(0); 2945 assertEquals(0, arrayList.size()); 2946 p.readList(arrayList, mcl); 2947 assertEquals(5, arrayList.size()); 2948 for (int i = 0; i < arrayList.size(); i++) { 2949 assertEquals(arrayList.get(i), arrayList2.get(i)); 2950 } 2951 p.recycle(); 2952 } 2953 2954 public void testBinderDataProtection() { 2955 Parcel p; 2956 IBinder b = new Binder(); 2957 2958 p = Parcel.obtain(); 2959 final int firstIntPos = p.dataPosition(); 2960 p.writeInt(1); 2961 p.writeStrongBinder(b); 2962 final int secondIntPos = p.dataPosition(); 2963 p.writeInt(2); 2964 p.writeStrongBinder(b); 2965 final int thirdIntPos = p.dataPosition(); 2966 p.writeInt(3); 2967 2968 for (int pos = 0; pos <= thirdIntPos; pos++) { 2969 p.setDataPosition(pos); 2970 int value = p.readInt(); 2971 if (pos == firstIntPos) { 2972 assertEquals(1, value); 2973 } else if (pos == secondIntPos) { 2974 assertEquals(2, value); 2975 } else if (pos == thirdIntPos) { 2976 assertEquals(3, value); 2977 } else { 2978 // All other read attempts cross into protected data and will return 0 2979 assertEquals(0, value); 2980 } 2981 } 2982 2983 p.recycle(); 2984 } 2985 2986 public void testBinderDataProtectionIncrements() { 2987 Parcel p; 2988 IBinder b = new Binder(); 2989 2990 p = Parcel.obtain(); 2991 final int firstIntPos = p.dataPosition(); 2992 p.writeInt(1); 2993 p.writeStrongBinder(b); 2994 final int secondIntPos = p.dataPosition(); 2995 p.writeInt(2); 2996 p.writeStrongBinder(b); 2997 final int thirdIntPos = p.dataPosition(); 2998 p.writeInt(3); 2999 final int end = p.dataPosition(); 3000 3001 p.setDataPosition(0); 3002 int pos; 3003 do { 3004 pos = p.dataPosition(); 3005 int value = p.readInt(); 3006 if (pos == firstIntPos) { 3007 assertEquals(1, value); 3008 } else if (pos == secondIntPos) { 3009 assertEquals(2, value); 3010 } else if (pos == thirdIntPos) { 3011 assertEquals(3, value); 3012 } else { 3013 // All other read attempts cross into protected data and will return 0 3014 assertEquals(0, value); 3015 } 3016 } while(pos < end); 3017 3018 p.recycle(); 3019 } 3020 3021 private class MockClassLoader extends ClassLoader { 3022 public MockClassLoader() { 3023 super(); 3024 } 3025 } 3026 3027 private class MockIInterface implements IInterface { 3028 public Binder binder; 3029 3030 public MockIInterface() { 3031 binder = new Binder(); 3032 } 3033 3034 public IBinder asBinder() { 3035 return binder; 3036 } 3037 } 3038 3039 private static boolean parcelableWithBadCreatorInitializerHasRun; 3040 private static boolean invalidCreatorIntializerHasRun; 3041 3042 /** 3043 * A class that would be Parcelable except that it doesn't have a CREATOR field declared to be 3044 * of the correct type. 3045 */ 3046 @SuppressWarnings("unused") // Referenced via reflection only 3047 private static class ParcelableWithBadCreator implements Parcelable { 3048 3049 static { 3050 ParcelTest.parcelableWithBadCreatorInitializerHasRun = true; 3051 } 3052 3053 private static class InvalidCreator 3054 implements Parcelable.Creator<ParcelableWithBadCreator> { 3055 3056 static { 3057 invalidCreatorIntializerHasRun = true; 3058 } 3059 3060 @Override 3061 public ParcelableWithBadCreator createFromParcel(Parcel source) { 3062 return null; 3063 } 3064 3065 @Override 3066 public ParcelableWithBadCreator[] newArray(int size) { 3067 return new ParcelableWithBadCreator[0]; 3068 } 3069 3070 } 3071 3072 // Invalid declaration: Must be declared as Parcelable.Creator or a subclass. 3073 public static Object CREATOR = new InvalidCreator(); 3074 3075 @Override 3076 public int describeContents() { 3077 return 0; 3078 } 3079 3080 @Override 3081 public void writeToParcel(Parcel dest, int flags) { 3082 3083 } 3084 } 3085 3086 // http://b/1171613 3087 public void testBadStream_invalidCreator() { 3088 Parcel parcel = Parcel.obtain(); 3089 // Create an invalid stream by manipulating the Parcel. 3090 parcel.writeString(getClass().getName() + "$ParcelableWithBadCreator"); 3091 byte[] badData = parcel.marshall(); 3092 parcel.recycle(); 3093 3094 // Now try to read the bad data. 3095 parcel = Parcel.obtain(); 3096 parcel.unmarshall(badData, 0, badData.length); 3097 parcel.setDataPosition(0); 3098 try { 3099 parcel.readParcelable(getClass().getClassLoader()); 3100 fail(); 3101 } catch (BadParcelableException expected) { 3102 } finally { 3103 parcel.recycle(); 3104 } 3105 3106 assertFalse(invalidCreatorIntializerHasRun); 3107 assertFalse(parcelableWithBadCreatorInitializerHasRun); 3108 } 3109 3110 private static boolean doesNotImplementParcelableInitializerHasRun; 3111 3112 /** A class that would be Parcelable except that it does not implement Parcelable. */ 3113 @SuppressWarnings("unused") // Referenced via reflection only 3114 private static class DoesNotImplementParcelable { 3115 3116 static { 3117 doesNotImplementParcelableInitializerHasRun = true; 3118 } 3119 3120 public static Parcelable.Creator<Object> CREATOR = new Parcelable.Creator<Object>() { 3121 @Override 3122 public Object createFromParcel(Parcel source) { 3123 return new DoesNotImplementParcelable(); 3124 } 3125 3126 @Override 3127 public Object[] newArray(int size) { 3128 return new Object[size]; 3129 } 3130 }; 3131 } 3132 3133 // http://b/1171613 3134 public void testBadStream_objectDoesNotImplementParcelable() { 3135 Parcel parcel = Parcel.obtain(); 3136 // Create an invalid stream by manipulating the Parcel. 3137 parcel.writeString(getClass().getName() + "$DoesNotImplementParcelable"); 3138 byte[] badData = parcel.marshall(); 3139 parcel.recycle(); 3140 3141 // Now try to read the bad data. 3142 parcel = Parcel.obtain(); 3143 parcel.unmarshall(badData, 0, badData.length); 3144 parcel.setDataPosition(0); 3145 try { 3146 parcel.readParcelable(getClass().getClassLoader()); 3147 fail(); 3148 } catch (BadParcelableException expected) { 3149 } finally { 3150 parcel.recycle(); 3151 } 3152 3153 assertFalse(doesNotImplementParcelableInitializerHasRun); 3154 } 3155 3156 public static class SimpleParcelable implements Parcelable { 3157 private final int value; 3158 3159 public SimpleParcelable(int value) { 3160 this.value = value; 3161 } 3162 3163 private SimpleParcelable(Parcel in) { 3164 this.value = in.readInt(); 3165 } 3166 3167 public int getValue() { 3168 return value; 3169 } 3170 3171 @Override 3172 public int describeContents() { 3173 return 0; 3174 } 3175 3176 @Override 3177 public void writeToParcel(Parcel out, int flags) { 3178 out.writeInt(value); 3179 } 3180 3181 public static Parcelable.Creator<SimpleParcelable> CREATOR = 3182 new Parcelable.Creator<SimpleParcelable>() { 3183 3184 @Override 3185 public SimpleParcelable createFromParcel(Parcel source) { 3186 return new SimpleParcelable(source); 3187 } 3188 3189 @Override 3190 public SimpleParcelable[] newArray(int size) { 3191 return new SimpleParcelable[size]; 3192 } 3193 }; 3194 } 3195 3196 public void testReadWriteParcellableList() { 3197 Parcel parcel = Parcel.obtain(); 3198 3199 ArrayList<SimpleParcelable> list = new ArrayList<>(); 3200 list.add(new SimpleParcelable(57)); 3201 3202 // Writing a |null| list to a parcel should work, and reading it back 3203 // from a parcel should clear the target list. 3204 parcel.writeParcelableList(null, 0); 3205 parcel.setDataPosition(0); 3206 parcel.readParcelableList(list, SimpleParcelable.class.getClassLoader()); 3207 assertEquals(0, list.size()); 3208 3209 list.clear(); 3210 list.add(new SimpleParcelable(42)); 3211 list.add(new SimpleParcelable(56)); 3212 3213 parcel.setDataPosition(0); 3214 parcel.writeParcelableList(list, 0); 3215 3216 // Populate the list with a value, we will later assert that the 3217 // value has been removed. 3218 list.clear(); 3219 list.add(new SimpleParcelable(100)); 3220 3221 parcel.setDataPosition(0); 3222 parcel.readParcelableList(list, SimpleParcelable.class.getClassLoader()); 3223 3224 assertEquals(2, list.size()); 3225 assertEquals(42, list.get(0).getValue()); 3226 assertEquals(56, list.get(1).getValue()); 3227 } 3228 } 3229