From e17972bd05806ce12f14a5e10a8902d1db316897 Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Wed, 29 May 2024 12:00:02 -0700 Subject: [PATCH 1/5] Handle snapshoting private preview methods and improve parameterized test naming --- ...eSnapshotReflectiveParameterizedInvoker.kt | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt index 9a3db10b..b9c8b6a8 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt @@ -1,6 +1,7 @@ package com.emergetools.snapshots.compose import android.util.Log +import androidx.compose.runtime.Composer import androidx.compose.runtime.currentComposer import androidx.compose.ui.test.junit4.ComposeContentTestRule import androidx.compose.ui.test.junit4.createComposeRule @@ -17,14 +18,22 @@ import org.junit.runners.Parameterized import java.io.File @RunWith(Parameterized::class) -class EmergeComposeSnapshotReflectiveParameterizedInvoker(private val previewConfig: ComposePreviewSnapshotConfig) { +class EmergeComposeSnapshotReflectiveParameterizedInvoker(private val parameter: EmergeComposeSnapshotReflectiveParameters) { + + // Wrapper class to give us better parameterized test naming + data class EmergeComposeSnapshotReflectiveParameters( + val previewConfig: ComposePreviewSnapshotConfig + ) { + override fun toString(): String = previewConfig.keyName() + } + companion object { const val TAG = "EmergeComposeSnapshotReflectiveParameterizedInvoker" const val ARG_REFLECTIVE_INVOKE_DATA_PATH = "invoke_data_path" @JvmStatic - @Parameterized.Parameters - fun data(): Iterable { + @Parameterized.Parameters(name = "{index} {0}") + fun data(): Iterable { val args = InstrumentationRegistry.getArguments() val invokeDataPath = args.getString(ARG_REFLECTIVE_INVOKE_DATA_PATH) ?: run { @@ -41,7 +50,9 @@ class EmergeComposeSnapshotReflectiveParameterizedInvoker(private val previewCon ignoreUnknownKeys = true } - return json.decodeFromString(invokeDataFile.readText()).snapshots + return json.decodeFromString(invokeDataFile.readText()).snapshots.map { + EmergeComposeSnapshotReflectiveParameters(it) + } } } @@ -54,14 +65,23 @@ class EmergeComposeSnapshotReflectiveParameterizedInvoker(private val previewCon @Test @Suppress("TooGenericExceptionCaught") fun reflectiveComposableInvoker() { + val previewConfig = parameter.previewConfig try { composeRule.setContent { val klass = Class.forName(previewConfig.fullyQualifiedClassName) + Log.d(TAG, "Found class for ${previewConfig.fullyQualifiedClassName}: ${klass.name}") + val methodName = previewConfig.originalFqn.substringAfterLast(".") val composableMethod = klass.methods.find { - it.name == previewConfig.originalFqn.substringAfterLast(".") + Log.d(TAG, "Checking method in class ${klass.name}: ${it.name}") + it.name == methodName + } ?: klass.getDeclaredMethod(methodName, Composer::class.java, Int::class.javaPrimitiveType) + + if (composableMethod != null && !composableMethod.isAccessible) { + Log.e(TAG, "Marking composable method as accessible: ${previewConfig.originalFqn}") + composableMethod.isAccessible = true } - Log.d(TAG, "Invoking composable method: $composableMethod") + Log.d(TAG, "Invoking composable method: ${composableMethod?.name}") composableMethod?.let { it.isAccessible = true From 1be752a0131a71ab783053585f2c847a9a8b7404 Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Wed, 29 May 2024 13:40:12 -0700 Subject: [PATCH 2/5] Detekt --- .../EmergeComposeSnapshotReflectiveParameterizedInvoker.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt index b9c8b6a8..48efc756 100644 --- a/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt +++ b/snapshots/snapshots/src/main/kotlin/com/emergetools/snapshots/compose/EmergeComposeSnapshotReflectiveParameterizedInvoker.kt @@ -18,7 +18,9 @@ import org.junit.runners.Parameterized import java.io.File @RunWith(Parameterized::class) -class EmergeComposeSnapshotReflectiveParameterizedInvoker(private val parameter: EmergeComposeSnapshotReflectiveParameters) { +class EmergeComposeSnapshotReflectiveParameterizedInvoker( + private val parameter: EmergeComposeSnapshotReflectiveParameters +) { // Wrapper class to give us better parameterized test naming data class EmergeComposeSnapshotReflectiveParameters( From 54eb82229fab6aba59c14a091c1dd294fce1b2c5 Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Wed, 29 May 2024 13:46:12 -0700 Subject: [PATCH 3/5] Add private sample --- .../snapshots/sample/ui/TextRowWithIcon.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt b/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt index 969722d7..7682ce01 100644 --- a/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt +++ b/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt @@ -54,22 +54,23 @@ internal fun TextRowWithIconPreviewFromMainInternalFunctionMultiPreview() { ) } -@LocalePreviews -@FontScalePreviews + +@SnapshotTestingPreview @Composable -fun TextRowWithIconPreviewFromMainJustStackedMultiPreview() { +private fun TextRowWithIconPreviewFromMainPrivateFunctionMultiPreview() { TextRowWithIcon( titleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_title), subtitleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_subtitle) ) } -@SnapshotTestingPreview +@LocalePreviews +@FontScalePreviews @Composable -fun TextRowWithIconPreviewFromMainJustSnapshotTestingPreview() { +fun TextRowWithIconPreviewFromMainJustStackedMultiPreview() { TextRowWithIcon( - titleText = "Title SnapshotTestingPreview2", - subtitleText = "Subtitle SnapshotTestingPreview" + titleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_title), + subtitleText = stringResource(com.emergetools.snapshots.sample.R.string.sample_subtitle) ) } From b1a26f264f2f121c4eb9f43669dbbdc910fbe7cd Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Wed, 29 May 2024 13:51:59 -0700 Subject: [PATCH 4/5] Detekt --- .../com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt b/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt index 7682ce01..f377ed77 100644 --- a/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt +++ b/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt @@ -54,7 +54,6 @@ internal fun TextRowWithIconPreviewFromMainInternalFunctionMultiPreview() { ) } - @SnapshotTestingPreview @Composable private fun TextRowWithIconPreviewFromMainPrivateFunctionMultiPreview() { From ceceff467954a2c2fa39ad41f8ab695bd9f17675 Mon Sep 17 00:00:00 2001 From: Ryan Brooks Date: Wed, 29 May 2024 13:56:36 -0700 Subject: [PATCH 5/5] Detekt --- .../com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt b/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt index f377ed77..3b0a3bd6 100644 --- a/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt +++ b/snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/TextRowWithIcon.kt @@ -54,6 +54,7 @@ internal fun TextRowWithIconPreviewFromMainInternalFunctionMultiPreview() { ) } +@Suppress("UnusedPrivateMember") @SnapshotTestingPreview @Composable private fun TextRowWithIconPreviewFromMainPrivateFunctionMultiPreview() {