Add test on MediaViewerView
This commit is contained in:
parent
1dc6b0273b
commit
c35fca3a1b
3 changed files with 187 additions and 1 deletions
|
|
@ -22,6 +22,11 @@ plugins {
|
||||||
|
|
||||||
android {
|
android {
|
||||||
namespace = "io.element.android.libraries.mediaviewer.api"
|
namespace = "io.element.android.libraries.mediaviewer.api"
|
||||||
|
testOptions {
|
||||||
|
unitTests {
|
||||||
|
isIncludeAndroidResources = true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
anvil {
|
anvil {
|
||||||
|
|
@ -60,6 +65,8 @@ dependencies {
|
||||||
testImplementation(libs.test.turbine)
|
testImplementation(libs.test.turbine)
|
||||||
testImplementation(libs.coroutines.core)
|
testImplementation(libs.coroutines.core)
|
||||||
testImplementation(libs.coroutines.test)
|
testImplementation(libs.coroutines.test)
|
||||||
|
testImplementation(libs.androidx.compose.ui.test.junit)
|
||||||
|
testReleaseImplementation(libs.androidx.compose.ui.test.manifest)
|
||||||
|
|
||||||
ksp(libs.showkase.processor)
|
ksp(libs.showkase.processor)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,7 @@ fun aMediaViewerState(
|
||||||
mediaInfo: MediaInfo = anImageInfo(),
|
mediaInfo: MediaInfo = anImageInfo(),
|
||||||
canDownload: Boolean = true,
|
canDownload: Boolean = true,
|
||||||
canShare: Boolean = true,
|
canShare: Boolean = true,
|
||||||
|
eventSink: (MediaViewerEvents) -> Unit = {},
|
||||||
) = MediaViewerState(
|
) = MediaViewerState(
|
||||||
mediaInfo = mediaInfo,
|
mediaInfo = mediaInfo,
|
||||||
thumbnailSource = null,
|
thumbnailSource = null,
|
||||||
|
|
@ -94,4 +95,5 @@ fun aMediaViewerState(
|
||||||
snackbarMessage = null,
|
snackbarMessage = null,
|
||||||
canDownload = canDownload,
|
canDownload = canDownload,
|
||||||
canShare = canShare,
|
canShare = canShare,
|
||||||
) {}
|
eventSink = eventSink,
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,177 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024 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.api.viewer
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.compose.ui.test.assertHasClickAction
|
||||||
|
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
|
||||||
|
import androidx.compose.ui.test.junit4.createAndroidComposeRule
|
||||||
|
import androidx.compose.ui.test.onNodeWithContentDescription
|
||||||
|
import androidx.compose.ui.test.performClick
|
||||||
|
import androidx.compose.ui.test.performTouchInput
|
||||||
|
import androidx.compose.ui.test.swipeDown
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import io.element.android.libraries.architecture.AsyncData
|
||||||
|
import io.element.android.libraries.mediaviewer.api.local.LocalMedia
|
||||||
|
import io.element.android.libraries.mediaviewer.api.local.anImageInfo
|
||||||
|
import io.element.android.libraries.ui.strings.CommonStrings
|
||||||
|
import io.element.android.tests.testutils.EnsureNeverCalled
|
||||||
|
import io.element.android.tests.testutils.EventsRecorder
|
||||||
|
import io.element.android.tests.testutils.clickOn
|
||||||
|
import io.element.android.tests.testutils.ensureCalledOnce
|
||||||
|
import io.element.android.tests.testutils.pressBack
|
||||||
|
import org.junit.Ignore
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.rules.TestRule
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class MediaViewerViewTest {
|
||||||
|
@get:Rule val rule = createAndroidComposeRule<ComponentActivity>()
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on back invokes expected callback`() {
|
||||||
|
val eventsRecorder = EventsRecorder<MediaViewerEvents>(expectEvents = false)
|
||||||
|
ensureCalledOnce { callback ->
|
||||||
|
rule.setMediaViewerView(
|
||||||
|
aMediaViewerState(
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onBackPressed = callback,
|
||||||
|
)
|
||||||
|
rule.pressBack()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on open emit expected Event`() {
|
||||||
|
testMenuAction(CommonStrings.action_open_with, MediaViewerEvents.OpenWith)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on save emit expected Event`() {
|
||||||
|
testMenuAction(CommonStrings.action_save, MediaViewerEvents.SaveOnDisk)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `clicking on share emit expected Event`() {
|
||||||
|
testMenuAction(CommonStrings.action_share, MediaViewerEvents.Share)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun testMenuAction(contentDescriptionRes: Int, expectedEvent: MediaViewerEvents) {
|
||||||
|
val eventsRecorder = EventsRecorder<MediaViewerEvents>()
|
||||||
|
rule.setMediaViewerView(
|
||||||
|
aMediaViewerState(
|
||||||
|
downloadedMedia = AsyncData.Success(
|
||||||
|
LocalMedia(Uri.EMPTY, anImageInfo())
|
||||||
|
),
|
||||||
|
mediaInfo = anImageInfo(),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
)
|
||||||
|
val contentDescription = rule.activity.getString(contentDescriptionRes)
|
||||||
|
rule.onNodeWithContentDescription(contentDescription).performClick()
|
||||||
|
eventsRecorder.assertSingle(expectedEvent)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Ignore("This test is not passing yet, maybe due to interaction with ZoomableAsyncImage?")
|
||||||
|
@Test
|
||||||
|
fun `clicking on image hides the overlay`() {
|
||||||
|
val eventsRecorder = EventsRecorder<MediaViewerEvents>(expectEvents = false)
|
||||||
|
rule.setMediaViewerView(
|
||||||
|
aMediaViewerState(
|
||||||
|
downloadedMedia = AsyncData.Success(
|
||||||
|
LocalMedia(Uri.EMPTY, anImageInfo())
|
||||||
|
),
|
||||||
|
mediaInfo = anImageInfo(),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
)
|
||||||
|
// Ensure that the action are visible
|
||||||
|
val contentDescription = rule.activity.getString(CommonStrings.action_open_with)
|
||||||
|
rule.onNodeWithContentDescription(contentDescription).assertHasClickAction()
|
||||||
|
val imageContentDescription = rule.activity.getString(CommonStrings.common_image)
|
||||||
|
rule.onNodeWithContentDescription(imageContentDescription).performClick()
|
||||||
|
// assertHasNoClickAction does not work as expected (?)
|
||||||
|
// rule.onNodeWithContentDescription(contentDescription).assertHasNoClickAction()
|
||||||
|
rule.onNodeWithContentDescription(contentDescription).performClick()
|
||||||
|
// No emitted event
|
||||||
|
}
|
||||||
|
|
||||||
|
@Ignore("This test is not passing yet, maybe due to interaction with ZoomableAsyncImage?")
|
||||||
|
@Test
|
||||||
|
fun `clicking swipe on the image invokes the expected callback`() {
|
||||||
|
val eventsRecorder = EventsRecorder<MediaViewerEvents>(expectEvents = false)
|
||||||
|
ensureCalledOnce { callback ->
|
||||||
|
rule.setMediaViewerView(
|
||||||
|
aMediaViewerState(
|
||||||
|
downloadedMedia = AsyncData.Success(
|
||||||
|
LocalMedia(Uri.EMPTY, anImageInfo())
|
||||||
|
),
|
||||||
|
mediaInfo = anImageInfo(),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
onBackPressed = callback,
|
||||||
|
)
|
||||||
|
val imageContentDescription = rule.activity.getString(CommonStrings.common_image)
|
||||||
|
rule.onNodeWithContentDescription(imageContentDescription).performTouchInput { swipeDown() }
|
||||||
|
rule.mainClock.advanceTimeBy(1_000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `error case, click on retry emits the expected Event`() {
|
||||||
|
val eventsRecorder = EventsRecorder<MediaViewerEvents>()
|
||||||
|
rule.setMediaViewerView(
|
||||||
|
aMediaViewerState(
|
||||||
|
downloadedMedia = AsyncData.Failure(IllegalStateException("error")),
|
||||||
|
mediaInfo = anImageInfo(),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
)
|
||||||
|
rule.clickOn(CommonStrings.action_retry)
|
||||||
|
eventsRecorder.assertSingle(MediaViewerEvents.RetryLoading)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `error case, click on cancel emits the expected Event`() {
|
||||||
|
val eventsRecorder = EventsRecorder<MediaViewerEvents>()
|
||||||
|
rule.setMediaViewerView(
|
||||||
|
aMediaViewerState(
|
||||||
|
downloadedMedia = AsyncData.Failure(IllegalStateException("error")),
|
||||||
|
mediaInfo = anImageInfo(),
|
||||||
|
eventSink = eventsRecorder
|
||||||
|
),
|
||||||
|
)
|
||||||
|
rule.clickOn(CommonStrings.action_cancel)
|
||||||
|
eventsRecorder.assertSingle(MediaViewerEvents.ClearLoadingError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setMediaViewerView(
|
||||||
|
state: MediaViewerState,
|
||||||
|
onBackPressed: () -> Unit = EnsureNeverCalled(),
|
||||||
|
) {
|
||||||
|
setContent {
|
||||||
|
MediaViewerView(
|
||||||
|
state = state,
|
||||||
|
onBackPressed = onBackPressed,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue