-
Notifications
You must be signed in to change notification settings - Fork 35
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 5 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,16 @@ class GeneratePreviewTestTest { | |
checkHasImages() | ||
} | ||
} | ||
|
||
@Test | ||
fun whenIncludePrivatePreviewsAndRecordRunImagesShouldBeRecorded() { | ||
RoborazziGradleRootProject(testProjectDir).previewModule.apply { | ||
buildGradle.isIncludePrivatePreviews = true | ||
record() | ||
|
||
checkHasPrivatePreviewImages() | ||
} | ||
} | ||
} | ||
|
||
class PreviewModule( | ||
|
@@ -35,20 +46,28 @@ class PreviewModule( | |
companion object { | ||
val moduleName = "sample-generate-preview-tests" | ||
} | ||
|
||
val buildGradle = BuildGradle(testProjectDir) | ||
|
||
class BuildGradle(private val projectFolder: TemporaryFolder) { | ||
private val PATH = moduleName + "/build.gradle.kts" | ||
var isKmp = false | ||
var isIncludePrivatePreviews = false | ||
fun write() { | ||
val file = | ||
projectFolder.root.resolve(PATH) | ||
file.parentFile.mkdirs() | ||
val includePrivatePreviewsExpr = if (isIncludePrivatePreviews) { | ||
"""includePrivatePreviews = $isIncludePrivatePreviews""" | ||
} else { | ||
"" | ||
} | ||
val roborazziExtension = """ | ||
roborazzi { | ||
generateComposePreviewRobolectricTests { | ||
enable = true | ||
packages = listOf("com.github.takahirom.preview.tests") | ||
$includePrivatePreviewsExpr | ||
} | ||
} | ||
""".trimIndent() | ||
|
@@ -89,7 +108,7 @@ class PreviewModule( | |
} | ||
|
||
""".trimIndent() | ||
val buildGradleText = if(isKmp) | ||
val buildGradleText = if (isKmp) | ||
""" | ||
plugins { | ||
kotlin("multiplatform") | ||
|
@@ -144,7 +163,7 @@ class PreviewModule( | |
maven { url = uri("https://jitpack.io") } | ||
} | ||
""".trimIndent() | ||
else """ | ||
else """ | ||
plugins { | ||
id("com.android.application") | ||
// id("com.android.library") | ||
|
@@ -205,4 +224,13 @@ class PreviewModule( | |
println("images:" + images?.toList()) | ||
assert(images?.isNotEmpty() == 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import org.gradle.api.tasks.testing.Test | |
import org.gradle.invocation.DefaultGradle | ||
import java.io.File | ||
import java.net.URLEncoder | ||
import java.util.Locale | ||
import javax.inject.Inject | ||
|
||
open class GenerateComposePreviewRobolectricTestsExtension @Inject constructor(objects: ObjectFactory) { | ||
|
@@ -31,10 +32,10 @@ open class GenerateComposePreviewRobolectricTestsExtension @Inject constructor(o | |
val packages: ListProperty<String> = objects.listProperty(String::class.java) | ||
|
||
/** | ||
* The fully qualified class name of the custom test class that implements [com.github.takahirom.roborazzi.ComposePreviewTester]. | ||
* If true, the private previews will be included in the test. | ||
*/ | ||
val testerQualifiedClassName: Property<String> = objects.property(String::class.java) | ||
.convention("com.github.takahirom.roborazzi.AndroidComposePreviewTester") | ||
val includePrivatePreviews: Property<Boolean> = objects.property(Boolean::class.java) | ||
.convention(false) | ||
|
||
/** | ||
* [robolectricConfig] will be passed to the Robolectric's @Config annotation in the generated test class. | ||
|
@@ -48,6 +49,14 @@ open class GenerateComposePreviewRobolectricTestsExtension @Inject constructor(o | |
"qualifiers" to "RobolectricDeviceQualifiers.Pixel4a", | ||
) | ||
) | ||
|
||
/** | ||
* The fully qualified class name of the custom test class that implements [com.github.takahirom.roborazzi.ComposePreviewTester]. | ||
* This is advanced usage. You can implement your own test class that implements [com.github.takahirom.roborazzi.ComposePreviewTester]. | ||
*/ | ||
val testerQualifiedClassName: Property<String> = objects.property(String::class.java) | ||
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. I've simply changed the order because testerQualifiedClassName is an advanced option. |
||
.convention("com.github.takahirom.roborazzi.AndroidComposePreviewTester") | ||
|
||
} | ||
|
||
fun generateComposePreviewRobolectricTestsIfNeeded( | ||
|
@@ -63,7 +72,7 @@ fun generateComposePreviewRobolectricTestsIfNeeded( | |
setupGenerateComposePreviewRobolectricTestsTask( | ||
project = project, | ||
variant = variant, | ||
scanPackages = extension.packages, | ||
extension = extension, | ||
testerQualifiedClassName = extension.testerQualifiedClassName, | ||
robolectricConfig = extension.robolectricConfig | ||
) | ||
|
@@ -79,23 +88,24 @@ fun generateComposePreviewRobolectricTestsIfNeeded( | |
private fun setupGenerateComposePreviewRobolectricTestsTask( | ||
project: Project, | ||
variant: Variant, | ||
scanPackages: ListProperty<String>, | ||
extension: GenerateComposePreviewRobolectricTestsExtension, | ||
testerQualifiedClassName: Property<String>, | ||
robolectricConfig: MapProperty<String, String>, | ||
) { | ||
check(scanPackages.get().orEmpty().isNotEmpty()) { | ||
check(extension.packages.get().orEmpty().isNotEmpty()) { | ||
"Please set roborazzi.generateRobolectricPreviewTests.packages in the generatePreviewTests extension or set roborazzi.generateRobolectricPreviewTests.enable = false." + | ||
"See https://github.com/sergio-sastre/ComposablePreviewScanner?tab=readme-ov-file#how-to-use for more information." | ||
} | ||
|
||
val generateTestsTask = project.tasks.register( | ||
"generate${variant.name.capitalize()}ComposePreviewRobolectricTests", | ||
"generate${variant.name.capitalize(Locale.ROOT)}ComposePreviewRobolectricTests", | ||
GenerateComposePreviewRobolectricTestsTask::class.java | ||
) { | ||
// It seems that this directory path is overridden by addGeneratedSourceDirectory. | ||
// The generated tests will be located in build/JAVA/generate[VariantName]ComposePreviewRobolectricTests. | ||
it.outputDir.set(project.layout.buildDirectory.dir("generated/roborazzi/preview-screenshot")) | ||
it.scanPackageTrees.set(scanPackages) | ||
it.scanPackageTrees.set(extension.packages) | ||
it.includePrivatePreviews.set(extension.includePrivatePreviews) | ||
it.testerQualifiedClassName.set(testerQualifiedClassName) | ||
it.robolectricConfig.set(robolectricConfig) | ||
} | ||
|
@@ -114,6 +124,9 @@ abstract class GenerateComposePreviewRobolectricTestsTask : DefaultTask() { | |
@get:Input | ||
var scanPackageTrees: ListProperty<String> = project.objects.listProperty(String::class.java) | ||
|
||
@get:Input | ||
abstract val includePrivatePreviews: Property<Boolean> | ||
|
||
@get:Input | ||
abstract val testerQualifiedClassName: Property<String> | ||
|
||
|
@@ -126,6 +139,7 @@ abstract class GenerateComposePreviewRobolectricTestsTask : DefaultTask() { | |
testDir.mkdirs() | ||
|
||
val packagesExpr = scanPackageTrees.get().joinToString(", ") { "\"$it\"" } | ||
val includePrivatePreviewsExpr = includePrivatePreviews.get() | ||
|
||
val generatedClassFQDN = "com.github.takahirom.roborazzi.RoborazziPreviewParameterizedTests" | ||
val packageName = generatedClassFQDN.substringBeforeLast(".") | ||
|
@@ -140,8 +154,11 @@ abstract class GenerateComposePreviewRobolectricTestsTask : DefaultTask() { | |
File(directory, "$className.kt").writeText( | ||
""" | ||
package $packageName | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.rules.TestWatcher | ||
import org.junit.rules.RuleChain | ||
import org.robolectric.ParameterizedRobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
import org.robolectric.annotation.GraphicsMode | ||
|
@@ -158,26 +175,42 @@ abstract class GenerateComposePreviewRobolectricTestsTask : DefaultTask() { | |
class $className( | ||
private val preview: ComposablePreview<Any>, | ||
) { | ||
|
||
private val testLifecycleOptions = tester.options().testLifecycleOptions | ||
@get:Rule | ||
val rule = RuleChain.outerRule( | ||
if(testLifecycleOptions is ComposePreviewTester.Options.JUnit4TestLifecycleOptions) { | ||
(testLifecycleOptions as ComposePreviewTester.Options.JUnit4TestLifecycleOptions).testRule | ||
} else { | ||
object : TestWatcher() {} | ||
} | ||
) | ||
companion object { | ||
val tester = getComposePreviewTester("$testerQualifiedClassNameString") | ||
// lazy for performance | ||
val previews: List<ComposablePreview<Any>> by lazy { | ||
getComposePreviewTester("$testerQualifiedClassNameString").previews( | ||
$packagesExpr | ||
) | ||
setupDefaultOptions() | ||
tester.previews() | ||
} | ||
@JvmStatic | ||
@ParameterizedRobolectricTestRunner.Parameters(name = "{0}") | ||
fun values(): List<ComposablePreview<Any>> = | ||
previews | ||
fun values(): List<ComposablePreview<Any>> = previews | ||
|
||
fun setupDefaultOptions() { | ||
ComposePreviewTester.defaultOptionsFromPlugin = ComposePreviewTester.Options( | ||
scanOptions = ComposePreviewTester.Options.ScanOptions( | ||
packages = listOf($packagesExpr), | ||
includePrivatePreviews = $includePrivatePreviewsExpr, | ||
) | ||
) | ||
} | ||
} | ||
|
||
|
||
@GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
$robolectricConfigString | ||
@Test | ||
fun test() { | ||
getComposePreviewTester("$testerQualifiedClassNameString").test(preview) | ||
tester.test(preview) | ||
} | ||
|
||
} | ||
|
@@ -289,7 +322,8 @@ private fun verifyLibraryDependencies( | |
val dependencies = this | ||
val libNameArray = libraryName.split(":") | ||
if (!dependencies.contains(libNameArray[0] to libNameArray[1])) { | ||
val configurationNames = "'testImplementation'(For Android Project) or 'kotlin.sourceSets.androidUnitTest.dependencies.implementation'(For KMP)" | ||
val configurationNames = | ||
"'testImplementation'(For Android Project) or 'kotlin.sourceSets.androidUnitTest.dependencies.implementation'(For KMP)" | ||
error( | ||
"Roborazzi: Please add the following $configurationNames dependency to the 'dependencies' block in the 'build.gradle' file: '$libraryName' for the $configurationNames configuration.\n" + | ||
"For your convenience, visit https://www.google.com/search?q=" + URLEncoder.encode( | ||
|
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.