Add first tests on compose click interaction.
This commit is contained in:
@@ -22,6 +22,12 @@ plugins {
|
||||
|
||||
android {
|
||||
namespace = "io.element.android.features.logout.impl"
|
||||
|
||||
testOptions {
|
||||
unitTests {
|
||||
isIncludeAndroidResources = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
anvil {
|
||||
@@ -48,6 +54,9 @@ dependencies {
|
||||
testImplementation(libs.molecule.runtime)
|
||||
testImplementation(libs.test.truth)
|
||||
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.featureflag.test)
|
||||
testImplementation(projects.tests.testutils)
|
||||
|
||||
@@ -48,6 +48,7 @@ fun aLogoutState(
|
||||
recoveryState: RecoveryState = RecoveryState.ENABLED,
|
||||
backupUploadState: BackupUploadState = BackupUploadState.Unknown,
|
||||
logoutAction: AsyncAction<String?> = AsyncAction.Uninitialized,
|
||||
eventSink: (LogoutEvents) -> Unit = {},
|
||||
) = LogoutState(
|
||||
isLastSession = isLastSession,
|
||||
backupState = backupState,
|
||||
@@ -55,5 +56,5 @@ fun aLogoutState(
|
||||
recoveryState = recoveryState,
|
||||
backupUploadState = backupUploadState,
|
||||
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 {
|
||||
implementation(libs.test.junit)
|
||||
implementation(libs.test.truth)
|
||||
implementation(libs.coroutines.test)
|
||||
implementation(projects.libraries.core)
|
||||
implementation(libs.test.turbine)
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user