|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +package io.element.android.features.login.impl.screens.loginpassword |
| 9 | + |
| 10 | +import androidx.activity.ComponentActivity |
| 11 | +import androidx.compose.ui.test.assertIsEnabled |
| 12 | +import androidx.compose.ui.test.assertIsNotEnabled |
| 13 | +import androidx.compose.ui.test.junit4.AndroidComposeTestRule |
| 14 | +import androidx.compose.ui.test.junit4.createAndroidComposeRule |
| 15 | +import androidx.compose.ui.test.onNodeWithContentDescription |
| 16 | +import androidx.compose.ui.test.onNodeWithText |
| 17 | +import androidx.compose.ui.test.performClick |
| 18 | +import androidx.compose.ui.test.performTextInput |
| 19 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 20 | +import io.element.android.libraries.matrix.test.A_PASSWORD |
| 21 | +import io.element.android.libraries.matrix.test.A_USER_NAME |
| 22 | +import io.element.android.libraries.ui.strings.CommonStrings |
| 23 | +import io.element.android.tests.testutils.EnsureNeverCalled |
| 24 | +import io.element.android.tests.testutils.EventsRecorder |
| 25 | +import io.element.android.tests.testutils.clickOn |
| 26 | +import io.element.android.tests.testutils.ensureCalledOnce |
| 27 | +import io.element.android.tests.testutils.pressBack |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.rules.TestRule |
| 31 | +import org.junit.runner.RunWith |
| 32 | +import org.robolectric.annotation.Config |
| 33 | + |
| 34 | +@RunWith(AndroidJUnit4::class) |
| 35 | +class LoginPasswordViewTest { |
| 36 | + @get:Rule |
| 37 | + val rule = createAndroidComposeRule<ComponentActivity>() |
| 38 | + |
| 39 | + @Test |
| 40 | + fun `clicking on back invoke back callback`() { |
| 41 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>(expectEvents = false) |
| 42 | + ensureCalledOnce { callback -> |
| 43 | + rule.setLoginPasswordView( |
| 44 | + aLoginPasswordState( |
| 45 | + eventSink = eventsRecorder |
| 46 | + ), |
| 47 | + onBackClick = callback, |
| 48 | + ) |
| 49 | + rule.pressBack() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `changing login invokes the expected event`() { |
| 55 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>() |
| 56 | + rule.setLoginPasswordView( |
| 57 | + aLoginPasswordState( |
| 58 | + eventSink = eventsRecorder, |
| 59 | + ), |
| 60 | + ) |
| 61 | + val userNameHint = rule.activity.getString(CommonStrings.common_username) |
| 62 | + rule.onNodeWithText(userNameHint).performTextInput(A_USER_NAME) |
| 63 | + eventsRecorder.assertSingle( |
| 64 | + LoginPasswordEvents.SetLogin(A_USER_NAME) |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `changing login removes new lines the expected event`() { |
| 70 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>() |
| 71 | + rule.setLoginPasswordView( |
| 72 | + aLoginPasswordState( |
| 73 | + eventSink = eventsRecorder, |
| 74 | + ), |
| 75 | + ) |
| 76 | + val userNameHint = rule.activity.getString(CommonStrings.common_username) |
| 77 | + rule.onNodeWithText(userNameHint).performTextInput("a\nb") |
| 78 | + eventsRecorder.assertSingle( |
| 79 | + LoginPasswordEvents.SetLogin("ab") |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun `clearing login invokes the expected event`() { |
| 85 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>() |
| 86 | + rule.setLoginPasswordView( |
| 87 | + aLoginPasswordState( |
| 88 | + formState = aLoginFormState(A_USER_NAME), |
| 89 | + eventSink = eventsRecorder, |
| 90 | + ), |
| 91 | + ) |
| 92 | + val a11yClear = rule.activity.getString(CommonStrings.action_clear) |
| 93 | + rule.onNodeWithContentDescription(a11yClear).performClick() |
| 94 | + eventsRecorder.assertSingle( |
| 95 | + LoginPasswordEvents.SetLogin("") |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun `changing password invokes the expected event`() { |
| 101 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>() |
| 102 | + rule.setLoginPasswordView( |
| 103 | + aLoginPasswordState( |
| 104 | + eventSink = eventsRecorder, |
| 105 | + ), |
| 106 | + ) |
| 107 | + val userNameHint = rule.activity.getString(CommonStrings.common_password) |
| 108 | + rule.onNodeWithText(userNameHint).performTextInput(A_PASSWORD) |
| 109 | + eventsRecorder.assertSingle( |
| 110 | + LoginPasswordEvents.SetPassword(A_PASSWORD) |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun `reveal password makes the password visible`() { |
| 116 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>(expectEvents = false) |
| 117 | + rule.setLoginPasswordView( |
| 118 | + aLoginPasswordState( |
| 119 | + formState = aLoginFormState(password = A_PASSWORD), |
| 120 | + eventSink = eventsRecorder, |
| 121 | + ), |
| 122 | + ) |
| 123 | + rule.onNodeWithText(A_PASSWORD).assertDoesNotExist() |
| 124 | + // Show password |
| 125 | + val a11yShowPassword = rule.activity.getString(CommonStrings.a11y_show_password) |
| 126 | + rule.onNodeWithContentDescription(a11yShowPassword).performClick() |
| 127 | + rule.onNodeWithText(A_PASSWORD).assertExists() |
| 128 | + // Hide password |
| 129 | + val a11yHidePassword = rule.activity.getString(CommonStrings.a11y_hide_password) |
| 130 | + rule.onNodeWithContentDescription(a11yHidePassword).performClick() |
| 131 | + rule.onNodeWithText(A_PASSWORD).assertDoesNotExist() |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `when login is empty, continue button is not enabled`() { |
| 136 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>(expectEvents = false) |
| 137 | + rule.setLoginPasswordView( |
| 138 | + aLoginPasswordState( |
| 139 | + formState = aLoginFormState(password = A_PASSWORD), |
| 140 | + eventSink = eventsRecorder, |
| 141 | + ), |
| 142 | + ) |
| 143 | + val continueStr = rule.activity.getString(CommonStrings.action_continue) |
| 144 | + rule.onNodeWithText(continueStr).assertIsNotEnabled() |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + fun `when password is empty, continue button is not enabled`() { |
| 149 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>(expectEvents = false) |
| 150 | + rule.setLoginPasswordView( |
| 151 | + aLoginPasswordState( |
| 152 | + formState = aLoginFormState(login = A_USER_NAME), |
| 153 | + eventSink = eventsRecorder, |
| 154 | + ), |
| 155 | + ) |
| 156 | + val continueStr = rule.activity.getString(CommonStrings.action_continue) |
| 157 | + rule.onNodeWithText(continueStr).assertIsNotEnabled() |
| 158 | + } |
| 159 | + |
| 160 | + @Config(qualifiers = "h1024dp") |
| 161 | + @Test |
| 162 | + fun `clicking on Continue sends expected event`() { |
| 163 | + val eventsRecorder = EventsRecorder<LoginPasswordEvents>() |
| 164 | + rule.setLoginPasswordView( |
| 165 | + aLoginPasswordState( |
| 166 | + formState = aLoginFormState(login = A_USER_NAME, password = A_PASSWORD), |
| 167 | + eventSink = eventsRecorder, |
| 168 | + ), |
| 169 | + ) |
| 170 | + val continueStr = rule.activity.getString(CommonStrings.action_continue) |
| 171 | + rule.onNodeWithText(continueStr).assertIsEnabled() |
| 172 | + rule.clickOn(CommonStrings.action_continue) |
| 173 | + eventsRecorder.assertSingle( |
| 174 | + LoginPasswordEvents.Submit |
| 175 | + ) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setLoginPasswordView( |
| 180 | + state: LoginPasswordState, |
| 181 | + onBackClick: () -> Unit = EnsureNeverCalled(), |
| 182 | +) { |
| 183 | + setContent { |
| 184 | + LoginPasswordView( |
| 185 | + state = state, |
| 186 | + onBackClick = onBackClick, |
| 187 | + ) |
| 188 | + } |
| 189 | +} |
0 commit comments