More tests for RageshakeDetectionPresenterTest

This commit is contained in:
Benoit Marty 2023-02-09 14:27:48 +01:00 committed by Benoit Marty
parent afbae0a15d
commit 059d8ec6ce
3 changed files with 43 additions and 3 deletions

View file

@ -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)
}

View file

@ -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()

View file

@ -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"