PersistableBundleTest.kt revision 9c80550cbbe357a89e2abeeb9c7769fcaefc3a65
1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *       http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.core.os
18
19import android.support.test.InstrumentationRegistry
20import android.support.test.filters.SdkSuppress
21import android.view.View
22import androidx.testutils.assertThrows
23import com.google.common.truth.Truth.assertThat
24import org.junit.Assert.assertArrayEquals
25import org.junit.Assert.assertEquals
26import org.junit.Assert.assertNull
27import org.junit.Test
28
29@SdkSuppress(minSdkVersion = 21)
30class PersistableBundleTest {
31    @Test fun persistableBundleOfValid() {
32        val bundle = persistableBundleOf(
33            "null" to null,
34
35            "double" to 1.0,
36            "int" to 1,
37            "long" to 1L,
38
39            "string" to "hey",
40
41            "doubleArray" to doubleArrayOf(),
42            "intArray" to intArrayOf(),
43            "longArray" to longArrayOf(),
44
45            "stringArray" to arrayOf("hey")
46        )
47
48        assertEquals(9, bundle.size())
49
50        assertNull(bundle["null"])
51
52        assertEquals(1.0, bundle["double"])
53        assertEquals(1, bundle["int"])
54        assertEquals(1L, bundle["long"])
55
56        assertEquals("hey", bundle["string"])
57
58        assertArrayEquals(doubleArrayOf(), bundle["doubleArray"] as DoubleArray, 0.0)
59        assertArrayEquals(intArrayOf(), bundle["intArray"] as IntArray)
60        assertArrayEquals(longArrayOf(), bundle["longArray"] as LongArray)
61
62        assertThat(bundle["stringArray"] as Array<*>).asList().containsExactly("hey")
63    }
64
65    @SdkSuppress(minSdkVersion = 22)
66    @Test fun persistableBundleOfValidApi22() {
67        val bundle = persistableBundleOf(
68            "boolean" to true,
69            "booleanArray" to booleanArrayOf()
70        )
71
72        assertEquals(true, bundle["boolean"])
73        assertArrayEquals(booleanArrayOf(), bundle["booleanArray"] as BooleanArray)
74    }
75
76    @Test fun persistableBundleOfInvalid() {
77        assertThrows<IllegalArgumentException> {
78            persistableBundleOf("nope" to View(InstrumentationRegistry.getContext()))
79        }.hasMessageThat().isEqualTo("Illegal value type android.view.View for key \"nope\"")
80
81        assertThrows<IllegalArgumentException> {
82            persistableBundleOf("nopes" to arrayOf(View(InstrumentationRegistry.getContext())))
83        }.hasMessageThat().isEqualTo("Illegal value array type android.view.View for key \"nopes\"")
84    }
85}
86