1 /*
2  * Copyright 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 androidx.lifecycle.viewmodel.testing
18 
19 import android.os.Bundle
20 import android.os.Parcel
21 import android.os.Parcelable
22 import androidx.core.os.bundleOf
23 import androidx.kruth.assertThat
24 import androidx.kruth.assertThrows
25 import androidx.lifecycle.SavedStateHandle
26 import androidx.lifecycle.ViewModel
27 import androidx.lifecycle.createSavedStateHandle
28 import kotlin.test.Test
29 
30 internal class AndroidViewModelScenarioTest : RobolectricTest() {
31 
32     @Test
recreate_tooLargeExceptionnull33     fun recreate_tooLargeException() {
34         val scenario = viewModelScenario { TestViewModel(handle = createSavedStateHandle()) }
35         scenario.viewModel.handle["key"] = ByteArray(size = 1024 * 1024 + 1) // 1 MB is the max.
36 
37         assertThrows<IllegalStateException> { scenario.recreate() }
38     }
39 
40     @Test
recreate_bundlenull41     fun recreate_bundle() {
42         val expectedParcelable = TestParcelable(value = 1)
43         val scenario = viewModelScenario { TestViewModel(handle = createSavedStateHandle()) }
44         scenario.viewModel.handle["key"] = bundleOf("key" to expectedParcelable)
45 
46         scenario.recreate()
47 
48         val actualBundle = scenario.viewModel.handle.get<Bundle>("key")!!
49         @Suppress("DEPRECATION") val actualParcelable = actualBundle["key"]!!
50         assertThat(actualParcelable).isNotSameInstanceAs(expectedParcelable)
51         assertThat(actualParcelable).isEqualTo(expectedParcelable)
52     }
53 
54     @Test
recreate_parcelablenull55     fun recreate_parcelable() {
56         val expectedParcelable = TestParcelable(value = 1)
57         val scenario = viewModelScenario { TestViewModel(handle = createSavedStateHandle()) }
58         scenario.viewModel.handle["key"] = expectedParcelable
59 
60         scenario.recreate()
61 
62         val actualParcelable = scenario.viewModel.handle.get<TestParcelable>("key")!!
63         assertThat(actualParcelable).isNotSameInstanceAs(expectedParcelable)
64         assertThat(actualParcelable).isEqualTo(expectedParcelable)
65     }
66 
67     private class TestViewModel(val handle: SavedStateHandle = SavedStateHandle()) : ViewModel()
68 
69     private data class TestParcelable(val value: Int) : Parcelable {
70 
describeContentsnull71         override fun describeContents(): Int = 0
72 
73         override fun writeToParcel(dest: Parcel, flags: Int) {
74             dest.writeInt(value)
75         }
76 
77         companion object {
78             @Suppress("unused")
79             @JvmField
80             val CREATOR =
81                 object : Parcelable.Creator<TestParcelable> {
createFromParcelnull82                     override fun createFromParcel(source: Parcel) =
83                         TestParcelable(value = source.readInt())
84 
85                     override fun newArray(size: Int) = arrayOfNulls<TestParcelable>(size)
86                 }
87         }
88     }
89 }
90