Optional day night preview annotation (#793)

Adds the `@DayNightPreviews` annotation that when used on a composable will:
- Display both a day mode and night mode preview in Android Studio.
- Produce both a day and night screenshot during screenshot testing.

The usage of this new annotation is optional, all the current previews continue to work without breakages.
New code can use, when appropriate, the new `@DayNightPreviews` annotation and replace the pattern using three `LightPreview/DarkPreview/ContentToPreview` functions with:

```
@DayNightPreviews
@Composable
fun MyScreenPreview(@PreviewParameter(MyStateProvider::class) state: MyState) {
    ElementPreview {
        MyScreen(
            state = state,
        )
    }
}
```
This commit is contained in:
Marco Romano 2023-07-06 12:35:54 +02:00 committed by GitHub
parent bbd1ff31a3
commit aed4b92761
8 changed files with 81 additions and 14 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
@ -95,6 +96,10 @@ class ScreenshotTest {
),
LocalConfiguration provides Configuration().apply {
setLocales(LocaleList(localeStr.toLocale()))
// Dark mode previews have name "N" so their component name contains "- 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}"
}