diff --git a/features/logout/impl/build.gradle.kts b/features/logout/impl/build.gradle.kts index 68f7b389b3..699d6c93b8 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,10 @@ 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) + testReleaseImplementation(libs.androidx.compose.ui.test.manifest) 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..4958afabe7 --- /dev/null +++ b/features/logout/impl/src/test/kotlin/io/element/android/features/logout/impl/LogoutViewTest.kt @@ -0,0 +1,148 @@ +/* + * 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.activity.ComponentActivity +import androidx.compose.ui.test.junit4.createAndroidComposeRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import io.element.android.libraries.architecture.AsyncAction +import io.element.android.libraries.ui.strings.CommonStrings +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 io.element.android.tests.testutils.ensureCalledOnce +import io.element.android.tests.testutils.ensureCalledOnceWithParam +import io.element.android.tests.testutils.pressBack +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class LogoutViewTest { + + @get:Rule val rule = createAndroidComposeRule() + + @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(CommonStrings.action_signout) + eventsRecorder.assertSingle(LogoutEvents.Logout(false)) + } + + @Test + fun `clicking on back invoke back callback`() { + val eventsRecorder = EventsRecorder(expectEvents = false) + ensureCalledOnce { callback -> + rule.setContent { + LogoutView( + aLogoutState( + eventSink = eventsRecorder + ), + onChangeRecoveryKeyClicked = EnsureNeverCalled(), + onBackClicked = callback, + onSuccessLogout = EnsureNeverCalledWithParam(), + ) + } + rule.pressBack() + } + } + + @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(CommonStrings.action_signout_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(CommonStrings.action_cancel) + eventsRecorder.assertSingle(LogoutEvents.CloseDialogs) + } + + @Test + fun `success logout invoke onSuccessLogout`() { + val data = "data" + val eventsRecorder = EventsRecorder(expectEvents = false) + ensureCalledOnceWithParam(data) { callback -> + rule.setContent { + LogoutView( + aLogoutState( + logoutAction = AsyncAction.Success(data), + eventSink = eventsRecorder + ), + onChangeRecoveryKeyClicked = EnsureNeverCalled(), + onBackClicked = EnsureNeverCalled(), + onSuccessLogout = callback, + ) + } + } + } + + @Test + fun `last session setting button invoke onChangeRecoveryKeyClicked`() { + val eventsRecorder = EventsRecorder(expectEvents = false) + ensureCalledOnce { callback -> + rule.setContent { + LogoutView( + aLogoutState( + isLastSession = true, + eventSink = eventsRecorder + ), + onChangeRecoveryKeyClicked = callback, + onBackClicked = EnsureNeverCalled(), + onSuccessLogout = EnsureNeverCalledWithParam(), + ) + } + rule.clickOn(CommonStrings.common_settings) + } + } +} diff --git a/tests/testutils/build.gradle.kts b/tests/testutils/build.gradle.kts index 8275c975d2..4b586043dc 100644 --- a/tests/testutils/build.gradle.kts +++ b/tests/testutils/build.gradle.kts @@ -28,8 +28,11 @@ android { dependencies { implementation(libs.test.junit) + implementation(libs.test.truth) implementation(libs.coroutines.test) implementation(projects.libraries.core) + implementation(projects.libraries.uiStrings) 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..ddc8e22a5c --- /dev/null +++ b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureCalledOnce.kt @@ -0,0 +1,60 @@ +/* + * 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") + } + } +} + +fun ensureCalledOnce(block: (callback: EnsureCalledOnce) -> Unit) { + val callback = EnsureCalledOnce() + block(callback) + callback.assertSuccess() +} + +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") + } + } +} + +fun ensureCalledOnceWithParam(param: T, block: (callback: EnsureCalledOnceWithParam) -> Unit) { + val callback = EnsureCalledOnceWithParam(param) + block(callback) + callback.assertSuccess() +} 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..e98c025238 --- /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 and is called with $p1") + } +} 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..e99825c265 --- /dev/null +++ b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/SemanticsNodeInteractionsProviderExtensions.kt @@ -0,0 +1,38 @@ +/* + * 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.activity.ComponentActivity +import androidx.annotation.StringRes +import androidx.compose.ui.test.hasClickAction +import androidx.compose.ui.test.hasContentDescription +import androidx.compose.ui.test.hasText +import androidx.compose.ui.test.junit4.AndroidComposeTestRule +import androidx.compose.ui.test.performClick +import io.element.android.libraries.ui.strings.CommonStrings +import org.junit.rules.TestRule + +fun AndroidComposeTestRule.clickOn(@StringRes res: Int) { + val text = activity.getString(res) + onNode(hasText(text) and hasClickAction()) + .performClick() +} + +fun AndroidComposeTestRule.pressBack() { + val text = activity.getString(CommonStrings.action_back) + onNode(hasContentDescription(text)).performClick() +}