Skip to content

Disable UsernamePassword Login button when fields are empty #523

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

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,26 +3,37 @@ package com.arcgismaps.toolkit.authentication
import android.view.KeyEvent
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.hasImeAction
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.StateRestorationTester
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performImeAction
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.text.input.ImeAction
import androidx.test.platform.app.InstrumentationRegistry
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.httpcore.authentication.ArcGISAuthenticationChallengeResponse
import com.arcgismaps.httpcore.authentication.NetworkAuthenticationChallenge
import com.arcgismaps.httpcore.authentication.NetworkAuthenticationChallengeResponse
import com.arcgismaps.httpcore.authentication.NetworkAuthenticationType
import com.arcgismaps.httpcore.authentication.TokenCredential
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import io.mockk.verify
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceUntilIdle
Expand All @@ -40,7 +51,7 @@ import org.junit.rules.TestRule
*/
class UsernamePasswordTests {

val authenticatorState = AuthenticatorState()
private val authenticatorState = AuthenticatorState()

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
Expand Down Expand Up @@ -84,6 +95,85 @@ class UsernamePasswordTests {
assert(response is NetworkAuthenticationChallengeResponse.ContinueWithCredential)
}

/**
* Given an UsernamePasswordAuthenticator
* When the username or password fields are empty
* Then the login button should be disabled
* And when the username and password fields are both filled
* Then the login button should be enabled
*
* @since 200.5.0
*/
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun loginButtonEnabledState() = runTest {
composeTestRule.setContent {
UsernamePasswordAuthenticator(
UsernamePasswordChallenge(
url = "https://arcgis.com",
onUsernamePasswordReceived = { _, _ -> },
onCancel = {}
)
)
}
advanceUntilIdle()
// verify the login button is disabled when the fields are empty
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.login)).assertIsNotEnabled()
// verify the login button is disabled when only the username field is filled
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.username_label))
.performTextInput("testuser")
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.login)).assertIsNotEnabled()
// verify the login button is disabled when only the password field is filled
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.username_label))
.performTextClearance()
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.password_label))
.performTextInput("testPassword")
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.login)).assertIsNotEnabled()
// verify it is enabled when both are filled
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.username_label))
.performTextInput("testuser")
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.login)).assertIsEnabled()
}

/**
* Given an UsernamePasswordAuthenticator
* When the username field is clicked
* Then the keyboard should be displayed with ImeAction.Next
* And when the password field is clicked
* Then the keyboard should be displayed with ImeAction.Send
*
* When the the ImeAction.Send is clicked
* And both text fields are empty
* Then the credentials should not be submitted
*
* @since 200.5.0
*/
@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun keyboardActions() = runTest {
val usernamePasswordChallengeMock = mockk<UsernamePasswordChallenge>()
every { usernamePasswordChallengeMock.url } returns "https://arcgis.com"
every { usernamePasswordChallengeMock.additionalMessage } answers { MutableStateFlow("") }
every { usernamePasswordChallengeMock.continueWithCredentials(any(), any()) } just Runs

composeTestRule.setContent {
UsernamePasswordAuthenticator(usernamePasswordChallengeMock)
}

// ensure the dialog prompt is displayed as expected
advanceUntilIdle()
// verify that clicking on the username field displays the keyboard with ImeAction.Next
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.username_label)).performClick()
composeTestRule.onNode(hasImeAction(ImeAction.Next)).assertExists()
// verify that clicking on the password field displays the keyboard with ImeAction.Send
composeTestRule.onNodeWithText(composeTestRule.activity.getString(R.string.password_label)).performClick()
composeTestRule.onNode(hasImeAction(ImeAction.Send)).assertExists()
// verify that clicking on ImeAction.Send will not submit the form when the fields are empty
composeTestRule.onNode(hasImeAction(ImeAction.Send)).performImeAction()
verify(exactly = 0) { usernamePasswordChallengeMock.continueWithCredentials(any(), any()) }
}


/**
* Given a Dialog Authenticator
* When a username and password challenge is issued
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ public fun UsernamePasswordAuthenticator(
var passwordFieldText by rememberSaveable { mutableStateOf("") }

fun submitUsernamePassword() {
usernamePasswordChallenge.continueWithCredentials(
usernameFieldText,
passwordFieldText
)
passwordFieldText = ""
if (usernameFieldText.isNotEmpty() && passwordFieldText.isNotEmpty()) {
usernamePasswordChallenge.continueWithCredentials(
usernameFieldText,
passwordFieldText
)
passwordFieldText = ""
}
}

val keyboardActions = remember {
Expand All @@ -136,7 +138,6 @@ public fun UsernamePasswordAuthenticator(
keyboardType = KeyboardType.Email,
imeAction = ImeAction.Next
),
keyboardActions = keyboardActions,
label = { Text(text = stringResource(id = R.string.username_label)) },
singleLine = true
)
Expand All @@ -160,6 +161,7 @@ public fun UsernamePasswordAuthenticator(
) {
Button(
modifier = Modifier.padding(4.dp).fillMaxWidth(),
enabled = usernameFieldText.isNotEmpty() && passwordFieldText.isNotEmpty(),
onClick = { submitUsernamePassword() }
) {
Text(stringResource(id = R.string.login))
Expand Down