Skip to content
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 @Preview param "apiLevel" #560

Open
sergio-sastre opened this issue Nov 18, 2024 · 1 comment
Open

Support @Preview param "apiLevel" #560

sergio-sastre opened this issue Nov 18, 2024 · 1 comment

Comments

@sergio-sastre
Copy link
Contributor

Now that Robolectric 1.14 is out with support for TestParameterInjector, supporting apiLevel in the very same test class should be feasible.

I'll try to explore it and create a PR once I find the time

@sergio-sastre sergio-sastre changed the title Support apiLevel @Preview param Support @Preview param "apiLevel" Nov 18, 2024
@sergio-sastre
Copy link
Contributor Author

sergio-sastre commented Nov 18, 2024

This is an example of something I've just got working to showcase the idea behind:

private val cachedPreviews: List<ComposablePreview<AndroidPreviewInfo>> by lazy {
    AndroidComposablePreviewScanner()
        .scanPackageTrees("snapshot.testing.lazycolumn_previews.roborazzi")
        .includeAnnotationInfoForAllOf(RoborazziConfig::class.java)
        .getPreviews()
}

// One needs to generate one ComposablePreviewProvider per apiLevel
object ComposablePreviewProviderApi35 : TestParameterValuesProvider() {
    override fun provideValues(context: Context?): List<ComposablePreview<AndroidPreviewInfo>> =
        // apiLevel default = -1 -> renders with 35
        cachedPreviews.filter { it.previewInfo.apiLevel < 28 || it.previewInfo.apiLevel == 35 }
}

object ComposablePreviewProviderApi31 : TestParameterValuesProvider() {
    override fun provideValues(context: Context?): List<ComposablePreview<AndroidPreviewInfo>> =
        cachedPreviews.filter { it.previewInfo.apiLevel == 31 }
}
...

@RunWith(RobolectricTestParameterInjector::class)
class PaparazziComposePreviewTests {
    @get:Rule
    val composeTestRule = createAndroidComposeRule<ComponentActivity>()

    private fun snapshot(preview: ComposablePreview<AndroidPreviewInfo>) {
        composeTestRule.configureFor(preview)

        composeTestRule
            .apply { setContent { preview() } }
            .onRoot()
            .captureRoboImage(
                filePath = filePath(AndroidPreviewScreenshotIdBuilder(preview).build()),
                roborazziOptions = RoborazziOptionsMapper.createFor(preview)
            )
    }

   // Generate 1 Test per apiLevel
    @GraphicsMode(NATIVE)
    @Config(sdk = [35])
    @Test
    fun snapshotApi35(
        @TestParameter(valuesProvider = ComposablePreviewProviderApi35::class)
        preview: ComposablePreview<AndroidPreviewInfo>,
    ) {
        snapshot(preview)
    }

    @GraphicsMode(NATIVE)
    @Config(sdk = [31])
    @Test
    fun snapshotApi31(
        @TestParameter(valuesProvider = ComposablePreviewProviderApi31::class)
        preview: ComposablePreview<AndroidPreviewInfo>,
    ) {
        snapshot(preview)
    }
    ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant