Auto generate dark mode previews and screenshots (#776)

With this change, composable previews and screenshots should be created with just:
```
@ElementPreviews
@Composable
fun MyViewPreview() {
    ElementPreview { 
        MyView()
    }
}
```

- Adds `@ElementPreviews` which is a shorthand for:
```
@Preview(name = "D")
@Preview(name = "N", uiMode = Configuration.UI_MODE_NIGHT_YES)
```
Should be used in connection with the now public `fun ElementPreview()` composable.

- Adds ElementPreviews to previewAnnotations in dangerfile
- Screenshots of night mode previews are now rendered with night mode
- Replaces `ElementPreviewLight` and `ElementThemedPreview` with `ElementPreview`
- Deprecates `ElementPreviewDark` which should be removed.
- Remaining usages of `ElementPreviewDark` are now ignored during screenshot tests
This commit is contained in:
Marco Romano 2023-07-05 13:58:24 +02:00 committed by GitHub
parent d68526ee18
commit 79b529193c
1247 changed files with 2264 additions and 1939 deletions

View file

@ -38,5 +38,7 @@ class ColorTestPreview(
)
}
override val name: String = showkaseBrowserColor.colorName
override fun toString(): String = "Color_${showkaseBrowserColor.colorGroup}_${showkaseBrowserColor.colorName}"
}

View file

@ -25,5 +25,7 @@ class ComponentTestPreview(
@Composable
override fun Content() = showkaseBrowserComponent.component()
override val name: String = showkaseBrowserComponent.componentName
override fun toString(): String = showkaseBrowserComponent.componentKey
}

View file

@ -38,6 +38,7 @@ import com.airbnb.android.showkase.models.Showkase
import com.android.ide.common.rendering.api.SessionParams
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import io.element.android.libraries.designsystem.preview.NIGHT_MODE_NAME
import io.element.android.libraries.theme.ElementTheme
import org.junit.Rule
import org.junit.Test
@ -59,7 +60,11 @@ class ScreenshotTest {
object PreviewProvider : TestParameter.TestParameterValuesProvider {
override fun provideValues(): List<TestPreview> {
val metadata = Showkase.getMetadata()
val components = metadata.componentList.map(::ComponentTestPreview)
val components = metadata.componentList.filterNot {
// skip all previews that have "dark" in the name because
// all previews get a dark mode screenshot automatically now
it.componentKey.contains("dark", true)
}.map(::ComponentTestPreview)
val colors = metadata.colorList.map(::ColorTestPreview)
val typography = metadata.typographyList.map(::TypographyTestPreview)
@ -95,6 +100,10 @@ class ScreenshotTest {
),
LocalConfiguration provides Configuration().apply {
setLocales(LocaleList(localeStr.toLocale()))
// Dark mode previews have name "N" so their component is subfixed with "- N"
if (componentTestPreview.name.contains("- $NIGHT_MODE_NAME")){
uiMode = Configuration.UI_MODE_NIGHT_YES
}
},
// Needed so that UI that uses it don't crash during screenshot tests
LocalOnBackPressedDispatcherOwner provides object : OnBackPressedDispatcherOwner {

View file

@ -21,4 +21,6 @@ import androidx.compose.runtime.Composable
interface TestPreview {
@Composable
fun Content()
val name: String
}

View file

@ -44,5 +44,7 @@ class TypographyTestPreview(
)
}
override val name: String = showkaseBrowserTypography.typographyName
override fun toString(): String = "Typo_${showkaseBrowserTypography.typographyGroup}_${showkaseBrowserTypography.typographyName}"
}