Skip to content

Commit

Permalink
Add ReactViewGroupTest (#46843)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46843

Add some simple tests for ReactViewGroup clipping logic

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D63903975

fbshipit-source-id: 8ce683a4aeba2f3916a26d7b4e93b4a30227c6d5
  • Loading branch information
Thomas Nardone authored and pull[bot] committed Dec 10, 2024
1 parent c55c638 commit 3399332
Showing 1 changed file with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.view

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class ReactViewGroupTest {

private lateinit var context: Context

@Before
fun setUp() {
ReactNativeFeatureFlagsForTests.setUp()
context = Robolectric.buildActivity(Activity::class.java).create().get()
}

@Test
fun `View clipping - ensure allChildren properly resizes when adding views in sequence`() {
val rvg = ReactViewGroup(context)
rvg.left = 0
rvg.right = 100
rvg.top = 0
rvg.bottom = 100
FrameLayout(context).addView(rvg)
rvg.removeClippedSubviews = true
for (i in 0..20) {
rvg.addViewWithSubviewClippingEnabled(TestView(context, i * 10), i)
}
rvg.updateClippingRect()
assertThat(rvg.childCount).isEqualTo(10)
}

@Test
fun `View clipping - ensure allChildren properly resizes when adding views out of sequence`() {
val rvg = ReactViewGroup(context)
rvg.left = 0
rvg.right = 100
rvg.top = 0
rvg.bottom = 100
FrameLayout(context).addView(rvg)
rvg.removeClippedSubviews = true
for (i in 0..10) {
rvg.addViewWithSubviewClippingEnabled(TestView(context, i * 10), i)
}
repeat(10) { rvg.addViewWithSubviewClippingEnabled(TestView(context, 90), 10) }
rvg.updateClippingRect()
assertThat(rvg.childCount).isEqualTo(20)
}
}

class TestView(context: Context, yPos: Int) : View(context) {
init {
left = 0
right = 100
top = yPos
bottom = top + 10
}
}

class TestParent(context: Context) : ViewGroup(context) {
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) = Unit
}

0 comments on commit 3399332

Please sign in to comment.