Home | History | Annotate | Download | only in shadows
      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.content.Intent;
     11 import android.os.Bundle;
     12 import android.os.Parcel;
     13 
     14 import com.xtremelabs.robolectric.Robolectric;
     15 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
     16 
     17 import org.junit.Before;
     18 import org.junit.Test;
     19 import org.junit.runner.RunWith;
     20 
     21 import java.util.ArrayList;
     22 import java.util.Arrays;
     23 import java.util.List;
     24 
     25 @RunWith(WithTestDefaultsRunner.class)
     26 public class ParcelTest {
     27 
     28     private Parcel parcel;
     29     private ShadowParcel shadowParcel;
     30 
     31     @Before
     32     public void setup() {
     33         parcel = Parcel.obtain();
     34         shadowParcel = Robolectric.shadowOf(parcel);
     35     }
     36 
     37     @Test
     38     public void testObtain() {
     39         assertThat(parcel, notNullValue());
     40         assertThat(shadowParcel.getIndex(), equalTo(0));
     41         assertThat(shadowParcel.getParcelData().size(), equalTo(0));
     42     }
     43 
     44     @Test
     45     public void testReadIntWhenEmpty() {
     46         assertThat(parcel.readInt(), equalTo(0));
     47     }
     48 
     49     @Test
     50     public void testReadLongWhenEmpty() {
     51         assertThat(parcel.readLong(), equalTo(0l));
     52     }
     53 
     54     @Test
     55     public void testReadStringWhenEmpty() {
     56         assertThat(parcel.readString(), nullValue());
     57     }
     58 
     59     @Test
     60     public void testReadWriteSingleString() {
     61         String val = "test";
     62         parcel.writeString(val);
     63         assertThat(parcel.readString(), equalTo(val));
     64     }
     65 
     66 	@Test
     67 	public void testWriteNullString() {
     68 		parcel.writeString( null );
     69 		assertThat( parcel.readString(), nullValue() );
     70 		assertThat( shadowParcel.getIndex(), equalTo( 1 ) );
     71 		assertThat( shadowParcel.getParcelData().size(), equalTo( 1 ) );
     72 	}
     73 
     74     @Test
     75     public void testReadWriteMultipleStrings() {
     76         for (int i = 0; i < 10; ++i) {
     77             parcel.writeString(Integer.toString(i));
     78         }
     79         for (int i = 0; i < 10; ++i) {
     80             assertThat(parcel.readString(), equalTo(Integer.toString(i)));
     81         }
     82         // now try to read past the number of items written and see what happens
     83         assertThat(parcel.readString(), nullValue());
     84     }
     85 
     86     @Test
     87     public void testReadWriteSingleInt() {
     88         int val = 5;
     89         parcel.writeInt(val);
     90         assertThat(parcel.readInt(), equalTo(val));
     91     }
     92 
     93     @Test
     94     public void testReadWriteIntArray() throws Exception {
     95         final int[] ints = {1, 2};
     96         parcel.writeIntArray(ints);
     97         final int[] ints2 = new int[ints.length];
     98         parcel.readIntArray(ints2);
     99         assertTrue(Arrays.equals(ints, ints2));
    100     }
    101 
    102     @Test
    103     public void testReadWriteLongArray() throws Exception {
    104         final long[] longs = {1, 2};
    105         parcel.writeLongArray(longs);
    106         final long[] longs2 = new long[longs.length];
    107         parcel.readLongArray(longs2);
    108         assertTrue(Arrays.equals(longs, longs2));
    109     }
    110 
    111     @Test
    112     public void testReadWriteSingleFloat() {
    113         float val = 5.2f;
    114         parcel.writeFloat(val);
    115         assertThat(parcel.readFloat(), equalTo(val));
    116     }
    117 
    118     @Test
    119     public void testReadWriteFloatArray() throws Exception {
    120         final float[] floats = {1.1f, 2.0f};
    121         parcel.writeFloatArray(floats);
    122         final float[] floats2 = new float[floats.length];
    123         parcel.readFloatArray(floats2);
    124         assertTrue(Arrays.equals(floats, floats2));
    125     }
    126 
    127     @Test
    128     public void testReadWriteDoubleArray() throws Exception {
    129         final double[] doubles = {1.1f, 2.0f};
    130         parcel.writeDoubleArray(doubles);
    131         final double[] doubles2 = new double[doubles.length];
    132         parcel.readDoubleArray(doubles2);
    133         assertTrue(Arrays.equals(doubles, doubles2));
    134     }
    135 
    136     @Test
    137     public void testReadWriteStringArray() throws Exception {
    138         final String[] strings = {"foo", "bar"};
    139         parcel.writeStringArray(strings);
    140         final String[] strings2 = new String[strings.length];
    141         parcel.readStringArray(strings2);
    142         assertTrue(Arrays.equals(strings, strings2));
    143     }
    144 
    145     @Test
    146     public void testReadWriteMultipleInts() {
    147         for (int i = 0; i < 10; ++i) {
    148             parcel.writeInt(i);
    149         }
    150         for (int i = 0; i < 10; ++i) {
    151             assertThat(parcel.readInt(), equalTo(i));
    152         }
    153         // now try to read past the number of items written and see what happens
    154         assertThat(parcel.readInt(), equalTo(0));
    155     }
    156 
    157     @Test
    158     public void testReadWriteSingleByte() {
    159         byte val = 1;
    160         parcel.writeByte(val);
    161         assertThat(parcel.readByte(), equalTo(val));
    162     }
    163 
    164     @Test
    165     public void testReadWriteMultipleBytes() {
    166         for (byte i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; ++i) {
    167             parcel.writeByte(i);
    168         }
    169         for (byte i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; ++i) {
    170             assertThat(parcel.readByte(), equalTo(i));
    171         }
    172         // now try to read past the number of items written and see what happens
    173         assertThat(parcel.readByte(), equalTo((byte) 0));
    174     }
    175 
    176 
    177     @Test
    178     public void testReadWriteStringInt() {
    179         for (int i = 0; i < 10; ++i) {
    180             parcel.writeString(Integer.toString(i));
    181             parcel.writeInt(i);
    182         }
    183         for (int i = 0; i < 10; ++i) {
    184             assertThat(parcel.readString(), equalTo(Integer.toString(i)));
    185             assertThat(parcel.readInt(), equalTo(i));
    186         }
    187         // now try to read past the number of items written and see what happens
    188         assertThat(parcel.readString(), nullValue());
    189         assertThat(parcel.readInt(), equalTo(0));
    190     }
    191 
    192     @Test(expected = ClassCastException.class)
    193     public void testWriteStringReadInt() {
    194         String val = "test";
    195         parcel.writeString(val);
    196         parcel.readInt();
    197     }
    198 
    199     @Test(expected = ClassCastException.class)
    200     public void testWriteIntReadString() {
    201         int val = 9;
    202         parcel.writeInt(val);
    203         parcel.readString();
    204     }
    205 
    206     @Test
    207     public void testReadWriteSingleLong() {
    208         long val = 5;
    209         parcel.writeLong(val);
    210         assertThat(parcel.readLong(), equalTo(val));
    211     }
    212 
    213     @Test
    214     public void testReadWriteMultipleLongs() {
    215         for (long i = 0; i < 10; ++i) {
    216             parcel.writeLong(i);
    217         }
    218         for (long i = 0; i < 10; ++i) {
    219             assertThat(parcel.readLong(), equalTo(i));
    220         }
    221         // now try to read past the number of items written and see what happens
    222         assertThat(parcel.readLong(), equalTo(0l));
    223     }
    224 
    225     @Test
    226     public void testReadWriteStringLong() {
    227         for (long i = 0; i < 10; ++i) {
    228             parcel.writeString(Long.toString(i));
    229             parcel.writeLong(i);
    230         }
    231         for (long i = 0; i < 10; ++i) {
    232             assertThat(parcel.readString(), equalTo(Long.toString(i)));
    233             assertThat(parcel.readLong(), equalTo(i));
    234         }
    235         // now try to read past the number of items written and see what happens
    236         assertThat(parcel.readString(), nullValue());
    237         assertThat(parcel.readLong(), equalTo(0l));
    238     }
    239 
    240     @Test(expected = ClassCastException.class)
    241     public void testWriteStringReadLong() {
    242         String val = "test";
    243         parcel.writeString(val);
    244         parcel.readLong();
    245     }
    246 
    247     @Test(expected = ClassCastException.class)
    248     public void testWriteLongReadString() {
    249         long val = 9;
    250         parcel.writeLong(val);
    251         parcel.readString();
    252     }
    253 
    254     @Test
    255     public void testReadWriteParcelable() {
    256         Intent i1 = new Intent("anAction");
    257         parcel.writeParcelable(i1, 0);
    258 
    259         Intent i2 = parcel.readParcelable(Intent.class.getClassLoader());
    260         assertEquals(i1, i2);
    261     }
    262 
    263     @Test
    264     public void testReadWriteBundle() {
    265         Bundle b1 = new Bundle();
    266         b1.putString("hello", "world");
    267         parcel.writeBundle(b1);
    268         Bundle b2 = parcel.readBundle();
    269 
    270         assertEquals(b1, b2);
    271         assertEquals("world", b2.getString("hello"));
    272 
    273         parcel.writeBundle(b1);
    274         b2 = parcel.readBundle(null /* ClassLoader */);
    275         assertEquals(b1, b2);
    276         assertEquals("world", b2.getString("hello"));
    277     }
    278 
    279     @Test
    280     public void testWriteCreateStringArray() {
    281       final String[] strings = { "foo", "bar" };
    282       parcel.writeStringArray(strings);
    283       final String[] strings2 = parcel.createStringArray();
    284       assertTrue(Arrays.equals(strings, strings2));
    285     }
    286 
    287     @Test
    288     public void testReadWriteStringList() {
    289         final List<String> strings = Arrays.asList( "foo", "bar" );
    290         parcel.writeStringList(strings);
    291         List<String> extractedStrings = new ArrayList<String>();
    292         parcel.readStringList(extractedStrings);
    293         assertEquals(strings, extractedStrings);
    294     }
    295 
    296     @Test
    297     public void testWriteCreateStringArrayList() {
    298         final List<String> strings = Arrays.asList( "foo", "bar" );
    299         parcel.writeStringList(strings);
    300         List<String> extractedStrings = parcel.createStringArrayList();
    301         assertEquals(strings, extractedStrings);
    302     }
    303 
    304     @Test
    305     public void testReadWriteByteArray() throws Exception {
    306         final byte[] bytes = {1, 2};
    307         parcel.writeByteArray(bytes);
    308         final byte[] bytes2 = new byte[bytes.length];
    309         parcel.readByteArray(bytes2);
    310         assertTrue(Arrays.equals(bytes, bytes2));
    311     }
    312 
    313     @Test
    314     public void testReadWriteBooleanArray() {
    315         final boolean[] booleans = {false, true, true};
    316         parcel.writeBooleanArray(booleans);
    317         final boolean[] booleans2 = new boolean[booleans.length];
    318         parcel.readBooleanArray(booleans2);
    319         assertTrue(Arrays.equals(booleans, booleans2));
    320     }
    321 
    322     @Test
    323     public void testReadWriteCharArray() {
    324         final char[] chars = {'a', 'b', 'c'};
    325         parcel.writeCharArray(chars);
    326         final char[] chars2 = new char[chars.length];
    327         parcel.readCharArray(chars2);
    328         assertTrue(Arrays.equals(chars, chars2));
    329     }
    330 
    331     @Test
    332     public void testWriteCreateBooleanArray() {
    333         final boolean[] booleans = {false, true, true};
    334         parcel.writeBooleanArray(booleans);
    335         final boolean[] booleans2 = parcel.createBooleanArray();
    336         assertTrue(Arrays.equals(booleans, booleans2));
    337     }
    338 
    339     @Test
    340     public void testWriteCreateByteArray() {
    341         final byte[] bytes = {1, 2};
    342         parcel.writeByteArray(bytes);
    343         final byte[] bytes2 = parcel.createByteArray();
    344         assertTrue(Arrays.equals(bytes, bytes2));
    345     }
    346 
    347     @Test
    348     public void testWriteCreateCharArray() {
    349         final char[] chars = {'a', 'b', 'c'};
    350         parcel.writeCharArray(chars);
    351         final char[] chars2 = parcel.createCharArray();
    352         assertTrue(Arrays.equals(chars, chars2));
    353     }
    354 
    355     @Test
    356     public void testWriteCreateIntArray() {
    357         final int[] ints = {1, 2};
    358         parcel.writeIntArray(ints);
    359         final int[] ints2 = parcel.createIntArray();
    360         assertTrue(Arrays.equals(ints, ints2));
    361     }
    362 
    363     @Test
    364     public void testWriteCreateLongArray() {
    365         final long[] longs = {1, 2};
    366         parcel.writeLongArray(longs);
    367         final long[] longs2 = parcel.createLongArray();
    368         assertTrue(Arrays.equals(longs, longs2));
    369     }
    370 
    371     @Test
    372     public void testWriteCreateFloatArray() {
    373         final float[] floats = {1.5f, 2.25f};
    374         parcel.writeFloatArray(floats);
    375         final float[] floats2 = parcel.createFloatArray();
    376         assertTrue(Arrays.equals(floats, floats2));
    377     }
    378 
    379     @Test
    380     public void testWriteCreateDoubleArray() {
    381         final double[] doubles = {1.2, 2.2};
    382         parcel.writeDoubleArray(doubles);
    383         final double[] doubles2 = parcel.createDoubleArray();
    384         assertTrue(Arrays.equals(doubles, doubles2));
    385     }
    386 }
    387