Skip to content

Commit

Permalink
Migrate LegacyLithoViewRule to LithoViewRule for `MountStateViewC…
Browse files Browse the repository at this point in the history
…lickTest`

Summary: as per title

Reviewed By: kingsleyadio

Differential Revision: D63540839

fbshipit-source-id: 278dee68f3267edb40ef5673fe9638d100c8a205
  • Loading branch information
Andrew Wang authored and facebook-github-bot committed Sep 27, 2024
1 parent f55b087 commit 9be024d
Showing 1 changed file with 31 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package com.facebook.litho

import android.content.Context
import android.view.ViewGroup
import androidx.test.core.app.ApplicationProvider
import com.facebook.litho.config.TempComponentsConfigurations
import com.facebook.litho.testing.LegacyLithoViewRule
import com.facebook.litho.testing.LithoViewRule
import com.facebook.litho.testing.eventhandler.EventHandlerTestHelper
import com.facebook.litho.testing.helper.ComponentTestHelper
import com.facebook.litho.testing.inlinelayoutspec.InlineLayoutSpec
Expand All @@ -39,13 +38,20 @@ import org.junit.runner.RunWith
class MountStateViewClickTest {

private lateinit var context: ComponentContext
private lateinit var componentTree: ComponentTree

@JvmField @Rule val legacyLithoViewRule = LegacyLithoViewRule()
@JvmField @Rule val lithoViewRule = LithoViewRule()

@Before
fun setup() {
TempComponentsConfigurations.setShouldAddHostViewForRootComponent(true)
context = ComponentContext(ApplicationProvider.getApplicationContext<Context>())
componentTree =
ComponentTree.create(context)
.componentsConfiguration(
context.lithoConfiguration.componentsConfig.copy(
incrementalMountEnabled = false, visibilityProcessingEnabled = false))
.build()
}

@After
Expand All @@ -55,12 +61,14 @@ class MountStateViewClickTest {

@Test
fun testInnerComponentHostClickable() {
val component =
LayoutWithInnerClickableChildTester.create(context).shouldSetClickHandler(true).build()
legacyLithoViewRule.setRoot(component)
val lithoView = legacyLithoViewRule.lithoView
setupLithoViewParentAndComponentTree(lithoView, component)
legacyLithoViewRule.attachToWindow().measure().layout()
val lithoView =
lithoViewRule
.render(componentTree = componentTree) {
LayoutWithInnerClickableChildTester.create(context)
.shouldSetClickHandler(true)
.build()
}
.lithoView
assertThat(lithoView.childCount).isEqualTo(1)
assertThat(lithoView.isClickable).isFalse
assertThat(lithoView.isLongClickable).isFalse
Expand All @@ -71,12 +79,14 @@ class MountStateViewClickTest {

@Test
fun testInnerComponentHostClickableWithLongClickHandler() {
val component =
LayoutWithInnerClickableChildTester.create(context).shouldSetLongClickHandler(true).build()
legacyLithoViewRule.setRoot(component)
val lithoView = legacyLithoViewRule.lithoView
setupLithoViewParentAndComponentTree(lithoView, component)
legacyLithoViewRule.attachToWindow().measure().layout()
val lithoView =
lithoViewRule
.render(componentTree = componentTree) {
LayoutWithInnerClickableChildTester.create(context)
.shouldSetLongClickHandler(true)
.build()
}
.lithoView
assertThat(lithoView.childCount).isEqualTo(1)
assertThat(lithoView.isClickable).isFalse
assertThat(lithoView.isLongClickable).isFalse
Expand Down Expand Up @@ -121,33 +131,15 @@ class MountStateViewClickTest {

@Test
fun testRootHostClickableUnmount() {
val component =
SimpleLayoutSpecWithClickHandlersTester.create(legacyLithoViewRule.context).build()
legacyLithoViewRule.setRoot(component)
setupLithoViewParentAndComponentTree(legacyLithoViewRule.lithoView, component)
legacyLithoViewRule.attachToWindow().measure().layout()
val rootHost = legacyLithoViewRule.lithoView.getChildAt(0)
val testLithoView =
lithoViewRule.render(componentTree = componentTree) {
SimpleLayoutSpecWithClickHandlersTester.create(context).build()
}
val rootHost = testLithoView.lithoView.getChildAt(0)
assertThat(rootHost.isClickable).isTrue
assertThat(rootHost.isLongClickable).isTrue
legacyLithoViewRule.lithoView.unmountAllItems()
testLithoView.lithoView.unmountAllItems()
assertThat(rootHost.isClickable).isFalse
assertThat(rootHost.isLongClickable).isFalse
}

// When testing a LithoView via a LegacyLithoViewRule - we must set the parent and component tree
// prior to attach / measure / layout for that LithoView otherwise mounting will not behave
// properly.
private fun setupLithoViewParentAndComponentTree(lithoView: LithoView, component: Component) {
val parent: ViewGroup =
object : ViewGroup(lithoView.context) {
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) = Unit
}
parent.addView(lithoView)
lithoView.componentTree =
ComponentTree.create(context, component)
.componentsConfiguration(
context.lithoConfiguration.componentsConfig.copy(
incrementalMountEnabled = false, visibilityProcessingEnabled = false))
.build()
}
}

0 comments on commit 9be024d

Please sign in to comment.