Add Screenshot Test to SettingsDialog

pull/1367/head
yongsuk44 1 year ago
parent d1e9fd54e2
commit 76a7354012

@ -28,10 +28,12 @@ import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.onRoot
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
import com.github.takahirom.roborazzi.RoborazziOptions
import com.github.takahirom.roborazzi.RoborazziOptions.CompareOptions
import com.github.takahirom.roborazzi.RoborazziOptions.RecordOptions
import com.github.takahirom.roborazzi.captureRoboImage
import com.github.takahirom.roborazzi.captureScreenRoboImage
import com.google.accompanist.testharness.TestHarness
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import org.robolectric.RuntimeEnvironment
@ -49,6 +51,7 @@ enum class DefaultTestDevices(val description: String, val spec: String) {
FOLDABLE("foldable", "spec:shape=Normal,width=673,height=841,unit=dp,dpi=480"),
TABLET("tablet", "spec:shape=Normal,width=1280,height=800,unit=dp,dpi=480"),
}
fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.captureMultiDevice(
screenshotName: String,
body: @Composable () -> Unit,
@ -87,6 +90,25 @@ fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.c
)
}
@OptIn(ExperimentalRoborazziApi::class)
fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.captureDialog(
screenshotName: String,
body: @Composable () -> Unit,
) {
this.activity.setContent {
CompositionLocalProvider(
LocalInspectionMode provides true,
) {
body()
}
}
captureScreenRoboImage(
"src/test/screenshots/$screenshotName.png",
roborazziOptions = DefaultRoborazziOptions,
)
}
/**
* Takes six screenshots combining light/dark and default/Android themes and whether dynamic color
* is enabled.

@ -0,0 +1,175 @@
/*
* Copyright 2024 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.feature.settings
import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.DARK
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.LIGHT
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand.ANDROID
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand.DEFAULT
import com.google.samples.apps.nowinandroid.core.testing.util.captureDialog
import com.google.samples.apps.nowinandroid.feature.settings.SettingsUiState.Success
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)
@LooperMode(LooperMode.Mode.PAUSED)
@Config(application = HiltTestApplication::class, qualifiers = "w360dp-h640dp-xhdpi")
class SettingsDialogScreenshotTests {
@get:Rule
val composeTestRule = createAndroidComposeRule<ComponentActivity>()
private val defaultSettingsUiState = Success(
UserEditableSettings(
brand = DEFAULT,
darkThemeConfig = LIGHT,
useDynamicColor = false,
),
)
@Test
fun settingsDialogLoading() {
composeTestRule.captureDialog("SettingsDialogLoading") {
SettingsDialogDefaultTheme(
settingsUiState = SettingsUiState.Loading,
)
}
}
@Test
fun settingsDialogLoadingDark() {
composeTestRule.captureDialog("SettingsDialogLoadingDark") {
SettingsDialogDefaultTheme(
darkMode = true,
settingsUiState = SettingsUiState.Loading,
)
}
}
@Test
fun settingsDialogDefaultTheme() {
composeTestRule.captureDialog("SettingsDialogDefaultTheme") {
SettingsDialogDefaultTheme()
}
}
@Test
fun settingsDialogDefaultThemeDark() {
composeTestRule.captureDialog("SettingsDialogDefaultThemeDark") {
SettingsDialogDefaultTheme(
darkMode = true,
settingsUiState = Success(
defaultSettingsUiState.settings.copy(
darkThemeConfig = DARK,
),
),
)
}
}
@Test
fun settingsDialogDynamicColor() {
composeTestRule.captureDialog("SettingsDialogDynamicColor") {
SettingsDialogDefaultTheme(
disableDynamicTheming = false,
settingsUiState = Success(
defaultSettingsUiState.settings.copy(
useDynamicColor = true,
),
),
)
}
}
@Test
fun settingsDialogDynamicColorDark() {
composeTestRule.captureDialog("SettingsDialogDynamicColorDark") {
SettingsDialogDefaultTheme(
darkMode = true,
disableDynamicTheming = false,
settingsUiState = Success(
defaultSettingsUiState.settings.copy(
darkThemeConfig = DARK,
useDynamicColor = true,
),
),
)
}
}
@Test
fun settingsDialogAndroidTheme() {
composeTestRule.captureDialog("SettingsDialogAndroidTheme") {
SettingsDialogDefaultTheme(
androidTheme = true,
settingsUiState = Success(
defaultSettingsUiState.settings.copy(
brand = ANDROID,
),
),
)
}
}
@Test
fun settingsDialogAndroidThemeDark() {
composeTestRule.captureDialog("SettingsDialogAndroidThemeDark") {
SettingsDialogDefaultTheme(
darkMode = true,
androidTheme = true,
settingsUiState = Success(
defaultSettingsUiState.settings.copy(
darkThemeConfig = DARK,
brand = ANDROID,
),
),
)
}
}
@Composable
private fun SettingsDialogDefaultTheme(
darkMode: Boolean = false,
androidTheme: Boolean = false,
disableDynamicTheming: Boolean = true,
settingsUiState: SettingsUiState = defaultSettingsUiState,
) {
NiaTheme(
darkTheme = darkMode,
androidTheme = androidTheme,
disableDynamicTheming = disableDynamicTheming,
) {
SettingsDialog(
onDismiss = {},
settingsUiState = settingsUiState,
onChangeThemeBrand = {},
onChangeDynamicColorPreference = {},
onChangeDarkThemeConfig = {},
)
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Loading…
Cancel
Save