PersistableBundleTest.kt revision 82eef1935cabfba15ac582595c4955183568350e
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.os 18 19import android.support.test.InstrumentationRegistry 20import android.support.test.filters.SdkSuppress 21import android.view.View 22import androidx.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 "boolean" to true, 36 "double" to 1.0, 37 "int" to 1, 38 "long" to 1L, 39 40 "string" to "hey", 41 42 "booleanArray" to booleanArrayOf(), 43 "doubleArray" to doubleArrayOf(), 44 "intArray" to intArrayOf(), 45 "longArray" to longArrayOf(), 46 47 "stringArray" to arrayOf("hey") 48 ) 49 50 assertEquals(11, bundle.size()) 51 52 assertNull(bundle["null"]) 53 54 assertEquals(true, bundle["boolean"]) 55 assertEquals(1.0, bundle["double"]) 56 assertEquals(1, bundle["int"]) 57 assertEquals(1L, bundle["long"]) 58 59 assertEquals("hey", bundle["string"]) 60 61 assertArrayEquals(booleanArrayOf(), bundle["booleanArray"] as BooleanArray) 62 assertArrayEquals(doubleArrayOf(), bundle["doubleArray"] as DoubleArray, 0.0) 63 assertArrayEquals(intArrayOf(), bundle["intArray"] as IntArray) 64 assertArrayEquals(longArrayOf(), bundle["longArray"] as LongArray) 65 66 assertThat(bundle["stringArray"] as Array<*>).asList().containsExactly("hey") 67 } 68 69 @Test fun persistableBundleOfInvalid() { 70 assertThrows<IllegalArgumentException> { 71 persistableBundleOf("nope" to View(InstrumentationRegistry.getContext())) 72 }.hasMessageThat().isEqualTo("Illegal value type android.view.View for key \"nope\"") 73 74 assertThrows<IllegalArgumentException> { 75 persistableBundleOf("nopes" to arrayOf(View(InstrumentationRegistry.getContext()))) 76 }.hasMessageThat().isEqualTo("Illegal value array type android.view.View for key \"nopes\"") 77 } 78} 79