-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support includePrivatePreviews of ComposablePreviewScanner #445
Merged
takahirom
merged 16 commits into
main
from
takahirom/support-includePrivatePreviews-of-ComposablePreviewcanner/2024-07-23
Jul 27, 2024
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5a4a1f2
Support includePrivatePreviews of ComposablePreviewScanner
takahirom 81b7ac2
Fix library name
takahirom 5e2f98b
Add integration test
takahirom 0091801
Fix to use includePrivatePreviews
takahirom c994e81
Introduce ComposePreviewTester.options() to avoid breaking changes an…
takahirom 9e20058
Make testRule testRuleFactory and add integration tests
takahirom d0f9a68
Remove unneeded log
takahirom 0085b06
Fix method name
takahirom 9511535
Add documents
takahirom 52812e8
Add README
takahirom 2f12551
Fix lifecycle of tester class
takahirom 044f49c
Remove unneeded transient
takahirom 386510e
Add document for ComposePreviewTester
takahirom 4e81397
Fix kdoc
takahirom 2ca3096
Add OptIn of ExperimentalRoborazziApi to RoborazziPreviewParameterize…
takahirom 2519f46
Remove removed parameter doc
takahirom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ class GeneratePreviewTestTest { | |
checkHasImages() | ||
} | ||
} | ||
|
||
@Test | ||
fun whenKmpModuleAndRecordRunImagesShouldBeRecorded() { | ||
RoborazziGradleRootProject(testProjectDir).previewModule.apply { | ||
|
@@ -26,6 +27,40 @@ class GeneratePreviewTestTest { | |
checkHasImages() | ||
} | ||
} | ||
|
||
@Test | ||
fun whenDisablePreviewAndRecordRunImagesShouldNotBeRecorded() { | ||
RoborazziGradleRootProject(testProjectDir).previewModule.apply { | ||
buildGradle.enable = false | ||
|
||
record() | ||
|
||
checkNoImages() | ||
} | ||
} | ||
|
||
@Test | ||
fun whenCustomTesterAndRecordRunImagesShouldBeRecordedAndCanSeeJUnitLog() { | ||
RoborazziGradleRootProject(testProjectDir).previewModule.apply { | ||
buildGradle.useCustomTester = true | ||
|
||
record { | ||
itShouldHaveJUnitRuleLog() | ||
} | ||
|
||
checkHasImages() | ||
} | ||
} | ||
|
||
@Test | ||
fun whenIncludePrivatePreviewsAndRecordRunImagesShouldBeRecorded() { | ||
RoborazziGradleRootProject(testProjectDir).previewModule.apply { | ||
buildGradle.isIncludePrivatePreviews = true | ||
record() | ||
|
||
checkHasPrivatePreviewImages() | ||
} | ||
} | ||
} | ||
|
||
class PreviewModule( | ||
|
@@ -35,6 +70,7 @@ class PreviewModule( | |
companion object { | ||
val moduleName = "sample-generate-preview-tests" | ||
} | ||
|
||
val buildGradle = BuildGradle(testProjectDir) | ||
|
||
class BuildGradle(private val projectFolder: TemporaryFolder) { | ||
|
@@ -44,14 +80,8 @@ class PreviewModule( | |
val file = | ||
projectFolder.root.resolve(PATH) | ||
file.parentFile.mkdirs() | ||
val roborazziExtension = """ | ||
roborazzi { | ||
generateComposePreviewRobolectricTests { | ||
enable = true | ||
packages = listOf("com.github.takahirom.preview.tests") | ||
} | ||
} | ||
""".trimIndent() | ||
|
||
val roborazziExtension = createRoborazziExtension() | ||
val androidBlock = """ | ||
android { | ||
namespace = "com.github.takahirom.preview.tests" | ||
|
@@ -89,7 +119,7 @@ class PreviewModule( | |
} | ||
|
||
""".trimIndent() | ||
val buildGradleText = if(isKmp) | ||
val buildGradleText = if (isKmp) | ||
""" | ||
plugins { | ||
kotlin("multiplatform") | ||
|
@@ -144,7 +174,7 @@ class PreviewModule( | |
maven { url = uri("https://jitpack.io") } | ||
} | ||
""".trimIndent() | ||
else """ | ||
else """ | ||
plugins { | ||
id("com.android.application") | ||
// id("com.android.library") | ||
|
@@ -180,10 +210,44 @@ class PreviewModule( | |
buildGradleText.trimIndent() | ||
) | ||
} | ||
|
||
var enable = true | ||
var isIncludePrivatePreviews = false | ||
var useCustomTester = false | ||
|
||
private fun createRoborazziExtension(): String { | ||
val includePrivatePreviewsExpr = if (isIncludePrivatePreviews) { | ||
"""includePrivatePreviews = $isIncludePrivatePreviews""" | ||
} else { | ||
"" | ||
} | ||
val customTesterExpr = if (useCustomTester) { | ||
"""testerQualifiedClassName = "com.github.takahirom.sample.CustomPreviewTester"""" | ||
} else { | ||
"" | ||
} | ||
val roborazziExtension = """ | ||
roborazzi { | ||
generateComposePreviewRobolectricTests { | ||
enable = $enable | ||
packages = listOf("com.github.takahirom.preview.tests") | ||
$includePrivatePreviewsExpr | ||
$customTesterExpr | ||
} | ||
} | ||
""".trimIndent() | ||
return roborazziExtension | ||
} | ||
} | ||
|
||
fun record(checks: BuildResult.() -> Unit = {}) { | ||
val result = runTask("recordRoborazziDebug") | ||
result.checks() | ||
} | ||
|
||
fun record() { | ||
runTask("recordRoborazziDebug") | ||
fun BuildResult.itShouldHaveJUnitRuleLog() { | ||
assert(output.contains("JUnit4TestLifecycleOptions starting")) | ||
assert(output.contains("JUnit4TestLifecycleOptions finished")) | ||
} | ||
|
||
private fun runTask( | ||
|
@@ -205,4 +269,24 @@ class PreviewModule( | |
println("images:" + images?.toList()) | ||
assert(images?.isNotEmpty() == true) | ||
} | ||
|
||
fun checkNoImages() { | ||
val images = testProjectDir.root.resolve("$moduleName/build/outputs/roborazzi/").listFiles() | ||
println("images:" + images?.toList()) | ||
checkResultCount( | ||
testProjectDir.root.resolve("$moduleName/build/test-results/roborazzi/results-summary.json") | ||
// All zero | ||
) | ||
|
||
assert(images?.isEmpty() == true) | ||
} | ||
|
||
fun checkHasPrivatePreviewImages() { | ||
val privateImages = | ||
testProjectDir.root.resolve("$moduleName/build/outputs/roborazzi/").listFiles() | ||
.orEmpty() | ||
.filter { it.name.contains("PreviewWithPrivate") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not to name it "WithPrivatePreviews" instead? |
||
println("images:" + privateImages.toList()) | ||
assert(privateImages.isNotEmpty() == true) | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...e-generate-preview-tests/src/test/java/com/github/takahirom/sample/CustomPreviewTester.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,26 @@ | ||
package com.github.takahirom.sample | ||
|
||
import org.junit.Assert.* | ||
import com.github.takahirom.roborazzi.* | ||
import org.junit.rules.TestWatcher | ||
import sergio.sastre.composable.preview.scanner.android.AndroidPreviewInfo | ||
|
||
class CustomPreviewTester : ComposePreviewTester<AndroidPreviewInfo> by AndroidComposePreviewTester() { | ||
override fun options(): ComposePreviewTester.Options = super.options().copy( | ||
testLifecycleOptions = ComposePreviewTester.Options.JUnit4TestLifecycleOptions( | ||
testRuleFactory = { | ||
object : TestWatcher() { | ||
override fun starting(description: org.junit.runner.Description?) { | ||
println("JUnit4TestLifecycleOptions starting") | ||
super.starting(description) | ||
} | ||
|
||
override fun finished(description: org.junit.runner.Description?) { | ||
println("JUnit4TestLifecycleOptions finished") | ||
super.finished(description) | ||
} | ||
} | ||
} | ||
) | ||
) | ||
} |
17 changes: 0 additions & 17 deletions
17
...ample-generate-preview-tests/src/test/java/com/github/takahirom/sample/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I named it like that and I had my own doubts.
Google is also inconsistent on whether to call them "Compose Previews", like in "Compose Preview Screenshot Testing tool" or "Composable Previews" like here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I know it should not be
Compose Composable Preview
. 😊I personally think we can use ComposePreview in an Android context, and also I think we can use ComposablePreview in a Jetpack Compose context. This is because Composable is an annotation name, so it's a minor distinction, while Compose is the framework name.