From 61693230439100dc1f7a9ba6dcc4cf20ee8b36b2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 9 Feb 2023 18:41:58 +0100 Subject: [PATCH] Move test data to TestData.kt --- .../kotlin/io/element/android/x/root/FakeBugReporter.kt | 5 ++--- .../android/features/login/root/LoginRootPresenterTest.kt | 6 +++--- .../features/logout/LogoutPreferencePresenterTest.kt | 6 +++--- .../features/rageshake/bugreport/BugReportPresenterTest.kt | 3 ++- .../android/features/rageshake/bugreport/FakeBugReporter.kt | 5 ++--- .../rageshake/detection/RageshakeDetectionPresenterTest.kt | 3 ++- .../android/features/roomlist/RoomListPresenterTests.kt | 5 +++-- .../io/element/android/libraries/matrixtest/TestData.kt | 5 ++++- 8 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/src/test/kotlin/io/element/android/x/root/FakeBugReporter.kt b/app/src/test/kotlin/io/element/android/x/root/FakeBugReporter.kt index f6cc6c8960..c6f7000cdf 100644 --- a/app/src/test/kotlin/io/element/android/x/root/FakeBugReporter.kt +++ b/app/src/test/kotlin/io/element/android/x/root/FakeBugReporter.kt @@ -19,12 +19,11 @@ package io.element.android.x.root import io.element.android.features.rageshake.reporter.BugReporter import io.element.android.features.rageshake.reporter.BugReporterListener import io.element.android.features.rageshake.reporter.ReportType +import io.element.android.libraries.matrixtest.A_FAILURE_REASON import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch -const val A_REASON = "There has been a failure" - // TODO Remove this duplicated class when we will rework modules. class FakeBugReporter(val mode: FakeBugReporterMode = FakeBugReporterMode.Success) : BugReporter { override fun sendBugReport( @@ -49,7 +48,7 @@ class FakeBugReporter(val mode: FakeBugReporterMode = FakeBugReporterMode.Succes when (mode) { FakeBugReporterMode.Success -> Unit FakeBugReporterMode.Failure -> { - listener?.onUploadFailed(A_REASON) + listener?.onUploadFailed(A_FAILURE_REASON) return@launch } FakeBugReporterMode.Cancel -> { diff --git a/features/login/src/test/kotlin/io/element/android/features/login/root/LoginRootPresenterTest.kt b/features/login/src/test/kotlin/io/element/android/features/login/root/LoginRootPresenterTest.kt index f9eed8d66f..3fa20ae93a 100644 --- a/features/login/src/test/kotlin/io/element/android/features/login/root/LoginRootPresenterTest.kt +++ b/features/login/src/test/kotlin/io/element/android/features/login/root/LoginRootPresenterTest.kt @@ -23,11 +23,11 @@ import app.cash.molecule.moleculeFlow import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.matrix.core.SessionId -import io.element.android.libraries.matrixtest.A_FAILURE import io.element.android.libraries.matrixtest.A_HOMESERVER import io.element.android.libraries.matrixtest.A_HOMESERVER_2 import io.element.android.libraries.matrixtest.A_PASSWORD import io.element.android.libraries.matrixtest.A_SESSION_ID +import io.element.android.libraries.matrixtest.A_THROWABLE import io.element.android.libraries.matrixtest.A_USER_NAME import io.element.android.libraries.matrixtest.auth.FakeAuthenticationService import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -106,12 +106,12 @@ class LoginRootPresenterTest { initialState.eventSink.invoke(LoginRootEvents.SetPassword(A_PASSWORD)) skipItems(1) val loginAndPasswordState = awaitItem() - authenticationService.givenLoginError(A_FAILURE) + authenticationService.givenLoginError(A_THROWABLE) loginAndPasswordState.eventSink.invoke(LoginRootEvents.Submit) val submitState = awaitItem() assertThat(submitState.loggedInState).isEqualTo(LoggedInState.LoggingIn) val loggedInState = awaitItem() - assertThat(loggedInState.loggedInState).isEqualTo(LoggedInState.ErrorLoggingIn(A_FAILURE)) + assertThat(loggedInState.loggedInState).isEqualTo(LoggedInState.ErrorLoggingIn(A_THROWABLE)) } } diff --git a/features/logout/src/test/kotlin/io/element/android/features/logout/LogoutPreferencePresenterTest.kt b/features/logout/src/test/kotlin/io/element/android/features/logout/LogoutPreferencePresenterTest.kt index 5367c21e4d..e84226b3e8 100644 --- a/features/logout/src/test/kotlin/io/element/android/features/logout/LogoutPreferencePresenterTest.kt +++ b/features/logout/src/test/kotlin/io/element/android/features/logout/LogoutPreferencePresenterTest.kt @@ -24,7 +24,7 @@ import app.cash.turbine.test import com.google.common.truth.Truth.assertThat import io.element.android.libraries.architecture.Async import io.element.android.libraries.matrix.core.SessionId -import io.element.android.libraries.matrixtest.A_FAILURE +import io.element.android.libraries.matrixtest.A_THROWABLE import io.element.android.libraries.matrixtest.FakeMatrixClient import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest @@ -71,12 +71,12 @@ class LogoutPreferencePresenterTest { presenter.present() }.test { val initialState = awaitItem() - matrixClient.givenLogoutError(A_FAILURE) + matrixClient.givenLogoutError(A_THROWABLE) initialState.eventSink.invoke(LogoutPreferenceEvents.Logout) val loadingState = awaitItem() assertThat(loadingState.logoutAction).isInstanceOf(Async.Loading::class.java) val successState = awaitItem() - assertThat(successState.logoutAction).isEqualTo(Async.Failure(A_FAILURE)) + assertThat(successState.logoutAction).isEqualTo(Async.Failure(A_THROWABLE)) } } } diff --git a/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/BugReportPresenterTest.kt b/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/BugReportPresenterTest.kt index bb367b15b3..484c3bd1b0 100644 --- a/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/BugReportPresenterTest.kt +++ b/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/BugReportPresenterTest.kt @@ -25,6 +25,7 @@ import com.google.common.truth.Truth.assertThat import io.element.android.features.rageshake.crash.ui.A_CRASH_DATA import io.element.android.features.rageshake.crash.ui.FakeCrashDataStore import io.element.android.libraries.architecture.Async +import io.element.android.libraries.matrixtest.A_FAILURE_REASON import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.junit.Test @@ -218,7 +219,7 @@ class BugReportPresenterTest { assertThat(awaitItem().sendingProgress).isEqualTo(0.5f) // Failure assertThat(awaitItem().sendingProgress).isEqualTo(0f) - assertThat((awaitItem().sending as Async.Failure).error.message).isEqualTo(A_REASON) + assertThat((awaitItem().sending as Async.Failure).error.message).isEqualTo(A_FAILURE_REASON) } } diff --git a/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/FakeBugReporter.kt b/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/FakeBugReporter.kt index a1a2c613a7..29977d7a95 100644 --- a/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/FakeBugReporter.kt +++ b/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/bugreport/FakeBugReporter.kt @@ -19,12 +19,11 @@ package io.element.android.features.rageshake.bugreport import io.element.android.features.rageshake.reporter.BugReporter import io.element.android.features.rageshake.reporter.BugReporterListener import io.element.android.features.rageshake.reporter.ReportType +import io.element.android.libraries.matrixtest.A_FAILURE_REASON import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch -const val A_REASON = "There has been a failure" - class FakeBugReporter(val mode: FakeBugReporterMode = FakeBugReporterMode.Success) : BugReporter { override fun sendBugReport( coroutineScope: CoroutineScope, @@ -48,7 +47,7 @@ class FakeBugReporter(val mode: FakeBugReporterMode = FakeBugReporterMode.Succes when (mode) { FakeBugReporterMode.Success -> Unit FakeBugReporterMode.Failure -> { - listener?.onUploadFailed(A_REASON) + listener?.onUploadFailed(A_FAILURE_REASON) return@launch } FakeBugReporterMode.Cancel -> { diff --git a/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/detection/RageshakeDetectionPresenterTest.kt b/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/detection/RageshakeDetectionPresenterTest.kt index f5fb8cbedc..1ef8941bff 100644 --- a/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/detection/RageshakeDetectionPresenterTest.kt +++ b/features/rageshake/src/test/kotlin/io/element/android/features/rageshake/detection/RageshakeDetectionPresenterTest.kt @@ -28,6 +28,7 @@ import io.element.android.features.rageshake.preferences.FakeRageShake import io.element.android.features.rageshake.preferences.FakeRageshakeDataStore import io.element.android.features.rageshake.preferences.RageshakePreferencesPresenter import io.element.android.features.rageshake.screenshot.ImageResult +import io.element.android.libraries.matrixtest.AN_EXCEPTION import io.mockk.mockk import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.first @@ -142,7 +143,7 @@ class RageshakeDetectionPresenterTest { rageshake.triggerPhoneRageshake() assertThat(awaitItem().takeScreenshot).isTrue() initialState.eventSink.invoke( - RageshakeDetectionEvents.ProcessScreenshot(ImageResult.Error(Exception("Error"))) + RageshakeDetectionEvents.ProcessScreenshot(ImageResult.Error(AN_EXCEPTION)) ) assertThat(awaitItem().showDialog).isTrue() initialState.eventSink.invoke(RageshakeDetectionEvents.Dismiss) diff --git a/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt b/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt index e15c08fecc..94256c8888 100644 --- a/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt +++ b/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt @@ -28,6 +28,7 @@ import io.element.android.libraries.dateformatter.LastMessageFormatter import io.element.android.libraries.designsystem.components.avatar.AvatarData import io.element.android.libraries.matrix.core.SessionId import io.element.android.libraries.matrixtest.AN_AVATAR_URL +import io.element.android.libraries.matrixtest.AN_EXCEPTION import io.element.android.libraries.matrixtest.A_MESSAGE import io.element.android.libraries.matrixtest.A_ROOM_ID import io.element.android.libraries.matrixtest.A_ROOM_NAME @@ -69,8 +70,8 @@ class RoomListPresenterTests { val presenter = RoomListPresenter( FakeMatrixClient( SessionId("sessionId"), - userDisplayName = Result.failure(Exception("Error")), - userAvatarURLString = Result.failure(Exception("Error")), + userDisplayName = Result.failure(AN_EXCEPTION), + userAvatarURLString = Result.failure(AN_EXCEPTION), ), createDateFormatter() ) diff --git a/libraries/matrixtest/src/main/kotlin/io/element/android/libraries/matrixtest/TestData.kt b/libraries/matrixtest/src/main/kotlin/io/element/android/libraries/matrixtest/TestData.kt index cd186c50c2..970a3882ab 100644 --- a/libraries/matrixtest/src/main/kotlin/io/element/android/libraries/matrixtest/TestData.kt +++ b/libraries/matrixtest/src/main/kotlin/io/element/android/libraries/matrixtest/TestData.kt @@ -38,4 +38,7 @@ const val A_SESSION_ID = "sessionId" const val AN_AVATAR_URL = "mxc://data" -val A_FAILURE = Throwable("error") +const val A_FAILURE_REASON = "There has been a failure" +val A_THROWABLE = Throwable(A_FAILURE_REASON) +val AN_EXCEPTION = Exception(A_FAILURE_REASON) +