ViewModelScenario
- Common/All
-
Cmn
class ViewModelScenario<VM : ViewModel>
ViewModelScenario
provides API to start and drive a ViewModel
's lifecycle state for testing.
ViewModelScenario.recreate
allows you to simulate a System Process Death and restoration.
ViewModelScenario
does not clean up the ViewModel
automatically. Call close
in your test to clean up the state or use AutoCloseable.use
to ensure ViewModelStore.clear
and ViewModel.onCleared
is invoked.
Summary
Public functions
recreate
fun recreate(): Unit
Simulates a System Process Death recreating the ViewModel
and all associated components.
This method:
-
Saves the state of the
ViewModel
withSavedStateRegistryController.performSave
. -
Recreates the
ViewModelStoreOwner
,LifecycleOwner
andSavedStateRegistryOwner
. -
Recreates the
ViewModelProvider
instance.
Call this method to verify that the ViewModel
correctly preserves and restores its state.
Public properties
viewModel
val viewModel: VM
The current ViewModel
being managed by this scenario. This instance is change if the ViewModelStore
is cleared or if recreate
is invoked.