0

我正在使用 Android Studio 北极狐,并尝试测试使用 rememberSaveable 存储的变量的行为。我通过在模拟器中执行“向左旋转”手动进行了测试。那里的行为与预期的一样:使用存储的值remember消失了。使用存储的值rememberSaveable仍然可用。现在我想摆脱手动测试并创建一个 Instrumented 测试。但是我没有得到预期的结果。在测试中,这两个值都显示在我的输出文本字段中。我试图通过添加来获得正确的行为,waitForIdleSync()但它没有帮助。

我认为这可能与“设置”方向有关。当我在测试用例中遗漏的方向发生变化时,系统可能会调用一种方法。但我不知道我错过了什么方法以及如何在我的测试中调用该方法。

@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")
    }
}

4

0 回答 0