{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

StateRestorationTester

@ExperimentalTestApi
public final class StateRestorationTester


Helps to test the state restoration for your Composable component.

Instead of calling ComposeUiTest.setContent you need to use setContent on this object, then change your state so there is some change to be restored, then execute emulateSaveAndRestore and assert your state is restored properly.

Note that this only tests the restoration of the local state of the composable you passed to setContent and it is useful for testing uses of androidx.compose.runtime.saveable.rememberSaveable. It is not testing the integration with app and/or platform specific lifecycles.

Summary

Public constructors

Public methods

final void

Emulates a save and restore cycle of the current composition.

final void
setContent(@Composable @NonNull Function0<Unit> composable)

This functions is a direct replacement for ComposeUiTest.setContent if you are going to use emulateSaveAndRestore in the test.

Public constructors

StateRestorationTester

public StateRestorationTester(@NonNull ComposeUiTest composeTest)

Public methods

emulateSaveAndRestore

public final void emulateSaveAndRestore()

Emulates a save and restore cycle of the current composition. First all state that is remembered with androidx.compose.runtime.saveable.rememberSaveable is stored, then the current composition is disposed, and finally the composition is composed again. This allows you to test how your component behaves when state restoration is happening. Note that state stored via remember will be lost.

setContent

public final void setContent(@Composable @NonNull Function0<Unit> composable)

This functions is a direct replacement for ComposeUiTest.setContent if you are going to use emulateSaveAndRestore in the test.

See also
setContent