Skip to content

Commit

Permalink
Merge pull request #3487 from CruGlobal/toolDetailsPaparazzi
Browse files Browse the repository at this point in the history
create some Paparazzi tests for ToolDetailsLayout
  • Loading branch information
frett authored Apr 27, 2024
2 parents 08f3313 + f28a2f0 commit d08655e
Show file tree
Hide file tree
Showing 36 changed files with 240 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import com.google.android.play.core.review.ReviewManagerFactory
import kotlinx.coroutines.launch
import org.ccci.gto.android.common.androidx.compose.foundation.layout.padding
import org.ccci.gto.android.common.androidx.compose.material3.ui.navigationdrawer.NavigationDrawerHeadline
import org.cru.godtools.BuildConfig
import org.cru.godtools.R
import org.cru.godtools.analytics.model.AnalyticsScreenEvent
import org.cru.godtools.base.appLanguage
Expand Down Expand Up @@ -321,7 +320,7 @@ private fun DrawerContentLayout(state: State) = ModalDrawerSheet {
// endregion About

NavigationDrawerHeadline(label = {
Text(stringResource(R.string.menu_version, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE))
Text(stringResource(R.string.menu_version, state.versionName, state.versionCode))
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import com.slack.circuit.runtime.CircuitUiEvent
import com.slack.circuit.runtime.CircuitUiState
import com.slack.circuit.runtime.screen.Screen
import kotlinx.parcelize.Parcelize
import org.cru.godtools.BuildConfig

@Parcelize
data object DrawerMenuScreen : Screen {
data class State(
val drawerState: DrawerState = DrawerState(DrawerValue.Closed),
val isLoggedIn: Boolean = false,
val versionName: String = BuildConfig.VERSION_NAME,
val versionCode: Int = BuildConfig.VERSION_CODE,
val eventSink: (Event) -> Unit = {},
) : CircuitUiState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private fun ToolDetailsContent(state: State, modifier: Modifier = Modifier) {
val secondLanguage by rememberUpdatedState(state.secondLanguage)
val secondTranslation by rememberUpdatedState(state.secondTranslation)
val downloadProgress by rememberUpdatedState(state.downloadProgress)
val shares by remember { derivedStateOf { tool?.shares ?: 0 } }
val shares by remember { derivedStateOf { tool?.totalShares ?: 0 } }
val pages by rememberUpdatedState(state.pages)

val coroutineScope = rememberCoroutineScope()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.cru.godtools.ui.drawer

import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import org.cru.godtools.ui.drawer.DrawerMenuScreen.State

object DrawerMenuScreenStateTestData {
val open = State(
drawerState = DrawerState(DrawerValue.Open),
versionName = "Paparazzi Snapshot",
versionCode = 1
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.cru.godtools.ui.tooldetails

import android.graphics.drawable.Drawable
import app.cash.paparazzi.DeviceConfig
import coil.Coil
import coil.ImageLoader
import coil.annotation.ExperimentalCoilApi
import coil.test.FakeImageLoaderEngine
import com.android.resources.NightMode
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import java.util.Locale
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.cru.godtools.downloadmanager.DownloadProgress
import org.cru.godtools.model.Language
import org.cru.godtools.model.randomTool
import org.cru.godtools.model.randomTranslation
import org.cru.godtools.ui.BasePaparazziTest
import org.cru.godtools.ui.drawer.DrawerMenuScreenStateTestData
import org.cru.godtools.ui.tooldetails.ToolDetailsScreen.State
import org.cru.godtools.ui.tools.ToolCard
import org.cru.godtools.ui.tools.ToolCardStateTestData
import org.junit.runner.RunWith

@RunWith(TestParameterInjector::class)
class ToolDetailsLayoutPaparazziTest(
@TestParameter(valuesProvider = DeviceConfigProvider::class) deviceConfig: DeviceConfig,
@TestParameter nightMode: NightMode,
@TestParameter accessibilityMode: AccessibilityMode,
) : BasePaparazziTest(deviceConfig = deviceConfig, nightMode = nightMode, accessibilityMode = accessibilityMode) {
private val banner = ToolCardStateTestData.banner
private val variants: ImmutableList<ToolCard.State> = persistentListOf(
ToolCardStateTestData.tool.copy(secondLanguage = null),
ToolCardStateTestData.tool.copy(secondLanguage = null)
)

private val state = State(
tool = randomTool(
detailsBannerYoutubeVideoId = null,
shares = 123355,
pendingShares = 101,
isFavorite = false
),
translation = randomTranslation(
name = "Tool",
description = "Description",
),
banner = banner,
)

@BeforeTest
@OptIn(ExperimentalCoilApi::class, ExperimentalCoroutinesApi::class)
fun setup() {
Dispatchers.setMain(UnconfinedTestDispatcher())
val file = Drawable.createFromStream(javaClass.getResourceAsStream("../tools/banner.jpg"), "banner.jpg")!!
Coil.setImageLoader(
ImageLoader.Builder(paparazzi.context)
.components {
add(
FakeImageLoaderEngine.Builder()
.intercept(banner, file)
.build()
)
}
.build()
)
}

@AfterTest
@OptIn(ExperimentalCoroutinesApi::class)
fun cleanup() {
Coil.reset()
Dispatchers.resetMain()
}

@Test
fun `ToolDetailsLayout()`() = snapshot { ToolDetailsLayout(state) }

@Test
fun `ToolDetailsLayout() - Drawer Open`() = snapshot {
ToolDetailsLayout(state.copy(drawerState = DrawerMenuScreenStateTestData.open))
}

@Test
fun `ToolDetailsLayout() - isFavorite=true`() = snapshot {
ToolDetailsLayout(
state.copy(
tool = randomTool(
detailsBannerYoutubeVideoId = null,
shares = 123456,
pendingShares = 0,
isFavorite = true
)
)
)
}

@Test
fun `ToolDetailsLayout() - Downloading`() = snapshot {
ToolDetailsLayout(state.copy(downloadProgress = DownloadProgress(2, 5)))
}

@Test
fun `ToolDetailsLayout() - Variants`() = snapshot {
ToolDetailsLayout(state.copy(variants = variants, pages = persistentListOf(ToolDetailsScreen.Page.VARIANTS)))
}

@Test
fun `ToolDetailsLayout() - Second Language Available`() = snapshot {
ToolDetailsLayout(
state.copy(
secondLanguage = Language(Locale.FRENCH),
secondTranslation = randomTranslation(),
)
)
}
}
9 changes: 6 additions & 3 deletions library/model/src/main/kotlin/org/cru/godtools/model/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,14 @@ fun randomTool(
bannerId: Long? = Random.nextLong().takeIf { Random.nextBoolean() },
detailsBannerId: Long? = Random.nextLong().takeIf { Random.nextBoolean() },
detailsBannerAnimationId: Long? = Random.nextLong().takeIf { Random.nextBoolean() },
detailsBannerYoutubeVideoId: String? = UUID.randomUUID().toString().takeIf { Random.nextBoolean() },
defaultLocale: Locale = Tool.DEFAULT_DEFAULT_LOCALE,
defaultOrder: Int = Random.nextInt(),
isFavorite: Boolean = Random.nextBoolean(),
isHidden: Boolean = Random.nextBoolean(),
isSpotlight: Boolean = Random.nextBoolean(),
shares: Int = Random.nextInt(),
pendingShares: Int = Random.nextInt(),
metatoolCode: String? = UUID.randomUUID().toString().takeIf { Random.nextBoolean() },
defaultVariantCode: String? = UUID.randomUUID().toString().takeIf { Random.nextBoolean() },
apiId: Long? = Random.nextLong().takeIf { Random.nextBoolean() },
Expand All @@ -271,16 +274,16 @@ fun randomTool(
bannerId = bannerId,
detailsBannerId = detailsBannerId,
detailsBannerAnimationId = detailsBannerAnimationId,
detailsBannerYoutubeVideoId = UUID.randomUUID().toString().takeIf { Random.nextBoolean() },
detailsBannerYoutubeVideoId = detailsBannerYoutubeVideoId,
defaultLocale = defaultLocale,
defaultOrder = defaultOrder,
order = Random.nextInt(),
isFavorite = isFavorite,
isHidden = isHidden,
isSpotlight = isSpotlight,
isScreenShareDisabled = Random.nextBoolean(),
shares = Random.nextInt(),
pendingShares = Random.nextInt(),
shares = shares,
pendingShares = pendingShares,
metatoolCode = metatoolCode,
defaultVariantCode = defaultVariantCode,
apiId = apiId,
Expand Down

0 comments on commit d08655e

Please sign in to comment.