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

open Unit

Finishes the managed ViewModel and clear the ViewModelStore.

Cmn
Unit

Simulates a System Process Death recreating the ViewModel and all associated components.

Cmn

Public properties

VM

The current ViewModel being managed by this scenario.

Cmn

Public functions

close

open fun close(): Unit

Finishes the managed ViewModel and clear the ViewModelStore.

recreate

fun recreate(): Unit

Simulates a System Process Death recreating the ViewModel and all associated components.

This method:

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.