From b305ccf382c33b72969da8d94363d9946323232f Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Thu, 7 Mar 2024 14:50:42 -0700 Subject: [PATCH] add Paparazzi tests for the LanguageDownloadProgressIndicator --- .../LanguageDownloadProgressIndicator.kt | 34 ++++++++++++------- ...st_LanguageDownloadProgressIndicator().png | 3 ++ ...eDownloadProgressIndicatorPaparazziTest.kt | 30 ++++++++++++++++ .../base/ui/compose/CompositionLocals.kt | 8 +++-- .../godtools/base/ui/theme/GodToolsTheme.kt | 4 +-- 5 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 app/src/test/snapshots/images/org.cru.godtools.ui.languages.downloadable_LanguageDownloadProgressIndicatorPaparazziTest_LanguageDownloadProgressIndicator().png create mode 100644 app/src/testDebug/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicatorPaparazziTest.kt diff --git a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt index c659fa927f..c028f5bef2 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicator.kt @@ -2,6 +2,9 @@ package org.cru.godtools.ui.languages.downloadable import androidx.compose.animation.core.animateFloatAsState import androidx.compose.foundation.border +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.aspectRatio +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape @@ -14,20 +17,23 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier -import androidx.compose.ui.unit.Dp +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.dp +private val DEFAULT_ICON_SIZE = 24.dp + @Composable internal fun LanguageDownloadProgressIndicator( isPinned: Boolean, downloaded: Int, total: Int, modifier: Modifier = Modifier, - iconSize: Dp = 24.dp, ) { val total = total.coerceAtLeast(0) val downloaded = downloaded.coerceIn(0, total) - val contentModifier = modifier.size(iconSize) + val contentModifier = modifier + .size(DEFAULT_ICON_SIZE) + .aspectRatio(1f) when { !isPinned -> Icon( @@ -51,15 +57,19 @@ internal fun LanguageDownloadProgressIndicator( }, ) - val iconPadding = iconSize / 12 - CircularProgressIndicator( - progress = { progress }, - color = MaterialTheme.colorScheme.primary, - strokeWidth = (iconSize / 2) - iconPadding, - modifier = contentModifier - .padding(iconPadding) - .border(iconSize / 12, MaterialTheme.colorScheme.primary, CircleShape) - ) + BoxWithConstraints(contentModifier) { + val size = with(LocalDensity.current) { constraints.maxWidth.toDp() } + val iconPadding = size / 12 + CircularProgressIndicator( + progress = { progress }, + color = MaterialTheme.colorScheme.primary, + strokeWidth = (size / 2) - iconPadding, + modifier = Modifier + .fillMaxSize() + .padding(iconPadding) + .border(size / 12, MaterialTheme.colorScheme.primary, CircleShape) + ) + } } } } diff --git a/app/src/test/snapshots/images/org.cru.godtools.ui.languages.downloadable_LanguageDownloadProgressIndicatorPaparazziTest_LanguageDownloadProgressIndicator().png b/app/src/test/snapshots/images/org.cru.godtools.ui.languages.downloadable_LanguageDownloadProgressIndicatorPaparazziTest_LanguageDownloadProgressIndicator().png new file mode 100644 index 0000000000..6935f93a05 --- /dev/null +++ b/app/src/test/snapshots/images/org.cru.godtools.ui.languages.downloadable_LanguageDownloadProgressIndicatorPaparazziTest_LanguageDownloadProgressIndicator().png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c6f61efafe1de02467b685c34a38021c17af15ca469fcd6650ee6de5160740ed +size 9218 diff --git a/app/src/testDebug/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicatorPaparazziTest.kt b/app/src/testDebug/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicatorPaparazziTest.kt new file mode 100644 index 0000000000..015e659f3d --- /dev/null +++ b/app/src/testDebug/kotlin/org/cru/godtools/ui/languages/downloadable/LanguageDownloadProgressIndicatorPaparazziTest.kt @@ -0,0 +1,30 @@ +package org.cru.godtools.ui.languages.downloadable + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Row +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import app.cash.paparazzi.Paparazzi +import kotlin.test.Test +import org.cru.godtools.base.ui.theme.GodToolsTheme +import org.junit.Rule + +class LanguageDownloadProgressIndicatorPaparazziTest { + @get:Rule + val paparazzi = Paparazzi() + + @Test + fun `LanguageDownloadProgressIndicator()`() { + paparazzi.snapshot { + GodToolsTheme(disableDagger = true) { + Box { + Row(Modifier.align(Alignment.Center)) { + for (it in 0..5) { + LanguageDownloadProgressIndicator(isPinned = true, downloaded = it, total = 5) + } + } + } + } + } + } +} diff --git a/ui/base/src/main/kotlin/org/cru/godtools/base/ui/compose/CompositionLocals.kt b/ui/base/src/main/kotlin/org/cru/godtools/base/ui/compose/CompositionLocals.kt index 353e063323..8798296b25 100644 --- a/ui/base/src/main/kotlin/org/cru/godtools/base/ui/compose/CompositionLocals.kt +++ b/ui/base/src/main/kotlin/org/cru/godtools/base/ui/compose/CompositionLocals.kt @@ -22,11 +22,13 @@ import org.greenrobot.eventbus.EventBus val LocalEventBus = staticCompositionLocalOf { EventBus() } @Composable -internal fun CompositionLocals(content: @Composable () -> Unit) { +internal fun CompositionLocals(disableDagger: Boolean = false, content: @Composable () -> Unit) { val context = LocalContext.current val daggerComponents = when { - LocalInspectionMode.current -> object : ComposeEntryPoint { - override val eventBus = EventBus() + LocalInspectionMode.current || disableDagger -> remember { + object : ComposeEntryPoint { + override val eventBus = EventBus() + } } else -> remember { EntryPointAccessors.fromApplication(context) } } diff --git a/ui/base/src/main/kotlin/org/cru/godtools/base/ui/theme/GodToolsTheme.kt b/ui/base/src/main/kotlin/org/cru/godtools/base/ui/theme/GodToolsTheme.kt index 64f2207b9b..f03d56ead2 100644 --- a/ui/base/src/main/kotlin/org/cru/godtools/base/ui/theme/GodToolsTheme.kt +++ b/ui/base/src/main/kotlin/org/cru/godtools/base/ui/theme/GodToolsTheme.kt @@ -114,7 +114,7 @@ object GodToolsTheme { } @Composable -fun GodToolsTheme(content: @Composable () -> Unit) { +fun GodToolsTheme(disableDagger: Boolean = false, content: @Composable () -> Unit) { val isDarkTheme = isSystemInDarkTheme() && BuildConfig.DEBUG MaterialTheme( colorScheme = when { @@ -123,7 +123,7 @@ fun GodToolsTheme(content: @Composable () -> Unit) { }, typography = GodToolsTheme.typography ) { - CompositionLocals { + CompositionLocals(disableDagger) { CompositionLocalProvider( LocalLightColorSchemeActive provides !isDarkTheme, LocalContentColor provides contentColorFor(MaterialTheme.colorScheme.background),