|
| 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 | +package com.facebook.react.views.text |
| 9 | + |
| 10 | +import android.app.Activity |
| 11 | +import android.content.Context |
| 12 | +import android.view.FocusFinder |
| 13 | +import android.view.View |
| 14 | +import android.view.ViewGroup |
| 15 | +import androidx.test.platform.app.InstrumentationRegistry |
| 16 | +import com.facebook.react.bridge.JavaOnlyMap |
| 17 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 18 | +import com.facebook.react.uimanager.ReactStylesDiffMap |
| 19 | +import com.facebook.react.uimanager.ViewManager |
| 20 | +import com.facebook.react.views.view.ReactViewGroup |
| 21 | +import org.assertj.core.api.Assertions.assertThat |
| 22 | +import org.junit.After |
| 23 | +import org.junit.Before |
| 24 | +import org.junit.Test |
| 25 | +import org.junit.runner.RunWith |
| 26 | +import org.robolectric.Robolectric |
| 27 | +import org.robolectric.RobolectricTestRunner |
| 28 | + |
| 29 | +abstract class TextKeyboardFocusTest<T : View> { |
| 30 | + |
| 31 | + protected lateinit var context: Context |
| 32 | + private lateinit var manager: ViewManager<T, *> |
| 33 | + |
| 34 | + protected abstract fun createManager(): ViewManager<T, *> |
| 35 | + |
| 36 | + protected abstract fun createTextView(context: Context): T |
| 37 | + |
| 38 | + protected abstract fun recycle(text: T) |
| 39 | + |
| 40 | + @Before |
| 41 | + fun setUp() { |
| 42 | + ReactNativeFeatureFlagsForTests.setUp() |
| 43 | + context = Robolectric.buildActivity(Activity::class.java).create().get() |
| 44 | + manager = createManager() |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun `focusable false - keeps an accessible text out of keyboard focus search`() { |
| 49 | + val root = ReactViewGroup(context) |
| 50 | + val leading = focusableView(top = 0) |
| 51 | + val text = accessibleText(top = 10, "focusable", false) |
| 52 | + val trailing = focusableView(top = 20) |
| 53 | + root.addView(leading) |
| 54 | + root.addView(text) |
| 55 | + root.addView(trailing) |
| 56 | + |
| 57 | + assertThat(collectKeyboardFocusables(root)).containsExactly(leading, trailing) |
| 58 | + // Accessibility services still treat the text as one focusable element. |
| 59 | + assertThat(text.isFocusable).isTrue() |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `focusable false - default focus passes over an excluded text`() { |
| 64 | + val root = ReactViewGroup(context) |
| 65 | + val text = accessibleText(top = 0, "focusable", false) |
| 66 | + val trailing = focusableView(top = 10) |
| 67 | + root.addView(text) |
| 68 | + root.addView(trailing) |
| 69 | + |
| 70 | + assertThat(root.requestFocus(View.FOCUS_DOWN)).isTrue() |
| 71 | + |
| 72 | + assertThat(text.isFocused).isFalse() |
| 73 | + assertThat(trailing.isFocused).isTrue() |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `focusable false - keyboard focus moves on from an excluded text that holds focus`() { |
| 78 | + val root = ReactViewGroup(context) |
| 79 | + val leading = focusableView(top = 0) |
| 80 | + val text = accessibleText(top = 10) |
| 81 | + val trailing = focusableView(top = 20) |
| 82 | + root.addView(leading) |
| 83 | + root.addView(text) |
| 84 | + root.addView(trailing) |
| 85 | + Robolectric.buildActivity(Activity::class.java).setup().get().setContentView(root) |
| 86 | + assertThat(text.requestFocus()).isTrue() |
| 87 | + |
| 88 | + applyProps(text, "focusable", false) |
| 89 | + |
| 90 | + assertThat(FocusFinder.getInstance().findNextFocus(root, text, View.FOCUS_FORWARD)) |
| 91 | + .isSameAs(trailing) |
| 92 | + assertThat(FocusFinder.getInstance().findNextFocus(root, text, View.FOCUS_BACKWARD)) |
| 93 | + .isSameAs(leading) |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun `focusable true - returns a text to keyboard focus search`() { |
| 98 | + val root = ReactViewGroup(context) |
| 99 | + val text = accessibleText(top = 0, "focusable", false) |
| 100 | + root.addView(text) |
| 101 | + assertThat(collectKeyboardFocusables(root)).isEmpty() |
| 102 | + |
| 103 | + applyProps(text, "focusable", true) |
| 104 | + |
| 105 | + assertThat(collectKeyboardFocusables(root)).containsExactly(text) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun `focusable removed - returns a text to keyboard focus search`() { |
| 110 | + val root = ReactViewGroup(context) |
| 111 | + val text = accessibleText(top = 0, "focusable", false) |
| 112 | + root.addView(text) |
| 113 | + assertThat(collectKeyboardFocusables(root)).isEmpty() |
| 114 | + |
| 115 | + applyProps(text, "focusable", null) |
| 116 | + |
| 117 | + assertThat(collectKeyboardFocusables(root)).containsExactly(text) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `focusable false - recycling a text returns it to keyboard focus search`() { |
| 122 | + val root = ReactViewGroup(context) |
| 123 | + val text = accessibleText(top = 0, "focusable", false) |
| 124 | + root.addView(text) |
| 125 | + assertThat(collectKeyboardFocusables(root)).isEmpty() |
| 126 | + root.removeView(text) |
| 127 | + |
| 128 | + recycle(text) |
| 129 | + applyProps(text, "accessible", true) |
| 130 | + root.addView(text) |
| 131 | + |
| 132 | + assertThat(collectKeyboardFocusables(root)).containsExactly(text) |
| 133 | + } |
| 134 | + |
| 135 | + protected fun accessibleText(top: Int, vararg props: Any?): T = |
| 136 | + createTextView(context).apply { |
| 137 | + layout(0, top, 100, top + 10) |
| 138 | + applyProps(this, "accessible", true, *props) |
| 139 | + } |
| 140 | + |
| 141 | + protected fun applyProps(text: T, vararg keysAndValues: Any?) { |
| 142 | + manager.updateProperties(text, ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues))) |
| 143 | + } |
| 144 | + |
| 145 | + protected fun focusableView(top: Int): View = |
| 146 | + View(context).apply { |
| 147 | + isFocusable = true |
| 148 | + layout(0, top, 100, top + 10) |
| 149 | + } |
| 150 | + |
| 151 | + protected fun collectKeyboardFocusables(root: ViewGroup): List<View> { |
| 152 | + val focusables = ArrayList<View>() |
| 153 | + root.addFocusables(focusables, View.FOCUS_FORWARD, View.FOCUSABLES_ALL) |
| 154 | + return focusables |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +@RunWith(RobolectricTestRunner::class) |
| 159 | +class ReactTextViewKeyboardFocusTest : TextKeyboardFocusTest<ReactTextView>() { |
| 160 | + |
| 161 | + override fun createManager(): ViewManager<ReactTextView, *> = ReactTextViewManager() |
| 162 | + |
| 163 | + override fun createTextView(context: Context): ReactTextView = ReactTextView(context) |
| 164 | + |
| 165 | + override fun recycle(text: ReactTextView) { |
| 166 | + text.recycleView() |
| 167 | + } |
| 168 | + |
| 169 | + @After |
| 170 | + fun tearDown() { |
| 171 | + InstrumentationRegistry.getInstrumentation().setInTouchMode(false) |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + fun `focusable false - a touch still focuses an excluded selectable text`() { |
| 176 | + InstrumentationRegistry.getInstrumentation().setInTouchMode(true) |
| 177 | + val root = ReactViewGroup(context) |
| 178 | + val text = accessibleText(top = 0, "focusable", false, "selectable", true) |
| 179 | + root.addView(text) |
| 180 | + Robolectric.buildActivity(Activity::class.java).setup().get().setContentView(root) |
| 181 | + assertThat(text.isInTouchMode).isTrue() |
| 182 | + |
| 183 | + assertThat(text.requestFocus()).isTrue() |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +@RunWith(RobolectricTestRunner::class) |
| 188 | +internal class PreparedLayoutTextViewKeyboardFocusTest : |
| 189 | + TextKeyboardFocusTest<PreparedLayoutTextView>() { |
| 190 | + |
| 191 | + override fun createManager(): ViewManager<PreparedLayoutTextView, *> = |
| 192 | + PreparedLayoutTextViewManager() |
| 193 | + |
| 194 | + override fun createTextView(context: Context): PreparedLayoutTextView = |
| 195 | + PreparedLayoutTextView(context) |
| 196 | + |
| 197 | + override fun recycle(text: PreparedLayoutTextView) { |
| 198 | + text.recycleView() |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + fun `focusable false - keeps the inline views of an excluded text in keyboard focus search`() { |
| 203 | + val root = ReactViewGroup(context) |
| 204 | + val text = accessibleText(top = 0, "focusable", false) |
| 205 | + val inlineView = focusableView(top = 0) |
| 206 | + text.addView(inlineView) |
| 207 | + root.addView(text) |
| 208 | + |
| 209 | + assertThat(collectKeyboardFocusables(root)).containsExactly(inlineView) |
| 210 | + } |
| 211 | +} |
0 commit comments