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 androidx.lifecycle.DEFAULT_ARGS_KEY
20 import androidx.lifecycle.SAVED_STATE_REGISTRY_OWNER_KEY
21 import androidx.lifecycle.SavedStateHandle
22 import androidx.lifecycle.VIEW_MODEL_STORE_OWNER_KEY
23 import androidx.lifecycle.ViewModelStore
24 import androidx.lifecycle.ViewModelStoreOwner
25 import androidx.lifecycle.viewmodel.CreationExtras
26 import androidx.lifecycle.viewmodel.testing.internal.createScenarioExtras
27 import androidx.savedstate.SavedState
28 import androidx.savedstate.SavedStateRegistryOwner
29 
30 /**
31  * Creates a default instance of [CreationExtras] pre-configured with all keys required to use
32  * [SavedStateHandle].
33  *
34  * This function sets up the instance with:
35  * - A fake [SavedStateRegistryOwner] assigned to [SAVED_STATE_REGISTRY_OWNER_KEY].
36  * - A fake [ViewModelStoreOwner] assigned to [VIEW_MODEL_STORE_OWNER_KEY], containing an empty
37  *   [ViewModelStore].
38  */
39 @Suppress("FunctionName")
DefaultCreationExtrasnull40 public fun DefaultCreationExtras(): CreationExtras {
41     return createScenarioExtras()
42 }
43 
44 /**
45  * Creates a default instance of [CreationExtras] pre-configured with all keys required to use
46  * [SavedStateHandle], with the specified [defaultArgs] as the [DEFAULT_ARGS_KEY].
47  *
48  * This function sets up the instance with:
49  * - A fake [SavedStateRegistryOwner] assigned to [SAVED_STATE_REGISTRY_OWNER_KEY].
50  * - A fake [ViewModelStoreOwner] assigned to [VIEW_MODEL_STORE_OWNER_KEY], containing an empty
51  *   [ViewModelStore].
52  */
53 @Suppress("FunctionName")
DefaultCreationExtrasnull54 public fun DefaultCreationExtras(defaultArgs: SavedState): CreationExtras {
55     return createScenarioExtras(defaultArgs = defaultArgs)
56 }
57