ShadowBundle: Adding put/get methods for bundles and arrays inside a bundle
diff --git a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBundle.java b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBundle.java
index a7c3499..93872d4 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBundle.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowBundle.java
@@ -44,13 +44,13 @@
Object value = map.get(key);
return value == null ? 0 : (Long) value;
}
-
+
@Implementation
public long getLong(String key,long defaultValue) {
Object value = map.get(key);
return value == null ? defaultValue : (Long) value;
}
-
+
@Implementation
public void putInt(String key, int value) {
map.put(key, value);
@@ -61,13 +61,13 @@
Object value = map.get(key);
return value == null ? 0 : (Integer) value;
}
-
+
@Implementation
public int getInt(String key, int defaultValue) {
Object value = map.get(key);
return value == null ? defaultValue : (Integer) value;
}
-
+
@Implementation
public void putDouble(String key, double value) {
map.put(key, value);
@@ -78,13 +78,13 @@
Object value = map.get(key);
return value == null ? 0 : (Double) value;
}
-
+
@Implementation
public double getDouble(String key, double defaultValue) {
Object value = map.get(key);
return value == null ? defaultValue : (Double) value;
}
-
+
@Implementation
public void putBoolean(String key, boolean value) {
map.put(key, value);
@@ -95,7 +95,7 @@
Object value = map.get(key);
return value == null ? false : (Boolean) value;
}
-
+
@Implementation
public boolean getBoolean(String key, boolean defaultValue) {
Object value = map.get(key);
@@ -118,7 +118,7 @@
Object value = map.get(key);
return value == null ? defaultValue : (Float) value;
}
-
+
@Implementation
public void putSerializable(String key, Serializable value) {
map.put(key, value);
@@ -133,7 +133,7 @@
public void putParcelable(String key, Parcelable value) {
map.put(key, value);
}
-
+
@Implementation
public void putParcelableArrayList(String key, ArrayList<? extends Parcelable> value) {
map.put(key, value);
@@ -143,7 +143,7 @@
public Parcelable getParcelable(String key) {
return (Parcelable) map.get(key);
}
-
+
@Implementation
public ArrayList<Parcelable> getParcelableArrayList(String key) {
return (ArrayList<Parcelable>)map.get(key);
@@ -170,6 +170,96 @@
}
@Implementation
+ public void putBundle(String key, Bundle value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public Bundle getBundle(String key) {
+ return (Bundle) map.get(key);
+ }
+
+ @Implementation
+ public void putBooleanArray(String key, boolean[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public boolean[] getBooleanArray(String key) {
+ return (boolean[]) map.get(key);
+ }
+
+ @Implementation
+ public void putByteArray(String key, byte[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public byte[] getByteArray(String key) {
+ return (byte[]) map.get(key);
+ }
+
+ @Implementation
+ public void putCharArray(String key, char[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public char[] getCharArray(String key) {
+ return (char[]) map.get(key);
+ }
+
+ @Implementation
+ public void putDoubleArray(String key, double[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public double[] getDoubleArray(String key) {
+ return (double[]) map.get(key);
+ }
+
+ @Implementation
+ public void putFloatArray(String key, float[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public float[] getFloatArray(String key) {
+ return (float[]) map.get(key);
+ }
+
+ @Implementation
+ public void putIntArray(String key, int[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public int[] getIntArray(String key) {
+ return (int[]) map.get(key);
+ }
+
+ @Implementation
+ public void putLongArray(String key, long[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public long[] getLongArray(String key) {
+ return (long[]) map.get(key);
+ }
+
+ @Implementation
+ public void putShortArray(String key, short[] value) {
+ map.put(key, value);
+ }
+
+ @Implementation
+ public short[] getShortArray(String key) {
+ return (short[]) map.get(key);
+ }
+
+ @Implementation
public void putAll(Bundle bundle) {
map.putAll(((ShadowBundle) Robolectric.shadowOf_(bundle)).map);
}
diff --git a/src/test/java/com/xtremelabs/robolectric/shadows/BundleTest.java b/src/test/java/com/xtremelabs/robolectric/shadows/BundleTest.java
index 1edef2d..d6801d5 100644
--- a/src/test/java/com/xtremelabs/robolectric/shadows/BundleTest.java
+++ b/src/test/java/com/xtremelabs/robolectric/shadows/BundleTest.java
@@ -2,6 +2,7 @@
import android.os.Bundle;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+import junit.framework.AssertionFailedError;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -27,7 +28,7 @@
bundle.putString("foo", "bar");
assertTrue(bundle.containsKey("foo"));
}
-
+
@Test
public void testInt() {
bundle.putInt("foo", 5);
@@ -35,7 +36,7 @@
assertEquals(0,bundle.getInt("bar"));
assertEquals(7, bundle.getInt("bar", 7));
}
-
+
@Test
public void testSize() {
assertEquals(0, bundle.size());
@@ -52,7 +53,7 @@
assertEquals(0,bundle.getLong("bar"));
assertEquals(7, bundle.getLong("bar", 7));
}
-
+
@Test
public void testDouble() {
bundle.putDouble("foo", 5);
@@ -90,7 +91,7 @@
Assert.assertArrayEquals(new String[] { "a" }, bundle.getStringArray("foo"));
assertNull(bundle.getStringArray("bar"));
}
-
+
@Test
public void testStringArrayList() {
ArrayList<String> list = new ArrayList<String>();
@@ -110,4 +111,109 @@
Assert.assertEquals(list, bundle.getIntegerArrayList("foo"));
assertNull(bundle.getIntegerArrayList("bar"));
}
+
+ @Test
+ public void testBundle() {
+ Bundle innerBundle = new Bundle();
+ innerBundle.putInt("int", 7);
+ bundle.putBundle("bundle", innerBundle);
+
+ assertEquals(innerBundle, bundle.getBundle("bundle"));
+ assertNull(bundle.getBundle("bar"));
+ }
+
+ @Test
+ public void testBooleanArray() {
+ boolean [] arr = new boolean[] { false, true };
+ bundle.putBooleanArray("foo", arr);
+
+ assertArrayEquals(arr, bundle.getBooleanArray("foo"));
+ assertNull(bundle.getBooleanArray("bar"));
+ }
+
+ @Test
+ public void testByteArray() {
+ byte [] arr = new byte[] { 12, 24 };
+ bundle.putByteArray("foo", arr);
+
+ Assert.assertArrayEquals(arr, bundle.getByteArray("foo"));
+ assertNull(bundle.getByteArray("bar"));
+ }
+
+ @Test
+ public void testCharArray() {
+ char [] arr = new char[] { 'c', 'j' };
+ bundle.putCharArray("foo", arr);
+
+ Assert.assertArrayEquals(arr, bundle.getCharArray("foo"));
+ assertNull(bundle.getCharArray("bar"));
+ }
+
+ @Test
+ public void testDoubleArray() {
+ double [] arr = new double[] { 1.2, 3.4 };
+ bundle.putDoubleArray("foo", arr);
+
+ assertArrayEquals(arr, bundle.getDoubleArray("foo"));
+ assertNull(bundle.getDoubleArray("bar"));
+ }
+
+ @Test
+ public void testIntArray() {
+ int [] arr = new int[] { 87, 65 };
+ bundle.putIntArray("foo", arr);
+
+ Assert.assertArrayEquals(arr, bundle.getIntArray("foo"));
+ assertNull(bundle.getIntArray("bar"));
+ }
+
+ @Test
+ public void testLongArray() {
+ long [] arr = new long[] { 23, 11 };
+ bundle.putLongArray("foo", arr);
+
+ Assert.assertArrayEquals(arr, bundle.getLongArray("foo"));
+ assertNull(bundle.getLongArray("bar"));
+ }
+
+ @Test
+ public void testShortArray() {
+ short [] arr = new short[] { 89, 37 };
+ bundle.putShortArray("foo", arr);
+
+ Assert.assertArrayEquals(arr, bundle.getShortArray("foo"));
+ assertNull(bundle.getShortArray("bar"));
+ }
+
+ private void assertArrayEquals(double[] expected, double[] actual) {
+ if (expected != null && actual == null) {
+ throw new AssertionFailedError();
+ } else if (expected == null && actual != null) {
+ throw new AssertionFailedError();
+ } else {
+ for (int i = 0; i < expected.length; i++) {
+ if (expected[i] != actual[i])
+ throw new AssertionFailedError();
+ }
+
+ if (expected.length != actual.length)
+ throw new AssertionFailedError();
+ }
+ }
+
+ private void assertArrayEquals(boolean[] expected, boolean[] actual) {
+ if (expected != null && actual == null) {
+ throw new AssertionFailedError();
+ } else if (expected == null && actual != null) {
+ throw new AssertionFailedError();
+ } else {
+ for (int i = 0; i < expected.length; i++) {
+ if (expected[i] != actual[i])
+ throw new AssertionFailedError();
+ }
+
+ if (expected.length != actual.length)
+ throw new AssertionFailedError();
+ }
+ }
}