Browse Source
* Use ComposablePreviewScanner to rework how screenshot testing works * Add test sharding * Update screenshots * Fixes for Element Gallery --------- Co-authored-by: ElementBot <benoitm+elementbot@element.io> Co-authored-by: Benoit Marty <benoit@matrix.org>pull/3133/head
Jorge Martin Espinosa
4 months ago
committed by
GitHub
2063 changed files with 1607 additions and 1768 deletions
@ -0,0 +1,63 @@
@@ -0,0 +1,63 @@
|
||||
/* |
||||
* Copyright (c) 2024 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. |
||||
*/ |
||||
|
||||
@file:Suppress("DEPRECATION") |
||||
|
||||
package base |
||||
|
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidComposablePreviewScanner |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo |
||||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview |
||||
|
||||
object ComposablePreviewProvider : TestParameter.TestParameterValuesProvider { |
||||
private val values: List<IndexedValue<ComposablePreview<AndroidPreviewInfo>>> by lazy { |
||||
AndroidComposablePreviewScanner() |
||||
.scanPackageTrees( |
||||
"io.element.android.features", |
||||
"io.element.android.libraries", |
||||
"io.element.android.services", |
||||
"io.element.android.appnav", |
||||
"io.element.android.x", |
||||
// Make sure we don't import Compound previews by mistake |
||||
) |
||||
.getPreviews() |
||||
.withIndex() |
||||
.toList() |
||||
} |
||||
|
||||
override fun provideValues(): List<IndexedValue<ComposablePreview<AndroidPreviewInfo>>> = values |
||||
} |
||||
|
||||
object Shard1ComposablePreviewProvider : TestParameter.TestParameterValuesProvider { |
||||
override fun provideValues(): List<ComposablePreview<AndroidPreviewInfo>> = |
||||
ComposablePreviewProvider.provideValues().filter { it.index % 4 == 0 }.map { it.value } |
||||
} |
||||
|
||||
object Shard2ComposablePreviewProvider : TestParameter.TestParameterValuesProvider { |
||||
override fun provideValues(): List<ComposablePreview<AndroidPreviewInfo>> = |
||||
ComposablePreviewProvider.provideValues().filter { it.index % 4 == 1 }.map { it.value } |
||||
} |
||||
|
||||
object Shard3ComposablePreviewProvider : TestParameter.TestParameterValuesProvider { |
||||
override fun provideValues(): List<ComposablePreview<AndroidPreviewInfo>> = |
||||
ComposablePreviewProvider.provideValues().filter { it.index % 4 == 2 }.map { it.value } |
||||
} |
||||
|
||||
object Shard4ComposablePreviewProvider : TestParameter.TestParameterValuesProvider { |
||||
override fun provideValues(): List<ComposablePreview<AndroidPreviewInfo>> = |
||||
ComposablePreviewProvider.provideValues().filter { it.index % 4 == 3 }.map { it.value } |
||||
} |
@ -0,0 +1,141 @@
@@ -0,0 +1,141 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 base |
||||
|
||||
import android.content.res.Configuration |
||||
import android.os.LocaleList |
||||
import androidx.compose.foundation.background |
||||
import androidx.compose.foundation.layout.Box |
||||
import androidx.compose.material3.MaterialTheme |
||||
import androidx.compose.runtime.CompositionLocalProvider |
||||
import androidx.compose.ui.Modifier |
||||
import androidx.compose.ui.platform.LocalConfiguration |
||||
import androidx.compose.ui.platform.LocalDensity |
||||
import androidx.compose.ui.platform.LocalInspectionMode |
||||
import androidx.compose.ui.unit.Density |
||||
import app.cash.paparazzi.DeviceConfig |
||||
import app.cash.paparazzi.Paparazzi |
||||
import app.cash.paparazzi.TestName |
||||
import com.android.resources.NightMode |
||||
import io.element.android.compound.theme.ElementTheme |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo |
||||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview |
||||
import java.util.Locale |
||||
|
||||
object ScreenshotTest { |
||||
val defaultDeviceConfig = BaseDeviceConfig.NEXUS_5.deviceConfig |
||||
|
||||
fun runTest( |
||||
paparazzi: Paparazzi, |
||||
preview: ComposablePreview<AndroidPreviewInfo>, |
||||
localeStr: String, |
||||
) { |
||||
val locale = localeStr.toLocale() |
||||
// Needed for regional settings, as first day of week |
||||
Locale.setDefault(locale) |
||||
|
||||
paparazzi.fixScreenshotName(preview, localeStr) |
||||
paparazzi.snapshot { |
||||
CompositionLocalProvider( |
||||
LocalInspectionMode provides true, |
||||
LocalDensity provides Density( |
||||
density = LocalDensity.current.density, |
||||
fontScale = 1.0f, |
||||
), |
||||
LocalConfiguration provides Configuration().apply { |
||||
setLocales(LocaleList(locale)) |
||||
uiMode = preview.previewInfo.uiMode |
||||
}, |
||||
) { |
||||
ElementTheme { |
||||
Box( |
||||
modifier = Modifier |
||||
.background(MaterialTheme.colorScheme.background) |
||||
) { |
||||
preview() |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private val testNameField = Paparazzi::class.java.getDeclaredField("testName").apply { |
||||
isAccessible = true |
||||
} |
||||
|
||||
private fun Paparazzi.fixScreenshotName(preview: ComposablePreview<AndroidPreviewInfo>, locale: String) { |
||||
val id = listOf(createScreenshotIdFor(preview), locale) |
||||
.filter { it.isNotEmpty() } |
||||
.joinToString("_") |
||||
val packageName = preview.declaringClass |
||||
// Remove common prefix |
||||
.replace("io.element.android.", "") |
||||
.split(".") |
||||
// Remove class name |
||||
.dropLast(1) |
||||
.joinToString(".") |
||||
val testName = TestName( |
||||
packageName = packageName, |
||||
className = preview.methodName.replace("Preview", ""), |
||||
methodName = id |
||||
) |
||||
testNameField.set(this, testName) |
||||
} |
||||
|
||||
private fun String.toLocale(): Locale { |
||||
return when (this) { |
||||
"en" -> Locale.US |
||||
"fr" -> Locale.FRANCE |
||||
"de" -> Locale.GERMAN |
||||
else -> Locale.Builder().setLanguage(this).build() |
||||
} |
||||
} |
||||
|
||||
fun createScreenshotIdFor(preview: ComposablePreview<AndroidPreviewInfo>) = buildList { |
||||
// `name` here can be `Day`, `Night`, or nothing at all |
||||
if (preview.previewInfo.name.isNotEmpty()) { |
||||
add(preview.previewInfo.name) |
||||
} |
||||
if (preview.previewInfo.group.isNotEmpty()) { |
||||
add(preview.previewInfo.group) |
||||
} |
||||
// If it's a day/night preview, we should add an index to be consistent even if there is only version of this composable |
||||
val needsIndex = preview.previewInfo.name == "Day" || preview.previewInfo.name == "Night" |
||||
if (preview.previewIndex != null || needsIndex) { |
||||
add((preview.previewIndex ?: 0).toString()) |
||||
} |
||||
}.joinToString("_") |
||||
|
||||
object PaparazziPreviewRule { |
||||
fun createFor(preview: ComposablePreview<AndroidPreviewInfo>, locale: String, deviceConfig: DeviceConfig = ScreenshotTest.defaultDeviceConfig): Paparazzi { |
||||
val densityScale = deviceConfig.density.dpiValue / 160f |
||||
val customScreenHeight = preview.previewInfo.heightDp.takeIf { it >= 0 }?.let { it * densityScale }?.toInt() |
||||
return Paparazzi( |
||||
deviceConfig = deviceConfig.copy( |
||||
nightMode = when (preview.previewInfo.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) { |
||||
true -> NightMode.NIGHT |
||||
false -> NightMode.NOTNIGHT |
||||
}, |
||||
locale = locale, |
||||
softButtons = false, |
||||
screenHeight = customScreenHeight ?: deviceConfig.screenHeight, |
||||
), |
||||
maxPercentDifference = 0.01 |
||||
) |
||||
} |
||||
} |
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 translations |
||||
|
||||
import android.content.res.Configuration |
||||
import base.ComposablePreviewProvider |
||||
import base.PaparazziPreviewRule |
||||
import base.ScreenshotTest |
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import com.google.testing.junit.testparameterinjector.TestParameterInjector |
||||
import org.junit.Rule |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo |
||||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview |
||||
|
||||
/** |
||||
* Test that takes a preview and a locale and runs a screenshot test on it. |
||||
*/ |
||||
@RunWith(TestParameterInjector::class) |
||||
class TranslationsScreenshotTest( |
||||
@TestParameter(valuesProvider = ComposablePreviewProvider::class) |
||||
val indexedPreview: IndexedValue<ComposablePreview<AndroidPreviewInfo>>, |
||||
@TestParameter(value = ["de"]) |
||||
val localeStr: String, |
||||
) { |
||||
@get:Rule |
||||
val paparazziRule = PaparazziPreviewRule.createFor(indexedPreview.value, locale = localeStr) |
||||
|
||||
@Test |
||||
fun snapshot() { |
||||
val (_, preview) = indexedPreview |
||||
// Skip for dark mode screenshots |
||||
if (preview.previewInfo.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES) { |
||||
return |
||||
} |
||||
// Skip for design system screenshots |
||||
if (preview.previewInfo.name.startsWith("io.element.android.libraries.designsystem")) { |
||||
return |
||||
} |
||||
ScreenshotTest.runTest(paparazzi = paparazziRule, preview = preview, localeStr = localeStr) |
||||
} |
||||
} |
@ -1,44 +0,0 @@
@@ -1,44 +0,0 @@
|
||||
/* |
||||
* 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 ui |
||||
|
||||
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 val name: String = showkaseBrowserColor.colorName |
||||
|
||||
override fun toString(): String = "Color_${showkaseBrowserColor.colorGroup}_${showkaseBrowserColor.colorName}" |
||||
} |
@ -1,46 +0,0 @@
@@ -1,46 +0,0 @@
|
||||
/* |
||||
* 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 ui |
||||
|
||||
import androidx.compose.runtime.Composable |
||||
import androidx.compose.ui.unit.Dp |
||||
import com.airbnb.android.showkase.models.ShowkaseBrowserComponent |
||||
|
||||
class ComponentTestPreview( |
||||
private val showkaseBrowserComponent: ShowkaseBrowserComponent |
||||
) : TestPreview { |
||||
@Composable |
||||
override fun Content() = showkaseBrowserComponent.component() |
||||
|
||||
override val name: String = showkaseBrowserComponent.componentName |
||||
|
||||
override fun customHeightDp(): Dp? { |
||||
return showkaseBrowserComponent.heightDp?.let { Dp(it.toFloat()) } |
||||
} |
||||
|
||||
override fun toString(): String = showkaseBrowserComponent.componentKey |
||||
// Strip common package beginning |
||||
.replace("io.element.android.features.", "f.") |
||||
.replace("io.element.android.libraries.", "l.") |
||||
.replace("io.element.android.", "") |
||||
// Reduce default group (if present) |
||||
.replace("_DefaultGroup_", "_") |
||||
// No need to include `Preview` suffix of function name |
||||
.replace("Preview_", "_") |
||||
// Also for preview annotated with @ElementPreview |
||||
.replace("Preview-", "-") |
||||
} |
@ -1,31 +0,0 @@
@@ -1,31 +0,0 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 ui |
||||
|
||||
import com.airbnb.android.showkase.models.Showkase |
||||
import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider |
||||
|
||||
object PreviewProvider : TestParameterValuesProvider() { |
||||
override fun provideValues(context: Context): List<TestPreview> { |
||||
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).filter { !it.toString().contains("compound") } |
||||
} |
||||
} |
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 ui |
||||
|
||||
import base.PaparazziPreviewRule |
||||
import base.ScreenshotTest |
||||
import base.Shard1ComposablePreviewProvider |
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import com.google.testing.junit.testparameterinjector.TestParameterInjector |
||||
import org.junit.Rule |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo |
||||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview |
||||
|
||||
/** |
||||
* Test that takes a preview and runs a screenshot test on it. |
||||
* It uses a sharded preview provider so multiple 'shards' can run in parallel, optimizing CPU and time usage. |
||||
*/ |
||||
@RunWith(TestParameterInjector::class) |
||||
class PreviewShard1Test( |
||||
@TestParameter(valuesProvider = Shard1ComposablePreviewProvider::class) |
||||
val preview: ComposablePreview<AndroidPreviewInfo>, |
||||
) { |
||||
@get:Rule |
||||
val paparazziRule = PaparazziPreviewRule.createFor(preview, locale = "en") |
||||
|
||||
@Test |
||||
fun snapshot() { |
||||
ScreenshotTest.runTest(paparazzi = paparazziRule, preview = preview, localeStr = "en") |
||||
} |
||||
} |
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 ui |
||||
|
||||
import base.PaparazziPreviewRule |
||||
import base.ScreenshotTest |
||||
import base.Shard2ComposablePreviewProvider |
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import com.google.testing.junit.testparameterinjector.TestParameterInjector |
||||
import org.junit.Rule |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo |
||||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview |
||||
|
||||
/** |
||||
* Test that takes a preview and runs a screenshot test on it. |
||||
* It uses a sharded preview provider so multiple 'shards' can run in parallel, optimizing CPU and time usage. |
||||
*/ |
||||
@RunWith(TestParameterInjector::class) |
||||
class PreviewShard2Test( |
||||
@TestParameter(valuesProvider = Shard2ComposablePreviewProvider::class) |
||||
val preview: ComposablePreview<AndroidPreviewInfo>, |
||||
) { |
||||
@get:Rule |
||||
val paparazziRule = PaparazziPreviewRule.createFor(preview, locale = "en") |
||||
|
||||
@Test |
||||
fun snapshot() { |
||||
ScreenshotTest.runTest(paparazzi = paparazziRule, preview = preview, localeStr = "en") |
||||
} |
||||
} |
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 ui |
||||
|
||||
import base.PaparazziPreviewRule |
||||
import base.ScreenshotTest |
||||
import base.Shard3ComposablePreviewProvider |
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import com.google.testing.junit.testparameterinjector.TestParameterInjector |
||||
import org.junit.Rule |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo |
||||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview |
||||
|
||||
/** |
||||
* Test that takes a preview and runs a screenshot test on it. |
||||
* It uses a sharded preview provider so multiple 'shards' can run in parallel, optimizing CPU and time usage. |
||||
*/ |
||||
@RunWith(TestParameterInjector::class) |
||||
class PreviewShard3Test( |
||||
@TestParameter(valuesProvider = Shard3ComposablePreviewProvider::class) |
||||
val preview: ComposablePreview<AndroidPreviewInfo>, |
||||
) { |
||||
@get:Rule |
||||
val paparazziRule = PaparazziPreviewRule.createFor(preview, locale = "en") |
||||
|
||||
@Test |
||||
fun snapshot() { |
||||
ScreenshotTest.runTest(paparazzi = paparazziRule, preview = preview, localeStr = "en") |
||||
} |
||||
} |
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 ui |
||||
|
||||
import base.PaparazziPreviewRule |
||||
import base.ScreenshotTest |
||||
import base.Shard4ComposablePreviewProvider |
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import com.google.testing.junit.testparameterinjector.TestParameterInjector |
||||
import org.junit.Rule |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo |
||||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview |
||||
|
||||
/** |
||||
* Test that takes a preview and runs a screenshot test on it. |
||||
* It uses a sharded preview provider so multiple 'shards' can run in parallel, optimizing CPU and time usage. |
||||
*/ |
||||
@RunWith(TestParameterInjector::class) |
||||
class PreviewShard4Test( |
||||
@TestParameter(valuesProvider = Shard4ComposablePreviewProvider::class) |
||||
val preview: ComposablePreview<AndroidPreviewInfo>, |
||||
) { |
||||
@get:Rule |
||||
val paparazziRule = PaparazziPreviewRule.createFor(preview, locale = "en") |
||||
|
||||
@Test |
||||
fun snapshot() { |
||||
ScreenshotTest.runTest(paparazzi = paparazziRule, preview = preview, localeStr = "en") |
||||
} |
||||
} |
@ -1,49 +0,0 @@
@@ -1,49 +0,0 @@
|
||||
/* |
||||
* 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 |
||||
* |
||||
* 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, |
||||
* 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 ui |
||||
|
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import com.google.testing.junit.testparameterinjector.TestParameterInjector |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
|
||||
/** |
||||
* Screenshot test for the English version only. |
||||
*/ |
||||
@RunWith(TestParameterInjector::class) |
||||
class S : ScreenshotTest() { |
||||
/** |
||||
* *Note*: keep the method name as short as possible to get shorter filename for generated screenshot. |
||||
* Long name was preview_test. |
||||
*/ |
||||
@Test |
||||
fun t( |
||||
@TestParameter(valuesProvider = PreviewProvider::class) componentTestPreview: TestPreview, |
||||
@TestParameter baseDeviceConfig: BaseDeviceConfig, |
||||
@TestParameter(value = ["1.0"]) fontScale: Float, |
||||
// Need to keep the TestParameter to have filename including the language. |
||||
@TestParameter(value = ["en"]) localeStr: String, |
||||
) { |
||||
doTest( |
||||
componentTestPreview = componentTestPreview, |
||||
baseDeviceConfig = baseDeviceConfig, |
||||
fontScale = fontScale, |
||||
localeStr = localeStr, |
||||
) |
||||
} |
||||
} |
@ -1,125 +0,0 @@
@@ -1,125 +0,0 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 ui |
||||
|
||||
import android.content.res.Configuration |
||||
import android.os.LocaleList |
||||
import androidx.activity.OnBackPressedDispatcher |
||||
import androidx.activity.OnBackPressedDispatcherOwner |
||||
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner |
||||
import androidx.compose.foundation.background |
||||
import androidx.compose.foundation.layout.Box |
||||
import androidx.compose.material3.MaterialTheme |
||||
import androidx.compose.runtime.CompositionLocalProvider |
||||
import androidx.compose.ui.Modifier |
||||
import androidx.compose.ui.platform.LocalConfiguration |
||||
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 androidx.lifecycle.Lifecycle |
||||
import app.cash.paparazzi.Paparazzi |
||||
import app.cash.paparazzi.detectEnvironment |
||||
import com.android.ide.common.rendering.api.SessionParams |
||||
import com.android.resources.NightMode |
||||
import io.element.android.compound.theme.ElementTheme |
||||
import org.junit.Rule |
||||
import java.util.Locale |
||||
|
||||
/** |
||||
* 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. |
||||
* |
||||
* *Note*: keep the class name as short as possible to get shorter filename for generated screenshot. |
||||
* Long name was ScreenshotTest. |
||||
*/ |
||||
open class ScreenshotTest { |
||||
@get:Rule |
||||
val paparazzi = Paparazzi( |
||||
environment = detectEnvironment(), |
||||
maxPercentDifference = 0.01, |
||||
renderingMode = SessionParams.RenderingMode.NORMAL, |
||||
) |
||||
|
||||
protected fun doTest( |
||||
componentTestPreview: TestPreview, |
||||
baseDeviceConfig: BaseDeviceConfig, |
||||
fontScale: Float, |
||||
localeStr: String, |
||||
) { |
||||
val locale = localeStr.toLocale() |
||||
Locale.setDefault(locale) // Needed for regional settings, as first day of week |
||||
val densityScale = baseDeviceConfig.deviceConfig.density.dpiValue / 160f |
||||
val customScreenHeight = componentTestPreview.customHeightDp()?.value?.let { it * densityScale }?.toInt() |
||||
paparazzi.unsafeUpdateConfig( |
||||
deviceConfig = baseDeviceConfig.deviceConfig.copy( |
||||
softButtons = false, |
||||
locale = localeStr, |
||||
nightMode = componentTestPreview.isNightMode().let { |
||||
when (it) { |
||||
true -> NightMode.NIGHT |
||||
false -> NightMode.NOTNIGHT |
||||
} |
||||
}, |
||||
screenHeight = customScreenHeight ?: baseDeviceConfig.deviceConfig.screenHeight, |
||||
), |
||||
) |
||||
paparazzi.snapshot { |
||||
val lifecycleOwner = LocalLifecycleOwner.current |
||||
CompositionLocalProvider( |
||||
LocalInspectionMode provides true, |
||||
LocalDensity provides Density( |
||||
density = LocalDensity.current.density, |
||||
fontScale = fontScale |
||||
), |
||||
LocalConfiguration provides Configuration().apply { |
||||
setLocales(LocaleList(locale)) |
||||
uiMode = when (componentTestPreview.isNightMode()) { |
||||
true -> Configuration.UI_MODE_NIGHT_YES |
||||
false -> Configuration.UI_MODE_NIGHT_NO |
||||
} |
||||
}, |
||||
// Needed so that UI that uses it don't crash during screenshot tests |
||||
LocalOnBackPressedDispatcherOwner provides object : OnBackPressedDispatcherOwner { |
||||
override val lifecycle: Lifecycle get() = lifecycleOwner.lifecycle |
||||
override val onBackPressedDispatcher: OnBackPressedDispatcher get() = OnBackPressedDispatcher() |
||||
} |
||||
) { |
||||
ElementTheme { |
||||
Box( |
||||
modifier = Modifier |
||||
.background(MaterialTheme.colorScheme.background) |
||||
) { |
||||
componentTestPreview.Content() |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private fun String.toLocale(): Locale { |
||||
return when (this) { |
||||
"en" -> Locale.US |
||||
"fr" -> Locale.FRANCE |
||||
"de" -> Locale.GERMAN |
||||
else -> Locale.Builder().setLanguage(this).build() |
||||
} |
||||
} |
@ -1,52 +0,0 @@
@@ -1,52 +0,0 @@
|
||||
/* |
||||
* Copyright (c) 2024 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 ui |
||||
|
||||
import com.google.testing.junit.testparameterinjector.TestParameter |
||||
import com.google.testing.junit.testparameterinjector.TestParameterInjector |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
|
||||
/** |
||||
* Screenshot test for the Locale other then English. |
||||
*/ |
||||
@RunWith(TestParameterInjector::class) |
||||
class T : ScreenshotTest() { |
||||
/** |
||||
* *Note*: keep the method name as short as possible to get shorter filename for generated screenshot. |
||||
* Long name was preview_test. |
||||
*/ |
||||
@SuppressWarnings("MemberNameEqualsClassName") |
||||
@Test |
||||
fun t( |
||||
@TestParameter(valuesProvider = PreviewProvider::class) componentTestPreview: TestPreview, |
||||
@TestParameter baseDeviceConfig: BaseDeviceConfig, |
||||
@TestParameter(value = ["1.0"]) fontScale: Float, |
||||
@TestParameter(value = ["de"]) localeStr: String, |
||||
) { |
||||
// Only test ComponentTestPreview, and only with the light theme |
||||
if (componentTestPreview.isNightMode() || componentTestPreview !is ComponentTestPreview) { |
||||
return |
||||
} |
||||
doTest( |
||||
componentTestPreview = componentTestPreview, |
||||
baseDeviceConfig = baseDeviceConfig, |
||||
fontScale = fontScale, |
||||
localeStr = localeStr, |
||||
) |
||||
} |
||||
} |
@ -1,41 +0,0 @@
@@ -1,41 +0,0 @@
|
||||
/* |
||||
* 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 ui |
||||
|
||||
import androidx.compose.runtime.Composable |
||||
import androidx.compose.ui.tooling.preview.Preview |
||||
import androidx.compose.ui.unit.Dp |
||||
import com.airbnb.android.showkase.models.ShowkaseElementsMetadata |
||||
import io.element.android.libraries.designsystem.preview.NIGHT_MODE_NAME |
||||
|
||||
interface TestPreview { |
||||
@Composable |
||||
fun Content() |
||||
|
||||
val name: String |
||||
|
||||
fun customHeightDp(): Dp? = null |
||||
} |
||||
|
||||
/** |
||||
* Showkase doesn't put the [Preview.uiMode] parameter in its [ShowkaseElementsMetadata] |
||||
* so we have to encode the night mode bit in a preview's name. |
||||
*/ |
||||
fun TestPreview.isNightMode(): Boolean { |
||||
// Dark mode previews have name "N" so their component name contains "- N" |
||||
return this.name.contains("- $NIGHT_MODE_NAME") |
||||
} |
@ -1,50 +0,0 @@
@@ -1,50 +0,0 @@
|
||||
/* |
||||
* 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 ui |
||||
|
||||
import androidx.compose.foundation.layout.fillMaxWidth |
||||
import androidx.compose.foundation.layout.padding |
||||
import androidx.compose.foundation.text.BasicText |
||||
import androidx.compose.material3.MaterialTheme |
||||
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.Locale |
||||
|
||||
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.copy( |
||||
color = MaterialTheme.colorScheme.onBackground |
||||
) |
||||
) |
||||
} |
||||
|
||||
override val name: String = showkaseBrowserTypography.typographyName |
||||
|
||||
override fun toString(): String = "Typo_${showkaseBrowserTypography.typographyGroup}_${showkaseBrowserTypography.typographyName}" |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue