Index: components/background_task_scheduler/android/javatests/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverterTest.java |
diff --git a/components/background_task_scheduler/android/javatests/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverterTest.java b/components/background_task_scheduler/android/javatests/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverterTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4f46d54d15bfcef16eb40fc8b4469d4b5f0c93c0 |
--- /dev/null |
+++ b/components/background_task_scheduler/android/javatests/src/org/chromium/components/background_task_scheduler/BundleToPersistableBundleConverterTest.java |
@@ -0,0 +1,95 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.components.background_task_scheduler; |
+ |
+import android.annotation.TargetApi; |
+import android.os.Build; |
+import android.os.Bundle; |
+import android.os.PersistableBundle; |
+import android.support.test.filters.SmallTest; |
+import android.test.InstrumentationTestCase; |
+ |
+import org.chromium.base.test.util.MinAndroidSdkLevel; |
+ |
+import java.util.ArrayList; |
+import java.util.Arrays; |
+import java.util.Set; |
+ |
+/** |
+ * Tests for {@link BundleToPersistableBundleConverter}. |
+ */ |
+@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) |
+@MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1) |
+public class BundleToPersistableBundleConverterTest extends InstrumentationTestCase { |
+ @SmallTest |
+ public void testAllValidConversions() { |
+ Bundle bundle = new Bundle(); |
+ bundle.putString("s", "bar"); |
+ bundle.putStringArray("sa", new String[] {"b", "a", "r"}); |
+ bundle.putBoolean("b", true); |
+ bundle.putBooleanArray("ba", new boolean[] {true, false, true}); |
+ bundle.putInt("i", 1342543); |
+ bundle.putIntArray("ia", new int[] {1, 2, 3}); |
+ bundle.putLong("l", 1342543L); |
+ bundle.putLongArray("la", new long[] {1L, 2L, 3L}); |
+ bundle.putDouble("d", 5.3223); |
+ bundle.putDoubleArray("da", new double[] {5.3223, 42.42}); |
+ |
+ BundleToPersistableBundleConverter.Result result = |
+ BundleToPersistableBundleConverter.convert(bundle); |
+ PersistableBundle pBundle = result.getPersistableBundle(); |
+ |
+ assertFalse(result.hasErrors()); |
+ assertEquals(bundle.getString("s"), pBundle.getString("s")); |
+ assertTrue(Arrays.equals(bundle.getStringArray("sa"), pBundle.getStringArray("sa"))); |
+ assertEquals(bundle.getBoolean("b"), pBundle.getBoolean("b")); |
+ assertTrue(Arrays.equals(bundle.getBooleanArray("ba"), pBundle.getBooleanArray("ba"))); |
+ assertEquals(bundle.getInt("i"), pBundle.getInt("i")); |
+ assertTrue(Arrays.equals(bundle.getIntArray("ia"), pBundle.getIntArray("ia"))); |
+ assertEquals(bundle.getLong("l"), pBundle.getLong("l")); |
+ assertTrue(Arrays.equals(bundle.getLongArray("la"), pBundle.getLongArray("la"))); |
+ assertEquals(bundle.getDouble("d"), pBundle.getDouble("d")); |
+ assertTrue(Arrays.equals(bundle.getDoubleArray("da"), pBundle.getDoubleArray("da"))); |
+ } |
+ |
+ @SmallTest |
+ public void testSomeBadConversions() { |
+ Bundle bundle = new Bundle(); |
+ bundle.putString("s", "this should be there"); |
+ bundle.putByte("byte", (byte) 0x30); |
+ bundle.putFloat("float", 14.04F); |
+ ArrayList<String> arrayList = new ArrayList<>(); |
+ arrayList.add("a"); |
+ arrayList.add("b"); |
+ bundle.putStringArrayList("arrayList", arrayList); |
+ |
+ BundleToPersistableBundleConverter.Result result = |
+ BundleToPersistableBundleConverter.convert(bundle); |
+ |
+ assertTrue(result.hasErrors()); |
+ Set<String> failedKeys = result.getFailedKeys(); |
+ assertEquals(3, failedKeys.size()); |
+ assertTrue(failedKeys.contains("byte")); |
+ assertTrue(failedKeys.contains("float")); |
+ assertTrue(failedKeys.contains("arrayList")); |
+ assertEquals(bundle.getString("s"), result.getPersistableBundle().getString("s")); |
+ } |
+ |
+ @SmallTest |
+ public void testNullValue() { |
+ Bundle bundle = new Bundle(); |
+ bundle.putString("foo", "value1"); |
+ bundle.putString("bar", ""); |
+ bundle.putString("qux", null); |
+ |
+ BundleToPersistableBundleConverter.Result result = |
+ BundleToPersistableBundleConverter.convert(bundle); |
+ |
+ assertFalse(result.hasErrors()); |
+ assertEquals(bundle.getString("foo"), result.getPersistableBundle().getString("foo")); |
+ assertEquals(bundle.getString("bar"), result.getPersistableBundle().getString("bar")); |
+ assertEquals(bundle.getString("qux"), result.getPersistableBundle().getString("qux")); |
+ } |
+} |