From 059d8ec6ce47fa037f042ab2377a455f0b2bccbd Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 9 Feb 2023 14:27:48 +0100 Subject: [PATCH] More tests for `RageshakeDetectionPresenterTest` --- features/rageshake/build.gradle.kts | 1 + .../RageshakeDetectionPresenterTest.kt | 43 ++++++++++++++++++- gradle/libs.versions.toml | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/features/rageshake/build.gradle.kts b/features/rageshake/build.gradle.kts index 17160b37ab..45cc9cdd64 100644 --- a/features/rageshake/build.gradle.kts +++ b/features/rageshake/build.gradle.kts @@ -52,6 +52,7 @@ dependencies { testImplementation(libs.test.truth) testImplementation(libs.test.turbine) testImplementation(projects.libraries.matrixtest) + testImplementation(libs.test.mockk) androidTestImplementation(libs.test.junitext) } 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 6b0d1e38f7..f5fb8cbedc 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 @@ -18,6 +18,7 @@ package io.element.android.features.rageshake.detection +import android.graphics.Bitmap import app.cash.molecule.RecompositionClock import app.cash.molecule.moleculeFlow import app.cash.turbine.test @@ -27,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.mockk.mockk import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest @@ -83,7 +85,41 @@ class RageshakeDetectionPresenterTest { } @Test - fun `present - screenshot then dismiss`() = runTest { + fun `present - screenshot with success then dismiss`() = runTest { + val screenshotHolder = FakeScreenshotHolder(screenshotUri = null) + val rageshake = FakeRageShake(isAvailableValue = true) + val rageshakeDataStore = FakeRageshakeDataStore(isEnabled = true) + val presenter = RageshakeDetectionPresenter( + screenshotHolder = screenshotHolder, + rageShake = rageshake, + preferencesPresenter = RageshakePreferencesPresenter( + rageshake = rageshake, + rageshakeDataStore = rageshakeDataStore, + ) + ) + moleculeFlow(RecompositionClock.Immediate) { + presenter.present() + }.test { + skipItems(1) + val initialState = awaitItem() + assertThat(initialState.isStarted).isFalse() + initialState.eventSink.invoke(RageshakeDetectionEvents.StartDetection) + assertThat(awaitItem().isStarted).isTrue() + rageshake.triggerPhoneRageshake() + assertThat(awaitItem().takeScreenshot).isTrue() + initialState.eventSink.invoke( + RageshakeDetectionEvents.ProcessScreenshot(ImageResult.Success(aBitmap())) + ) + assertThat(awaitItem().showDialog).isTrue() + initialState.eventSink.invoke(RageshakeDetectionEvents.Dismiss) + val finalState = awaitItem() + assertThat(finalState.showDialog).isFalse() + assertThat(rageshakeDataStore.isEnabled().first()).isTrue() + } + } + + @Test + fun `present - screenshot with error then dismiss`() = runTest { val screenshotHolder = FakeScreenshotHolder(screenshotUri = null) val rageshake = FakeRageShake(isAvailableValue = true) val rageshakeDataStore = FakeRageshakeDataStore(isEnabled = true) @@ -140,7 +176,7 @@ class RageshakeDetectionPresenterTest { rageshake.triggerPhoneRageshake() assertThat(awaitItem().takeScreenshot).isTrue() initialState.eventSink.invoke( - RageshakeDetectionEvents.ProcessScreenshot(ImageResult.Error(Exception("Error"))) + RageshakeDetectionEvents.ProcessScreenshot(ImageResult.Success(aBitmap())) ) assertThat(awaitItem().showDialog).isTrue() initialState.eventSink.invoke(RageshakeDetectionEvents.Disable) @@ -150,3 +186,6 @@ class RageshakeDetectionPresenterTest { } } } + +private fun aBitmap(): Bitmap = mockk() + diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 036a9060be..8ed886b47f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -97,7 +97,7 @@ test_junit = "junit:junit:4.13.2" test_runner = "androidx.test:runner:1.4.0" test_uiautomator = "androidx.test.uiautomator:uiautomator:2.2.0" test_junitext = "androidx.test.ext:junit:1.1.3" -test_mockk = "io.mockk:mockk:1.13.2" +test_mockk = "io.mockk:mockk:1.13.4" test_barista = "com.adevinta.android:barista:4.2.0" test_hamcrest = "org.hamcrest:hamcrest:2.2" test_orchestrator = "androidx.test:orchestrator:1.4.1"