Photo by Mark Boss on Unsplash

Configuration changes in Android can lead to unexpected behavior and data loss, making testing a critical aspect of app development. In this article, we will explore an example of testing configuration changes using the StateRestorationTester.

Configuration changes occur when the device’s configuration, such as orientation, locale, or screen size, is changed. Android handles configuration changes by restarting the activity, which can lead to loss of state if not managed correctly. To deliver a seamless user experience, your app should be able to handle these changes without disruptions.

Here we have two variables: saveableCount and count. While they both track the number of clicks, they demonstrate distinct behavior when it comes to configuration changes.

  • saveableCount: using the rememberSaveable , this variable is designed to withstand both configuration changes and recompositions.
  • count: using the remember , this variable is designed to withstand recompositions.

Now let’s explore the process of testing the behavior of our ClickCounter component during configuration changes using the StateRestorationTester.

In this example, we’re simulating a configuration change by using the “emulateSavedInstanceStateRestore” method. The test subsequently asserts that the saveable variable is preserved across the configuration change, while the non-saveable one is not.

You can find the complete code for this project on GitHub:

Happy coding! 👨‍💻

Source link