Wait for idling resources

This commit is contained in:
Alex Gabor 2022-03-01 17:35:43 +02:00
parent 4feb92d4ed
commit 6ddd057e66
3 changed files with 11 additions and 14 deletions

View file

@ -5,8 +5,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
import org.fnives.test.showcase.testutils.MockServerScenarioSetupResetingTestRule import org.fnives.test.showcase.testutils.MockServerScenarioSetupResetingTestRule
import org.fnives.test.showcase.testutils.idling.MainDispatcherTestRule import org.fnives.test.showcase.testutils.idling.MainDispatcherTestRule
import org.fnives.test.showcase.testutils.idling.anyResourceIdling
import org.fnives.test.showcase.ui.compose.ComposeActivity import org.fnives.test.showcase.ui.compose.ComposeActivity
import org.fnives.test.showcase.ui.compose.TestShowCaseApp
import org.junit.Before import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
@ -32,12 +32,10 @@ class AuthComposeInstrumentedTest : KoinTest {
val ruleOrder: RuleChain = RuleChain.outerRule(mockServerScenarioSetupTestRule) val ruleOrder: RuleChain = RuleChain.outerRule(mockServerScenarioSetupTestRule)
.around(mainDispatcherTestRule) .around(mainDispatcherTestRule)
@Before @Before
fun setup() { fun setup() {
robot = ComposeLoginRobot(composeTestRule) robot = ComposeLoginRobot(composeTestRule)
composeTestRule.setContent {
TestShowCaseApp()
}
} }
// @After // @After
@ -51,16 +49,18 @@ class AuthComposeInstrumentedTest : KoinTest {
mockServerScenarioSetup.setScenario( mockServerScenarioSetup.setScenario(
AuthScenario.Success(password = "alma", username = "banan") AuthScenario.Success(password = "alma", username = "banan")
) )
composeTestRule.mainClock.advanceTimeBy(500L)
composeTestRule.mainClock.advanceTimeUntil { anyResourceIdling() }
composeTestRule.waitForIdle() composeTestRule.waitForIdle()
robot robot
.setPassword("alma") .setPassword("alma")
.setUsername("banan") .setUsername("banan")
.assertPassword("alma")
.assertUsername("banan") .assertUsername("banan")
.assertPassword("alma")
.clickOnLogin() .clickOnLogin()
// .assertLoadingBeforeRequests() // .assertLoadingBeforeRequests()
mainDispatcherTestRule.advanceUntilIdleWithIdlingResources() // mainDispatcherTestRule.advanceUntilIdleWithIdlingResources()
// robot.assertNavigatedToHome() // robot.assertNavigatedToHome()
} }

View file

@ -1,10 +1,7 @@
package org.fnives.test.showcase.ui package org.fnives.test.showcase.ui
import androidx.compose.ui.test.assertTextEquals import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.ComposeTestRule import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import org.fnives.test.showcase.ui.compose.screen.auth.AuthScreenTag import org.fnives.test.showcase.ui.compose.screen.auth.AuthScreenTag
class ComposeLoginRobot( class ComposeLoginRobot(
@ -20,11 +17,11 @@ class ComposeLoginRobot(
} }
fun assertPassword(password: String): ComposeLoginRobot = apply { fun assertPassword(password: String): ComposeLoginRobot = apply {
composeTestRule.onNodeWithTag(AuthScreenTag.PasswordInput).assertTextEquals(password) composeTestRule.onNodeWithTag(AuthScreenTag.PasswordInput).assertTextContains(password)
} }
fun assertUsername(username: String): ComposeLoginRobot = apply { fun assertUsername(username: String): ComposeLoginRobot = apply {
composeTestRule.onNodeWithTag(AuthScreenTag.UsernameInput).assertTextEquals(username) composeTestRule.onNodeWithTag(AuthScreenTag.UsernameInput).assertTextContains(username)
} }
fun clickOnLogin(): ComposeLoginRobot = apply { fun clickOnLogin(): ComposeLoginRobot = apply {

View file

@ -22,13 +22,13 @@ class MainDispatcherTestRule : TestRule {
@Throws(Throwable::class) @Throws(Throwable::class)
override fun evaluate() { override fun evaluate() {
val dispatcher = StandardTestDispatcher() val dispatcher = StandardTestDispatcher()
Dispatchers.setMain(dispatcher) // Dispatchers.setMain(dispatcher)
testDispatcher = dispatcher testDispatcher = dispatcher
DatabaseInitialization.dispatcher = dispatcher DatabaseInitialization.dispatcher = dispatcher
try { try {
base.evaluate() base.evaluate()
} finally { } finally {
Dispatchers.resetMain() // Dispatchers.resetMain()
} }
} }
} }