From dee8008917b055620724df05c127ed6ac3b18d72 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 8 Jan 2024 09:50:08 +0100 Subject: [PATCH] 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() +}