Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.os.Build;
      4 import android.os.Bundle;
      5 import android.os.Parcelable;
      6 import com.xtremelabs.robolectric.Robolectric;
      7 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
      8 import junit.framework.AssertionFailedError;
      9 import org.junit.Assert;
     10 import org.junit.Before;
     11 import org.junit.Test;
     12 import org.junit.runner.RunWith;
     13 
     14 import java.util.ArrayList;
     15 
     16 import static org.junit.Assert.*;
     17 
     18 
     19 @RunWith(WithTestDefaultsRunner.class)
     20 public class BundleTest {
     21 
     22     private Bundle bundle;
     23 
     24     @Before public void setUp() throws Exception {
     25         bundle = new Bundle();
     26     }
     27 
     28     @Test
     29     public void testContainsKey() throws Exception {
     30         assertFalse(bundle.containsKey("foo"));
     31         bundle.putString("foo", "bar");
     32         assertTrue(bundle.containsKey("foo"));
     33     }
     34 
     35     @Test
     36     public void testInt() {
     37         bundle.putInt("foo", 5);
     38         assertEquals(5,bundle.getInt("foo"));
     39         assertEquals(0,bundle.getInt("bar"));
     40         assertEquals(7, bundle.getInt("bar", 7));
     41     }
     42 
     43     @Test
     44     public void testSize() {
     45         assertEquals(0, bundle.size());
     46         bundle.putInt("foo", 5);
     47         assertEquals(1, bundle.size());
     48         bundle.putInt("bar", 5);
     49         assertEquals(2, bundle.size());
     50     }
     51 
     52     @Test
     53     public void testLong() {
     54         bundle.putLong("foo", 5);
     55         assertEquals(5, bundle.getLong("foo"));
     56         assertEquals(0,bundle.getLong("bar"));
     57         assertEquals(7, bundle.getLong("bar", 7));
     58     }
     59 
     60     @Test
     61     public void testDouble() {
     62         bundle.putDouble("foo", 5);
     63         assertEquals(Double.valueOf(5), Double.valueOf(bundle.getDouble("foo")));
     64         assertEquals(Double.valueOf(0),Double.valueOf(bundle.getDouble("bar")));
     65         assertEquals(Double.valueOf(7), Double.valueOf(bundle.getDouble("bar", 7)));
     66     }
     67 
     68     @Test
     69     public void testByte() {
     70         bundle.putByte("foo", (byte) 0xA);
     71         assertEquals((byte) 0xA, bundle.getByte("foo"));
     72         assertEquals((byte) 0x0, bundle.getByte("bar"));
     73         assertEquals((byte) 0x37, bundle.getByte("bar", (byte) 0x37).byteValue());
     74     }
     75 
     76     @Test
     77     public void testBoolean() {
     78         bundle.putBoolean("foo", true);
     79         assertEquals(true, bundle.getBoolean("foo"));
     80         assertEquals(false, bundle.getBoolean("bar"));
     81         assertEquals(true, bundle.getBoolean("bar", true));
     82     }
     83 
     84     @Test
     85     public void testShort() {
     86         bundle.putShort("foo", (short) 0xA);
     87         assertEquals((short) 0xA, bundle.getShort("foo"));
     88         assertEquals((short) 0x0, bundle.getShort("bar"));
     89         assertEquals((short) 0x37, bundle.getShort("bar", (short) 0x37));
     90     }
     91 
     92     @Test
     93     public void testFloat() {
     94         bundle.putFloat("foo", 5f);
     95         assertEquals(Float.valueOf(5), Float.valueOf(bundle.getFloat("foo")));
     96         assertEquals(Float.valueOf(0),Float.valueOf(bundle.getFloat("bar")));
     97         assertEquals(Float.valueOf(7), Float.valueOf(bundle.getFloat("bar", 7)));
     98     }
     99 
    100     @Test
    101     public void testStringHasValue() {
    102         bundle.putString("key", "value");
    103         assertEquals("value", bundle.getString("key"));
    104     }
    105 
    106     @Test
    107     public void testStringDoesNotHaveValue() {
    108         assertNull(bundle.getString("key"));
    109     }
    110 
    111     @Test
    112     public void testStringNullKey() {
    113         bundle.putString(null, "value");
    114         assertEquals("value", bundle.getString(null));
    115     }
    116 
    117     @Test
    118     public void testStringNullValue() {
    119         bundle.putString("key", null);
    120         assertNull(bundle.getString("key"));
    121     }
    122 
    123     @Test
    124     public void testStringApi1() {
    125         int previousApiLevel = Build.VERSION.SDK_INT;
    126         Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    127                 Build.VERSION_CODES.BASE);
    128 
    129         try {
    130             bundle.getString("value", "defaultValue");
    131             fail();
    132         } catch (RuntimeException e) {
    133             // Expected
    134         } finally {
    135             Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    136                     previousApiLevel);
    137         }
    138     }
    139 
    140     @Test
    141     public void testStringApi12HasKey() {
    142         int previousApiLevel = Build.VERSION.SDK_INT;
    143         Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    144                 Build.VERSION_CODES.HONEYCOMB_MR1);
    145 
    146         try {
    147             bundle.putString("key", "value");
    148             assertEquals("value", bundle.getString("key", "defaultValue"));
    149         } finally {
    150             Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    151                     previousApiLevel);
    152         }
    153     }
    154 
    155     @Test
    156     public void testStringApi12DoesNotHaveKey() {
    157         int previousApiLevel = Build.VERSION.SDK_INT;
    158         Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    159                 Build.VERSION_CODES.HONEYCOMB_MR1);
    160 
    161         try {
    162             bundle.putString("key", "value");
    163             assertEquals("defaultValue", bundle.getString("foo", "defaultValue"));
    164         } finally {
    165             Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    166                     previousApiLevel);
    167         }
    168     }
    169 
    170     @Test
    171     public void testStringApi12NullKey() {
    172         int previousApiLevel = Build.VERSION.SDK_INT;
    173         Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    174                 Build.VERSION_CODES.HONEYCOMB_MR1);
    175 
    176         try {
    177             bundle.putString(null, "value");
    178             assertEquals("value", bundle.getString(null, "defaultValue"));
    179         } finally {
    180             Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    181                     previousApiLevel);
    182         }
    183     }
    184 
    185     @Test
    186     public void testStringApi12NullValue() {
    187         int previousApiLevel = Build.VERSION.SDK_INT;
    188         Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    189                 Build.VERSION_CODES.HONEYCOMB_MR1);
    190 
    191         try {
    192             bundle.putString("key", null);
    193             assertEquals("defaultValue", bundle.getString("key", "defaultValue"));
    194         } finally {
    195             Robolectric.Reflection.setFinalStaticField(Build.VERSION.class, "SDK_INT",
    196                     previousApiLevel);
    197         }
    198     }
    199 
    200     @Test
    201     public void testGetOfWrongType() {
    202         bundle.putFloat("foo", 5f);
    203         assertEquals(0, bundle.getChar("foo"));
    204         assertEquals(null, bundle.getCharArray("foo"));
    205         assertEquals(0, bundle.getInt("foo"));
    206         assertEquals(null, bundle.getIntArray("foo"));
    207         assertEquals(null, bundle.getIntegerArrayList("foo"));
    208         assertEquals(0, bundle.getShort("foo"));
    209         assertEquals(null, bundle.getShortArray("foo"));
    210         assertEquals(false, bundle.getBoolean("foo"));
    211         assertEquals(null, bundle.getBooleanArray("foo"));
    212         assertEquals(0, bundle.getLong("foo"));
    213         assertEquals(null, bundle.getLongArray("foo"));
    214         assertEquals(null, bundle.getFloatArray("foo"));
    215         assertEquals(0, bundle.getDouble("foo"), 0.005);
    216         assertEquals(null, bundle.getDoubleArray("foo"));
    217         assertEquals(null, bundle.getString("foo"));
    218         assertEquals(null, bundle.getStringArray("foo"));
    219         assertEquals(null, bundle.getStringArrayList("foo"));
    220         assertEquals(null, bundle.getBundle("foo"));
    221         assertEquals(null, bundle.getParcelable("foo"));
    222         assertEquals(null, bundle.getParcelableArray("foo"));
    223         assertEquals(null, bundle.getParcelableArrayList("foo"));
    224 
    225         bundle.putInt("foo", 1);
    226         assertEquals(0, bundle.getFloat("foo"), 0.005f);
    227     }
    228 
    229     @Test
    230     public void testRemove() {
    231         bundle.putFloat("foo", 5f);
    232         bundle.putFloat("foo2", 5f);
    233 
    234         bundle.remove("foo");
    235 
    236         assertFalse(bundle.containsKey("foo"));
    237         assertTrue(bundle.containsKey("foo2"));
    238     }
    239 
    240     @Test
    241     public void testClear() {
    242         bundle.putFloat("foo", 5f);
    243 
    244         bundle.clear();
    245 
    246         assertEquals(0, bundle.size());
    247     }
    248 
    249     @Test
    250     public void testIsEmpty() {
    251         assertTrue(bundle.isEmpty());
    252         bundle.putBoolean("foo", true);
    253         assertFalse(bundle.isEmpty());
    254     }
    255 
    256     @Test
    257     public void testStringArray() {
    258         bundle.putStringArray("foo", new String[] { "a" });
    259         Assert.assertArrayEquals(new String[] { "a" }, bundle.getStringArray("foo"));
    260         assertNull(bundle.getStringArray("bar"));
    261     }
    262 
    263     @Test
    264     public void testStringArrayList() {
    265         ArrayList<String> list = new ArrayList<String>();
    266         list.add("a");
    267 
    268         bundle.putStringArrayList("foo", new ArrayList<String>(list));
    269         Assert.assertEquals(list, bundle.getStringArrayList("foo"));
    270         assertNull(bundle.getStringArrayList("bar"));
    271     }
    272 
    273     @Test
    274     public void testIntegerArrayList() {
    275         ArrayList<Integer> list = new ArrayList<Integer>();
    276         list.add(100);
    277 
    278         bundle.putIntegerArrayList("foo", new ArrayList<Integer>(list));
    279         Assert.assertEquals(list, bundle.getIntegerArrayList("foo"));
    280         assertNull(bundle.getIntegerArrayList("bar"));
    281     }
    282 
    283     @Test
    284     public void testBundle() {
    285         Bundle innerBundle = new Bundle();
    286         innerBundle.putInt("int", 7);
    287         bundle.putBundle("bundle", innerBundle);
    288 
    289         assertEquals(innerBundle, bundle.getBundle("bundle"));
    290         assertNull(bundle.getBundle("bar"));
    291     }
    292 
    293     @Test
    294     public void testBooleanArray() {
    295         boolean [] arr = new boolean[] { false, true };
    296         bundle.putBooleanArray("foo", arr);
    297 
    298         assertArrayEquals(arr, bundle.getBooleanArray("foo"));
    299         assertNull(bundle.getBooleanArray("bar"));
    300     }
    301 
    302     @Test
    303     public void testByteArray() {
    304         byte [] arr = new byte[] { 12, 24 };
    305         bundle.putByteArray("foo", arr);
    306 
    307         Assert.assertArrayEquals(arr, bundle.getByteArray("foo"));
    308         assertNull(bundle.getByteArray("bar"));
    309     }
    310 
    311     @Test
    312     public void testCharArray() {
    313         char [] arr = new char[] { 'c', 'j' };
    314         bundle.putCharArray("foo", arr);
    315 
    316         Assert.assertArrayEquals(arr, bundle.getCharArray("foo"));
    317         assertNull(bundle.getCharArray("bar"));
    318     }
    319 
    320     @Test
    321     public void testDoubleArray() {
    322         double [] arr = new double[] { 1.2, 3.4 };
    323         bundle.putDoubleArray("foo", arr);
    324 
    325         assertArrayEquals(arr, bundle.getDoubleArray("foo"));
    326         assertNull(bundle.getDoubleArray("bar"));
    327     }
    328 
    329     @Test
    330     public void testIntArray() {
    331         int [] arr = new int[] { 87, 65 };
    332         bundle.putIntArray("foo", arr);
    333 
    334         Assert.assertArrayEquals(arr, bundle.getIntArray("foo"));
    335         assertNull(bundle.getIntArray("bar"));
    336     }
    337 
    338     @Test
    339     public void testLongArray() {
    340         long [] arr = new long[] { 23, 11 };
    341         bundle.putLongArray("foo", arr);
    342 
    343         Assert.assertArrayEquals(arr, bundle.getLongArray("foo"));
    344         assertNull(bundle.getLongArray("bar"));
    345     }
    346 
    347     @Test
    348     public void testShortArray() {
    349         short [] arr = new short[] { 89, 37 };
    350         bundle.putShortArray("foo", arr);
    351 
    352         Assert.assertArrayEquals(arr, bundle.getShortArray("foo"));
    353         assertNull(bundle.getShortArray("bar"));
    354     }
    355 
    356     @Test
    357     public void testParcelableArray() {
    358         Bundle innerBundle = new Bundle();
    359         innerBundle.putInt("value", 1);
    360         Parcelable[] arr = new Parcelable[] { innerBundle };
    361         bundle.putParcelableArray("foo", arr);
    362 
    363         Assert.assertArrayEquals(arr, bundle.getParcelableArray("foo"));
    364         assertNull(bundle.getParcelableArray("bar"));
    365     }
    366 
    367     @Test
    368     public void testCopyConstructor() {
    369         bundle.putInt("value", 1);
    370         Bundle copiedBundle = new Bundle(bundle);
    371 
    372         Assert.assertEquals(copiedBundle, bundle);
    373     }
    374 
    375     private void assertArrayEquals(double[] expected, double[] actual) {
    376         if (expected != null && actual == null) {
    377             throw new AssertionFailedError();
    378         } else if (expected == null && actual != null) {
    379             throw new AssertionFailedError();
    380         } else {
    381             for (int i = 0; i < expected.length; i++) {
    382                 if (expected[i] != actual[i])
    383                     throw new AssertionFailedError();
    384             }
    385 
    386             if (expected.length != actual.length)
    387                 throw new AssertionFailedError();
    388         }
    389     }
    390 
    391     private void assertArrayEquals(boolean[] expected, boolean[] actual) {
    392         if (expected != null && actual == null) {
    393             throw new AssertionFailedError();
    394         } else if (expected == null && actual != null) {
    395             throw new AssertionFailedError();
    396         } else {
    397             for (int i = 0; i < expected.length; i++) {
    398                 if (expected[i] != actual[i])
    399                     throw new AssertionFailedError();
    400             }
    401 
    402             if (expected.length != actual.length)
    403                 throw new AssertionFailedError();
    404         }
    405     }
    406 }
    407