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