From b7a6d8996a16c1c2182121662d2afe5f9ffce5e7 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 30 Nov 2023 15:41:40 +0100 Subject: [PATCH] Add unit test for AndroidLocalMediaActions --- libraries/mediaviewer/impl/build.gradle.kts | 2 + .../local/AndroidLocalMediaActionsTest.kt | 81 +++++++++++++++++++ .../local/NoOpActivityResultRegistryOwner.kt | 36 +++++++++ 3 files changed, 119 insertions(+) create mode 100644 libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/AndroidLocalMediaActionsTest.kt create mode 100644 libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/NoOpActivityResultRegistryOwner.kt diff --git a/libraries/mediaviewer/impl/build.gradle.kts b/libraries/mediaviewer/impl/build.gradle.kts index b47617be8c..0ccfd0b65e 100644 --- a/libraries/mediaviewer/impl/build.gradle.kts +++ b/libraries/mediaviewer/impl/build.gradle.kts @@ -46,6 +46,8 @@ dependencies { testImplementation(libs.test.junit) testImplementation(libs.test.truth) testImplementation(libs.test.mockk) + testImplementation(libs.molecule.runtime) + testImplementation(libs.test.turbine) testImplementation(libs.test.robolectric) testImplementation(libs.test.turbine) testImplementation(libs.coroutines.core) diff --git a/libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/AndroidLocalMediaActionsTest.kt b/libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/AndroidLocalMediaActionsTest.kt new file mode 100644 index 0000000000..0e981d73af --- /dev/null +++ b/libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/AndroidLocalMediaActionsTest.kt @@ -0,0 +1,81 @@ +/* + * 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.libraries.mediaviewer.impl.local + +import android.net.Uri +import androidx.activity.compose.LocalActivityResultRegistryOwner +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.platform.LocalContext +import app.cash.molecule.RecompositionMode +import app.cash.molecule.moleculeFlow +import app.cash.turbine.test +import com.google.common.truth.Truth.assertThat +import io.element.android.libraries.matrix.test.core.aBuildMeta +import io.element.android.libraries.mediaviewer.test.viewer.aLocalMedia +import io.element.android.tests.testutils.testCoroutineDispatchers +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.runTest +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +@RunWith(RobolectricTestRunner::class) +class AndroidLocalMediaActionsTest { + + @Test + fun `present - AndroidLocalMediaAction configure`() = runTest { + val sut = createAndroidLocalMediaActions() + moleculeFlow(RecompositionMode.Immediate) { + CompositionLocalProvider( + LocalContext provides RuntimeEnvironment.getApplication(), + LocalActivityResultRegistryOwner provides NoOpActivityResultRegistryOwner() + ) { + sut.Configure() + } + }.test { + awaitItem() + } + } + + @Test + fun `test AndroidLocalMediaAction share`() = runTest { + val sut = createAndroidLocalMediaActions() + val result = sut.share(aLocalMedia(Uri.parse("file://afile"))) + assertThat(result.exceptionOrNull()).isNotNull() + } + + @Test + fun `test AndroidLocalMediaAction open`() = runTest { + val sut = createAndroidLocalMediaActions() + val result = sut.open(aLocalMedia(Uri.parse("file://afile"))) + assertThat(result.exceptionOrNull()).isNotNull() + } + + @Test + fun `test AndroidLocalMediaAction save on disk`() = runTest { + val sut = createAndroidLocalMediaActions() + val result = sut.saveOnDisk(aLocalMedia(Uri.parse("file://afile"))) + assertThat(result.exceptionOrNull()).isNotNull() + } + + private fun TestScope.createAndroidLocalMediaActions() = AndroidLocalMediaActions( + RuntimeEnvironment.getApplication(), + testCoroutineDispatchers(), + aBuildMeta() + ) +} diff --git a/libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/NoOpActivityResultRegistryOwner.kt b/libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/NoOpActivityResultRegistryOwner.kt new file mode 100644 index 0000000000..9d774bb163 --- /dev/null +++ b/libraries/mediaviewer/impl/src/test/kotlin/io/element/android/libraries/mediaviewer/impl/local/NoOpActivityResultRegistryOwner.kt @@ -0,0 +1,36 @@ +/* + * 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.libraries.mediaviewer.impl.local + +import androidx.activity.result.ActivityResultRegistry +import androidx.activity.result.ActivityResultRegistryOwner +import androidx.activity.result.contract.ActivityResultContract +import androidx.core.app.ActivityOptionsCompat + +class NoOpActivityResultRegistryOwner : ActivityResultRegistryOwner { + override val activityResultRegistry: ActivityResultRegistry + get() = NoOpActivityResultRegistry() +} + +class NoOpActivityResultRegistry : ActivityResultRegistry() { + override fun onLaunch( + requestCode: Int, + contract: ActivityResultContract, + input: I, + options: ActivityOptionsCompat?, + ) = Unit +}