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 5cfaae24b8
commit 3449ea486a
8 changed files with 81 additions and 14 deletions
@@ -38,5 +38,7 @@ class ColorTestPreview(
)
}
override val name: String = showkaseBrowserColor.colorName
override fun toString(): String = "Color_${showkaseBrowserColor.colorGroup}_${showkaseBrowserColor.colorName}"
}
@@ -25,5 +25,7 @@ class ComponentTestPreview(
@Composable
override fun Content() = showkaseBrowserComponent.component()
override val name: String = showkaseBrowserComponent.componentName
override fun toString(): String = showkaseBrowserComponent.componentKey
}
@@ -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 {
@@ -21,4 +21,6 @@ import androidx.compose.runtime.Composable
interface TestPreview {
@Composable
fun Content()
val name: String
}
@@ -44,5 +44,7 @@ class TypographyTestPreview(
)
}
override val name: String = showkaseBrowserTypography.typographyName
override fun toString(): String = "Typo_${showkaseBrowserTypography.typographyGroup}_${showkaseBrowserTypography.typographyName}"
}