Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.os.Parcel;
      4 import android.os.Parcelable;
      5 import android.os.Bundle;
      6 import com.xtremelabs.robolectric.Robolectric;
      7 import com.xtremelabs.robolectric.internal.Implementation;
      8 import com.xtremelabs.robolectric.internal.Implements;
      9 import com.xtremelabs.robolectric.internal.RealObject;
     10 
     11 import java.util.ArrayList;
     12 import java.util.List;
     13 
     14 @Implements(Parcel.class)
     15 @SuppressWarnings("unchecked")
     16 public class ShadowParcel {
     17     private final ArrayList parcelData = new ArrayList();
     18     private int index = 0;
     19 
     20     @RealObject
     21     private Parcel realParcel;
     22 
     23     @Implementation
     24     public static Parcel obtain() {
     25         return Robolectric.newInstanceOf(Parcel.class);
     26     }
     27 
     28     @Implementation
     29     public void writeString(String str) {
     30         parcelData.add(str);
     31     }
     32 
     33     @Implementation
     34     public void writeInt(int i) {
     35         parcelData.add(i);
     36     }
     37 
     38     @Implementation
     39     public void writeLong(long i) {
     40         parcelData.add(i);
     41     }
     42 
     43     @Implementation
     44     public void writeFloat(float f) {
     45         parcelData.add(f);
     46     }
     47 
     48     @Implementation
     49     public void writeDouble(double f) {
     50         parcelData.add(f);
     51     }
     52 
     53     public void writeBoolean(boolean b) {
     54         parcelData.add(b);
     55     }
     56 
     57     public void writeChar(char b) {
     58         parcelData.add(b);
     59     }
     60 
     61     @Implementation
     62     @SuppressWarnings("unchecked")
     63     public void writeByte(byte b) {
     64         parcelData.add(b);
     65     }
     66 
     67     @Implementation
     68     public String readString() {
     69         return index < parcelData.size() ? (String) parcelData.get(index++) : null;
     70     }
     71 
     72     @Implementation
     73     public int readInt() {
     74         return index < parcelData.size() ? (Integer) parcelData.get(index++) : 0;
     75     }
     76 
     77     @Implementation
     78     public float readFloat() {
     79         return index < parcelData.size() ? (Float) parcelData.get(index++) : 0;
     80     }
     81 
     82     @Implementation
     83     public double readDouble() {
     84         return index < parcelData.size() ? (Double) parcelData.get(index++) : 0;
     85     }
     86 
     87     @Implementation
     88     public byte readByte() {
     89         return index < parcelData.size() ? (Byte) parcelData.get(index++) : 0;
     90     }
     91 
     92     public boolean readBoolean() {
     93         return index < parcelData.size() ? (Boolean) parcelData.get(index++) : false;
     94     }
     95 
     96     public char readChar() {
     97         return index < parcelData.size() ? (Character) parcelData.get(index++) : 0;
     98     }
     99 
    100     @Implementation
    101     public long readLong() {
    102         return index < parcelData.size() ? (Long) parcelData.get(index++) : 0;
    103     }
    104 
    105     @Implementation
    106     public Bundle readBundle() {
    107         return index < parcelData.size() ? (Bundle) parcelData.get(index++) : null;
    108     }
    109 
    110     @Implementation
    111     public Bundle readBundle(ClassLoader loader) {
    112         return readBundle();
    113     }
    114 
    115     @Implementation
    116     public void writeBundle(Bundle bundle) {
    117         parcelData.add(bundle);
    118     }
    119 
    120     @Implementation
    121     public void writeParcelable(Parcelable p, int flags) {
    122         parcelData.add(p);
    123     }
    124 
    125     @Implementation
    126     public Parcelable readParcelable(ClassLoader cl) {
    127         return index < parcelData.size() ? (Parcelable) parcelData.get(index++) : null;
    128     }
    129 
    130     @Implementation
    131     public void readBooleanArray(boolean[] val) {
    132         int n = readInt();
    133         if (val.length != n) throw new RuntimeException("bad array lengths");
    134         for (int i = 0; i < val.length; i++) {
    135             val[i] = readBoolean();
    136         }
    137     }
    138 
    139     @Implementation
    140     public void writeBooleanArray(boolean[] val) {
    141         if (val == null) {
    142             writeInt(-1);
    143             return;
    144         }
    145         writeInt(val.length);
    146         for (boolean b : val) writeBoolean(b);
    147     }
    148 
    149     @Implementation
    150     public boolean[] createBooleanArray() {
    151         int n = readInt();
    152         if (n < 0) {
    153             return null;
    154         }
    155         boolean[] val = new boolean[n];
    156         for (int i = 0; i < val.length; i++) {
    157             val[i] = readBoolean();
    158         }
    159         return val;
    160     }
    161 
    162     @Implementation
    163     public void readCharArray(char[] val) {
    164         int n = readInt();
    165         if (val.length != n) throw new RuntimeException("bad array lengths");
    166         for (int i = 0; i < val.length; i++) {
    167             val[i] = readChar();
    168         }
    169     }
    170 
    171     @Implementation
    172     public void writeCharArray(char[] val) {
    173         if (val == null) {
    174             writeInt(-1);
    175             return;
    176         }
    177         writeInt(val.length);
    178         for (char b : val) writeChar(b);
    179     }
    180 
    181     @Implementation
    182     public char[] createCharArray() {
    183         int n = readInt();
    184         if (n < 0) {
    185             return null;
    186         }
    187         char[] val = new char[n];
    188         for (int i = 0; i < val.length; i++) {
    189             val[i] = readChar();
    190         }
    191         return val;
    192     }
    193 
    194     @Implementation
    195     public void readFloatArray(float[] val) {
    196         int n = readInt();
    197         if (val.length != n) throw new RuntimeException("bad array lengths");
    198         for (int i = 0; i < val.length; i++) {
    199             val[i] = readFloat();
    200         }
    201     }
    202 
    203     @Implementation
    204     public void writeFloatArray(float[] val) {
    205         if (val == null) {
    206             writeInt(-1);
    207             return;
    208         }
    209         writeInt(val.length);
    210         for (float f : val) writeFloat(f);
    211     }
    212 
    213     @Implementation
    214     public float[] createFloatArray() {
    215         int n = readInt();
    216         if (n < 0) {
    217             return null;
    218         }
    219         float[] val = new float[n];
    220         for (int i = 0; i < val.length; i++) {
    221             val[i] = readFloat();
    222         }
    223         return val;
    224     }
    225 
    226     @Implementation
    227     public void writeDoubleArray(double[] val) {
    228         if (val == null) {
    229             writeInt(-1);
    230             return;
    231         }
    232         writeInt(val.length);
    233         for (double f : val) writeDouble(f);
    234     }
    235 
    236     @Implementation
    237     public void readDoubleArray(double[] val) {
    238         int n = readInt();
    239         if (val.length != n) throw new RuntimeException("bad array lengths");
    240         for (int i = 0; i < val.length; i++) {
    241             val[i] = readDouble();
    242         }
    243     }
    244 
    245     @Implementation
    246     public double[] createDoubleArray() {
    247         int n = readInt();
    248         if (n < 0) {
    249             return null;
    250         }
    251         double[] val = new double[n];
    252         for (int i = 0; i < val.length; i++) {
    253             val[i] = readDouble();
    254         }
    255         return val;
    256     }
    257 
    258     @Implementation
    259     public void writeIntArray(int[] val) {
    260         if (val == null) {
    261             writeInt(-1);
    262             return;
    263         }
    264         writeInt(val.length);
    265         for (int f : val) writeInt(f);
    266     }
    267 
    268     @Implementation
    269     public void readIntArray(int[] val) {
    270         int n = readInt();
    271         if (val.length != n) throw new RuntimeException("bad array lengths");
    272         for (int i = 0; i < val.length; i++) {
    273             val[i] = readInt();
    274         }
    275     }
    276 
    277     @Implementation
    278     public int[] createIntArray() {
    279         int n = readInt();
    280         if (n < 0) {
    281             return null;
    282         }
    283         int[] val = new int[n];
    284         for (int i = 0; i < val.length; i++) {
    285             val[i] = readInt();
    286         }
    287         return val;
    288     }
    289 
    290     @Implementation
    291     public void writeByteArray(byte[] val) {
    292         if (val == null) {
    293             writeInt(-1);
    294             return;
    295         }
    296         writeInt(val.length);
    297         for (byte f : val) writeByte(f);
    298     }
    299 
    300     @Implementation
    301     public void readByteArray(byte[] val) {
    302         int n = readInt();
    303         if (val.length != n) throw new RuntimeException("bad array lengths");
    304         for (int i = 0; i < val.length; i++) {
    305             val[i] = readByte();
    306         }
    307     }
    308 
    309     @Implementation
    310     public byte[] createByteArray() {
    311         int n = readInt();
    312         if (n < 0) {
    313             return null;
    314         }
    315         byte[] val = new byte[n];
    316         for (int i = 0; i < val.length; i++) {
    317             val[i] = readByte();
    318         }
    319         return val;
    320     }
    321 
    322     @Implementation
    323     public void writeLongArray(long[] val) {
    324         if (val == null) {
    325             writeInt(-1);
    326             return;
    327         }
    328         writeInt(val.length);
    329         for (long f : val) writeLong(f);
    330     }
    331 
    332     @Implementation
    333     public void readLongArray(long[] val) {
    334         int n = readInt();
    335         if (val.length != n) throw new RuntimeException("bad array lengths");
    336         for (int i = 0; i < val.length; i++) {
    337             val[i] = readLong();
    338         }
    339     }
    340 
    341     @Implementation
    342     public long[] createLongArray() {
    343         int n = readInt();
    344         if (n < 0) {
    345             return null;
    346         }
    347         long[] val = new long[n];
    348         for (int i = 0; i < val.length; i++) {
    349             val[i] = readLong();
    350         }
    351         return val;
    352     }
    353 
    354     @Implementation
    355     public void writeStringArray(String[] val) {
    356         if (val == null) {
    357             writeInt(-1);
    358             return;
    359         }
    360         writeInt(val.length);
    361         for (String f : val) writeString(f);
    362     }
    363 
    364     @Implementation
    365     public String[] createStringArray() {
    366         String[] array = null;
    367 
    368         int length = readInt();
    369         if (length >= 0) {
    370             array = new String[length];
    371             for (int i = 0; i < length; i++) {
    372                 array[i] = readString();
    373             }
    374         }
    375         return array;
    376     }
    377 
    378     @Implementation
    379     public void readStringArray(String[] dest) {
    380         int n = readInt();
    381         if (dest.length != n) throw new RuntimeException("bad array lengths");
    382         for (int i = 0; i < dest.length; i++) {
    383             dest[i] = readString();
    384         }
    385     }
    386 
    387     @Implementation
    388     public void writeStringList(List<String> strings) {
    389         if (strings == null) {
    390             writeInt(-1);
    391             return;
    392         }
    393         int count = strings.size();
    394         int i=0;
    395         writeInt(count);
    396         while (i < count) {
    397             writeString(strings.get(i));
    398             i++;
    399         }
    400     }
    401 
    402     @Implementation
    403     public void readStringList(List<String> list) {
    404         int listSizeBeforeChange = list.size();
    405         int addCount = readInt();
    406         int i = 0;
    407         for (; i < listSizeBeforeChange && i < addCount; i++) {
    408             list.set(i, readString());
    409         }
    410         for (; i<addCount; i++) {
    411             list.add(readString());
    412         }
    413         for (; i<listSizeBeforeChange; i++) {
    414             list.remove(addCount);
    415         }
    416     }
    417 
    418     @Implementation
    419     public ArrayList<String> createStringArrayList() {
    420         int n = readInt();
    421         if (n < 0) {
    422             return null;
    423         }
    424 
    425         ArrayList<String> l = new ArrayList<String>(n);
    426         while (n > 0) {
    427             l.add(readString());
    428             n--;
    429         }
    430         return l;
    431     }
    432 
    433     @Implementation
    434     public ArrayList createTypedArrayList(Parcelable.Creator c) {
    435         int n = readInt();
    436         if (n < 0) {
    437             return null;
    438         }
    439 
    440         ArrayList l = new ArrayList(n);
    441 
    442         while (n > 0) {
    443             if (readInt() != 0) {
    444                 l.add(c.createFromParcel(realParcel));
    445             } else {
    446                 l.add(null);
    447             }
    448             n--;
    449         }
    450         return l;
    451     }
    452 
    453     @Implementation
    454     public void writeTypedList(List val) {
    455         if (val == null) {
    456             writeInt(-1);
    457             return;
    458         }
    459 
    460         int n = val.size();
    461         int i = 0;
    462         writeInt(n);
    463         while (i < n) {
    464             Object item = val.get(i);
    465             if (item != null) {
    466                 writeInt(1);
    467                 ((Parcelable) item).writeToParcel(realParcel, 0);
    468             } else {
    469                 writeInt(0);
    470             }
    471             i++;
    472         }
    473     }
    474 
    475     public int getIndex() {
    476         return index;
    477     }
    478 
    479     public List getParcelData() {
    480         return parcelData;
    481     }
    482 }
    483