I am using Android Studio Arctic fox and I try to test the behaviour of a variable that is stored using rememberSaveable. I did the test manualy by doing "Rotate Left" in Emulator. There the behaviour is as expected : value stored using remember
is gone. value stored using rememberSaveable
is still available.
Now I want to get rid of manually testing and create an Instrumented test. But there I don't get the expected result.
In the test both values are displayed in my output-Textfield.
I tried to get right behaviour by adding waitForIdleSync()
but it did not help.
I think it could have to do with "setting" the orientation. Maybe there is a method that is called by system when the orientation changes that I miss in my test case. But I have no clue what method I miss and how to call that method in my test.
@RunWith(AndroidJUnit4::class)
class SavingTurnTest {
@get:Rule
val composeTestRule = createComposeRule()
@Before
fun setUp(){
composeTestRule.setContent{
MyApplicationTheme {
SampleRemember_saveable()
}
}
composeTestRule.onNodeWithTag("remember").performTextInput("Text_Remember")
composeTestRule.onNodeWithTag("saveable").performTextInput("Text_Saveable")
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}
@Test
fun saveTextOnTurn(){
uiAutomation.setRotation(ROTATION_FREEZE_0)
uiAutomation.setRotation(ROTATION_UNFREEZE)
uiAutomation.setRotation(ROTATION_FREEZE_90)
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
// The next Line should fail, but test passes
// composeTestRule.onNodeWithTag("output").assertTextContains("Text_Remember")
// This test is ok
composeTestRule.onNodeWithTag("output").assertTextContains("Text_Saveable")
// The next Line should pass, but test fails
composeTestRule.onNodeWithTag("output").assertTextEquals("Text_Saveable")
}
}