diff --git a/features/logout/impl/build.gradle.kts b/features/logout/impl/build.gradle.kts index 68f7b389b3..6193e3fba4 100644 --- a/features/logout/impl/build.gradle.kts +++ b/features/logout/impl/build.gradle.kts @@ -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) diff --git a/features/logout/impl/src/main/kotlin/io/element/android/features/logout/impl/LogoutStateProvider.kt b/features/logout/impl/src/main/kotlin/io/element/android/features/logout/impl/LogoutStateProvider.kt index 33c60a851f..f1b11f61e9 100644 --- a/features/logout/impl/src/main/kotlin/io/element/android/features/logout/impl/LogoutStateProvider.kt +++ b/features/logout/impl/src/main/kotlin/io/element/android/features/logout/impl/LogoutStateProvider.kt @@ -48,6 +48,7 @@ fun aLogoutState( recoveryState: RecoveryState = RecoveryState.ENABLED, backupUploadState: BackupUploadState = BackupUploadState.Unknown, logoutAction: AsyncAction = 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, ) diff --git a/features/logout/impl/src/test/kotlin/io/element/android/features/logout/impl/LogoutViewTest.kt b/features/logout/impl/src/test/kotlin/io/element/android/features/logout/impl/LogoutViewTest.kt new file mode 100644 index 0000000000..6193f67d48 --- /dev/null +++ b/features/logout/impl/src/test/kotlin/io/element/android/features/logout/impl/LogoutViewTest.kt @@ -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() + 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(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() + 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() + 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(expectEvents = false) + val callback = EnsureCalledOnceWithParam(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(expectEvents = false) + val callback = EnsureCalledOnce() + rule.setContent { + LogoutView( + aLogoutState( + isLastSession = true, + eventSink = eventsRecorder + ), + onChangeRecoveryKeyClicked = callback, + onBackClicked = EnsureNeverCalled(), + onSuccessLogout = EnsureNeverCalledWithParam(), + ) + } + rule.clickOn("Settings") + callback.assertSuccess() + } +} diff --git a/tests/testutils/build.gradle.kts b/tests/testutils/build.gradle.kts index 8275c975d2..f81db46f2a 100644 --- a/tests/testutils/build.gradle.kts +++ b/tests/testutils/build.gradle.kts @@ -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) } diff --git a/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureCalledOnce.kt b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureCalledOnce.kt new file mode 100644 index 0000000000..721ced9dcb --- /dev/null +++ b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureCalledOnce.kt @@ -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( + 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") + } + } +} diff --git a/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureNeverCalled.kt b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureNeverCalled.kt new file mode 100644 index 0000000000..9a106c86ad --- /dev/null +++ b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureNeverCalled.kt @@ -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) -> Unit { + override fun invoke(p1: T) { + throw AssertionError("Should not be called") + } +} diff --git a/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EventsRecorder.kt b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EventsRecorder.kt new file mode 100644 index 0000000000..4cc9bd078c --- /dev/null +++ b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EventsRecorder.kt @@ -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( + private val expectEvents: Boolean = true +) : (T) -> Unit { + private val events = mutableListOf() + + 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) { + assertThat(events).isEqualTo(expectedEvents) + } +} diff --git a/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/SemanticsNodeInteractionsProviderExtensions.kt b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/SemanticsNodeInteractionsProviderExtensions.kt new file mode 100644 index 0000000000..2a42bd115b --- /dev/null +++ b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/SemanticsNodeInteractionsProviderExtensions.kt @@ -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() +}