ViewModelScenario
public final class ViewModelScenario<VM extends 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 methods |
|
|---|---|
void |
close()Finishes the managed |
final @NonNull VM |
The current |
final void |
recreate()Simulates a System Process Death recreating the |
Public methods
close
public void close()
Finishes the managed ViewModel and clear the ViewModelStore.
getViewModel
public final @NonNull VM getViewModel()
The current ViewModel being managed by this scenario. This instance is change if the ViewModelStore is cleared or if recreate is invoked.
recreate
public final void recreate()
Simulates a System Process Death recreating the ViewModel and all associated components.
This method:
-
Saves the state of the
ViewModelwithSavedStateRegistryController.performSave. -
Recreates the
ViewModelStoreOwner,LifecycleOwnerandSavedStateRegistryOwner. -
Recreates the
ViewModelProviderinstance.
Call this method to verify that the ViewModel correctly preserves and restores its state.