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