-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add visibility fix for nested litho view
Summary: Add visibility fix for nested litho view Reviewed By: adityasharat Differential Revision: D57512293 fbshipit-source-id: b440351bc8913d4478567813dbc2b063e79de252
- Loading branch information
1 parent
26cea59
commit 73c2455
Showing
6 changed files
with
135 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...o-it/src/test/java/com/facebook/litho/LithoVisibilityEventsControllerForNestedViewTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.facebook.litho | ||
|
||
import com.facebook.litho.config.ComponentsConfiguration | ||
import com.facebook.litho.core.height | ||
import com.facebook.litho.core.width | ||
import com.facebook.litho.kotlin.widget.Text | ||
import com.facebook.litho.testing.LithoViewRule | ||
import com.facebook.litho.visibility.onInvisible | ||
import com.facebook.litho.visibility.onVisible | ||
import com.facebook.litho.widget.collection.LazyList | ||
import com.facebook.rendercore.sp | ||
import com.facebook.testing.robolectric.WithTestDefaultsRunner | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.annotation.LooperMode | ||
|
||
@LooperMode(LooperMode.Mode.LEGACY) | ||
@RunWith(WithTestDefaultsRunner::class) | ||
class LithoVisibilityEventsControllerForNestedViewTest { | ||
@Rule | ||
@JvmField | ||
val lithoViewRule: LithoViewRule = | ||
LithoViewRule(lithoVisibilityEventsController = { lithoVisibilityEventsControllerDelegate }) | ||
private val lithoVisibilityEventsControllerDelegate: LithoVisibilityEventsController = | ||
LithoVisibilityEventsControllerDelegate() | ||
private val invisibleTags: MutableSet<Int> = mutableSetOf() | ||
|
||
@Before | ||
fun setup() { | ||
ComponentsConfiguration.defaultInstance = | ||
ComponentsConfiguration.defaultInstance.copy(enableVisibilityFixForNestedLithoView = true) | ||
} | ||
|
||
@After | ||
fun breakdown() { | ||
ComponentsConfiguration.defaultInstance = | ||
ComponentsConfiguration.defaultInstance.copy(enableVisibilityFixForNestedLithoView = false) | ||
} | ||
|
||
@Test | ||
fun `test visibility events for nested LithoView`() { | ||
val testLithoView = | ||
lithoViewRule.render { | ||
LazyList(style = Style.height(100.sp).width(100.sp)) { | ||
for (i in 0 until 10) { | ||
child( | ||
component = | ||
VisibilityTrackingComponent(tag = i, invisibleTracking = invisibleTags)) | ||
} | ||
} | ||
} | ||
testLithoView.lithoView.subscribeComponentTreeToLifecycleProvider( | ||
lithoVisibilityEventsControllerDelegate) | ||
|
||
// testing initial state | ||
assertThat( | ||
testLithoView.lithoView.componentTree?.lithoVisibilityEventsController?.visibilityState) | ||
.isEqualTo(LithoVisibilityEventsController.LithoVisibilityState.HINT_VISIBLE) | ||
assertThat(invisibleTags).isEmpty() | ||
|
||
// move to invisible | ||
lithoVisibilityEventsControllerDelegate.moveToVisibilityState( | ||
LithoVisibilityEventsController.LithoVisibilityState.HINT_INVISIBLE) | ||
assertThat(invisibleTags).isEqualTo((0 until 10).toSet()) | ||
|
||
// move to visible | ||
lithoVisibilityEventsControllerDelegate.moveToVisibilityState( | ||
LithoVisibilityEventsController.LithoVisibilityState.HINT_VISIBLE) | ||
assertThat(invisibleTags).isEmpty() | ||
} | ||
} | ||
|
||
class VisibilityTrackingComponent(val invisibleTracking: MutableSet<Int>, val tag: Int) : | ||
KComponent() { | ||
override fun ComponentScope.render(): Component { | ||
|
||
return Text( | ||
text = "test", | ||
style = | ||
Style.height(5.sp) | ||
.onVisible { invisibleTracking.remove(tag) } | ||
.onInvisible { invisibleTracking.add(tag) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters