-
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 #7 from DeNA/droidkaigi2024
「Composable Preview Scannerを使ってプレビュー画面のスクリーンショットを撮る」追加
- Loading branch information
Showing
17 changed files
with
579 additions
and
32 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
Empty file.
119 changes: 119 additions & 0 deletions
119
app/src/testAnswer/java/com/google/samples/apps/nowinandroid/MyComposePreviewTester.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,119 @@ | ||
package com.google.samples.apps.nowinandroid | ||
|
||
import android.content.Context | ||
import android.content.res.Configuration | ||
import androidx.activity.ComponentActivity | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onRoot | ||
import androidx.compose.ui.unit.Density | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.github.takahirom.roborazzi.ComposePreviewTester | ||
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi | ||
import com.github.takahirom.roborazzi.captureRoboImage | ||
import com.google.samples.apps.nowinandroid.core.ui.DelayedPreview | ||
import org.robolectric.RuntimeEnvironment | ||
import org.robolectric.Shadows | ||
import org.robolectric.shadows.ShadowDisplay | ||
import sergio.sastre.composable.preview.scanner.android.AndroidComposablePreviewScanner | ||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo | ||
import sergio.sastre.composable.preview.scanner.android.screenshotid.AndroidPreviewScreenshotIdBuilder | ||
import sergio.sastre.composable.preview.scanner.core.preview.ComposablePreview | ||
import sergio.sastre.composable.preview.scanner.core.preview.getAnnotation | ||
import kotlin.math.roundToInt | ||
|
||
@OptIn(ExperimentalRoborazziApi::class) | ||
class MyComposePreviewTester : ComposePreviewTester<AndroidPreviewInfo> { | ||
val composeTestRule = createAndroidComposeRule<ComponentActivity>() | ||
override fun options(): ComposePreviewTester.Options { | ||
val testLifecycleOptions = ComposePreviewTester.Options.JUnit4TestLifecycleOptions( | ||
testRuleFactory = { composeTestRule } | ||
) | ||
return super.options().copy(testLifecycleOptions = testLifecycleOptions) | ||
} | ||
|
||
override fun previews(): List<ComposablePreview<AndroidPreviewInfo>> { | ||
val options = options() | ||
return AndroidComposablePreviewScanner() | ||
.scanPackageTrees(*options.scanOptions.packages.toTypedArray()) | ||
.includeAnnotationInfoForAllOf(DelayedPreview::class.java) | ||
.getPreviews() | ||
} | ||
|
||
override fun test(preview: ComposablePreview<AndroidPreviewInfo>) { | ||
val delay = preview.getAnnotation<DelayedPreview>()?.delay ?: 0L | ||
val previewScannerFileName = | ||
AndroidPreviewScreenshotIdBuilder(preview).build() | ||
val fileName = | ||
if (delay == 0L) previewScannerFileName else "${previewScannerFileName}_delay$delay" | ||
val filePath = "$fileName.png" | ||
preview.myApplyToRobolectricConfiguration() | ||
composeTestRule.activityRule.scenario.recreate() | ||
composeTestRule.apply { | ||
try { | ||
if (delay != 0L) { | ||
mainClock.autoAdvance = false | ||
} | ||
setContent { | ||
ApplyToCompositionLocal(preview) { | ||
preview() | ||
} | ||
} | ||
if (delay != 0L) { | ||
mainClock.advanceTimeBy(delay) | ||
} | ||
onRoot().captureRoboImage(filePath = filePath) | ||
} finally { | ||
mainClock.autoAdvance = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun ApplyToCompositionLocal( | ||
preview: ComposablePreview<AndroidPreviewInfo>, | ||
content: @Composable () -> Unit | ||
) { | ||
val fontScale = preview.previewInfo.fontScale | ||
val density = LocalDensity.current | ||
val customDensity = | ||
Density(density = density.density, fontScale = density.fontScale * fontScale) | ||
CompositionLocalProvider(LocalDensity provides customDensity) { | ||
content() | ||
} | ||
|
||
} | ||
|
||
|
||
fun ComposablePreview<AndroidPreviewInfo>.myApplyToRobolectricConfiguration() { | ||
val preview = this | ||
// ナイトモード | ||
when (preview.previewInfo.uiMode and Configuration.UI_MODE_NIGHT_MASK) { | ||
Configuration.UI_MODE_NIGHT_YES -> RuntimeEnvironment.setQualifiers("+night") | ||
Configuration.UI_MODE_NIGHT_NO -> RuntimeEnvironment.setQualifiers("+notnight") | ||
else -> { /* do nothing */ | ||
} | ||
} | ||
|
||
// 画面サイズ | ||
if (preview.previewInfo.widthDp != -1 && preview.previewInfo.heightDp != -1) { | ||
setDisplaySize(preview.previewInfo.widthDp, preview.previewInfo.heightDp) | ||
} | ||
} | ||
|
||
private fun setDisplaySize(widthDp: Int, heightDp: Int) { | ||
val context = ApplicationProvider.getApplicationContext<Context>() | ||
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) | ||
} | ||
} |
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
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
3 changes: 3 additions & 0 deletions
3
core/ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/DelayedPreview.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,3 @@ | ||
package com.google.samples.apps.nowinandroid.core.ui | ||
|
||
annotation class DelayedPreview(val delay: Long) |
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
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.