-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from DeNA/visual_regression_test_advanced
ハンズオン 「さまざまなケースでComposeの画面スクリーンショットを撮る」を追加
- Loading branch information
Showing
25 changed files
with
1,531 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
app/src/testAnswer/java/com/google/samples/apps/nowinandroid/VariousPreviewScreenshotTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package com.google.samples.apps.nowinandroid | ||
|
||
import android.content.Context | ||
import androidx.activity.ComponentActivity | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalInspectionMode | ||
import androidx.compose.ui.test.SemanticsMatcher | ||
import androidx.compose.ui.test.filter | ||
import androidx.compose.ui.test.isRoot | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onFirst | ||
import androidx.compose.ui.unit.Density | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.airbnb.android.showkase.models.Showkase | ||
import com.airbnb.android.showkase.models.ShowkaseBrowserComponent | ||
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi | ||
import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers | ||
import com.github.takahirom.roborazzi.captureRoboImage | ||
import com.github.takahirom.roborazzi.captureScreenRoboImage | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.ParameterizedRobolectricTestRunner | ||
import org.robolectric.RuntimeEnvironment | ||
import org.robolectric.Shadows | ||
import org.robolectric.annotation.Config | ||
import org.robolectric.annotation.GraphicsMode | ||
import org.robolectric.shadows.ShadowDisplay | ||
import kotlin.math.roundToInt | ||
|
||
@RunWith(ParameterizedRobolectricTestRunner::class) | ||
@GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
@Config(qualifiers = RobolectricDeviceQualifiers.Pixel7) | ||
class VariousPreviewScreenshotTest( | ||
private val testCase: TestCase | ||
) { | ||
@get:Rule | ||
val composeTestRule = createAndroidComposeRule<ComponentActivity>() | ||
lateinit var context: Context | ||
|
||
@Before | ||
fun setUp() { | ||
context = ApplicationProvider.getApplicationContext() | ||
} | ||
|
||
@Test | ||
fun testDefault() { | ||
val filePath = "${testCase.showkaseBrowserComponent.componentKey}.png" | ||
capturePreview(filePath = filePath) | ||
} | ||
|
||
@Test | ||
fun testFont2x() { | ||
val filePath = "${testCase.showkaseBrowserComponent.componentKey}_font2x.png" | ||
capturePreview(filePath = filePath, fontMagnification = 2f) | ||
} | ||
|
||
@OptIn(ExperimentalRoborazziApi::class) | ||
private fun capturePreview(filePath: String, fontMagnification: Float = 1f) { | ||
setGroupParameterToQualifiers(group = testCase.showkaseBrowserComponent.group) | ||
val widthDp = testCase.showkaseBrowserComponent.widthDp | ||
val heightDp = testCase.showkaseBrowserComponent.heightDp | ||
if (widthDp != null || heightDp != null) { | ||
setDisplaySize(widthDp, heightDp) | ||
} | ||
// Activityを再生成して変更した画面サイズを反映させる | ||
composeTestRule.activityRule.scenario.recreate() | ||
|
||
composeTestRule.setContent { | ||
val density = LocalDensity.current | ||
val customDensity = Density( | ||
fontScale = density.fontScale * fontMagnification, | ||
density = density.density, | ||
) | ||
CompositionLocalProvider( | ||
LocalInspectionMode provides true, | ||
LocalDensity provides customDensity, | ||
) { | ||
testCase.showkaseBrowserComponent.component() | ||
} | ||
} | ||
|
||
kotlin.runCatching { | ||
// 複数Windowがある場合、子コンポーネントがいる最初のものをとってくる | ||
composeTestRule.onAllNodes(isRoot()) | ||
.filter(hasAnyChild()) | ||
.onFirst() | ||
.assertExists() // 念のため存在していることをassertしている。assertに失敗したらnullを返し、captureScreenRoboImage()を使って全画面スクリーンショットを撮る | ||
}.getOrNull()?.captureRoboImage(filePath) ?: captureScreenRoboImage(filePath) | ||
} | ||
|
||
private fun setGroupParameterToQualifiers(group: String) { | ||
if (group != "Default Group") { | ||
val tags = testCase.showkaseBrowserComponent.group.split("-") | ||
tags.forEach { tag -> | ||
RuntimeEnvironment.setQualifiers("+$tag") | ||
} | ||
} | ||
} | ||
|
||
private fun setDisplaySize(widthDp: Int?, heightDp: Int?) { | ||
val display = ShadowDisplay.getDefaultDisplay() | ||
val density = context.resources.displayMetrics.density | ||
widthDp?.let { | ||
val widthPx = (widthDp * density).roundToInt() | ||
Shadows.shadowOf(display).setWidth(widthPx) | ||
} | ||
heightDp?.let { | ||
val heightPx = (heightDp * density).roundToInt() | ||
Shadows.shadowOf(display).setHeight(heightPx) | ||
} | ||
} | ||
|
||
fun hasAnyChild(): SemanticsMatcher { | ||
return SemanticsMatcher("hasAnyChildThat") { | ||
it.children.isNotEmpty() | ||
} | ||
} | ||
|
||
companion object { | ||
class TestCase( | ||
val showkaseBrowserComponent: ShowkaseBrowserComponent | ||
) { | ||
override fun toString() = showkaseBrowserComponent.componentKey | ||
} | ||
|
||
|
||
@ParameterizedRobolectricTestRunner.Parameters(name = "[{index}] {0}") | ||
@JvmStatic | ||
fun components(): Iterable<Array<*>> = Showkase.getMetadata().componentList.map { | ||
arrayOf(TestCase(it)) | ||
} | ||
} | ||
} |
39 changes: 38 additions & 1 deletion
39
app/src/testExercise/java/com/google/samples/apps/nowinandroid/AllPreviewScreenshotTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,40 @@ | ||
package com.google.samples.apps.nowinandroid | ||
|
||
class AllPreviewScreenshotTest() | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onRoot | ||
import com.airbnb.android.showkase.models.Showkase | ||
import com.airbnb.android.showkase.models.ShowkaseBrowserComponent | ||
import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers | ||
import com.github.takahirom.roborazzi.captureRoboImage | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.ParameterizedRobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
import org.robolectric.annotation.GraphicsMode | ||
|
||
@RunWith(ParameterizedRobolectricTestRunner::class) | ||
@GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
@Config(qualifiers = RobolectricDeviceQualifiers.Pixel7) | ||
class AllPreviewScreenshotTest( | ||
private val showkaseBrowserComponent: ShowkaseBrowserComponent, | ||
) { | ||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun test() { | ||
composeTestRule.setContent { | ||
// showkaseBrowserComponent.component() がPreview指定されているComposable関数 | ||
showkaseBrowserComponent.component() | ||
} | ||
// ここでスクリーンショットを取得する | ||
composeTestRule.onRoot().captureRoboImage() | ||
} | ||
|
||
companion object { | ||
@ParameterizedRobolectricTestRunner.Parameters | ||
@JvmStatic | ||
fun components(): Iterable<Array<*>> = Showkase.getMetadata().componentList.map { arrayOf(it) } | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...rc/testExercise/java/com/google/samples/apps/nowinandroid/VariousPreviewScreenshotTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.google.samples.apps.nowinandroid | ||
|
||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.ui.platform.LocalInspectionMode | ||
import androidx.compose.ui.test.SemanticsMatcher | ||
import androidx.compose.ui.test.filter | ||
import androidx.compose.ui.test.isRoot | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onFirst | ||
import com.airbnb.android.showkase.models.Showkase | ||
import com.airbnb.android.showkase.models.ShowkaseBrowserComponent | ||
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi | ||
import com.github.takahirom.roborazzi.RobolectricDeviceQualifiers | ||
import com.github.takahirom.roborazzi.captureRoboImage | ||
import com.github.takahirom.roborazzi.captureScreenRoboImage | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.ParameterizedRobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
import org.robolectric.annotation.GraphicsMode | ||
|
||
@RunWith(ParameterizedRobolectricTestRunner::class) | ||
@GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
@Config(qualifiers = RobolectricDeviceQualifiers.Pixel7) | ||
class VariousPreviewScreenshotTest( | ||
private val testCase: TestCase | ||
) { | ||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@OptIn(ExperimentalRoborazziApi::class) | ||
@Test | ||
fun test() { | ||
val filePath = "${testCase.showkaseBrowserComponent.componentKey}.png" | ||
composeTestRule.setContent { | ||
CompositionLocalProvider( | ||
LocalInspectionMode provides true, | ||
) { | ||
testCase.showkaseBrowserComponent.component() | ||
} | ||
} | ||
kotlin.runCatching { | ||
// 複数Windowがある場合、子コンポーネントがいる最初のものをとってくる | ||
composeTestRule.onAllNodes(isRoot()) | ||
.filter(hasAnyChild()) | ||
.onFirst() | ||
.assertExists() // 念のため存在していることをassertしている。assertに失敗したらnullを返し、captureScreenRoboImage()を使って全画面スクリーンショットを撮る | ||
}.getOrNull()?.captureRoboImage(filePath) ?: captureScreenRoboImage(filePath) | ||
} | ||
|
||
fun hasAnyChild(): SemanticsMatcher { | ||
return SemanticsMatcher("hasAnyChildThat") { | ||
it.children.isNotEmpty() | ||
} | ||
} | ||
|
||
companion object { | ||
class TestCase( | ||
val showkaseBrowserComponent: ShowkaseBrowserComponent | ||
) { | ||
override fun toString() = showkaseBrowserComponent.componentKey | ||
} | ||
|
||
@ParameterizedRobolectricTestRunner.Parameters(name = "[{index}] {0}") | ||
@JvmStatic | ||
fun components(): Iterable<Array<*>> = Showkase.getMetadata().componentList.map { | ||
arrayOf(TestCase(it)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.