ShadowParcel.java revision 053f4fe6cc16a60b7c96ad79472274b3c8604ad2
1package com.xtremelabs.robolectric.shadows;
2
3import android.os.Parcel;
4import android.os.Parcelable;
5import android.os.Bundle;
6import com.xtremelabs.robolectric.Robolectric;
7import com.xtremelabs.robolectric.internal.Implementation;
8import com.xtremelabs.robolectric.internal.Implements;
9import com.xtremelabs.robolectric.internal.RealObject;
10
11import java.util.ArrayList;
12import java.util.List;
13
14@Implements(Parcel.class)
15@SuppressWarnings("unchecked")
16public class ShadowParcel {
17    private 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        if (str == null) {
31            return;
32        }
33        parcelData.add(str);
34    }
35
36    @Implementation
37    public void writeInt(int i) {
38        parcelData.add(i);
39    }
40
41    @Implementation
42    public void writeLong(long i) {
43        parcelData.add(i);
44    }
45
46    @Implementation
47    public void writeFloat(float f) {
48        parcelData.add(f);
49    }
50
51    @Implementation
52    public void writeDouble(double f) {
53        parcelData.add(f);
54    }
55
56    @Implementation
57    @SuppressWarnings("unchecked")
58    public void writeByte(byte b) {
59        parcelData.add(b);
60    }
61
62    @Implementation
63    public String readString() {
64        return index < parcelData.size() ? (String) parcelData.get(index++) : null;
65    }
66
67    @Implementation
68    public int readInt() {
69        return index < parcelData.size() ? (Integer) parcelData.get(index++) : 0;
70    }
71
72    @Implementation
73    public float readFloat() {
74        return index < parcelData.size() ? (Float) parcelData.get(index++) : 0;
75    }
76
77    @Implementation
78    public double readDouble() {
79        return index < parcelData.size() ? (Double) parcelData.get(index++) : 0;
80    }
81
82    @Implementation
83    public byte readByte() {
84        return index < parcelData.size() ? (Byte) parcelData.get(index++) : 0;
85    }
86
87    @Implementation
88    public long readLong() {
89        return index < parcelData.size() ? (Long) parcelData.get(index++) : 0;
90    }
91
92    @Implementation
93    public Bundle readBundle() {
94        return index < parcelData.size() ? (Bundle) parcelData.get(index++) : null;
95    }
96
97    @Implementation
98    public Bundle readBundle(ClassLoader loader) {
99        return readBundle();
100    }
101
102    @Implementation
103    public void writeBundle(Bundle bundle) {
104        parcelData.add(bundle);
105    }
106
107    @Implementation
108    public void writeParcelable(Parcelable p, int flags) {
109        parcelData.add(p);
110    }
111
112    @Implementation
113    public Parcelable readParcelable(ClassLoader cl) {
114        return index < parcelData.size() ? (Parcelable) parcelData.get(index++) : null;
115    }
116
117    @Implementation
118    public void readFloatArray(float[] val) {
119        int n = readInt();
120        if (val.length != n) throw new RuntimeException("bad array lengths");
121        for (int i = 0; i < val.length; i++) {
122            val[i] = readFloat();
123        }
124    }
125
126    @Implementation
127    public void writeFloatArray(float[] val) {
128        writeInt(val.length);
129        for (float f : val) writeFloat(f);
130    }
131
132    @Implementation
133    public void writeDoubleArray(double[] val) {
134        writeInt(val.length);
135        for (double f : val) writeDouble(f);
136    }
137
138    @Implementation
139    public void readDoubleArray(double[] val) {
140        int n = readInt();
141        if (val.length != n) throw new RuntimeException("bad array lengths");
142        for (int i = 0; i < val.length; i++) {
143            val[i] = readDouble();
144        }
145    }
146
147    @Implementation
148    public void writeIntArray(int[] val) {
149        writeInt(val.length);
150        for (int f : val) writeInt(f);
151    }
152
153    @Implementation
154    public void readIntArray(int[] val) {
155        int n = readInt();
156        if (val.length != n) throw new RuntimeException("bad array lengths");
157        for (int i = 0; i < val.length; i++) {
158            val[i] = readInt();
159        }
160    }
161
162    @Implementation
163    public void writeLongArray(long[] val) {
164        writeInt(val.length);
165        for (long f : val) writeLong(f);
166    }
167
168    @Implementation
169    public void readLongArray(long[] val) {
170        int n = readInt();
171        if (val.length != n) throw new RuntimeException("bad array lengths");
172        for (int i = 0; i < val.length; i++) {
173            val[i] = readLong();
174        }
175    }
176
177    @Implementation
178    public void writeStringArray(String[] val) {
179        writeInt(val.length);
180        for (String f : val) writeString(f);
181    }
182
183    @Implementation
184    public void readStringArray(String[] val) {
185        int n = readInt();
186        if (val.length != n) throw new RuntimeException("bad array lengths");
187        for (int i = 0; i < val.length; i++) {
188            val[i] = readString();
189        }
190    }
191
192    @Implementation
193    public final ArrayList<String> createStringArrayList() {
194        int n = readInt();
195        if (n < 0) {
196            return null;
197        }
198
199        ArrayList<String> l = new ArrayList<String>(n);
200        while (n > 0) {
201            l.add(readString());
202            n--;
203        }
204        return l;
205    }
206
207    @Implementation
208    public ArrayList createTypedArrayList(Parcelable.Creator c) {
209        int n = readInt();
210        if (n < 0) {
211            return null;
212        }
213
214        ArrayList l = new ArrayList(n);
215
216        while (n > 0) {
217            if (readInt() != 0) {
218                l.add(c.createFromParcel(realParcel));
219            } else {
220                l.add(null);
221            }
222            n--;
223        }
224        return l;
225    }
226
227    @Implementation
228    public final void writeTypedList(List val) {
229        if (val == null) {
230            writeInt(-1);
231            return;
232        }
233
234        int n = val.size();
235        int i = 0;
236        writeInt(n);
237        while (i < n) {
238            Object item = val.get(i);
239            if (item != null) {
240                writeInt(1);
241                ((Parcelable) item).writeToParcel(realParcel, 0);
242            } else {
243                writeInt(0);
244            }
245            i++;
246        }
247    }
248
249    public int getIndex() {
250        return index;
251    }
252
253    public List getParcelData() {
254        return parcelData;
255    }
256
257}
258