From cf0c26e65bddb7cddb2444e99d7af6e52b8d578b Mon Sep 17 00:00:00 2001 From: Debbie Mong'are Date: Fri, 14 Jul 2023 16:28:32 +0300 Subject: [PATCH 1/3] Add LoginScreen tests to verify different error states --- .../quest/ui/login/LoginScreenTest.kt | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt index 60f0dfd2bf..7b798f7005 100644 --- a/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt +++ b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt @@ -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 { @@ -52,6 +56,8 @@ class LoginScreenTest { loginConfig = LoginConfig(showLogo = true), ) + private val context = InstrumentationRegistry.getInstrumentation().targetContext + @Test fun testLoginPage() { composeRule.setContent { @@ -110,4 +116,119 @@ 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() + } } From 2c419ca06f0f89d1ccfcc9d4c973f8f82578e755 Mon Sep 17 00:00:00 2001 From: pld Date: Fri, 14 Jul 2023 13:12:47 -0400 Subject: [PATCH 2/3] spotless --- .../quest/ui/login/LoginScreenTest.kt | 66 +++++++++++++------ 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt index 7b798f7005..f860694843 100644 --- a/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt +++ b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt @@ -118,31 +118,46 @@ class LoginScreenTest { } @Test - fun testLoginFailsWithUnknownTextErrorMessage (){ - verifyUnknownTextErrorMessage(LoginErrorState.UNKNOWN_HOST, R.string.login_call_fail_error_message ) + 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 ) + 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) + 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) + 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) + fun testLoginFailsWithInvalidOfflineStateErrorMessage() { + verifyInvalidOfflineState( + LoginErrorState.INVALID_OFFLINE_STATE, + R.string.invalid_offline_login_state + ) } - private fun verifyUnknownTextErrorMessage(loginErrorState: LoginErrorState, errorMessageId: Int){ + private fun verifyUnknownTextErrorMessage(loginErrorState: LoginErrorState, errorMessageId: Int) { composeRule.setContent { LoginPage( applicationConfiguration = applicationConfiguration, @@ -157,10 +172,14 @@ class LoginScreenTest { ) } composeRule - .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))).assertIsDisplayed() + .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))) + .assertIsDisplayed() } - private fun verifyInvalidCredentialsErrorMessage(loginErrorState: LoginErrorState, errorMessageId: Int){ + private fun verifyInvalidCredentialsErrorMessage( + loginErrorState: LoginErrorState, + errorMessageId: Int + ) { composeRule.setContent { LoginPage( applicationConfiguration = applicationConfiguration, @@ -175,10 +194,14 @@ class LoginScreenTest { ) } composeRule - .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))).assertIsDisplayed() + .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))) + .assertIsDisplayed() } - private fun verifyMultiUserLoginErrorMessage(loginErrorState: LoginErrorState, errorMessageId: Int){ + private fun verifyMultiUserLoginErrorMessage( + loginErrorState: LoginErrorState, + errorMessageId: Int + ) { composeRule.setContent { LoginPage( applicationConfiguration = applicationConfiguration, @@ -193,10 +216,11 @@ class LoginScreenTest { ) } composeRule - .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))).assertIsDisplayed() + .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))) + .assertIsDisplayed() } - private fun verifyErrorFetchingUser(loginErrorState: LoginErrorState, errorMessageId: Int){ + private fun verifyErrorFetchingUser(loginErrorState: LoginErrorState, errorMessageId: Int) { composeRule.setContent { LoginPage( applicationConfiguration = applicationConfiguration, @@ -211,10 +235,11 @@ class LoginScreenTest { ) } composeRule - .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))).assertIsDisplayed() + .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))) + .assertIsDisplayed() } - private fun verifyInvalidOfflineState(loginErrorState: LoginErrorState, errorMessageId: Int){ + private fun verifyInvalidOfflineState(loginErrorState: LoginErrorState, errorMessageId: Int) { composeRule.setContent { LoginPage( applicationConfiguration = applicationConfiguration, @@ -229,6 +254,7 @@ class LoginScreenTest { ) } composeRule - .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))).assertIsDisplayed() + .onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId))) + .assertIsDisplayed() } } From f752b2594628ae382a6b62c1e3781f08b30e38ee Mon Sep 17 00:00:00 2001 From: pld Date: Fri, 14 Jul 2023 13:19:47 -0400 Subject: [PATCH 3/3] spotless --- .../fhircore/quest/ui/login/LoginScreenTest.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt index f860694843..9b690b7142 100644 --- a/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt +++ b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/ui/login/LoginScreenTest.kt @@ -121,7 +121,7 @@ class LoginScreenTest { fun testLoginFailsWithUnknownTextErrorMessage() { verifyUnknownTextErrorMessage( LoginErrorState.UNKNOWN_HOST, - R.string.login_call_fail_error_message + R.string.login_call_fail_error_message, ) } @@ -129,7 +129,7 @@ class LoginScreenTest { fun testLoginFailsWithInvalidCredentialsErrorMessage() { verifyInvalidCredentialsErrorMessage( LoginErrorState.INVALID_CREDENTIALS, - R.string.invalid_login_credentials + R.string.invalid_login_credentials, ) } @@ -137,7 +137,7 @@ class LoginScreenTest { fun testLoginFailsWithMultiUserLoginErrorMessage() { verifyMultiUserLoginErrorMessage( LoginErrorState.MULTI_USER_LOGIN_ATTEMPT, - R.string.multi_user_login_attempt + R.string.multi_user_login_attempt, ) } @@ -145,7 +145,7 @@ class LoginScreenTest { fun testLoginFailsWithErrorFetchingUserMessage() { verifyErrorFetchingUser( LoginErrorState.ERROR_FETCHING_USER, - R.string.error_fetching_user_details + R.string.error_fetching_user_details, ) } @@ -153,7 +153,7 @@ class LoginScreenTest { fun testLoginFailsWithInvalidOfflineStateErrorMessage() { verifyInvalidOfflineState( LoginErrorState.INVALID_OFFLINE_STATE, - R.string.invalid_offline_login_state + R.string.invalid_offline_login_state, ) } @@ -178,7 +178,7 @@ class LoginScreenTest { private fun verifyInvalidCredentialsErrorMessage( loginErrorState: LoginErrorState, - errorMessageId: Int + errorMessageId: Int, ) { composeRule.setContent { LoginPage( @@ -200,7 +200,7 @@ class LoginScreenTest { private fun verifyMultiUserLoginErrorMessage( loginErrorState: LoginErrorState, - errorMessageId: Int + errorMessageId: Int, ) { composeRule.setContent { LoginPage(