testImplementation "androidx.savedstate:savedstate-testing:1.1.0"
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
@Test
fun testStateRestoration() {
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
val tester = StateRestorationTester(scenario)
// Call methods on your activity to modify its state
// ...
// Save and restore the activity state using the tester
tester.saveInstanceState()
tester.restoreInstanceState()
// Assert that the activity state has been restored correctly
// ...
}
}
}
Call methods on your activity to modify its state (e.g. set a text view text, toggle a switch, etc.)
Use the StateRestorationTester instance to save and restore the activity state.
tester.saveInstanceState()
tester.restoreInstanceState()
// Assert that a text view has the expected text
tester.assertViewState(R.id.textView, "Hello, world!")
// Assert that a switch is checked
tester.assertViewState(R.id.switchView, true)
You can use the other methods provided by the StateRestorationTester class to test other types of view state, including scroll position, checked state, selected item, and text input.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-04-30 11:00:00 +0000
Seen: 12 times
Last updated: Sep 05 '21
What are the typical approaches found in a Gradle build script?
What steps can be taken to increase the runtime of a debugger in NativeScript?
What is the method to show the dependencies in Java?
How can I incorporate BeanComparator into my gradle project?
What does the "Invalid source release 1.7" error mean when using gradle for compilation?