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

Finishes the managed ViewModel and clear the ViewModelStore.

final @NonNull VM

The current ViewModel being managed by this scenario.

final void

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

Public methods

close

Added in 2.9.0-alpha08
public void close()

Finishes the managed ViewModel and clear the ViewModelStore.

getViewModel

Added in 2.9.0-alpha08
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

Added in 2.9.0-alpha08
public final void recreate()

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.