Skip to content

A fully functional Android app built entirely with Kotlin and Jetpack Compose

Adds screenshot tests for snackbar (#1343)

Jose Alcérreca16f45c0b1aff
diff --git a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt
index eaed15a4e..aab864f5b 100644
--- a/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt
+++ b/app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt
@@ -75,10 +75,6 @@ import com.google.samples.apps.nowinandroid.navigation.NiaNavHost
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
import com.google.samples.apps.nowinandroid.feature.settings.R as settingsR
-@OptIn(
- ExperimentalMaterial3Api::class,
- ExperimentalComposeUiApi::class,
-)
@Composable
fun NiaApp(appState: NiaAppState) {
val shouldShowGradientBackground =
@@ -108,95 +104,114 @@ fun NiaApp(appState: NiaAppState) {
}
}
- if (showSettingsDialog) {
- SettingsDialog(
- onDismiss = { showSettingsDialog = false },
+ NiaApp(
+ appState = appState,
+ snackbarHostState = snackbarHostState,
+ showSettingsDialog = showSettingsDialog,
+ onSettingsDismissed = { showSettingsDialog = false },
+ onTopAppBarActionClick = { showSettingsDialog = true },
+ )
+ }
+ }
+}
+
+@Composable
+@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
+internal fun NiaApp(
+ appState: NiaAppState,
+ snackbarHostState: SnackbarHostState,
+ showSettingsDialog: Boolean,
+ onSettingsDismissed: () -> Unit,
+ onTopAppBarActionClick: () -> Unit,
+ modifier: Modifier = Modifier,
+) {
+ val unreadDestinations by appState.topLevelDestinationsWithUnreadResources
+ .collectAsStateWithLifecycle()
+
+ if (showSettingsDialog) {
+ SettingsDialog(
+ onDismiss = { onSettingsDismissed() },
+ )
+ }
+ Scaffold(
+ modifier = modifier.semantics {
+ testTagsAsResourceId = true
+ },
+ containerColor = Color.Transparent,
+ contentColor = MaterialTheme.colorScheme.onBackground,
+ contentWindowInsets = WindowInsets(0, 0, 0, 0),
+ snackbarHost = { SnackbarHost(snackbarHostState) },
+ bottomBar = {
+ if (appState.shouldShowBottomBar) {
+ NiaBottomBar(
+ destinations = appState.topLevelDestinations,
+ destinationsWithUnreadResources = unreadDestinations,
+ onNavigateToDestination = appState::navigateToTopLevelDestination,
+ currentDestination = appState.currentDestination,
+ modifier = Modifier.testTag("NiaBottomBar"),
+ )
+ }
+ },
+ ) { padding ->
+ Row(
+ Modifier
+ .fillMaxSize()
+ .padding(padding)
+ .consumeWindowInsets(padding)
+ .windowInsetsPadding(
+ WindowInsets.safeDrawing.only(
+ WindowInsetsSides.Horizontal,
+ ),
+ ),
+ ) {
+ if (appState.shouldShowNavRail) {
+ NiaNavRail(
+ destinations = appState.topLevelDestinations,
+ destinationsWithUnreadResources = unreadDestinations,
+ onNavigateToDestination = appState::navigateToTopLevelDestination,
+ currentDestination = appState.currentDestination,
+ modifier = Modifier
+ .testTag("NiaNavRail")
+ .safeDrawingPadding(),
)
}
- val unreadDestinations by appState.topLevelDestinationsWithUnreadResources.collectAsStateWithLifecycle()
-
- Scaffold(
- modifier = Modifier.semantics {
- testTagsAsResourceId = true
- },
- containerColor = Color.Transparent,
- contentColor = MaterialTheme.colorScheme.onBackground,
- contentWindowInsets = WindowInsets(0, 0, 0, 0),
- snackbarHost = { SnackbarHost(snackbarHostState) },
- bottomBar = {
- if (appState.shouldShowBottomBar) {
- NiaBottomBar(
- destinations = appState.topLevelDestinations,
- destinationsWithUnreadResources = unreadDestinations,
- onNavigateToDestination = appState::navigateToTopLevelDestination,
- currentDestination = appState.currentDestination,
- modifier = Modifier.testTag("NiaBottomBar"),
- )
- }
- },
- ) { padding ->
- Row(
- Modifier
- .fillMaxSize()
- .padding(padding)
- .consumeWindowInsets(padding)
- .windowInsetsPadding(
- WindowInsets.safeDrawing.only(
- WindowInsetsSides.Horizontal,
- ),
+ Column(Modifier.fillMaxSize()) {
+ // Show the top app bar on top level destinations.
+ val destination = appState.currentTopLevelDestination
+ if (destination != null) {
+ NiaTopAppBar(
+ titleRes = destination.titleTextId,
+ navigationIcon = NiaIcons.Search,
+ navigationIconContentDescription = stringResource(
+ id = settingsR.string.feature_settings_top_app_bar_navigation_icon_description,
),
- ) {
- if (appState.shouldShowNavRail) {
- NiaNavRail(
- destinations = appState.topLevelDestinations,
- destinationsWithUnreadResources = unreadDestinations,
- onNavigateToDestination = appState::navigateToTopLevelDestination,
- currentDestination = appState.currentDestination,
- modifier = Modifier
- .testTag("NiaNavRail")
- .safeDrawingPadding(),
- )
- }
-
- Column(Modifier.fillMaxSize()) {
- // Show the top app bar on top level destinations.
- val destination = appState.currentTopLevelDestination
- if (destination != null) {
- NiaTopAppBar(
- titleRes = destination.titleTextId,
- navigationIcon = NiaIcons.Search,
- navigationIconContentDescription = stringResource(
- id = settingsR.string.feature_settings_top_app_bar_navigation_icon_description,
- ),
- actionIcon = NiaIcons.Settings,
- actionIconContentDescription = stringResource(
- id = settingsR.string.feature_settings_top_app_bar_action_icon_description,
- ),
- colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
- containerColor = Color.Transparent,
- ),
- onActionClick = { showSettingsDialog = true },
- onNavigationClick = { appState.navigateToSearch() },
- )
- }
-
- NiaNavHost(
- appState = appState,
- onShowSnackbar = { message, action ->
- snackbarHostState.showSnackbar(
- message = message,
- actionLabel = action,
- duration = Short,
- ) == ActionPerformed
- },
- )
- }
-
- // TODO: We may want to add padding or spacer when the snackbar is shown so that
- // content doesn't display behind it.
+ actionIcon = NiaIcons.Settings,
+ actionIconContentDescription = stringResource(
+ id = settingsR.string.feature_settings_top_app_bar_action_icon_description,
+ ),
+ colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
+ containerColor = Color.Transparent,
+ ),
+ onActionClick = { onTopAppBarActionClick() },
+ onNavigationClick = { appState.navigateToSearch() },
+ )
}
+
+ NiaNavHost(
+ appState = appState,
+ onShowSnackbar = { message, action ->
+ snackbarHostState.showSnackbar(
+ message = message,
+ actionLabel = action,
+ duration = Short,
+ ) == ActionPerformed
+ },
+ )
}
+
+ // TODO: We may want to add padding or spacer when the snackbar is shown so that
+ // content doesn't display behind it.
}
}
}
diff --git a/app/src/testDemo/kotlin/com/google/samples/apps/nowinandroid/ui/SnackbarScreenshotTests.kt b/app/src/testDemo/kotlin/com/google/samples/apps/nowinandroid/ui/SnackbarScreenshotTests.kt
new file mode 100644
index 000000000..fa1ba1036
--- /dev/null
+++ b/app/src/testDemo/kotlin/com/google/samples/apps/nowinandroid/ui/SnackbarScreenshotTests.kt
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2023 The Android Open Source Project
+ *
+ * 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
+ *
+ * https://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 com.google.samples.apps.nowinandroid.ui
+
+import android.util.Log
+import androidx.compose.foundation.layout.BoxWithConstraints
+import androidx.compose.material3.SnackbarDuration.Indefinite
+import androidx.compose.material3.SnackbarHostState
+import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
+import androidx.compose.material3.windowsizeclass.WindowSizeClass
+import androidx.compose.runtime.rememberCoroutineScope
+import androidx.compose.ui.test.junit4.createAndroidComposeRule
+import androidx.compose.ui.test.onRoot
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.DpSize
+import androidx.compose.ui.unit.dp
+import androidx.test.platform.app.InstrumentationRegistry
+import androidx.work.Configuration
+import androidx.work.testing.SynchronousExecutor
+import androidx.work.testing.WorkManagerTestInitHelper
+import com.github.takahirom.roborazzi.captureRoboImage
+import com.google.accompanist.testharness.TestHarness
+import com.google.samples.apps.nowinandroid.core.data.repository.TopicsRepository
+import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourceRepository
+import com.google.samples.apps.nowinandroid.core.data.test.repository.FakeUserDataRepository
+import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
+import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
+import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
+import com.google.samples.apps.nowinandroid.core.testing.util.DefaultRoborazziOptions
+import com.google.samples.apps.nowinandroid.uitesthiltmanifest.HiltComponentActivity
+import dagger.hilt.android.testing.BindValue
+import dagger.hilt.android.testing.HiltAndroidRule
+import dagger.hilt.android.testing.HiltAndroidTest
+import dagger.hilt.android.testing.HiltTestApplication
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.flow.first
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.runBlocking
+import org.junit.Before
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.TemporaryFolder
+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 javax.inject.Inject
+
+/**
+ * Tests that the Snackbar is correctly displayed on different screen sizes.
+ */
+@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
+@RunWith(RobolectricTestRunner::class)
+@GraphicsMode(GraphicsMode.Mode.NATIVE)
+// Configure Robolectric to use a very large screen size that can fit all of the test sizes.
+// This allows enough room to render the content under test without clipping or scaling.
+@Config(application = HiltTestApplication::class, qualifiers = "w1000dp-h1000dp-480dpi")
+@LooperMode(LooperMode.Mode.PAUSED)
+@HiltAndroidTest
+class SnackbarScreenshotTests {
+
+ /**
+ * Manages the components' state and is used to perform injection on your test
+ */
+ @get:Rule(order = 0)
+ val hiltRule = HiltAndroidRule(this)
+
+ /**
+ * Create a temporary folder used to create a Data Store file. This guarantees that
+ * the file is removed in between each test, preventing a crash.
+ */
+ @BindValue
+ @get:Rule(order = 1)
+ val tmpFolder: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()
+
+ /**
+ * Use a test activity to set the content on.
+ */
+ @get:Rule(order = 2)
+ val composeTestRule = createAndroidComposeRule<HiltComponentActivity>()
+
+ @Inject
+ lateinit var networkMonitor: NetworkMonitor
+
+ @Inject
+ lateinit var timeZoneMonitor: TimeZoneMonitor
+
+ @Inject
+ lateinit var userDataRepository: FakeUserDataRepository
+
+ @Inject
+ lateinit var topicsRepository: TopicsRepository
+
+ @Inject
+ lateinit var userNewsResourceRepository: UserNewsResourceRepository
+
+ @Before
+ fun setup() {
+ val config = Configuration.Builder()
+ .setMinimumLoggingLevel(Log.DEBUG)
+ .setExecutor(SynchronousExecutor())
+ .build()
+
+ // Initialize WorkManager for instrumentation tests.
+ WorkManagerTestInitHelper.initializeTestWorkManager(
+ InstrumentationRegistry.getInstrumentation().context,
+ config,
+ )
+
+ hiltRule.inject()
+
+ // Configure user data
+ runBlocking {
+ userDataRepository.setShouldHideOnboarding(true)
+
+ userDataRepository.setFollowedTopicIds(
+ setOf(topicsRepository.getTopics().first().first().id),
+ )
+ }
+ }
+
+ @Test
+ fun phone_noSnackbar() {
+ val snackbarHostState = SnackbarHostState()
+ testSnackbarScreenshotWithSize(
+ snackbarHostState,
+ 400.dp,
+ 500.dp,
+ "snackbar_compact_medium_noSnackbar",
+ action = { },
+ )
+ }
+
+ @Test
+ fun snackbarShown_phone() {
+ val snackbarHostState = SnackbarHostState()
+ testSnackbarScreenshotWithSize(
+ snackbarHostState,
+ 400.dp,
+ 500.dp,
+ "snackbar_compact_medium",
+ ) {
+ snackbarHostState.showSnackbar(
+ "This is a test snackbar message",
+ actionLabel = "Action Label",
+ duration = Indefinite,
+ )
+ }
+ }
+
+ @Test
+ fun snackbarShown_foldable() {
+ val snackbarHostState = SnackbarHostState()
+ testSnackbarScreenshotWithSize(
+ snackbarHostState,
+ 600.dp,
+ 600.dp,
+ "snackbar_medium_medium",
+ ) {
+ snackbarHostState.showSnackbar(
+ "This is a test snackbar message",
+ actionLabel = "Action Label",
+ duration = Indefinite,
+ )
+ }
+ }
+
+ @Test
+ fun snackbarShown_tablet() {
+ val snackbarHostState = SnackbarHostState()
+ testSnackbarScreenshotWithSize(
+ snackbarHostState,
+ 900.dp,
+ 900.dp,
+ "snackbar_expanded_expanded",
+ ) {
+ snackbarHostState.showSnackbar(
+ "This is a test snackbar message",
+ actionLabel = "Action Label",
+ duration = Indefinite,
+ )
+ }
+ }
+
+ private fun testSnackbarScreenshotWithSize(
+ snackbarHostState: SnackbarHostState,
+ width: Dp,
+ height: Dp,
+ screenshotName: String,
+ action: suspend () -> Unit,
+ ) {
+ lateinit var scope: CoroutineScope
+ composeTestRule.setContent {
+ scope = rememberCoroutineScope()
+
+ TestHarness(size = DpSize(width, height)) {
+ BoxWithConstraints {
+ val appState = rememberNiaAppState(
+ windowSizeClass = WindowSizeClass.calculateFromSize(
+ DpSize(maxWidth, maxHeight),
+ ),
+ networkMonitor = networkMonitor,
+ userNewsResourceRepository = userNewsResourceRepository,
+ timeZoneMonitor = timeZoneMonitor,
+ )
+ NiaTheme {
+ NiaApp(appState, snackbarHostState, false, {}, {})
+ }
+ }
+ }
+ }
+
+ scope.launch {
+ action()
+ }
+
+ composeTestRule.onRoot()
+ .captureRoboImage(
+ "src/testDemo/screenshots/$screenshotName.png",
+ roborazziOptions = DefaultRoborazziOptions,
+ )
+ }
+}
diff --git a/app/src/testDemo/screenshots/snackbar_compact_medium.png b/app/src/testDemo/screenshots/snackbar_compact_medium.png
new file mode 100644
index 000000000..70d15deb3
Binary files /dev/null and b/app/src/testDemo/screenshots/snackbar_compact_medium.png differ
diff --git a/app/src/testDemo/screenshots/snackbar_compact_medium_noSnackbar.png b/app/src/testDemo/screenshots/snackbar_compact_medium_noSnackbar.png
new file mode 100644
index 000000000..9c3eb133d
Binary files /dev/null and b/app/src/testDemo/screenshots/snackbar_compact_medium_noSnackbar.png differ
diff --git a/app/src/testDemo/screenshots/snackbar_expanded_expanded.png b/app/src/testDemo/screenshots/snackbar_expanded_expanded.png
new file mode 100644
index 000000000..e4c7520ab
Binary files /dev/null and b/app/src/testDemo/screenshots/snackbar_expanded_expanded.png differ
diff --git a/app/src/testDemo/screenshots/snackbar_medium_medium.png b/app/src/testDemo/screenshots/snackbar_medium_medium.png
new file mode 100644
index 000000000..a9f131fd7
Binary files /dev/null and b/app/src/testDemo/screenshots/snackbar_medium_medium.png differ
diff --git a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt
index 070c7ed38..0cdec6090 100644
--- a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt
+++ b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeNewsRepository.kt
@@ -39,7 +39,7 @@ import javax.inject.Inject
* This allows us to run the app with fake data, without needing an internet connection or working
* backend.
*/
-internal class FakeNewsRepository @Inject constructor(
+class FakeNewsRepository @Inject constructor(
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
private val datasource: DemoNiaNetworkDataSource,
) : NewsRepository {
diff --git a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeUserDataRepository.kt b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeUserDataRepository.kt
index 4871baad9..61ab422af 100644
--- a/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeUserDataRepository.kt
+++ b/core/data-test/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/test/repository/FakeUserDataRepository.kt
@@ -30,7 +30,7 @@ import javax.inject.Inject
* This allows us to run the app with fake data, without needing an internet connection or working
* backend.
*/
-internal class FakeUserDataRepository @Inject constructor(
+class FakeUserDataRepository @Inject constructor(
private val niaPreferencesDataSource: NiaPreferencesDataSource,
) : UserDataRepository {