OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.components.background_task_scheduler; |
| 6 |
| 7 import android.annotation.TargetApi; |
| 8 import android.os.Build; |
| 9 import android.os.Bundle; |
| 10 import android.os.PersistableBundle; |
| 11 import android.support.test.filters.SmallTest; |
| 12 import android.test.InstrumentationTestCase; |
| 13 |
| 14 import org.chromium.base.test.util.MinAndroidSdkLevel; |
| 15 |
| 16 import java.util.ArrayList; |
| 17 import java.util.Arrays; |
| 18 import java.util.Set; |
| 19 |
| 20 /** |
| 21 * Tests for {@link BundleToPersistableBundleConverter}. |
| 22 */ |
| 23 @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1) |
| 24 @MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1) |
| 25 public class BundleToPersistableBundleConverterTest extends InstrumentationTestC
ase { |
| 26 @SmallTest |
| 27 public void testAllValidConversions() { |
| 28 Bundle bundle = new Bundle(); |
| 29 bundle.putString("s", "bar"); |
| 30 bundle.putStringArray("sa", new String[] {"b", "a", "r"}); |
| 31 bundle.putBoolean("b", true); |
| 32 bundle.putBooleanArray("ba", new boolean[] {true, false, true}); |
| 33 bundle.putInt("i", 1342543); |
| 34 bundle.putIntArray("ia", new int[] {1, 2, 3}); |
| 35 bundle.putLong("l", 1342543L); |
| 36 bundle.putLongArray("la", new long[] {1L, 2L, 3L}); |
| 37 bundle.putDouble("d", 5.3223); |
| 38 bundle.putDoubleArray("da", new double[] {5.3223, 42.42}); |
| 39 |
| 40 BundleToPersistableBundleConverter.Result result = |
| 41 BundleToPersistableBundleConverter.convert(bundle); |
| 42 PersistableBundle pBundle = result.getPersistableBundle(); |
| 43 |
| 44 assertFalse(result.hasErrors()); |
| 45 assertEquals(bundle.getString("s"), pBundle.getString("s")); |
| 46 assertTrue(Arrays.equals(bundle.getStringArray("sa"), pBundle.getStringA
rray("sa"))); |
| 47 assertEquals(bundle.getBoolean("b"), pBundle.getBoolean("b")); |
| 48 assertTrue(Arrays.equals(bundle.getBooleanArray("ba"), pBundle.getBoolea
nArray("ba"))); |
| 49 assertEquals(bundle.getInt("i"), pBundle.getInt("i")); |
| 50 assertTrue(Arrays.equals(bundle.getIntArray("ia"), pBundle.getIntArray("
ia"))); |
| 51 assertEquals(bundle.getLong("l"), pBundle.getLong("l")); |
| 52 assertTrue(Arrays.equals(bundle.getLongArray("la"), pBundle.getLongArray
("la"))); |
| 53 assertEquals(bundle.getDouble("d"), pBundle.getDouble("d")); |
| 54 assertTrue(Arrays.equals(bundle.getDoubleArray("da"), pBundle.getDoubleA
rray("da"))); |
| 55 } |
| 56 |
| 57 @SmallTest |
| 58 public void testSomeBadConversions() { |
| 59 Bundle bundle = new Bundle(); |
| 60 bundle.putString("s", "this should be there"); |
| 61 bundle.putByte("byte", (byte) 0x30); |
| 62 bundle.putFloat("float", 14.04F); |
| 63 ArrayList<String> arrayList = new ArrayList<>(); |
| 64 arrayList.add("a"); |
| 65 arrayList.add("b"); |
| 66 bundle.putStringArrayList("arrayList", arrayList); |
| 67 |
| 68 BundleToPersistableBundleConverter.Result result = |
| 69 BundleToPersistableBundleConverter.convert(bundle); |
| 70 |
| 71 assertTrue(result.hasErrors()); |
| 72 Set<String> failedKeys = result.getFailedKeys(); |
| 73 assertEquals(3, failedKeys.size()); |
| 74 assertTrue(failedKeys.contains("byte")); |
| 75 assertTrue(failedKeys.contains("float")); |
| 76 assertTrue(failedKeys.contains("arrayList")); |
| 77 assertEquals(bundle.getString("s"), result.getPersistableBundle().getStr
ing("s")); |
| 78 } |
| 79 |
| 80 @SmallTest |
| 81 public void testNullValue() { |
| 82 Bundle bundle = new Bundle(); |
| 83 bundle.putString("foo", "value1"); |
| 84 bundle.putString("bar", ""); |
| 85 bundle.putString("qux", null); |
| 86 |
| 87 BundleToPersistableBundleConverter.Result result = |
| 88 BundleToPersistableBundleConverter.convert(bundle); |
| 89 |
| 90 assertFalse(result.hasErrors()); |
| 91 assertEquals(bundle.getString("foo"), result.getPersistableBundle().getS
tring("foo")); |
| 92 assertEquals(bundle.getString("bar"), result.getPersistableBundle().getS
tring("bar")); |
| 93 assertEquals(bundle.getString("qux"), result.getPersistableBundle().getS
tring("qux")); |
| 94 } |
| 95 } |
OLD | NEW |