ShadowParcel: Add support for create*Array methods, readCharArray, writeCharArray, readBooleanArray, writeBooleanArray
diff --git a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowParcel.java b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowParcel.java
index 93ea734..55713b9 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowParcel.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowParcel.java
@@ -49,6 +49,14 @@
parcelData.add(f);
}
+ public void writeBoolean(boolean b) {
+ parcelData.add(b);
+ }
+
+ public void writeChar(char b) {
+ parcelData.add(b);
+ }
+
@Implementation
@SuppressWarnings("unchecked")
public void writeByte( byte b ) {
@@ -80,6 +88,14 @@
return index < parcelData.size() ? (Byte) parcelData.get(index++) : 0;
}
+ public boolean readBoolean() {
+ return index < parcelData.size() ? (Boolean) parcelData.get(index++) : false;
+ }
+
+ public char readChar() {
+ return index < parcelData.size() ? (Character) parcelData.get(index++) : 0;
+ }
+
@Implementation
public long readLong() {
return index < parcelData.size() ? (Long) parcelData.get(index++) : 0;
@@ -111,10 +127,60 @@
}
@Implementation
+ public void readBooleanArray(boolean[] val) {
+ int n = readInt();
+ if (val.length != n) throw new RuntimeException("bad array lengths");
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readBoolean();
+ }
+ }
+
+ @Implementation
+ public void writeBooleanArray(boolean[] val) {
+ writeInt(val.length);
+ for (boolean b : val) writeBoolean(b);
+ }
+
+ @Implementation
+ public boolean[] createBooleanArray() {
+ int n = readInt();
+ boolean[] val = new boolean[n];
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readBoolean();
+ }
+ return val;
+ }
+
+ @Implementation
+ public void readCharArray(char[] val) {
+ int n = readInt();
+ if (val.length != n) throw new RuntimeException("bad array lengths");
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readChar();
+ }
+ }
+
+ @Implementation
+ public void writeCharArray(char[] val) {
+ writeInt(val.length);
+ for (char b : val) writeChar(b);
+ }
+
+ @Implementation
+ public char[] createCharArray() {
+ int n = readInt();
+ char[] val = new char[n];
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readChar();
+ }
+ return val;
+ }
+
+ @Implementation
public void readFloatArray(float[] val) {
int n = readInt();
if (val.length != n) throw new RuntimeException("bad array lengths");
- for (int i = 0; i< val.length; i++) {
+ for (int i = 0; i < val.length; i++) {
val[i] = readFloat();
}
}
@@ -126,6 +192,16 @@
}
@Implementation
+ public float[] createFloatArray() {
+ int n = readInt();
+ float[] val = new float[n];
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readFloat();
+ }
+ return val;
+ }
+
+ @Implementation
public void writeDoubleArray(double[] val) {
writeInt(val.length);
for (double f : val) writeDouble(f);
@@ -135,12 +211,22 @@
public void readDoubleArray(double[] val) {
int n = readInt();
if (val.length != n) throw new RuntimeException("bad array lengths");
- for (int i = 0; i< val.length; i++) {
+ for (int i = 0; i < val.length; i++) {
val[i] = readDouble();
}
}
@Implementation
+ public double[] createDoubleArray() {
+ int n = readInt();
+ double[] val = new double[n];
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readDouble();
+ }
+ return val;
+ }
+
+ @Implementation
public void writeIntArray(int[] val) {
writeInt(val.length);
for (int f : val) writeInt(f);
@@ -150,12 +236,22 @@
public void readIntArray(int[] val) {
int n = readInt();
if (val.length != n) throw new RuntimeException("bad array lengths");
- for (int i = 0; i< val.length; i++) {
+ for (int i = 0; i < val.length; i++) {
val[i] = readInt();
}
}
@Implementation
+ public int[] createIntArray() {
+ int n = readInt();
+ int[] val = new int[n];
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readInt();
+ }
+ return val;
+ }
+
+ @Implementation
public void writeByteArray(byte[] val) {
writeInt(val.length);
for (byte f : val) writeByte(f);
@@ -165,12 +261,22 @@
public void readByteArray(byte[] val) {
int n = readInt();
if (val.length != n) throw new RuntimeException("bad array lengths");
- for (int i = 0; i< val.length; i++) {
+ for (int i = 0; i < val.length; i++) {
val[i] = readByte();
}
}
@Implementation
+ public byte[] createByteArray() {
+ int n = readInt();
+ byte[] val = new byte[n];
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readByte();
+ }
+ return val;
+ }
+
+ @Implementation
public void writeLongArray(long[] val) {
writeInt(val.length);
for (long f : val) writeLong(f);
@@ -180,12 +286,22 @@
public void readLongArray(long[] val) {
int n = readInt();
if (val.length != n) throw new RuntimeException("bad array lengths");
- for (int i = 0; i< val.length; i++) {
+ for (int i = 0; i < val.length; i++) {
val[i] = readLong();
}
}
@Implementation
+ public long[] createLongArray() {
+ int n = readInt();
+ long[] val = new long[n];
+ for (int i = 0; i < val.length; i++) {
+ val[i] = readLong();
+ }
+ return val;
+ }
+
+ @Implementation
public void writeStringArray(String[] val) {
writeInt(val.length);
for (String f : val) writeString(f);
@@ -209,7 +325,7 @@
public void readStringArray(String[] val) {
int n = readInt();
if (val.length != n) throw new RuntimeException("bad array lengths");
- for (int i = 0; i< val.length; i++) {
+ for (int i = 0; i < val.length; i++) {
val[i] = readString();
}
}
diff --git a/src/test/java/com/xtremelabs/robolectric/shadows/ParcelTest.java b/src/test/java/com/xtremelabs/robolectric/shadows/ParcelTest.java
index 3965e93..a06b5ce 100644
--- a/src/test/java/com/xtremelabs/robolectric/shadows/ParcelTest.java
+++ b/src/test/java/com/xtremelabs/robolectric/shadows/ParcelTest.java
@@ -307,4 +307,78 @@
parcel.readByteArray(bytes2);
assertTrue(Arrays.equals(bytes, bytes2));
}
+
+ @Test
+ public void testReadWriteBooleanArray() {
+ final boolean[] booleans = {false, true, true};
+ parcel.writeBooleanArray(booleans);
+ final boolean[] booleans2 = new boolean[booleans.length];
+ parcel.readBooleanArray(booleans2);
+ assertTrue(Arrays.equals(booleans, booleans2));
+ }
+
+ @Test
+ public void testReadWriteCharArray() {
+ final char[] chars = {'a', 'b', 'c'};
+ parcel.writeCharArray(chars);
+ final char[] chars2 = new char[chars.length];
+ parcel.readCharArray(chars2);
+ assertTrue(Arrays.equals(chars, chars2));
+ }
+
+ @Test
+ public void testWriteCreateBooleanArray() {
+ final boolean[] booleans = {false, true, true};
+ parcel.writeBooleanArray(booleans);
+ final boolean[] booleans2 = parcel.createBooleanArray();
+ assertTrue(Arrays.equals(booleans, booleans2));
+ }
+
+ @Test
+ public void testWriteCreateByteArray() {
+ final byte[] bytes = {1, 2};
+ parcel.writeByteArray(bytes);
+ final byte[] bytes2 = parcel.createByteArray();
+ assertTrue(Arrays.equals(bytes, bytes2));
+ }
+
+ @Test
+ public void testWriteCreateCharArray() {
+ final char[] chars = {'a', 'b', 'c'};
+ parcel.writeCharArray(chars);
+ final char[] chars2 = parcel.createCharArray();
+ assertTrue(Arrays.equals(chars, chars2));
+ }
+
+ @Test
+ public void testWriteCreateIntArray() {
+ final int[] ints = {1, 2};
+ parcel.writeIntArray(ints);
+ final int[] ints2 = parcel.createIntArray();
+ assertTrue(Arrays.equals(ints, ints2));
+ }
+
+ @Test
+ public void testWriteCreateLongArray() {
+ final long[] longs = {1, 2};
+ parcel.writeLongArray(longs);
+ final long[] longs2 = parcel.createLongArray();
+ assertTrue(Arrays.equals(longs, longs2));
+ }
+
+ @Test
+ public void testWriteCreateFloatArray() {
+ final float[] floats = {1.5f, 2.25f};
+ parcel.writeFloatArray(floats);
+ final float[] floats2 = parcel.createFloatArray();
+ assertTrue(Arrays.equals(floats, floats2));
+ }
+
+ @Test
+ public void testWriteCreateDoubleArray() {
+ final double[] doubles = {1.2, 2.2};
+ parcel.writeDoubleArray(doubles);
+ final double[] doubles2 = parcel.createDoubleArray();
+ assertTrue(Arrays.equals(doubles, doubles2));
+ }
}