Merge pull request #113 from fknives/issue#107-resolve-state-restoration

Issue#107 resolve state restoration
This commit is contained in:
Gergely Hegedis 2022-07-19 12:26:31 +03:00 committed by GitHub
commit 8f8f44f980
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View file

@ -175,6 +175,7 @@ class AuthComposeInstrumentedTest : KoinTest {
.assertPassword("banan") .assertPassword("banan")
stateRestorationTester.emulateSavedInstanceStateRestore() stateRestorationTester.emulateSavedInstanceStateRestore()
composeTestRule.mainClock.advanceTimeBy(SPLASH_DELAY) // ensure all time based operation run
navigationRobot.assertAuthScreen() navigationRobot.assertAuthScreen()
robot.assertUsername("alma") robot.assertUsername("alma")

View file

@ -24,9 +24,11 @@ fun AppNavigation(isUserLogeInUseCase: IsUserLoggedInUseCase = get()) {
val navController = rememberNavController() val navController = rememberNavController()
LaunchedEffect(isUserLogeInUseCase) { LaunchedEffect(isUserLogeInUseCase) {
val loginStateRoute = if (isUserLogeInUseCase.invoke()) RouteTag.HOME else RouteTag.AUTH
if (navController.currentDestination?.route == loginStateRoute) return@LaunchedEffect
delay(500) delay(500)
navController.navigate( navController.navigate(
route = if (isUserLogeInUseCase.invoke()) RouteTag.HOME else RouteTag.AUTH, route = loginStateRoute,
navOptions = NavOptions.Builder().setPopUpTo(route = RouteTag.SPLASH, inclusive = true).build() navOptions = NavOptions.Builder().setPopUpTo(route = RouteTag.SPLASH, inclusive = true).build()
) )
} }

View file

@ -184,7 +184,7 @@ mockServerScenarioSetup.setScenario(
Then we wait a bit, more precisely we wait for the app to navigate us correctly to AuthScreen since we're not logged in: Then we wait a bit, more precisely we wait for the app to navigate us correctly to AuthScreen since we're not logged in:
```kotlin ```kotlin
composeTestRule.mainClock.advanceTimeBy(510L) composeTestRule.mainClock.advanceTimeBy(600L)
``` ```
We assert that we are indeed on the correct screen We assert that we are indeed on the correct screen
@ -227,7 +227,7 @@ Next up we verify what happens if the user doesn't set their password. We don't
First we check that we are in the write place: First we check that we are in the write place:
```kotlin ```kotlin
composeTestRule.mainClock.advanceTimeBy(510L) composeTestRule.mainClock.advanceTimeBy(600L)
navigationRobot.assertAuthScreen() navigationRobot.assertAuthScreen()
``` ```
@ -252,7 +252,7 @@ This will be really similar as the previous test, so try to do it on your own. T
Still, here is the complete code: Still, here is the complete code:
```kotlin ```kotlin
composeTestRule.mainClock.advanceTimeBy(510L) composeTestRule.mainClock.advanceTimeBy(600L)
navigationRobot.assertAuthScreen() navigationRobot.assertAuthScreen()
robot robot
@ -277,7 +277,7 @@ mockServerScenarioSetup.setScenario(
Now input the credentials and fire the event: Now input the credentials and fire the event:
```kotlin ```kotlin
composeTestRule.mainClock.advanceTimeBy(510L) composeTestRule.mainClock.advanceTimeBy(600L)
navigationRobot.assertAuthScreen() navigationRobot.assertAuthScreen()
robot.setUsername("alma") robot.setUsername("alma")
.setPassword("banan") .setPassword("banan")
@ -310,7 +310,7 @@ mockServerScenarioSetup.setScenario(
AuthScenario.GenericError(username = "alma", password = "banan") AuthScenario.GenericError(username = "alma", password = "banan")
) )
composeTestRule.mainClock.advanceTimeBy(510L) composeTestRule.mainClock.advanceTimeBy(600L)
navigationRobot.assertAuthScreen() navigationRobot.assertAuthScreen()
robot.setUsername("alma") robot.setUsername("alma")
.setPassword("banan") .setPassword("banan")
@ -342,8 +342,10 @@ Then in `setup()`, we need to `setContent` on `stateRestorationTester` instead o
Now for the actual test, we first setup the content then we trigger restoration by calling `stateRestorationTester.emulateSavedInstanceStateRestore()`, afterwards we can verify that the content is recreated in the correct way: Now for the actual test, we first setup the content then we trigger restoration by calling `stateRestorationTester.emulateSavedInstanceStateRestore()`, afterwards we can verify that the content is recreated in the correct way:
> Note: We also add the time advancement, to ensure no time based effect messes up anything!
```kotlin ```kotlin
composeTestRule.mainClock.advanceTimeBy(510L) composeTestRule.mainClock.advanceTimeBy(600L)
navigationRobot.assertAuthScreen() navigationRobot.assertAuthScreen()
robot.setUsername("alma") robot.setUsername("alma")
.setPassword("banan") .setPassword("banan")
@ -351,6 +353,7 @@ robot.setUsername("alma")
.assertPassword("banan") .assertPassword("banan")
stateRestorationTester.emulateSavedInstanceStateRestore() stateRestorationTester.emulateSavedInstanceStateRestore()
composeTestRule.mainClock.advanceTimeBy(600L)
navigationRobot.assertAuthScreen() navigationRobot.assertAuthScreen()
robot.assertUsername("alma") robot.assertUsername("alma")