From 7ffadcef115f3abefa2c1648c348d816320c2c68 Mon Sep 17 00:00:00 2001 From: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com> Date: Fri, 4 Dec 2026 20:32:26 +0200 Subject: [PATCH] Return no next view when a ScrollView's content view is gone findNextFocusableView read host.getChildAt(0).id without checking for the child, so a focus search on a scroll view whose content view had already been removed threw a NullPointerException, as #53046 reports for a screen navigating back with a focused TextInput. It now returns null, which leaves the platform's own answer in place. Signed-off-by: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com> --- .../views/scroll/ReactScrollViewHelper.kt | 7 +- .../ReactScrollViewHelperFocusSearchTest.kt | 81 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewHelperFocusSearchTest.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt index 4edac4af7bb6..cb29c428165b 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt @@ -589,6 +589,8 @@ public object ReactScrollViewHelper { return null } + val contentView = host.getChildAt(0) ?: return null + val uimanager = UIManagerHelper.getUIManager(host.context as ReactContext, UIManagerType.FABRIC) ?: return null @@ -598,9 +600,8 @@ public object ReactScrollViewHelper { ?: return null val ancestorIdList = - uimanager - .getRelativeAncestorList(host.getChildAt(0).id, nextFocusableViewId) - ?.toMutableSet() ?: return null + uimanager.getRelativeAncestorList(contentView.id, nextFocusableViewId)?.toMutableSet() + ?: return null ancestorIdList.add(nextFocusableViewId) diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewHelperFocusSearchTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewHelperFocusSearchTest.kt new file mode 100644 index 000000000000..31e56520fb24 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewHelperFocusSearchTest.kt @@ -0,0 +1,81 @@ +/* + * 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.scroll + +import android.view.View +import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext +import com.facebook.react.fabric.FabricUIManager +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.uimanager.UIManagerHelper +import com.facebook.react.uimanager.common.UIManagerType +import com.facebook.testutils.shadows.ShadowSoLoader +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.MockedStatic +import org.mockito.Mockito.mockStatic +import org.mockito.kotlin.any +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** Verifies the shadow-tree focus search on a scroll view whose content view is gone. */ +@RunWith(RobolectricTestRunner::class) +@Config(shadows = [ShadowSoLoader::class]) +class ReactScrollViewHelperFocusSearchTest { + + private lateinit var themedContext: ThemedReactContext + private lateinit var uiManagerHelperMock: MockedStatic + + @Before + fun setup() { + ReactNativeFeatureFlagsForTests.setUp() + val context = createTestReactApplicationContext() + themedContext = ThemedReactContext(context, context, null, -1) + + val fabricUIManager: FabricUIManager = mock() + whenever(fabricUIManager.findNextFocusableElement(any(), any(), any())).thenReturn(NEXT_TAG) + + uiManagerHelperMock = mockStatic(UIManagerHelper::class.java) + uiManagerHelperMock + .`when` { UIManagerHelper.getUIManager(any(), eq(UIManagerType.FABRIC)) } + .thenReturn(fabricUIManager) + } + + @After + fun teardown() { + uiManagerHelperMock.close() + } + + @Test + fun testScrollViewWithoutContentViewHasNoNextFocusableView() { + val scrollView = ReactScrollView(themedContext) + val focused = View(themedContext) + + assertThat(ReactScrollViewHelper.findNextFocusableView(scrollView, focused, View.FOCUS_DOWN)) + .isNull() + } + + @Test + fun testHorizontalScrollViewWithoutContentViewHasNoNextFocusableView() { + val scrollView = ReactHorizontalScrollView(themedContext) + val focused = View(themedContext) + + assertThat(ReactScrollViewHelper.findNextFocusableView(scrollView, focused, View.FOCUS_RIGHT)) + .isNull() + } + + private companion object { + const val NEXT_TAG = 42 + } +}