Add first tests on compose click interaction.
This commit is contained in:
parent
00c27ffafc
commit
69e8384163
8 changed files with 305 additions and 1 deletions
|
|
@ -22,6 +22,12 @@ plugins {
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "io.element.android.features.logout.impl"
|
namespace = "io.element.android.features.logout.impl"
|
||||||
|
|
||||||
|
testOptions {
|
||||||
|
unitTests {
|
||||||
|
isIncludeAndroidResources = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
anvil {
|
anvil {
|
||||||
|
|
@ -48,6 +54,9 @@ dependencies {
|
||||||
testImplementation(libs.molecule.runtime)
|
testImplementation(libs.molecule.runtime)
|
||||||
testImplementation(libs.test.truth)
|
testImplementation(libs.test.truth)
|
||||||
testImplementation(libs.test.turbine)
|
testImplementation(libs.test.turbine)
|
||||||
|
testImplementation(libs.test.robolectric)
|
||||||
|
testImplementation(libs.test.junitext)
|
||||||
|
testImplementation(libs.androidx.compose.ui.test.junit)
|
||||||
testImplementation(projects.libraries.matrix.test)
|
testImplementation(projects.libraries.matrix.test)
|
||||||
testImplementation(projects.libraries.featureflag.test)
|
testImplementation(projects.libraries.featureflag.test)
|
||||||
testImplementation(projects.tests.testutils)
|
testImplementation(projects.tests.testutils)
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ fun aLogoutState(
|
||||||
recoveryState: RecoveryState = RecoveryState.ENABLED,
|
recoveryState: RecoveryState = RecoveryState.ENABLED,
|
||||||
backupUploadState: BackupUploadState = BackupUploadState.Unknown,
|
backupUploadState: BackupUploadState = BackupUploadState.Unknown,
|
||||||
logoutAction: AsyncAction<String?> = AsyncAction.Uninitialized,
|
logoutAction: AsyncAction<String?> = AsyncAction.Uninitialized,
|
||||||
|
eventSink: (LogoutEvents) -> Unit = {},
|
||||||
) = LogoutState(
|
) = LogoutState(
|
||||||
isLastSession = isLastSession,
|
isLastSession = isLastSession,
|
||||||
backupState = backupState,
|
backupState = backupState,
|
||||||
|
|
@ -55,5 +56,5 @@ fun aLogoutState(
|
||||||
recoveryState = recoveryState,
|
recoveryState = recoveryState,
|
||||||
backupUploadState = backupUploadState,
|
backupUploadState = backupUploadState,
|
||||||
logoutAction = logoutAction,
|
logoutAction = logoutAction,
|
||||||
eventSink = {}
|
eventSink = eventSink,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,147 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.features.logout.impl
|
||||||
|
|
||||||
|
import androidx.compose.ui.test.hasContentDescription
|
||||||
|
import androidx.compose.ui.test.junit4.createComposeRule
|
||||||
|
import androidx.compose.ui.test.performClick
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import io.element.android.libraries.architecture.AsyncAction
|
||||||
|
import io.element.android.tests.testutils.EnsureCalledOnce
|
||||||
|
import io.element.android.tests.testutils.EnsureCalledOnceWithParam
|
||||||
|
import io.element.android.tests.testutils.EnsureNeverCalled
|
||||||
|
import io.element.android.tests.testutils.EnsureNeverCalledWithParam
|
||||||
|
import io.element.android.tests.testutils.EventsRecorder
|
||||||
|
import io.element.android.tests.testutils.clickOn
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class LogoutViewTest {
|
||||||
|
|
||||||
|
@get:Rule val rule = createComposeRule()
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on logout sends a LogoutEvents`() {
|
||||||
|
val eventsRecorder = EventsRecorder<LogoutEvents>()
|
||||||
|
rule.setContent {
|
||||||
|
LogoutView(
|
||||||
|
aLogoutState(
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onChangeRecoveryKeyClicked = EnsureNeverCalled(),
|
||||||
|
onBackClicked = EnsureNeverCalled(),
|
||||||
|
onSuccessLogout = EnsureNeverCalledWithParam(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
rule.clickOn("Sign out")
|
||||||
|
eventsRecorder.assertSingle(LogoutEvents.Logout(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on back invoke back callback`() {
|
||||||
|
val eventsRecorder = EventsRecorder<LogoutEvents>(expectEvents = false)
|
||||||
|
val callback = EnsureCalledOnce()
|
||||||
|
rule.setContent {
|
||||||
|
LogoutView(
|
||||||
|
aLogoutState(
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onChangeRecoveryKeyClicked = EnsureNeverCalled(),
|
||||||
|
onBackClicked = callback,
|
||||||
|
onSuccessLogout = EnsureNeverCalledWithParam(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
rule.onNode(hasContentDescription("Back")).performClick()
|
||||||
|
callback.assertSuccess()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on confirm after error sends a LogoutEvents`() {
|
||||||
|
val eventsRecorder = EventsRecorder<LogoutEvents>()
|
||||||
|
rule.setContent {
|
||||||
|
LogoutView(
|
||||||
|
aLogoutState(
|
||||||
|
logoutAction = AsyncAction.Failure(Exception("Failed to logout")),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onChangeRecoveryKeyClicked = EnsureNeverCalled(),
|
||||||
|
onBackClicked = EnsureNeverCalled(),
|
||||||
|
onSuccessLogout = EnsureNeverCalledWithParam(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
rule.clickOn("Sign out anyway")
|
||||||
|
eventsRecorder.assertSingle(LogoutEvents.Logout(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on cancel after error sends a LogoutEvents`() {
|
||||||
|
val eventsRecorder = EventsRecorder<LogoutEvents>()
|
||||||
|
rule.setContent {
|
||||||
|
LogoutView(
|
||||||
|
aLogoutState(
|
||||||
|
logoutAction = AsyncAction.Failure(Exception("Failed to logout")),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onChangeRecoveryKeyClicked = EnsureNeverCalled(),
|
||||||
|
onBackClicked = EnsureNeverCalled(),
|
||||||
|
onSuccessLogout = EnsureNeverCalledWithParam(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
rule.clickOn("Cancel")
|
||||||
|
eventsRecorder.assertSingle(LogoutEvents.CloseDialogs)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `success logout invoke onSuccessLogout`() {
|
||||||
|
val data = "data"
|
||||||
|
val eventsRecorder = EventsRecorder<LogoutEvents>(expectEvents = false)
|
||||||
|
val callback = EnsureCalledOnceWithParam<String?>(data)
|
||||||
|
rule.setContent {
|
||||||
|
LogoutView(
|
||||||
|
aLogoutState(
|
||||||
|
logoutAction = AsyncAction.Success(data),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onChangeRecoveryKeyClicked = EnsureNeverCalled(),
|
||||||
|
onBackClicked = EnsureNeverCalled(),
|
||||||
|
onSuccessLogout = callback,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
callback.assertSuccess()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `last session setting button invoke onChangeRecoveryKeyClicked`() {
|
||||||
|
val eventsRecorder = EventsRecorder<LogoutEvents>(expectEvents = false)
|
||||||
|
val callback = EnsureCalledOnce()
|
||||||
|
rule.setContent {
|
||||||
|
LogoutView(
|
||||||
|
aLogoutState(
|
||||||
|
isLastSession = true,
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onChangeRecoveryKeyClicked = callback,
|
||||||
|
onBackClicked = EnsureNeverCalled(),
|
||||||
|
onSuccessLogout = EnsureNeverCalledWithParam(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
rule.clickOn("Settings")
|
||||||
|
callback.assertSuccess()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -28,8 +28,10 @@ android {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(libs.test.junit)
|
implementation(libs.test.junit)
|
||||||
|
implementation(libs.test.truth)
|
||||||
implementation(libs.coroutines.test)
|
implementation(libs.coroutines.test)
|
||||||
implementation(projects.libraries.core)
|
implementation(projects.libraries.core)
|
||||||
implementation(libs.test.turbine)
|
implementation(libs.test.turbine)
|
||||||
implementation(libs.molecule.runtime)
|
implementation(libs.molecule.runtime)
|
||||||
|
implementation(libs.androidx.compose.ui.test.junit)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.tests.testutils
|
||||||
|
|
||||||
|
class EnsureCalledOnce : () -> Unit {
|
||||||
|
private var counter = 0
|
||||||
|
override fun invoke() {
|
||||||
|
counter++
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assertSuccess() {
|
||||||
|
if (counter != 1) {
|
||||||
|
throw AssertionError("Expected to be called once, but was called $counter times")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class EnsureCalledOnceWithParam<T>(
|
||||||
|
private val expectedParam: T
|
||||||
|
) : (T) -> Unit {
|
||||||
|
private var counter = 0
|
||||||
|
override fun invoke(p1: T) {
|
||||||
|
if (p1 != expectedParam) {
|
||||||
|
throw AssertionError("Expected to be called with $expectedParam, but was called with $p1")
|
||||||
|
}
|
||||||
|
counter++
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assertSuccess() {
|
||||||
|
if (counter != 1) {
|
||||||
|
throw AssertionError("Expected to be called once, but was called $counter times")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.tests.testutils
|
||||||
|
|
||||||
|
class EnsureNeverCalled : () -> Unit {
|
||||||
|
override fun invoke() {
|
||||||
|
throw AssertionError("Should not be called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class EnsureNeverCalledWithParam<T> : (T) -> Unit {
|
||||||
|
override fun invoke(p1: T) {
|
||||||
|
throw AssertionError("Should not be called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.tests.testutils
|
||||||
|
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
|
||||||
|
class EventsRecorder<T>(
|
||||||
|
private val expectEvents: Boolean = true
|
||||||
|
) : (T) -> Unit {
|
||||||
|
private val events = mutableListOf<T>()
|
||||||
|
|
||||||
|
override fun invoke(event: T) {
|
||||||
|
if (expectEvents) {
|
||||||
|
events.add(event)
|
||||||
|
} else {
|
||||||
|
throw AssertionError("Unexpected event: $event")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assertSingle(event: T) {
|
||||||
|
assertList(listOf(event))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun assertList(expectedEvents: List<T>) {
|
||||||
|
assertThat(events).isEqualTo(expectedEvents)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.tests.testutils
|
||||||
|
|
||||||
|
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider
|
||||||
|
import androidx.compose.ui.test.hasClickAction
|
||||||
|
import androidx.compose.ui.test.hasText
|
||||||
|
import androidx.compose.ui.test.performClick
|
||||||
|
|
||||||
|
fun SemanticsNodeInteractionsProvider.clickOn(text: String) {
|
||||||
|
onNode(hasText(text) and hasClickAction())
|
||||||
|
.performClick()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue