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