Skip to content

Commit

Permalink
Added component screenshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEblan committed Sep 8, 2024
1 parent 0af73a7 commit 022ec08
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 177 deletions.
4 changes: 2 additions & 2 deletions core/design-system/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ dependencies {
api(libs.androidx.compose.foundation)
api(libs.androidx.compose.foundation.layout)
api(libs.androidx.compose.material3)
api(libs.androidx.compose.material3.adaptive)
api(libs.androidx.compose.material.iconsExtended)
api(libs.androidx.compose.runtime)
api(libs.androidx.compose.ui.util)
api(libs.coil.kt.compose)

testImplementation(libs.androidx.compose.ui.test)
testImplementation(libs.androidx.compose.ui.test.manifest)
testImplementation(libs.accompanist.testharness)
testImplementation(libs.hilt.android.testing)
testImplementation(libs.robolectric)
testImplementation(libs.roborazzi)
testImplementation(projects.core.screenshotTesting)
testImplementation(projects.core.testing)

androidTestImplementation(libs.androidx.compose.ui.test)
androidTestImplementation(projects.core.testing)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.eblan.socialworkreviewer.core.designsystem.component

import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ProgressIndicatorDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap

@Composable
fun SwrLinearProgressIndicator(
progress: () -> Float,
modifier: Modifier = Modifier,
color: Color = ProgressIndicatorDefaults.linearColor,
trackColor: Color = ProgressIndicatorDefaults.linearTrackColor,
strokeCap: StrokeCap = ProgressIndicatorDefaults.LinearStrokeCap,
) {
LinearProgressIndicator(
progress = progress,
modifier = modifier,
color = color,
trackColor = trackColor,
strokeCap = strokeCap,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.eblan.socialworkreviewer.core.designsystem.component

import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SwrLargeTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable (RowScope.() -> Unit) = {},
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
colors: TopAppBarColors = TopAppBarDefaults.largeTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null,
) {
LargeTopAppBar(
title = title,
modifier = modifier,
navigationIcon = navigationIcon,
actions = actions,
windowInsets = windowInsets,
colors = colors,
scrollBehavior = scrollBehavior,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.eblan.socialworkreviewer.core.designsystem

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import com.eblan.socialworkreviewer.core.designsystem.component.SwrLinearProgressIndicator
import com.eblan.socialworkreviewer.core.screenshottesting.util.captureMultiTheme
import dagger.hilt.android.testing.HiltTestApplication
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode

@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(application = HiltTestApplication::class, qualifiers = "480dpi")
@LooperMode(LooperMode.Mode.PAUSED)
class ProgressIndicatorScreenshotTests {

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Test
fun progressIndicator_multipleThemes() {
composeTestRule.captureMultiTheme("ProgressIndicator") {
SwrLinearProgressIndicator(
progress = { 0.5f },
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.eblan.socialworkreviewer.core.designsystem

import androidx.activity.ComponentActivity
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onRoot
import com.eblan.socialworkreviewer.core.designsystem.component.SwrLargeTopAppBar
import com.eblan.socialworkreviewer.core.designsystem.theme.LocalGradientColors
import com.eblan.socialworkreviewer.core.screenshottesting.util.DefaultRoborazziOptions
import com.eblan.socialworkreviewer.core.screenshottesting.util.captureMultiTheme
import com.github.takahirom.roborazzi.captureRoboImage
import dagger.hilt.android.testing.HiltTestApplication
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.annotation.LooperMode
import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.test.FontScale
import com.eblan.socialworkreviewer.core.designsystem.theme.SwrTheme

@OptIn(ExperimentalMaterial3Api::class)
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(application = HiltTestApplication::class, qualifiers = "480dpi")
@LooperMode(LooperMode.Mode.PAUSED)
class TopAppBarScreenshotTests {

@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Test
fun largeTopAppBar_multipleThemes() {
composeTestRule.captureMultiTheme("LargeTopAppBar") {
SwrLargeTopAppBarExample()
}
}

@Test
fun largeTopAppBar_hugeFont() {
composeTestRule.setContent {
CompositionLocalProvider(
LocalInspectionMode provides true,
) {
DeviceConfigurationOverride(
DeviceConfigurationOverride.FontScale(2f),
) {
SwrTheme {
SwrLargeTopAppBarExample()
}
}
}
}
composeTestRule.onRoot().captureRoboImage(
"src/test/screenshots/TopAppBar/TopAppBar_fontScale2.png",
roborazziOptions = DefaultRoborazziOptions,
)
}

@Composable
private fun SwrLargeTopAppBarExample() {
SwrLargeTopAppBar(
title = {
Text(
text = "Large Top App Bar",
style = MaterialTheme.typography.headlineSmall.copy(
brush = Brush.linearGradient(
colors = LocalGradientColors.current.topBarTitleColorsDefault,
),
),
)
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults.enterAlwaysScrollBehavior
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteDefaults
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffold
import androidx.compose.runtime.Composable
Expand All @@ -48,6 +46,7 @@ import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.eblan.socialworkreviewer.core.designsystem.component.SwrLargeTopAppBar
import com.eblan.socialworkreviewer.core.designsystem.theme.LocalGradientColors
import com.eblan.socialworkreviewer.feature.home.navigation.HomeDestination
import kotlin.reflect.KClass
Expand Down Expand Up @@ -128,9 +127,19 @@ internal fun HomeScreen(
) {
Scaffold(
topBar = {
HomeLargeTopAppBar(
title = stringResource(id = topBarTitleStringResource),
topAppBarScrollBehavior = topAppBarScrollBehavior,
SwrLargeTopAppBar(
title = {
Text(
text = stringResource(id = topBarTitleStringResource),
style = MaterialTheme.typography.headlineSmall.copy(
brush = Brush.linearGradient(
colors = LocalGradientColors.current.topBarTitleColorsDefault,
),
),
)
},
modifier = modifier.testTag("home:largeTopAppBar"),
scrollBehavior = topAppBarScrollBehavior,
)
},
snackbarHost = {
Expand All @@ -150,29 +159,6 @@ internal fun HomeScreen(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun HomeLargeTopAppBar(
modifier: Modifier = Modifier,
title: String,
topAppBarScrollBehavior: TopAppBarScrollBehavior,
) {
LargeTopAppBar(
title = {
Text(
text = title,
style = MaterialTheme.typography.headlineSmall.copy(
brush = Brush.linearGradient(
colors = LocalGradientColors.current.topBarTitleColorsDefault,
),
),
)
},
modifier = modifier.testTag("largeTopAppBar"),
scrollBehavior = topAppBarScrollBehavior,
)
}

private fun NavDestination?.isTopLevelDestinationInHierarchy(route: KClass<*>) =
this?.hierarchy?.any {
it.hasRoute(route)
Expand Down
Loading

0 comments on commit 022ec08

Please sign in to comment.