From 44ad93f625b8874f0d9fefe12ab69cd273f57d0a Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 5 Jan 2024 18:00:37 +0100 Subject: [PATCH 1/6] Add first tests on compose click interaction. --- features/logout/impl/build.gradle.kts | 9 ++ .../logout/impl/LogoutStateProvider.kt | 3 +- .../features/logout/impl/LogoutViewTest.kt | 147 ++++++++++++++++++ tests/testutils/build.gradle.kts | 2 + .../tests/testutils/EnsureCalledOnce.kt | 48 ++++++ .../tests/testutils/EnsureNeverCalled.kt | 29 ++++ .../android/tests/testutils/EventsRecorder.kt | 41 +++++ ...nticsNodeInteractionsProviderExtensions.kt | 27 ++++ 8 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 features/logout/impl/src/test/kotlin/io/element/android/features/logout/impl/LogoutViewTest.kt create mode 100644 tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureCalledOnce.kt create mode 100644 tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EnsureNeverCalled.kt create mode 100644 tests/testutils/src/main/kotlin/io/element/android/tests/testutils/EventsRecorder.kt create mode 100644 tests/testutils/src/main/kotlin/io/element/android/tests/testutils/SemanticsNodeInteractionsProviderExtensions.kt 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() +} From 7944804cd982b44beb7f77a2c4ed1a64eaa96eae Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 5 Jan 2024 18:20:26 +0100 Subject: [PATCH 2/6] Fix test in release. --- features/logout/impl/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/features/logout/impl/build.gradle.kts b/features/logout/impl/build.gradle.kts index 6193e3fba4..699d6c93b8 100644 --- a/features/logout/impl/build.gradle.kts +++ b/features/logout/impl/build.gradle.kts @@ -57,6 +57,7 @@ dependencies { 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) From fdc3c9ff396d6dfbe2ca254659d436d0e14f785a Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Jan 2024 09:35:14 +0100 Subject: [PATCH 3/6] Improve assertion message. --- .../io/element/android/tests/testutils/EnsureNeverCalled.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9a106c86ad..e98c025238 100644 --- 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 @@ -24,6 +24,6 @@ class EnsureNeverCalled : () -> Unit { class EnsureNeverCalledWithParam : (T) -> Unit { override fun invoke(p1: T) { - throw AssertionError("Should not be called") + throw AssertionError("Should not be called and is called with $p1") } } From dee8008917b055620724df05c127ed6ac3b18d72 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Jan 2024 09:50:08 +0100 Subject: [PATCH 4/6] Use string resource instead of hard-coded strings. --- .../features/logout/impl/LogoutViewTest.kt | 19 ++++++++++--------- tests/testutils/build.gradle.kts | 1 + ...nticsNodeInteractionsProviderExtensions.kt | 15 +++++++++++++-- 3 files changed, 24 insertions(+), 11 deletions(-) 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 index 6193f67d48..7fcad59795 100644 --- 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 @@ -16,17 +16,18 @@ 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.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.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 io.element.android.tests.testutils.pressBack import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -34,7 +35,7 @@ import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) class LogoutViewTest { - @get:Rule val rule = createComposeRule() + @get:Rule val rule = createAndroidComposeRule() @Test fun `clicking on logout sends a LogoutEvents`() { @@ -49,7 +50,7 @@ class LogoutViewTest { onSuccessLogout = EnsureNeverCalledWithParam(), ) } - rule.clickOn("Sign out") + rule.clickOn(CommonStrings.action_signout) eventsRecorder.assertSingle(LogoutEvents.Logout(false)) } @@ -67,7 +68,7 @@ class LogoutViewTest { onSuccessLogout = EnsureNeverCalledWithParam(), ) } - rule.onNode(hasContentDescription("Back")).performClick() + rule.pressBack() callback.assertSuccess() } @@ -85,7 +86,7 @@ class LogoutViewTest { onSuccessLogout = EnsureNeverCalledWithParam(), ) } - rule.clickOn("Sign out anyway") + rule.clickOn(CommonStrings.action_signout_anyway) eventsRecorder.assertSingle(LogoutEvents.Logout(true)) } @@ -103,7 +104,7 @@ class LogoutViewTest { onSuccessLogout = EnsureNeverCalledWithParam(), ) } - rule.clickOn("Cancel") + rule.clickOn(CommonStrings.action_cancel) eventsRecorder.assertSingle(LogoutEvents.CloseDialogs) } @@ -141,7 +142,7 @@ class LogoutViewTest { onSuccessLogout = EnsureNeverCalledWithParam(), ) } - rule.clickOn("Settings") + rule.clickOn(CommonStrings.common_settings) callback.assertSuccess() } } diff --git a/tests/testutils/build.gradle.kts b/tests/testutils/build.gradle.kts index f81db46f2a..4b586043dc 100644 --- a/tests/testutils/build.gradle.kts +++ b/tests/testutils/build.gradle.kts @@ -31,6 +31,7 @@ dependencies { 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/SemanticsNodeInteractionsProviderExtensions.kt b/tests/testutils/src/main/kotlin/io/element/android/tests/testutils/SemanticsNodeInteractionsProviderExtensions.kt index 2a42bd115b..e99825c265 100644 --- 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 @@ -16,12 +16,23 @@ package io.element.android.tests.testutils -import androidx.compose.ui.test.SemanticsNodeInteractionsProvider +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 SemanticsNodeInteractionsProvider.clickOn(text: String) { +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() +} From 2c667a0dc0a00dff33ec19b7ee97310316376e5f Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Jan 2024 09:55:22 +0100 Subject: [PATCH 5/6] Add `run` method to EnsureCalledOnce and EnsureCalledOnceWithParam to ensure that `assertSuccess` is always called. --- .../features/logout/impl/LogoutViewTest.kt | 74 +++++++++---------- .../tests/testutils/EnsureCalledOnce.kt | 10 +++ 2 files changed, 47 insertions(+), 37 deletions(-) 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 index 7fcad59795..f0abf45234 100644 --- 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 @@ -57,19 +57,19 @@ class LogoutViewTest { @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(), - ) + EnsureCalledOnce().run { callback -> + rule.setContent { + LogoutView( + aLogoutState( + eventSink = eventsRecorder + ), + onChangeRecoveryKeyClicked = EnsureNeverCalled(), + onBackClicked = callback, + onSuccessLogout = EnsureNeverCalledWithParam(), + ) + } + rule.pressBack() } - rule.pressBack() - callback.assertSuccess() } @Test @@ -112,37 +112,37 @@ class LogoutViewTest { 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, - ) + EnsureCalledOnceWithParam(data).run { callback -> + 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(), - ) + EnsureCalledOnce().run { callback -> + rule.setContent { + LogoutView( + aLogoutState( + isLastSession = true, + eventSink = eventsRecorder + ), + onChangeRecoveryKeyClicked = callback, + onBackClicked = EnsureNeverCalled(), + onSuccessLogout = EnsureNeverCalledWithParam(), + ) + } + rule.clickOn(CommonStrings.common_settings) } - rule.clickOn(CommonStrings.common_settings) - callback.assertSuccess() } } 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 index 721ced9dcb..a29c320a00 100644 --- 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 @@ -27,6 +27,11 @@ class EnsureCalledOnce : () -> Unit { throw AssertionError("Expected to be called once, but was called $counter times") } } + + fun run(block: (callback: EnsureCalledOnce) -> Unit) { + block(this) + assertSuccess() + } } class EnsureCalledOnceWithParam( @@ -45,4 +50,9 @@ class EnsureCalledOnceWithParam( throw AssertionError("Expected to be called once, but was called $counter times") } } + + fun run(block: (callback: EnsureCalledOnceWithParam) -> Unit) { + block(this) + assertSuccess() + } } From 2a6a3ded5e95f0462cfa17cedd25dcd6deeea3b9 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Jan 2024 10:07:48 +0100 Subject: [PATCH 6/6] Iterate on EnsureCalledOnce devX --- .../features/logout/impl/LogoutViewTest.kt | 10 +++++----- .../tests/testutils/EnsureCalledOnce.kt | 20 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) 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 index f0abf45234..4958afabe7 100644 --- 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 @@ -21,12 +21,12 @@ 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.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 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 @@ -57,7 +57,7 @@ class LogoutViewTest { @Test fun `clicking on back invoke back callback`() { val eventsRecorder = EventsRecorder(expectEvents = false) - EnsureCalledOnce().run { callback -> + ensureCalledOnce { callback -> rule.setContent { LogoutView( aLogoutState( @@ -112,7 +112,7 @@ class LogoutViewTest { fun `success logout invoke onSuccessLogout`() { val data = "data" val eventsRecorder = EventsRecorder(expectEvents = false) - EnsureCalledOnceWithParam(data).run { callback -> + ensureCalledOnceWithParam(data) { callback -> rule.setContent { LogoutView( aLogoutState( @@ -130,7 +130,7 @@ class LogoutViewTest { @Test fun `last session setting button invoke onChangeRecoveryKeyClicked`() { val eventsRecorder = EventsRecorder(expectEvents = false) - EnsureCalledOnce().run { callback -> + ensureCalledOnce { callback -> rule.setContent { LogoutView( aLogoutState( 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 index a29c320a00..ddc8e22a5c 100644 --- 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 @@ -27,11 +27,12 @@ class EnsureCalledOnce : () -> Unit { throw AssertionError("Expected to be called once, but was called $counter times") } } +} - fun run(block: (callback: EnsureCalledOnce) -> Unit) { - block(this) - assertSuccess() - } +fun ensureCalledOnce(block: (callback: EnsureCalledOnce) -> Unit) { + val callback = EnsureCalledOnce() + block(callback) + callback.assertSuccess() } class EnsureCalledOnceWithParam( @@ -50,9 +51,10 @@ class EnsureCalledOnceWithParam( throw AssertionError("Expected to be called once, but was called $counter times") } } - - fun run(block: (callback: EnsureCalledOnceWithParam) -> Unit) { - block(this) - assertSuccess() - } +} + +fun ensureCalledOnceWithParam(param: T, block: (callback: EnsureCalledOnceWithParam) -> Unit) { + val callback = EnsureCalledOnceWithParam(param) + block(callback) + callback.assertSuccess() }