Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to verify LoginScreen error states #2589

Merged
merged 6 commits into from
Jul 14, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ package org.smartregister.fhircore.quest.ui.login

import android.view.inputmethod.EditorInfo
import androidx.compose.ui.test.assertHasClickAction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performImeAction
import androidx.compose.ui.test.performTextInput
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Rule
import org.junit.Test
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
import org.smartregister.fhircore.engine.configuration.app.LoginConfig
import org.smartregister.fhircore.quest.R

@ExperimentalCoroutinesApi
class LoginScreenTest {
Expand All @@ -52,6 +56,8 @@ class LoginScreenTest {
loginConfig = LoginConfig(showLogo = true),
)

private val context = InstrumentationRegistry.getInstrumentation().targetContext

@Test
fun testLoginPage() {
composeRule.setContent {
Expand Down Expand Up @@ -110,4 +116,145 @@ class LoginScreenTest {
.performImeAction()
.equals(EditorInfo.IME_ACTION_DONE)
}

@Test
fun testLoginFailsWithUnknownTextErrorMessage() {
verifyUnknownTextErrorMessage(
LoginErrorState.UNKNOWN_HOST,
R.string.login_call_fail_error_message
)
}

@Test
fun testLoginFailsWithInvalidCredentialsErrorMessage() {
verifyInvalidCredentialsErrorMessage(
LoginErrorState.INVALID_CREDENTIALS,
R.string.invalid_login_credentials
)
}

@Test
fun testLoginFailsWithMultiUserLoginErrorMessage() {
verifyMultiUserLoginErrorMessage(
LoginErrorState.MULTI_USER_LOGIN_ATTEMPT,
R.string.multi_user_login_attempt
)
}

@Test
fun testLoginFailsWithErrorFetchingUserMessage() {
verifyErrorFetchingUser(
LoginErrorState.ERROR_FETCHING_USER,
R.string.error_fetching_user_details
)
}

@Test
fun testLoginFailsWithInvalidOfflineStateErrorMessage() {
verifyInvalidOfflineState(
LoginErrorState.INVALID_OFFLINE_STATE,
R.string.invalid_offline_login_state
)
}

private fun verifyUnknownTextErrorMessage(loginErrorState: LoginErrorState, errorMessageId: Int) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyInvalidCredentialsErrorMessage(
loginErrorState: LoginErrorState,
errorMessageId: Int
) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyMultiUserLoginErrorMessage(
loginErrorState: LoginErrorState,
errorMessageId: Int
) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyErrorFetchingUser(loginErrorState: LoginErrorState, errorMessageId: Int) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyInvalidOfflineState(loginErrorState: LoginErrorState, errorMessageId: Int) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}
}