1 package com.xtremelabs.robolectric.shadows; 2 3 import static org.hamcrest.CoreMatchers.equalTo; 4 import static org.hamcrest.CoreMatchers.notNullValue; 5 import static org.hamcrest.CoreMatchers.nullValue; 6 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertThat; 8 import static org.junit.Assert.assertTrue; 9 10 import android.accounts.Account; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.os.Parcel; 14 15 import com.xtremelabs.robolectric.Robolectric; 16 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 17 18 import org.junit.Before; 19 import org.junit.Test; 20 import org.junit.runner.RunWith; 21 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 import java.util.List; 25 26 @RunWith(WithTestDefaultsRunner.class) 27 public class ParcelTest { 28 29 private Parcel parcel; 30 private ShadowParcel shadowParcel; 31 32 @Before setup()33 public void setup() { 34 parcel = Parcel.obtain(); 35 shadowParcel = Robolectric.shadowOf(parcel); 36 } 37 38 @Test testObtain()39 public void testObtain() { 40 assertThat(parcel, notNullValue()); 41 assertThat(shadowParcel.getIndex(), equalTo(0)); 42 assertThat(shadowParcel.getParcelData().size(), equalTo(0)); 43 } 44 45 @Test testReadIntWhenEmpty()46 public void testReadIntWhenEmpty() { 47 assertThat(parcel.readInt(), equalTo(0)); 48 } 49 50 @Test testReadLongWhenEmpty()51 public void testReadLongWhenEmpty() { 52 assertThat(parcel.readLong(), equalTo(0l)); 53 } 54 55 @Test testReadStringWhenEmpty()56 public void testReadStringWhenEmpty() { 57 assertThat(parcel.readString(), nullValue()); 58 } 59 60 @Test testReadWriteSingleString()61 public void testReadWriteSingleString() { 62 String val = "test"; 63 parcel.writeString(val); 64 parcel.setDataPosition(0); 65 assertThat(parcel.readString(), equalTo(val)); 66 } 67 68 @Test testWriteNullString()69 public void testWriteNullString() { 70 parcel.writeString( null ); 71 parcel.setDataPosition(0); 72 assertThat( parcel.readString(), nullValue() ); 73 assertThat( shadowParcel.getIndex(), equalTo( 1 ) ); 74 assertThat( shadowParcel.getParcelData().size(), equalTo( 1 ) ); 75 } 76 77 @Test testReadWriteMultipleStrings()78 public void testReadWriteMultipleStrings() { 79 for (int i = 0; i < 10; ++i) { 80 parcel.writeString(Integer.toString(i)); 81 } 82 parcel.setDataPosition(0); 83 for (int i = 0; i < 10; ++i) { 84 assertThat(parcel.readString(), equalTo(Integer.toString(i))); 85 } 86 // now try to read past the number of items written and see what happens 87 assertThat(parcel.readString(), nullValue()); 88 } 89 90 @Test testReadWriteSingleInt()91 public void testReadWriteSingleInt() { 92 int val = 5; 93 parcel.writeInt(val); 94 parcel.setDataPosition(0); 95 assertThat(parcel.readInt(), equalTo(val)); 96 } 97 98 @Test testReadWriteIntArray()99 public void testReadWriteIntArray() throws Exception { 100 final int[] ints = {1, 2}; 101 parcel.writeIntArray(ints); 102 parcel.setDataPosition(0); 103 final int[] ints2 = new int[ints.length]; 104 parcel.readIntArray(ints2); 105 assertTrue(Arrays.equals(ints, ints2)); 106 } 107 108 @Test testReadWriteLongArray()109 public void testReadWriteLongArray() throws Exception { 110 final long[] longs = {1, 2}; 111 parcel.writeLongArray(longs); 112 parcel.setDataPosition(0); 113 final long[] longs2 = new long[longs.length]; 114 parcel.readLongArray(longs2); 115 assertTrue(Arrays.equals(longs, longs2)); 116 } 117 118 @Test testReadWriteSingleFloat()119 public void testReadWriteSingleFloat() { 120 float val = 5.2f; 121 parcel.writeFloat(val); 122 parcel.setDataPosition(0); 123 assertThat(parcel.readFloat(), equalTo(val)); 124 } 125 126 @Test testReadWriteFloatArray()127 public void testReadWriteFloatArray() throws Exception { 128 final float[] floats = {1.1f, 2.0f}; 129 parcel.writeFloatArray(floats); 130 parcel.setDataPosition(0); 131 final float[] floats2 = new float[floats.length]; 132 parcel.readFloatArray(floats2); 133 assertTrue(Arrays.equals(floats, floats2)); 134 } 135 136 @Test testReadWriteDoubleArray()137 public void testReadWriteDoubleArray() throws Exception { 138 final double[] doubles = {1.1f, 2.0f}; 139 parcel.writeDoubleArray(doubles); 140 parcel.setDataPosition(0); 141 final double[] doubles2 = new double[doubles.length]; 142 parcel.readDoubleArray(doubles2); 143 assertTrue(Arrays.equals(doubles, doubles2)); 144 } 145 146 @Test testReadWriteStringArray()147 public void testReadWriteStringArray() throws Exception { 148 final String[] strings = {"foo", "bar"}; 149 parcel.writeStringArray(strings); 150 parcel.setDataPosition(0); 151 final String[] strings2 = new String[strings.length]; 152 parcel.readStringArray(strings2); 153 assertTrue(Arrays.equals(strings, strings2)); 154 } 155 156 @Test testReadWriteMultipleInts()157 public void testReadWriteMultipleInts() { 158 for (int i = 0; i < 10; ++i) { 159 parcel.writeInt(i); 160 } 161 parcel.setDataPosition(0); 162 for (int i = 0; i < 10; ++i) { 163 assertThat(parcel.readInt(), equalTo(i)); 164 } 165 // now try to read past the number of items written and see what happens 166 assertThat(parcel.readInt(), equalTo(0)); 167 } 168 169 @Test testReadWriteSingleByte()170 public void testReadWriteSingleByte() { 171 byte val = 1; 172 parcel.writeByte(val); 173 parcel.setDataPosition(0); 174 assertThat(parcel.readByte(), equalTo(val)); 175 } 176 177 @Test testReadWriteMultipleBytes()178 public void testReadWriteMultipleBytes() { 179 for (byte i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; ++i) { 180 parcel.writeByte(i); 181 } 182 parcel.setDataPosition(0); 183 for (byte i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; ++i) { 184 assertThat(parcel.readByte(), equalTo(i)); 185 } 186 // now try to read past the number of items written and see what happens 187 assertThat(parcel.readByte(), equalTo((byte) 0)); 188 } 189 190 191 @Test testReadWriteStringInt()192 public void testReadWriteStringInt() { 193 for (int i = 0; i < 10; ++i) { 194 parcel.writeString(Integer.toString(i)); 195 parcel.writeInt(i); 196 } 197 parcel.setDataPosition(0); 198 for (int i = 0; i < 10; ++i) { 199 assertThat(parcel.readString(), equalTo(Integer.toString(i))); 200 assertThat(parcel.readInt(), equalTo(i)); 201 } 202 // now try to read past the number of items written and see what happens 203 assertThat(parcel.readString(), nullValue()); 204 assertThat(parcel.readInt(), equalTo(0)); 205 } 206 207 @Test testReadWriteSingleLong()208 public void testReadWriteSingleLong() { 209 long val = 5; 210 parcel.writeLong(val); 211 parcel.setDataPosition(0); 212 assertThat(parcel.readLong(), equalTo(val)); 213 } 214 215 @Test testReadWriteMultipleLongs()216 public void testReadWriteMultipleLongs() { 217 for (long i = 0; i < 10; ++i) { 218 parcel.writeLong(i); 219 } 220 parcel.setDataPosition(0); 221 for (long i = 0; i < 10; ++i) { 222 assertThat(parcel.readLong(), equalTo(i)); 223 } 224 // now try to read past the number of items written and see what happens 225 assertThat(parcel.readLong(), equalTo(0l)); 226 } 227 228 @Test testReadWriteStringLong()229 public void testReadWriteStringLong() { 230 for (long i = 0; i < 10; ++i) { 231 parcel.writeString(Long.toString(i)); 232 parcel.writeLong(i); 233 } 234 parcel.setDataPosition(0); 235 for (long i = 0; i < 10; ++i) { 236 assertThat(parcel.readString(), equalTo(Long.toString(i))); 237 assertThat(parcel.readLong(), equalTo(i)); 238 } 239 // now try to read past the number of items written and see what happens 240 assertThat(parcel.readString(), nullValue()); 241 assertThat(parcel.readLong(), equalTo(0l)); 242 } 243 244 @Test testReadWriteParcelable()245 public void testReadWriteParcelable() { 246 Intent i1 = new Intent("anAction"); 247 parcel.writeParcelable(i1, 0); 248 249 parcel.setDataPosition(0); 250 251 Intent i2 = parcel.readParcelable(Intent.class.getClassLoader()); 252 assertEquals(i1, i2); 253 } 254 255 @Test testReadWriteSimpleBundle()256 public void testReadWriteSimpleBundle() { 257 Bundle b1 = new Bundle(); 258 b1.putString("hello", "world"); 259 parcel.writeBundle(b1); 260 parcel.setDataPosition(0); 261 Bundle b2 = parcel.readBundle(); 262 263 assertEquals(b1, b2); 264 assertEquals("world", b2.getString("hello")); 265 266 parcel.setDataPosition(0); 267 parcel.writeBundle(b1); 268 parcel.setDataPosition(0); 269 b2 = parcel.readBundle(null /* ClassLoader */); 270 assertEquals(b1, b2); 271 assertEquals("world", b2.getString("hello")); 272 } 273 274 @Test testReadWriteNestedBundles()275 public void testReadWriteNestedBundles() { 276 Account account = new Account("accountName", "accountType"); 277 Bundle innerBundle = new Bundle(); 278 innerBundle.putString("hello", "world"); 279 innerBundle.putParcelable("account", account); 280 Bundle b1 = new Bundle(); 281 b1.putBundle("bundle", innerBundle); 282 b1.putInt("int", 23); 283 parcel.writeBundle(b1); 284 parcel.setDataPosition(0); 285 Bundle b2 = parcel.readBundle(); 286 287 assertEquals(b1, b2); 288 assertEquals(innerBundle, b2.getBundle("bundle")); 289 assertEquals(23, b2.getInt("int")); 290 assertEquals("world", b2.getBundle("bundle").getString("hello")); 291 292 parcel.setDataPosition(0); 293 parcel.writeBundle(b1); 294 parcel.setDataPosition(0); 295 b2 = parcel.readBundle(null /* ClassLoader */); 296 assertEquals(b1, b2); 297 assertEquals(innerBundle, b2.getBundle("bundle")); 298 assertEquals(23, b2.getInt("int")); 299 assertEquals("world", b2.getBundle("bundle").getString("hello")); 300 } 301 302 @Test testReadWriteBundleWithDifferentValueTypes()303 public void testReadWriteBundleWithDifferentValueTypes() { 304 Bundle b1 = new Bundle(); 305 b1.putString("hello", "world"); 306 b1.putBoolean("boolean", true); 307 b1.putByte("byte", (byte) 0xAA); 308 b1.putShort("short", (short)0xBABE); 309 b1.putInt("int", 1); 310 b1.putFloat("float", 0.5f); 311 b1.putDouble("double", 1.25); 312 parcel.writeBundle(b1); 313 parcel.setDataPosition(0); 314 315 Bundle b2 = parcel.readBundle(); 316 317 assertEquals(b1, b2); 318 assertEquals("world", b2.getString("hello")); 319 assertEquals(true, b2.getBoolean("boolean")); 320 assertEquals((byte) 0xAA, b2.getByte("byte")); 321 assertEquals((short) 0xBABE, b2.getShort("short")); 322 assertEquals(1, b2.getInt("int")); 323 assertEquals(0.5f, b2.getFloat("float"), 0.05); 324 assertEquals(1.25, b2.getDouble("double"), 0.05); 325 326 parcel.setDataPosition(0); 327 parcel.writeBundle(b1); 328 parcel.setDataPosition(0); 329 b2 = parcel.readBundle(null /* ClassLoader */); 330 assertEquals(b1, b2); 331 assertEquals("world", b2.getString("hello")); 332 assertEquals(true, b2.getBoolean("boolean")); 333 assertEquals((byte) 0xAA, b2.getByte("byte")); 334 assertEquals((short) 0xBABE, b2.getShort("short")); 335 assertEquals(1, b2.getInt("int")); 336 assertEquals(0.5f, b2.getFloat("float"), 0.05); 337 assertEquals(1.25, b2.getDouble("double"), 0.05); 338 } 339 340 @Test testWriteCreateStringArray()341 public void testWriteCreateStringArray() { 342 final String[] strings = { "foo", "bar" }; 343 parcel.writeStringArray(strings); 344 parcel.setDataPosition(0); 345 final String[] strings2 = parcel.createStringArray(); 346 assertTrue(Arrays.equals(strings, strings2)); 347 } 348 349 @Test testReadWriteStringList()350 public void testReadWriteStringList() { 351 final List<String> strings = Arrays.asList( "foo", "bar" ); 352 parcel.writeStringList(strings); 353 parcel.setDataPosition(0); 354 List<String> extractedStrings = new ArrayList<String>(); 355 parcel.readStringList(extractedStrings); 356 assertEquals(strings, extractedStrings); 357 } 358 359 @Test testWriteCreateStringArrayList()360 public void testWriteCreateStringArrayList() { 361 final List<String> strings = Arrays.asList( "foo", "bar" ); 362 parcel.writeStringList(strings); 363 parcel.setDataPosition(0); 364 List<String> extractedStrings = parcel.createStringArrayList(); 365 assertEquals(strings, extractedStrings); 366 } 367 368 @Test testReadWriteByteArray()369 public void testReadWriteByteArray() throws Exception { 370 final byte[] bytes = {1, 2}; 371 parcel.writeByteArray(bytes); 372 parcel.setDataPosition(0); 373 final byte[] bytes2 = new byte[bytes.length]; 374 parcel.readByteArray(bytes2); 375 assertTrue(Arrays.equals(bytes, bytes2)); 376 } 377 378 @Test testReadWriteBooleanArray()379 public void testReadWriteBooleanArray() { 380 final boolean[] booleans = {false, true, true}; 381 parcel.writeBooleanArray(booleans); 382 parcel.setDataPosition(0); 383 final boolean[] booleans2 = new boolean[booleans.length]; 384 parcel.readBooleanArray(booleans2); 385 assertTrue(Arrays.equals(booleans, booleans2)); 386 } 387 388 @Test testReadWriteCharArray()389 public void testReadWriteCharArray() { 390 final char[] chars = {'a', 'b', 'c'}; 391 parcel.writeCharArray(chars); 392 parcel.setDataPosition(0); 393 final char[] chars2 = new char[chars.length]; 394 parcel.readCharArray(chars2); 395 assertTrue(Arrays.equals(chars, chars2)); 396 } 397 398 @Test testWriteCreateBooleanArray()399 public void testWriteCreateBooleanArray() { 400 final boolean[] booleans = {false, true, true}; 401 parcel.writeBooleanArray(booleans); 402 parcel.setDataPosition(0); 403 final boolean[] booleans2 = parcel.createBooleanArray(); 404 assertTrue(Arrays.equals(booleans, booleans2)); 405 } 406 407 @Test testWriteCreateByteArray()408 public void testWriteCreateByteArray() { 409 final byte[] bytes = {1, 2}; 410 parcel.writeByteArray(bytes); 411 parcel.setDataPosition(0); 412 final byte[] bytes2 = parcel.createByteArray(); 413 assertTrue(Arrays.equals(bytes, bytes2)); 414 } 415 416 @Test testWriteCreateCharArray()417 public void testWriteCreateCharArray() { 418 final char[] chars = {'a', 'b', 'c'}; 419 parcel.writeCharArray(chars); 420 parcel.setDataPosition(0); 421 final char[] chars2 = parcel.createCharArray(); 422 assertTrue(Arrays.equals(chars, chars2)); 423 } 424 425 @Test testWriteCreateIntArray()426 public void testWriteCreateIntArray() { 427 final int[] ints = {1, 2}; 428 parcel.writeIntArray(ints); 429 parcel.setDataPosition(0); 430 final int[] ints2 = parcel.createIntArray(); 431 assertTrue(Arrays.equals(ints, ints2)); 432 } 433 434 @Test testWriteCreateLongArray()435 public void testWriteCreateLongArray() { 436 final long[] longs = {1, 2}; 437 parcel.writeLongArray(longs); 438 parcel.setDataPosition(0); 439 final long[] longs2 = parcel.createLongArray(); 440 assertTrue(Arrays.equals(longs, longs2)); 441 } 442 443 @Test testWriteCreateFloatArray()444 public void testWriteCreateFloatArray() { 445 final float[] floats = {1.5f, 2.25f}; 446 parcel.writeFloatArray(floats); 447 parcel.setDataPosition(0); 448 final float[] floats2 = parcel.createFloatArray(); 449 assertTrue(Arrays.equals(floats, floats2)); 450 } 451 452 @Test testWriteCreateDoubleArray()453 public void testWriteCreateDoubleArray() { 454 final double[] doubles = {1.2, 2.2}; 455 parcel.writeDoubleArray(doubles); 456 parcel.setDataPosition(0); 457 final double[] doubles2 = parcel.createDoubleArray(); 458 assertTrue(Arrays.equals(doubles, doubles2)); 459 } 460 461 @Test testDataPositionAfterStringWrite()462 public void testDataPositionAfterStringWrite() { 463 parcel.writeString("string"); 464 assertEquals(10, parcel.dataPosition()); 465 } 466 467 @Test testDataPositionAfterByteWrite()468 public void testDataPositionAfterByteWrite() { 469 parcel.writeByte((byte) 0); 470 assertEquals(1, parcel.dataPosition()); 471 } 472 473 @Test testDataPositionAfterIntWrite()474 public void testDataPositionAfterIntWrite() { 475 parcel.writeInt(1); 476 assertEquals(4, parcel.dataPosition()); 477 } 478 479 @Test testDataPositionAfterLongWrite()480 public void testDataPositionAfterLongWrite() { 481 parcel.writeLong(23); 482 assertEquals(8, parcel.dataPosition()); 483 } 484 485 @Test testDataPositionAfterFloatWrite()486 public void testDataPositionAfterFloatWrite() { 487 parcel.writeFloat(0.5f); 488 assertEquals(4, parcel.dataPosition()); 489 } 490 491 @Test testDataPositionAfterDoubleWrite()492 public void testDataPositionAfterDoubleWrite() { 493 parcel.writeDouble(8.8); 494 assertEquals(8, parcel.dataPosition()); 495 } 496 497 @Test testResetDataPositionAfterWrite()498 public void testResetDataPositionAfterWrite() { 499 parcel.writeInt(4); 500 parcel.setDataPosition(0); 501 assertEquals(0, parcel.dataPosition()); 502 } 503 504 @Test testOverwritePreviousValue()505 public void testOverwritePreviousValue() { 506 parcel.writeInt(4); 507 parcel.setDataPosition(0); 508 parcel.writeInt(34); 509 parcel.setDataPosition(0); 510 assertEquals(34, parcel.readInt()); 511 assertEquals(4, parcel.dataSize()); 512 } 513 } 514