From 33993326a44c76b1c9c8e5bd24d619d3af987b3c Mon Sep 17 00:00:00 2001 From: Thomas Nardone Date: Mon, 7 Oct 2024 10:37:44 -0700 Subject: [PATCH] Add ReactViewGroupTest (#46843) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46843 Add some simple tests for ReactViewGroup clipping logic Changelog: [Internal] Reviewed By: NickGerleman Differential Revision: D63903975 fbshipit-source-id: 8ce683a4aeba2f3916a26d7b4e93b4a30227c6d5 --- .../react/views/view/ReactViewGroupTest.kt | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt new file mode 100644 index 00000000000000..588d92963e388f --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt @@ -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 +}