Issue#16 Swap white box logout and login state into ui interaction based
This commit is contained in:
parent
c983949d70
commit
d7cb032ea2
10 changed files with 90 additions and 128 deletions
|
|
@ -0,0 +1,52 @@
|
|||
package org.fnives.test.showcase.testutils.statesetup
|
||||
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.test.core.app.ActivityScenario
|
||||
import org.fnives.test.showcase.network.mockserver.MockServerScenarioSetup
|
||||
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
||||
import org.fnives.test.showcase.testutils.configuration.MainDispatcherTestRule
|
||||
import org.fnives.test.showcase.ui.ActivityClassHolder
|
||||
import org.fnives.test.showcase.ui.home.HomeRobot
|
||||
import org.fnives.test.showcase.ui.login.LoginRobot
|
||||
import org.koin.test.KoinTest
|
||||
|
||||
object SetupAuthenticationState : KoinTest {
|
||||
|
||||
fun setupLogin(
|
||||
mainDispatcherTestRule: MainDispatcherTestRule,
|
||||
mockServerScenarioSetup: MockServerScenarioSetup
|
||||
) {
|
||||
mockServerScenarioSetup.setScenario(
|
||||
AuthScenario.Success(
|
||||
username = "a",
|
||||
password = "b"
|
||||
)
|
||||
)
|
||||
val activityScenario = ActivityScenario.launch(ActivityClassHolder.authActivity().java)
|
||||
activityScenario.moveToState(Lifecycle.State.RESUMED)
|
||||
val loginRobot = LoginRobot()
|
||||
loginRobot.setupIntentResults()
|
||||
loginRobot
|
||||
.setPassword("b")
|
||||
.setUsername("a")
|
||||
.clickOnLogin()
|
||||
|
||||
mainDispatcherTestRule.advanceUntilIdleOrActivityIsDestroyed()
|
||||
|
||||
activityScenario.moveToState(Lifecycle.State.DESTROYED)
|
||||
}
|
||||
|
||||
fun setupLogout(
|
||||
mainDispatcherTestRule: MainDispatcherTestRule
|
||||
) {
|
||||
val activityScenario = ActivityScenario.launch(ActivityClassHolder.mainActivity().java)
|
||||
activityScenario.moveToState(Lifecycle.State.RESUMED)
|
||||
val homeRobot = HomeRobot()
|
||||
homeRobot
|
||||
.clickSignOut()
|
||||
|
||||
mainDispatcherTestRule.advanceUntilIdleOrActivityIsDestroyed()
|
||||
|
||||
activityScenario.moveToState(Lifecycle.State.DESTROYED)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
// package org.fnives.test.showcase.testutils.statesetup
|
||||
//
|
||||
// import kotlinx.coroutines.runBlocking
|
||||
// import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase
|
||||
// import org.fnives.test.showcase.core.login.LoginUseCase
|
||||
// import org.fnives.test.showcase.core.login.LogoutUseCase
|
||||
// import org.fnives.test.showcase.model.auth.LoginCredentials
|
||||
// import org.fnives.test.showcase.network.mockserver.MockServerScenarioSetup
|
||||
// import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
||||
// import org.koin.test.KoinTest
|
||||
// import org.koin.test.get
|
||||
//
|
||||
// object SetupLoggedInState : KoinTest {
|
||||
//
|
||||
// private val logoutUseCase get() = get<LogoutUseCase>()
|
||||
// private val loginUseCase get() = get<LoginUseCase>()
|
||||
// private val isUserLoggedInUseCase get() = get<IsUserLoggedInUseCase>()
|
||||
//
|
||||
// fun setupLogin(mockServerScenarioSetup: MockServerScenarioSetup) {
|
||||
// mockServerScenarioSetup.setScenario(AuthScenario.Success("a", "b"))
|
||||
// runBlocking {
|
||||
// loginUseCase.invoke(LoginCredentials("a", "b"))
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fun isLoggedIn() = isUserLoggedInUseCase.invoke()
|
||||
//
|
||||
// fun setupLogout() {
|
||||
// runBlocking { logoutUseCase.invoke() }
|
||||
// }
|
||||
// }
|
||||
|
|
@ -37,6 +37,10 @@ class LoginRobot(
|
|||
|
||||
override fun init() {
|
||||
Intents.init()
|
||||
setupIntentResults()
|
||||
}
|
||||
|
||||
fun setupIntentResults() {
|
||||
intending(hasComponent(ActivityClassHolder.mainActivity().java.canonicalName))
|
||||
.respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, Intent()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,10 @@ package org.fnives.test.showcase.ui.splash
|
|||
import android.app.Instrumentation
|
||||
import androidx.test.espresso.intent.Intents
|
||||
import androidx.test.espresso.intent.matcher.IntentMatchers
|
||||
import org.fnives.test.showcase.network.mockserver.MockServerScenarioSetup
|
||||
import org.fnives.test.showcase.testutils.configuration.MainDispatcherTestRule
|
||||
import org.fnives.test.showcase.testutils.robot.Robot
|
||||
import org.fnives.test.showcase.testutils.statesetup.SetupAuthenticationState
|
||||
import org.fnives.test.showcase.testutils.viewactions.notIntended
|
||||
import org.fnives.test.showcase.ui.ActivityClassHolder
|
||||
|
||||
|
|
@ -21,6 +24,23 @@ class SplashRobot : Robot {
|
|||
Intents.release()
|
||||
}
|
||||
|
||||
fun setupLoggedInState(
|
||||
mainDispatcherTestRule: MainDispatcherTestRule,
|
||||
mockServerScenarioSetup: MockServerScenarioSetup
|
||||
) {
|
||||
SetupAuthenticationState.setupLogin(mainDispatcherTestRule, mockServerScenarioSetup)
|
||||
release()
|
||||
init()
|
||||
}
|
||||
|
||||
fun setupLoggedOutState(
|
||||
mainDispatcherTestRule: MainDispatcherTestRule
|
||||
) {
|
||||
SetupAuthenticationState.setupLogout(mainDispatcherTestRule)
|
||||
release()
|
||||
init()
|
||||
}
|
||||
|
||||
fun assertHomeIsStarted() = apply {
|
||||
Intents.intended(IntentMatchers.hasComponent(ActivityClassHolder.mainActivity().java.canonicalName))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
package org.fnives.test.showcase.testutils.statesetup
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase
|
||||
import org.fnives.test.showcase.core.login.LoginUseCase
|
||||
import org.fnives.test.showcase.core.login.LogoutUseCase
|
||||
import org.fnives.test.showcase.model.auth.LoginCredentials
|
||||
import org.fnives.test.showcase.network.mockserver.MockServerScenarioSetup
|
||||
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
||||
import javax.inject.Inject
|
||||
|
||||
class SetupLoggedInState @Inject constructor(
|
||||
private val logoutUseCase: LogoutUseCase,
|
||||
private val loginUseCase: LoginUseCase,
|
||||
private val isUserLoggedInUseCase: IsUserLoggedInUseCase
|
||||
) {
|
||||
|
||||
fun setupLogin(mockServerScenarioSetup: MockServerScenarioSetup) {
|
||||
mockServerScenarioSetup.setScenario(AuthScenario.Success("a", "b"))
|
||||
runBlocking {
|
||||
loginUseCase.invoke(LoginCredentials("a", "b"))
|
||||
}
|
||||
}
|
||||
|
||||
fun isLoggedIn() = isUserLoggedInUseCase.invoke()
|
||||
|
||||
fun setupLogout() {
|
||||
runBlocking { logoutUseCase.invoke() }
|
||||
}
|
||||
}
|
||||
|
|
@ -17,9 +17,8 @@ import org.fnives.test.showcase.testutils.idling.NetworkSynchronization
|
|||
import org.fnives.test.showcase.testutils.idling.loopMainThreadFor
|
||||
import org.fnives.test.showcase.testutils.idling.loopMainThreadUntilIdleWithIdlingResources
|
||||
import org.fnives.test.showcase.testutils.robot.RobotTestRule
|
||||
import org.fnives.test.showcase.testutils.statesetup.SetupLoggedInState
|
||||
import org.fnives.test.showcase.testutils.statesetup.SetupAuthenticationState
|
||||
import org.junit.After
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
|
@ -58,9 +57,6 @@ class MainActivityTest {
|
|||
@JvmField
|
||||
val hiltRule = HiltAndroidRule(this)
|
||||
|
||||
@Inject
|
||||
lateinit var setupLoggedInState: SetupLoggedInState
|
||||
|
||||
@Inject
|
||||
lateinit var networkSynchronization: NetworkSynchronization
|
||||
|
||||
|
|
@ -72,8 +68,8 @@ class MainActivityTest {
|
|||
.invoke(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
|
||||
hiltRule.inject()
|
||||
setupLoggedInState.setupLogin(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
disposable = networkSynchronization.registerNetworkingSynchronization()
|
||||
SetupAuthenticationState.setupLogin(mainDispatcherTestRule, mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
@ -94,7 +90,6 @@ class MainActivityTest {
|
|||
mainDispatcherTestRule.advanceUntilIdleOrActivityIsDestroyed()
|
||||
|
||||
homeRobot.assertNavigatedToAuth()
|
||||
Assert.assertEquals(false, setupLoggedInState.isLoggedIn())
|
||||
}
|
||||
|
||||
/** GIVEN success response WHEN data is returned THEN it is shown on the ui */
|
||||
|
|
@ -253,6 +248,5 @@ class MainActivityTest {
|
|||
mainDispatcherTestRule.advanceUntilIdleWithIdlingResources()
|
||||
|
||||
homeRobot.assertNavigatedToAuth()
|
||||
Assert.assertEquals(false, setupLoggedInState.isLoggedIn())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import org.fnives.test.showcase.testutils.configuration.SpecificTestConfiguratio
|
|||
import org.fnives.test.showcase.testutils.idling.Disposable
|
||||
import org.fnives.test.showcase.testutils.idling.NetworkSynchronization
|
||||
import org.fnives.test.showcase.testutils.robot.RobotTestRule
|
||||
import org.fnives.test.showcase.testutils.statesetup.SetupLoggedInState
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
|
|
@ -49,9 +48,6 @@ class SplashActivityTest : KoinTest {
|
|||
@JvmField
|
||||
val hiltRule = HiltAndroidRule(this)
|
||||
|
||||
@Inject
|
||||
lateinit var setupLoggedInState: SetupLoggedInState
|
||||
|
||||
@Inject
|
||||
lateinit var networkSynchronization: NetworkSynchronization
|
||||
|
||||
|
|
@ -74,7 +70,7 @@ class SplashActivityTest : KoinTest {
|
|||
/** GIVEN loggedInState WHEN opened THEN MainActivity is started */
|
||||
@Test
|
||||
fun loggedInStateNavigatesToHome() {
|
||||
setupLoggedInState.setupLogin(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
splashRobot.setupLoggedInState(mainDispatcherTestRule, mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
|
||||
activityScenario = ActivityScenario.launch(HiltSplashActivity::class.java)
|
||||
|
||||
|
|
@ -82,14 +78,12 @@ class SplashActivityTest : KoinTest {
|
|||
|
||||
splashRobot.assertHomeIsStarted()
|
||||
.assertAuthIsNotStarted()
|
||||
|
||||
setupLoggedInState.setupLogout()
|
||||
}
|
||||
|
||||
/** GIVEN loggedOffState WHEN opened THEN AuthActivity is started */
|
||||
@Test
|
||||
fun loggedOutStatesNavigatesToAuthentication() {
|
||||
setupLoggedInState.setupLogout()
|
||||
splashRobot.setupLoggedOutState(mainDispatcherTestRule)
|
||||
|
||||
activityScenario = ActivityScenario.launch(HiltSplashActivity::class.java)
|
||||
|
||||
|
|
@ -101,7 +95,7 @@ class SplashActivityTest : KoinTest {
|
|||
|
||||
@Test
|
||||
fun loggedOutStatesNotEnoughTime() {
|
||||
setupLoggedInState.setupLogout()
|
||||
splashRobot.setupLoggedOutState(mainDispatcherTestRule)
|
||||
|
||||
activityScenario = ActivityScenario.launch(HiltSplashActivity::class.java)
|
||||
|
||||
|
|
@ -114,7 +108,7 @@ class SplashActivityTest : KoinTest {
|
|||
/** GIVEN loggedInState and not enough time WHEN opened THEN no activity is started */
|
||||
@Test
|
||||
fun loggedInStatesNotEnoughTime() {
|
||||
setupLoggedInState.setupLogin(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
splashRobot.setupLoggedInState(mainDispatcherTestRule, mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
|
||||
activityScenario = ActivityScenario.launch(HiltSplashActivity::class.java)
|
||||
|
||||
|
|
@ -122,7 +116,5 @@ class SplashActivityTest : KoinTest {
|
|||
|
||||
splashRobot.assertHomeIsNotStarted()
|
||||
.assertAuthIsNotStarted()
|
||||
|
||||
setupLoggedInState.setupLogout()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
package org.fnives.test.showcase.testutils.statesetup
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase
|
||||
import org.fnives.test.showcase.core.login.LoginUseCase
|
||||
import org.fnives.test.showcase.core.login.LogoutUseCase
|
||||
import org.fnives.test.showcase.model.auth.LoginCredentials
|
||||
import org.fnives.test.showcase.network.mockserver.MockServerScenarioSetup
|
||||
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
||||
import org.koin.test.KoinTest
|
||||
import org.koin.test.get
|
||||
|
||||
object SetupLoggedInState : KoinTest {
|
||||
|
||||
private val logoutUseCase get() = get<LogoutUseCase>()
|
||||
private val loginUseCase get() = get<LoginUseCase>()
|
||||
private val isUserLoggedInUseCase get() = get<IsUserLoggedInUseCase>()
|
||||
|
||||
fun setupLogin(mockServerScenarioSetup: MockServerScenarioSetup) {
|
||||
mockServerScenarioSetup.setScenario(AuthScenario.Success("a", "b"))
|
||||
runBlocking {
|
||||
loginUseCase.invoke(LoginCredentials("a", "b"))
|
||||
}
|
||||
}
|
||||
|
||||
fun isLoggedIn() = isUserLoggedInUseCase.invoke()
|
||||
|
||||
fun setupLogout() {
|
||||
runBlocking { logoutUseCase.invoke() }
|
||||
}
|
||||
}
|
||||
|
|
@ -16,9 +16,8 @@ import org.fnives.test.showcase.testutils.idling.NetworkSynchronization
|
|||
import org.fnives.test.showcase.testutils.idling.loopMainThreadFor
|
||||
import org.fnives.test.showcase.testutils.idling.loopMainThreadUntilIdleWithIdlingResources
|
||||
import org.fnives.test.showcase.testutils.robot.RobotTestRule
|
||||
import org.fnives.test.showcase.testutils.statesetup.SetupLoggedInState
|
||||
import org.fnives.test.showcase.testutils.statesetup.SetupAuthenticationState
|
||||
import org.junit.After
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
|
@ -64,8 +63,10 @@ class MainActivityTest : KoinTest {
|
|||
SpecificTestConfigurationsFactory.createServerTypeConfiguration()
|
||||
.invoke(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
|
||||
SetupLoggedInState.setupLogin(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
disposable = NetworkSynchronization.registerNetworkingSynchronization()
|
||||
SetupAuthenticationState.setupLogin(
|
||||
mainDispatcherTestRule,
|
||||
mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
@ -86,7 +87,6 @@ class MainActivityTest : KoinTest {
|
|||
mainDispatcherTestRule.advanceUntilIdleOrActivityIsDestroyed()
|
||||
|
||||
homeRobot.assertNavigatedToAuth()
|
||||
Assert.assertEquals(false, SetupLoggedInState.isLoggedIn())
|
||||
}
|
||||
|
||||
/** GIVEN success response WHEN data is returned THEN it is shown on the ui */
|
||||
|
|
@ -245,6 +245,5 @@ class MainActivityTest : KoinTest {
|
|||
mainDispatcherTestRule.advanceUntilIdleWithIdlingResources()
|
||||
|
||||
homeRobot.assertNavigatedToAuth()
|
||||
Assert.assertEquals(false, SetupLoggedInState.isLoggedIn())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import org.fnives.test.showcase.testutils.configuration.SpecificTestConfiguratio
|
|||
import org.fnives.test.showcase.testutils.idling.Disposable
|
||||
import org.fnives.test.showcase.testutils.idling.NetworkSynchronization
|
||||
import org.fnives.test.showcase.testutils.robot.RobotTestRule
|
||||
import org.fnives.test.showcase.testutils.statesetup.SetupLoggedInState
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
|
|
@ -59,7 +58,7 @@ class SplashActivityTest : KoinTest {
|
|||
/** GIVEN loggedInState WHEN opened after some time THEN MainActivity is started */
|
||||
@Test
|
||||
fun loggedInStateNavigatesToHome() {
|
||||
SetupLoggedInState.setupLogin(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
splashRobot.setupLoggedInState(mainDispatcherTestRule, mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
|
||||
activityScenario = ActivityScenario.launch(SplashActivity::class.java)
|
||||
activityScenario.moveToState(Lifecycle.State.RESUMED)
|
||||
|
|
@ -68,15 +67,12 @@ class SplashActivityTest : KoinTest {
|
|||
|
||||
splashRobot.assertHomeIsStarted()
|
||||
.assertAuthIsNotStarted()
|
||||
|
||||
SetupLoggedInState.setupLogout()
|
||||
}
|
||||
|
||||
/** GIVEN loggedOffState WHEN opened after some time THEN AuthActivity is started */
|
||||
@Test
|
||||
fun loggedOutStatesNavigatesToAuthentication() {
|
||||
SetupLoggedInState.setupLogout()
|
||||
|
||||
splashRobot.setupLoggedOutState(mainDispatcherTestRule)
|
||||
activityScenario = ActivityScenario.launch(SplashActivity::class.java)
|
||||
activityScenario.moveToState(Lifecycle.State.RESUMED)
|
||||
|
||||
|
|
@ -89,8 +85,7 @@ class SplashActivityTest : KoinTest {
|
|||
/** GIVEN loggedOffState and not enough time WHEN opened THEN no activity is started */
|
||||
@Test
|
||||
fun loggedOutStatesNotEnoughTime() {
|
||||
SetupLoggedInState.setupLogout()
|
||||
|
||||
splashRobot.setupLoggedOutState(mainDispatcherTestRule)
|
||||
activityScenario = ActivityScenario.launch(SplashActivity::class.java)
|
||||
activityScenario.moveToState(Lifecycle.State.RESUMED)
|
||||
|
||||
|
|
@ -103,7 +98,7 @@ class SplashActivityTest : KoinTest {
|
|||
/** GIVEN loggedInState and not enough time WHEN opened THEN no activity is started */
|
||||
@Test
|
||||
fun loggedInStatesNotEnoughTime() {
|
||||
SetupLoggedInState.setupLogin(mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
splashRobot.setupLoggedInState(mainDispatcherTestRule, mockServerScenarioSetupTestRule.mockServerScenarioSetup)
|
||||
|
||||
activityScenario = ActivityScenario.launch(SplashActivity::class.java)
|
||||
activityScenario.moveToState(Lifecycle.State.RESUMED)
|
||||
|
|
@ -112,7 +107,5 @@ class SplashActivityTest : KoinTest {
|
|||
|
||||
splashRobot.assertHomeIsNotStarted()
|
||||
.assertAuthIsNotStarted()
|
||||
|
||||
SetupLoggedInState.setupLogout()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue