paparazzi icon indicating copy to clipboard operation
paparazzi copied to clipboard

Snapshots are not generated for Android Views that are annotated with Dagger's `@AndroidEntryPoint`

Open Tinder-FroWarner opened this issue 1 year ago • 4 comments

Description

We are working on creating a snapshot test for an Android View with injected dependencies via Dagger/Hilt, but are running into an issue when generating the snapshot if a view is annotated with @AndroidEntryPoint.

Steps to Reproduce

The following gives an example of the error and setup.

Exception

java.lang.IllegalStateException: Could not find an Application in the given context: com.android.layoutlib.bridge.android.BridgeContext@6425422c 
at dagger.hilt.android.internal.Contexts.getApplication(Contexts.java:42)
at dagger.hilt.android.internal.managers.ViewComponentManager.getParentContext(ViewComponentManager.java:146)
at dagger.hilt.android.internal.managers.ViewComponentManager.getParentComponentManager(ViewComponentManager.java:128)
at dagger.hilt.android.internal.managers.ViewComponentManager.createComponent(ViewComponentManager.java:86) 
at dagger.hilt.android.internal.managers.ViewComponentManager.generatedComponent(ViewComponentManager.java:77)
...

Example Code

Example of View:

@AndroidEntryPoint
class CustomView(
    context: Context,
    attrs: AttributeSet? = null,
) : CustomComponentView<CustomModel>(context, attrs) {

    private val binding = CustomViewBinding.inflate(LayoutInflater.from(context), this)

    @Inject
    lateinit var injectedLinkLauncher: InjectedLinkLauncher

    override fun bind(uiModel: CustomModel) {
        // ...
    }
// ... 

Example of Test:

class CustomViewSnapshotTest {
    @get:Rule
    val paparazzi = Paparazzi(
        deviceConfig = PIXEL_5,
        maxPercentDifference = 0.01
    )

    @Test
    fun `CustomView - content`() {
        val view = CustomView(paparazzi.context)
        view.bind(
            uiModel = CustomComponentUiModel.CustomModel(
                text = "Content",
                 // ...
            )
        )
        paparazzi.snapshot(view)
    }
}

Expected behavior

Snapshots generate and tests pass.

Actual Behavior

Snapshots only generate and tests pass when commenting out the injected vals and @AndroidEntryPoint i.e.

// @AndroidEntryPoint
class CustomView(
    context: Context,
    attrs: AttributeSet? = null,
) : CustomComponentView<CustomModel>(context, attrs) {

    private val binding = CustomViewBinding.inflate(LayoutInflater.from(context), this)

   // @Inject
   // lateinit var injectedLinkLauncher: InjectedLinkLauncher

    override fun bind(uiModel: CustomModel) {
        // ...
    }
// commented out injectedLinkLauncher usage as well

Additional information:

  • Paparazzi Version: 1.3.2
  • OS: Mac OS ver. 12.6 (M1 Max)
  • Gradle Version: 17.0.9
  • Android Gradle Plugin Version: 8.1.0

Tinder-FroWarner avatar Jan 22 '24 18:01 Tinder-FroWarner