From 4b91dc4d0f854fb2f1c8188d88e120b11b2c76a1 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 23 Dec 2022 15:22:14 +0100 Subject: [PATCH] Update screenshot test --- .../x/tests/uitests/BaseDeviceConfig.kt | 26 ++++++++ .../x/tests/uitests/ColorTestPreview.kt | 42 ++++++++++++ .../x/tests/uitests/ComponentTestPreview.kt | 28 ++++++++ ...osePapparazziTest.kt => ScreenshotTest.kt} | 66 ++++++++++++------- .../android/x/tests/uitests/TestPreview.kt | 24 +++++++ .../x/tests/uitests/TypographyTestPreview.kt | 45 +++++++++++++ 6 files changed, 209 insertions(+), 22 deletions(-) create mode 100644 tests/uitests/src/test/java/io/element/android/x/tests/uitests/BaseDeviceConfig.kt create mode 100644 tests/uitests/src/test/java/io/element/android/x/tests/uitests/ColorTestPreview.kt create mode 100644 tests/uitests/src/test/java/io/element/android/x/tests/uitests/ComponentTestPreview.kt rename tests/uitests/src/test/java/io/element/android/x/tests/uitests/{ComposePapparazziTest.kt => ScreenshotTest.kt} (51%) create mode 100644 tests/uitests/src/test/java/io/element/android/x/tests/uitests/TestPreview.kt create mode 100644 tests/uitests/src/test/java/io/element/android/x/tests/uitests/TypographyTestPreview.kt diff --git a/tests/uitests/src/test/java/io/element/android/x/tests/uitests/BaseDeviceConfig.kt b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/BaseDeviceConfig.kt new file mode 100644 index 0000000000..36770a7ed9 --- /dev/null +++ b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/BaseDeviceConfig.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 io.element.android.x.tests.uitests + +import app.cash.paparazzi.DeviceConfig + +enum class BaseDeviceConfig( + val deviceConfig: DeviceConfig, +) { + NEXUS_5(DeviceConfig.NEXUS_5), + PIXEL_C(DeviceConfig.PIXEL_C), +} diff --git a/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ColorTestPreview.kt b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ColorTestPreview.kt new file mode 100644 index 0000000000..bf6f5f5d5a --- /dev/null +++ b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ColorTestPreview.kt @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 io.element.android.x.tests.uitests + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.airbnb.android.showkase.models.ShowkaseBrowserColor + +class ColorTestPreview( + private val showkaseBrowserColor: ShowkaseBrowserColor +) : TestPreview { + @Composable + override fun Content() { + Box( + modifier = Modifier + .fillMaxWidth() + .height(250.dp) + .background(showkaseBrowserColor.color) + ) + } + + override fun toString(): String = "${showkaseBrowserColor.colorGroup}_${showkaseBrowserColor.colorName}" +} diff --git a/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ComponentTestPreview.kt b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ComponentTestPreview.kt new file mode 100644 index 0000000000..598acfadb0 --- /dev/null +++ b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ComponentTestPreview.kt @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 io.element.android.x.tests.uitests + +import androidx.compose.runtime.Composable +import com.airbnb.android.showkase.models.ShowkaseBrowserComponent + +class ComponentTestPreview( + private val showkaseBrowserComponent: ShowkaseBrowserComponent +) : TestPreview { + @Composable + override fun Content() = showkaseBrowserComponent.component() + override fun toString(): String = showkaseBrowserComponent.componentKey +} diff --git a/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ComposePapparazziTest.kt b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ScreenshotTest.kt similarity index 51% rename from tests/uitests/src/test/java/io/element/android/x/tests/uitests/ComposePapparazziTest.kt rename to tests/uitests/src/test/java/io/element/android/x/tests/uitests/ScreenshotTest.kt index d1fcff0de3..8943c43ee9 100644 --- a/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ComposePapparazziTest.kt +++ b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/ScreenshotTest.kt @@ -1,11 +1,12 @@ /* + * Copyright 2022 The Android Open Source Project * Copyright (c) 2022 New Vector Ltd * * 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 * - * http://www.apache.org/licenses/LICENSE-2.0 + * 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, @@ -17,38 +18,46 @@ package io.element.android.x.tests.uitests import android.os.Build -import androidx.compose.runtime.Composable +import androidx.activity.OnBackPressedDispatcher +import androidx.activity.OnBackPressedDispatcherOwner +import androidx.activity.compose.LocalOnBackPressedDispatcherOwner +import androidx.compose.foundation.layout.Box import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalInspectionMode +import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.unit.Density -import app.cash.paparazzi.DeviceConfig.Companion.PIXEL_5 import app.cash.paparazzi.Paparazzi import app.cash.paparazzi.androidHome import app.cash.paparazzi.detectEnvironment import com.airbnb.android.showkase.models.Showkase -import com.airbnb.android.showkase.models.ShowkaseBrowserComponent import com.google.testing.junit.testparameterinjector.TestParameter import com.google.testing.junit.testparameterinjector.TestParameterInjector -import io.element.android.x.designsystem.ElementXTheme import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith -class ComponentPreview( - private val showkaseBrowserComponent: ShowkaseBrowserComponent -) { - val content: @Composable () -> Unit = showkaseBrowserComponent.component - override fun toString(): String = - showkaseBrowserComponent.group + ":" + showkaseBrowserComponent.componentName -} +/** + * BMA: Inspired from https://github.com/airbnb/Showkase/blob/master/showkase-screenshot-testing-paparazzi-sample/src/test/java/com/airbnb/android/showkase/screenshot/testing/paparazzi/sample/PaparazziSampleScreenshotTest.kt + */ +/* + * Credit to Alex Vanyo for creating this sample in the Now In Android app by Google. + * PR here - https://github.com/android/nowinandroid/pull/101. Modified the test from that PR to + * my own needs for this sample. + */ @RunWith(TestParameterInjector::class) -class ComposePaparazziTests { +class ScreenshotTest { object PreviewProvider : TestParameter.TestParameterValuesProvider { - override fun provideValues(): List = - Showkase.getMetadata().componentList.map(::ComponentPreview) + override fun provideValues(): List { + val metadata = Showkase.getMetadata() + val components = metadata.componentList.map(::ComponentTestPreview) + val colors = metadata.colorList.map(::ColorTestPreview) + val typography = metadata.typographyList.map(::TypographyTestPreview) + + return components + colors + typography + } } @get:Rule @@ -59,28 +68,41 @@ class ComposePaparazziTests { compileSdkVersion = Build.VERSION_CODES.S_V2 /* 32 */ ), maxPercentDifference = 0.0, - deviceConfig = PIXEL_5.copy(softButtons = false), ) @Test fun preview_tests( - @TestParameter(valuesProvider = PreviewProvider::class) componentPreview: ComponentPreview, + @TestParameter(valuesProvider = PreviewProvider::class) componentTestPreview: TestPreview, + @TestParameter baseDeviceConfig: BaseDeviceConfig, @TestParameter(value = ["1.0", "1.5"]) fontScale: Float, - @TestParameter(value = ["light", "dark"]) theme: String, - // TODO Test other languages + // TODO Test other locale + // TODO Test other light and dark theme ) { + paparazzi.unsafeUpdateConfig( + baseDeviceConfig.deviceConfig.copy( + softButtons = false, + ) + ) paparazzi.snapshot { + val lifecycleOwner = LocalLifecycleOwner.current CompositionLocalProvider( LocalInspectionMode provides true, LocalDensity provides Density( density = LocalDensity.current.density, fontScale = fontScale - ) + ), + // Needed so that UI that uses it don't crash during screenshot tests + LocalOnBackPressedDispatcherOwner provides object : OnBackPressedDispatcherOwner { + override fun getLifecycle() = lifecycleOwner.lifecycle + + override fun getOnBackPressedDispatcher() = OnBackPressedDispatcher() + } ) { - ElementXTheme(darkTheme = (theme == "dark")) { - componentPreview.content() + Box { + componentTestPreview.Content() } } } } } + diff --git a/tests/uitests/src/test/java/io/element/android/x/tests/uitests/TestPreview.kt b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/TestPreview.kt new file mode 100644 index 0000000000..6398a418bf --- /dev/null +++ b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/TestPreview.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 io.element.android.x.tests.uitests + +import androidx.compose.runtime.Composable + +interface TestPreview { + @Composable + fun Content() +} diff --git a/tests/uitests/src/test/java/io/element/android/x/tests/uitests/TypographyTestPreview.kt b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/TypographyTestPreview.kt new file mode 100644 index 0000000000..e2d439b9e0 --- /dev/null +++ b/tests/uitests/src/test/java/io/element/android/x/tests/uitests/TypographyTestPreview.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 io.element.android.x.tests.uitests + +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.text.BasicText +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import com.airbnb.android.showkase.models.ShowkaseBrowserTypography +import com.airbnb.android.showkase.ui.padding4x +import java.util.* + +class TypographyTestPreview( + private val showkaseBrowserTypography: ShowkaseBrowserTypography +) : TestPreview { + @Composable + override fun Content() { + BasicText( + text = showkaseBrowserTypography.typographyName.replaceFirstChar { + it.titlecase(Locale.getDefault()) + }, + modifier = Modifier + .fillMaxWidth() + .padding(padding4x), + style = showkaseBrowserTypography.textStyle + ) + } + + override fun toString(): String = "${showkaseBrowserTypography.typographyGroup}_${showkaseBrowserTypography.typographyName}" +}