1 /* 2 * Copyright (C) 2024 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 17 package com.android.intentresolver.ext 18 19 import android.graphics.Point 20 import androidx.core.os.bundleOf 21 import androidx.lifecycle.DEFAULT_ARGS_KEY 22 import androidx.lifecycle.viewmodel.CreationExtras 23 import androidx.lifecycle.viewmodel.MutableCreationExtras 24 import androidx.test.ext.truth.os.BundleSubject.assertThat 25 import org.junit.Test 26 27 class CreationExtrasExtTest { 28 @Test addDefaultArgs_addsWhenAbsentnull29 fun addDefaultArgs_addsWhenAbsent() { 30 val creationExtras: CreationExtras = MutableCreationExtras() // empty 31 32 val updated = creationExtras.addDefaultArgs("POINT" to Point(1, 1)) 33 34 val defaultArgs = updated[DEFAULT_ARGS_KEY] 35 assertThat(defaultArgs).containsKey("POINT") 36 assertThat(defaultArgs).parcelable<Point>("POINT").marshallsEquallyTo(Point(1, 1)) 37 } 38 39 @Test addDefaultArgs_addsToExistingnull40 fun addDefaultArgs_addsToExisting() { 41 val creationExtras: CreationExtras = 42 MutableCreationExtras().apply { 43 set(DEFAULT_ARGS_KEY, bundleOf("POINT1" to Point(1, 1))) 44 } 45 46 val updated = creationExtras.addDefaultArgs("POINT2" to Point(2, 2)) 47 48 val defaultArgs = updated[DEFAULT_ARGS_KEY] 49 assertThat(defaultArgs).containsKey("POINT1") 50 assertThat(defaultArgs).containsKey("POINT2") 51 assertThat(defaultArgs).parcelable<Point>("POINT1").marshallsEquallyTo(Point(1, 1)) 52 assertThat(defaultArgs).parcelable<Point>("POINT2").marshallsEquallyTo(Point(2, 2)) 53 } 54 } 55