Skip to content

Commit 063ce7f

Browse files
Pass over a scroll view's focus candidate that takes no keyboard focus
When the next focus lies outside a scroll view, ReactScrollView and its horizontal and nested siblings ask the shadow tree for the next focusable element inside it, and that search offers every focusable or accessible node. A view focusable={false} keeps out of keyboard navigation is still accessible, so the search handed it back, the view refused focus and Tab stopped moving. findNextFocusableView now moves on from a candidate whose addFocusables adds nothing, and ends at a candidate it already offered, since forward candidates can lead back to each other. ReactViewGroup's addFocusables and requestFocus overrides are added to ReactAndroid.api.
1 parent 9f5e303 commit 063ce7f

3 files changed

Lines changed: 200 additions & 10 deletions

File tree

‎packages/react-native/ReactAndroid/api/ReactAndroid.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6445,6 +6445,7 @@ public final class com/facebook/react/views/view/ReactDrawableHelper {
64456445
public class com/facebook/react/views/view/ReactViewGroup : android/view/ViewGroup, com/facebook/react/touch/ReactHitSlopView, com/facebook/react/touch/ReactInterceptingViewGroup, com/facebook/react/uimanager/HasElevatedDescendantCache, com/facebook/react/uimanager/ReactClippingViewGroup, com/facebook/react/uimanager/ReactOverflowViewWithInset, com/facebook/react/uimanager/ReactPointerEventsView {
64466446
public fun <init> (Landroid/content/Context;)V
64476447
public fun addChildrenForAccessibility (Ljava/util/ArrayList;)V
6448+
public fun addFocusables (Ljava/util/ArrayList;II)V
64486449
public final fun cleanUpAxOrderListener ()V
64496450
protected fun dispatchDraw (Landroid/graphics/Canvas;)V
64506451
public fun dispatchGenericMotionEvent (Landroid/view/MotionEvent;)Z
@@ -6474,6 +6475,7 @@ public class com/facebook/react/views/view/ReactViewGroup : android/view/ViewGro
64746475
public fun onTouchEvent (Landroid/view/MotionEvent;)Z
64756476
public fun onViewAdded (Landroid/view/View;)V
64766477
public fun onViewRemoved (Landroid/view/View;)V
6478+
public fun requestFocus (ILandroid/graphics/Rect;)Z
64776479
public fun requestLayout ()V
64786480
public final fun setAxOrderList (Ljava/util/List;)V
64796481
public final fun setBackfaceVisibility (Ljava/lang/String;)V

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,20 +593,41 @@ public object ReactScrollViewHelper {
593593
UIManagerHelper.getUIManager(host.context as ReactContext, UIManagerType.FABRIC)
594594
?: return null
595595

596-
val nextFocusableViewId =
597-
(uimanager as FabricUIManager).findNextFocusableElement(host.id, focused.id, direction)
598-
?: return null
596+
// The shadow tree offers every focusable or accessible node, while a view can still keep
597+
// itself out of keyboard navigation (focusable={false} on an accessible view), so the search
598+
// moves on from a candidate that offers no keyboard focus. Forward and backward candidates can
599+
// lead back to each other, which the visited ids end.
600+
val visitedIds = mutableSetOf(focused.id)
601+
var sourceId = focused.id
602+
while (true) {
603+
val nextFocusableViewId =
604+
(uimanager as FabricUIManager).findNextFocusableElement(host.id, sourceId, direction)
605+
?: return null
606+
if (!visitedIds.add(nextFocusableViewId)) {
607+
return null
608+
}
609+
610+
val ancestorIdList =
611+
uimanager
612+
.getRelativeAncestorList(host.getChildAt(0).id, nextFocusableViewId)
613+
?.toMutableSet() ?: return null
599614

600-
val ancestorIdList =
601-
uimanager
602-
.getRelativeAncestorList(host.getChildAt(0).id, nextFocusableViewId)
603-
?.toMutableSet() ?: return null
615+
ancestorIdList.add(nextFocusableViewId)
604616

605-
ancestorIdList.add(nextFocusableViewId)
617+
host.updateClippingRect(ancestorIdList)
606618

607-
host.updateClippingRect(ancestorIdList)
619+
val nextFocusableView = host.findViewById<View>(nextFocusableViewId) ?: return null
620+
if (offersKeyboardFocus(nextFocusableView, direction)) {
621+
return nextFocusableView
622+
}
623+
sourceId = nextFocusableViewId
624+
}
625+
}
608626

609-
return host.findViewById(nextFocusableViewId)
627+
private fun offersKeyboardFocus(view: View, @FocusDirection direction: Int): Boolean {
628+
val focusables = ArrayList<View>()
629+
view.addFocusables(focusables, direction)
630+
return focusables.isNotEmpty()
610631
}
611632

612633
@JvmStatic
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// The deprecated APIs exercised here remain public and required by this Robolectric setup.
9+
@file:Suppress("DEPRECATION")
10+
11+
package com.facebook.react.views.scroll
12+
13+
import android.util.DisplayMetrics
14+
import android.view.View
15+
import com.facebook.react.bridge.ReactContext
16+
import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext
17+
import com.facebook.react.bridge.UIManager
18+
import com.facebook.react.fabric.FabricUIManager
19+
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
20+
import com.facebook.react.uimanager.DisplayMetricsHolder
21+
import com.facebook.react.uimanager.ThemedReactContext
22+
import com.facebook.react.uimanager.UIManagerHelper
23+
import com.facebook.react.uimanager.common.UIManagerType
24+
import com.facebook.react.views.view.ReactViewGroup
25+
import com.facebook.react.views.view.ReactViewManager
26+
import com.facebook.testutils.shadows.ShadowNativeLoader
27+
import com.facebook.testutils.shadows.ShadowSoLoader
28+
import org.assertj.core.api.Assertions.assertThat
29+
import org.junit.After
30+
import org.junit.Before
31+
import org.junit.Test
32+
import org.junit.runner.RunWith
33+
import org.mockito.MockedStatic
34+
import org.mockito.Mockito.mockStatic
35+
import org.mockito.kotlin.any
36+
import org.mockito.kotlin.eq
37+
import org.mockito.kotlin.mock
38+
import org.mockito.kotlin.whenever
39+
import org.robolectric.RobolectricTestRunner
40+
import org.robolectric.RuntimeEnvironment
41+
import org.robolectric.annotation.Config
42+
43+
/**
44+
* The shadow-tree search a scroll view falls back to, when the next focus lies outside it, passes
45+
* over a candidate the view tree keeps out of keyboard navigation.
46+
*/
47+
@RunWith(RobolectricTestRunner::class)
48+
@Config(shadows = [ShadowSoLoader::class, ShadowNativeLoader::class])
49+
class ReactScrollViewHelperFocusSearchTest {
50+
51+
private lateinit var themedContext: ThemedReactContext
52+
private lateinit var uiManagerHelperMock: MockedStatic<UIManagerHelper>
53+
private val fabricUIManager: FabricUIManager = mock()
54+
55+
@Before
56+
fun setUp() {
57+
ReactNativeFeatureFlagsForTests.setUp()
58+
val context = createTestReactApplicationContext(RuntimeEnvironment.getApplication())
59+
themedContext = ThemedReactContext(context, context, null, -1)
60+
DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics())
61+
uiManagerHelperMock = mockStatic(UIManagerHelper::class.java)
62+
uiManagerHelperMock
63+
.`when`<UIManager?> {
64+
UIManagerHelper.getUIManager(any<ReactContext>(), eq(UIManagerType.FABRIC))
65+
}
66+
.thenReturn(fabricUIManager)
67+
}
68+
69+
@After
70+
fun tearDown() {
71+
uiManagerHelperMock.close()
72+
DisplayMetricsHolder.setScreenDisplayMetrics(null)
73+
}
74+
75+
@Test
76+
fun `passes over a candidate that focusable false keeps out of keyboard navigation`() {
77+
val scene = buildScene()
78+
ReactViewManager().setFocusable(scene.candidate, false)
79+
offerInOrder(scene, scene.first, scene.candidate, scene.last)
80+
81+
assertThat(findNextFocusableView(scene)).isSameAs(scene.last)
82+
}
83+
84+
@Test
85+
fun `finds nothing when no later candidate takes keyboard focus`() {
86+
val scene = buildScene()
87+
ReactViewManager().setFocusable(scene.candidate, false)
88+
offerInOrder(scene, scene.first, scene.candidate)
89+
90+
assertThat(findNextFocusableView(scene)).isNull()
91+
}
92+
93+
@Test
94+
fun `ends when the candidates lead back to where the search started`() {
95+
val scene = buildScene()
96+
ReactViewManager().setFocusable(scene.candidate, false)
97+
offerInOrder(scene, scene.first, scene.candidate, scene.first)
98+
99+
assertThat(findNextFocusableView(scene)).isNull()
100+
}
101+
102+
@Test
103+
fun `takes a candidate whose descendant takes keyboard focus`() {
104+
val scene = buildScene()
105+
ReactViewManager().setFocusable(scene.candidate, false)
106+
scene.candidate.addView(focusableView(id = 4))
107+
offerInOrder(scene, scene.first, scene.candidate, scene.last)
108+
109+
assertThat(findNextFocusableView(scene)).isSameAs(scene.candidate)
110+
}
111+
112+
@Test
113+
fun `takes a candidate that takes keyboard focus itself`() {
114+
val scene = buildScene()
115+
offerInOrder(scene, scene.first, scene.candidate, scene.last)
116+
117+
assertThat(findNextFocusableView(scene)).isSameAs(scene.candidate)
118+
}
119+
120+
private fun buildScene(): Scene {
121+
val scrollView = ReactScrollView(themedContext).apply { id = 20 }
122+
val content = ReactViewGroup(themedContext).apply { id = 10 }
123+
val first = focusableView(id = 1)
124+
val candidate =
125+
ReactViewGroup(themedContext).apply {
126+
id = 2
127+
ReactViewManager().setAccessible(this, true)
128+
}
129+
val last = focusableView(id = 3)
130+
content.addView(first)
131+
content.addView(candidate)
132+
content.addView(last)
133+
scrollView.addView(content)
134+
whenever(fabricUIManager.getRelativeAncestorList(eq(content.id), any()))
135+
.thenReturn(intArrayOf(content.id))
136+
return Scene(scrollView, first, candidate, last)
137+
}
138+
139+
private fun focusableView(id: Int): View =
140+
View(themedContext).apply {
141+
this.id = id
142+
isFocusable = true
143+
}
144+
145+
private fun offerInOrder(scene: Scene, vararg order: View) {
146+
order.toList().zipWithNext().forEach { (source, next) ->
147+
whenever(
148+
fabricUIManager.findNextFocusableElement(
149+
eq(scene.scrollView.id),
150+
eq(source.id),
151+
eq(View.FOCUS_FORWARD),
152+
)
153+
)
154+
.thenReturn(next.id)
155+
}
156+
}
157+
158+
private fun findNextFocusableView(scene: Scene): View? =
159+
ReactScrollViewHelper.findNextFocusableView(scene.scrollView, scene.first, View.FOCUS_FORWARD)
160+
161+
private class Scene(
162+
val scrollView: ReactScrollView,
163+
val first: View,
164+
val candidate: ReactViewGroup,
165+
val last: View,
166+
)
167+
}

0 commit comments

Comments
 (0)