Skip to content

Allow for custom appCompatViewInflater #274 #480

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ class PaparazziPluginTest {
assertThat(snapshotImage).isSimilarTo(goldenImage).withDefaultThreshold()
}

@Test
@Ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you wanna remove the @Ignore here rather than the @Test

fun withMaterialComponents() {
val fixtureRoot = File("src/test/projects/material-components-present")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ android {
}

dependencies {
implementation 'com.google.android.material:material:1.5.0-alpha03'
}
implementation 'com.google.android.material:material:1.6.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import org.junit.Test

class ButtonViewTest {
@get:Rule
val paparazzi = Paparazzi(theme = "Theme.MaterialComponents")
val paparazzi = Paparazzi(
theme = "Theme.MaterialComponents",
appCompatViewInflaterClassName = "com.google.android.material.theme.MaterialComponentsViewInflater",
)

@Test
fun testViews() {
Expand Down
40 changes: 24 additions & 16 deletions paparazzi/src/main/java/app/cash/paparazzi/Paparazzi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import com.android.layoutlib.bridge.impl.RenderAction
import com.android.layoutlib.bridge.impl.RenderSessionImpl
import java.awt.image.BufferedImage
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.util.Date
import java.util.concurrent.TimeUnit
Expand All @@ -86,6 +87,7 @@ class Paparazzi @JvmOverloads constructor(
private val theme: String = "android:Theme.Material.NoActionBar.Fullscreen",
private val renderingMode: RenderingMode = RenderingMode.NORMAL,
private val appCompatEnabled: Boolean = true,
private val appCompatViewInflaterClassName: String = "androidx.appcompat.app.AppCompatViewInflater",
private val maxPercentDifference: Double = 0.1,
private val snapshotHandler: SnapshotHandler = determineHandler(maxPercentDifference),
private val renderExtensions: Set<RenderExtension> = setOf()
Expand Down Expand Up @@ -444,22 +446,8 @@ class Paparazzi @JvmOverloads constructor(
context: Context,
attrs: AttributeSet
): View? {
val appCompatViewInflaterClass =
Class.forName("androidx.appcompat.app.AppCompatViewInflater")

val createViewMethod = appCompatViewInflaterClass
.getDeclaredMethod(
"createView",
View::class.java,
String::class.java,
Context::class.java,
AttributeSet::class.java,
Boolean::class.javaPrimitiveType,
Boolean::class.javaPrimitiveType,
Boolean::class.javaPrimitiveType,
Boolean::class.javaPrimitiveType
)
.apply { isAccessible = true }
val appCompatViewInflaterClass = Class.forName(appCompatViewInflaterClassName)
val createViewMethod = getCreateViewMethod(appCompatViewInflaterClass) ?: throw IllegalStateException("View inflater doesn't have `createView` method")

val inheritContext = true
val readAndroidTheme = true
Expand All @@ -476,6 +464,26 @@ class Paparazzi @JvmOverloads constructor(
) as View?
}

fun getCreateViewMethod(clazz: Class<*>): Method? {
try {
return clazz
.getDeclaredMethod(
"createView",
View::class.java,
String::class.java,
Context::class.java,
AttributeSet::class.java,
Boolean::class.javaPrimitiveType,
Boolean::class.javaPrimitiveType,
Boolean::class.javaPrimitiveType,
Boolean::class.javaPrimitiveType
)
.apply { isAccessible = true }
} catch (error: NoSuchMethodException) {
return clazz.superclass?.let { getCreateViewMethod(it) }
}
}

override fun onCreateView(
name: String,
context: Context,
Expand Down