-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #222 from thomaskioko/screenshot-testing
Android Screenshot Testing
- Loading branch information
Showing
120 changed files
with
584 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
plugins { alias(libs.plugins.tvmaniac.compose.library) } | ||
|
||
android { namespace = "com.thomaskioko.tvmaniac.screenshottesting" } | ||
|
||
dependencies { | ||
implementation(projects.android.designsystem) | ||
|
||
api(libs.roborazzi) | ||
implementation(libs.androidx.compose.activity) | ||
implementation(libs.androidx.compose.ui.test) | ||
implementation(libs.robolectric) | ||
|
||
debugImplementation(libs.androidx.compose.ui.test.manifest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest /> |
86 changes: 86 additions & 0 deletions
86
...tests/src/main/kotlin/com/thomaskioko/tvmaniac/screenshottests/RoborazziScreenshotUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.thomaskioko.tvmaniac.screenshottests | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
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.RobolectricDeviceQualifiers | ||
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.thomaskioko.tvmaniac.compose.theme.TvManiacTheme | ||
import org.robolectric.RuntimeEnvironment | ||
|
||
val DefaultRoborazziOptions = | ||
RoborazziOptions( | ||
// Pixel-perfect matching | ||
compareOptions = CompareOptions(changeThreshold = 0f), | ||
// Reduce the size of the PNGs | ||
recordOptions = RecordOptions(resizeScale = 0.5), | ||
) | ||
|
||
enum class DefaultTestDevices(val spec: String) { | ||
Pixel7(RobolectricDeviceQualifiers.Pixel7), | ||
} | ||
|
||
fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.captureMultiDevice( | ||
name: String, | ||
content: @Composable () -> Unit, | ||
) { | ||
DefaultTestDevices.entries.forEach { | ||
this.captureMultiTheme( | ||
deviceSpec = it.spec, | ||
name = name, | ||
content = content, | ||
) | ||
} | ||
} | ||
|
||
/** Takes two screenshots combining light/dark themes. */ | ||
fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.captureMultiTheme( | ||
name: String, | ||
deviceSpec: String, | ||
overrideFileName: String? = null, | ||
shouldCompareDarkMode: Boolean = true, | ||
content: @Composable () -> Unit, | ||
) { | ||
|
||
// Set qualifiers from specs | ||
RuntimeEnvironment.setQualifiers(deviceSpec) | ||
|
||
val darkModeValues = if (shouldCompareDarkMode) listOf(true, false) else listOf(false) | ||
|
||
var darkMode by mutableStateOf(true) | ||
|
||
this.setContent { | ||
CompositionLocalProvider( | ||
LocalInspectionMode provides true, | ||
) { | ||
TvManiacTheme( | ||
darkTheme = darkMode, | ||
) { | ||
content() | ||
} | ||
} | ||
} | ||
|
||
darkModeValues.forEach { isDarkMode -> | ||
darkMode = isDarkMode | ||
val darkModeDesc = if (isDarkMode) "dark" else "light" | ||
|
||
val filename = overrideFileName ?: name | ||
|
||
this.onRoot() | ||
.captureRoboImage( | ||
"src/test/screenshots/" + filename + "_$darkModeDesc" + ".png", | ||
roborazziOptions = DefaultRoborazziOptions, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...i/discover/src/test/kotlin/com/thomaskioko/tvmaniac/ui/discover/DiscoverScreenshotTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.thomaskioko.tvmaniac.ui.discover | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.compose.foundation.pager.rememberPagerState | ||
import androidx.compose.material3.SnackbarHostState | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import com.thomaskioko.tvmaniac.compose.components.TvManiacBackground | ||
import com.thomaskioko.tvmaniac.presentation.discover.EmptyState | ||
import com.thomaskioko.tvmaniac.presentation.discover.ErrorState | ||
import com.thomaskioko.tvmaniac.presentation.discover.Loading | ||
import com.thomaskioko.tvmaniac.screenshottests.captureMultiDevice | ||
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) | ||
@Config(sdk = [33]) | ||
@GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
@LooperMode(LooperMode.Mode.PAUSED) | ||
class DiscoverScreenshotTest { | ||
|
||
@get:Rule val composeTestRule = createAndroidComposeRule<ComponentActivity>() | ||
|
||
@Test | ||
fun discoverScreenEmptyState() { | ||
composeTestRule.captureMultiDevice("DiscoverScreenEmptyState") { | ||
TvManiacBackground { | ||
DiscoverScreen( | ||
state = EmptyState, | ||
pagerState = rememberPagerState(pageCount = { 5 }), | ||
snackBarHostState = remember { SnackbarHostState() }, | ||
onAction = {}, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun discoverScreenLoading() { | ||
composeTestRule.captureMultiDevice("DiscoverScreenLoading") { | ||
TvManiacBackground { | ||
DiscoverScreen( | ||
state = Loading, | ||
pagerState = rememberPagerState(pageCount = { 5 }), | ||
snackBarHostState = remember { SnackbarHostState() }, | ||
onAction = {}, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun discoverScreenErrorState() { | ||
composeTestRule.captureMultiDevice("DiscoverScreenErrorState") { | ||
TvManiacBackground { | ||
DiscoverScreen( | ||
state = ErrorState(errorMessage = "Opps! Something went wrong"), | ||
pagerState = rememberPagerState(pageCount = { 5 }), | ||
snackBarHostState = remember { SnackbarHostState() }, | ||
onAction = {}, | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun discoverScreenDataLoaded() { | ||
composeTestRule.captureMultiDevice("DiscoverScreenDataLoaded") { | ||
TvManiacBackground { | ||
DiscoverScreen( | ||
state = discoverContentSuccess, | ||
pagerState = rememberPagerState(pageCount = { 5 }), | ||
snackBarHostState = remember { SnackbarHostState() }, | ||
onAction = {}, | ||
) | ||
} | ||
} | ||
} | ||
} |
Binary file added
BIN
+8.88 KB
android/ui/discover/src/test/screenshots/DiscoverScreenDataLoaded_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+21.2 KB
android/ui/discover/src/test/screenshots/DiscoverScreenDataLoaded_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.4 KB
android/ui/discover/src/test/screenshots/DiscoverScreenEmptyState_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.7 KB
android/ui/discover/src/test/screenshots/DiscoverScreenEmptyState_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.3 KB
android/ui/discover/src/test/screenshots/DiscoverScreenErrorState_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.2 KB
android/ui/discover/src/test/screenshots/DiscoverScreenErrorState_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.91 KB
android/ui/discover/src/test/screenshots/DiscoverScreenLoading_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+4.91 KB
android/ui/discover/src/test/screenshots/DiscoverScreenLoading_light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.