Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -6468,6 +6468,7 @@ public class com/facebook/react/views/view/ReactViewGroup : android/view/ViewGro
protected fun onAttachedToWindow ()V
protected fun onConfigurationChanged (Landroid/content/res/Configuration;)V
public fun onHoverEvent (Landroid/view/MotionEvent;)Z
public fun onInterceptHoverEvent (Landroid/view/MotionEvent;)Z
public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z
protected fun onLayout (ZIIII)V
protected fun onMeasure (II)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,14 +779,15 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
}

override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean {
// Ignore generic motion events (joystick, mouse wheel, trackpad) if scrolling is disabled
if (!scrollEnabled) return false

// We do not dispatch the motion event if its children are not supposed to receive it
if (!PointerEvents.canChildrenBeTouchTarget(pointerEvents)) return false

// Handle ACTION_SCROLL events (mouse wheel, trackpad, joystick)
if (ev.actionMasked == MotionEvent.ACTION_SCROLL) {
// Ignore them if scrolling is disabled. Hover events still reach the content, which
// TalkBack's touch exploration relies on.
if (!scrollEnabled) return false

val hScroll = ev.getAxisValue(MotionEvent.AXIS_HSCROLL)
if (hScroll != 0f) {
// Perform the scroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,11 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
}

override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean {
if (!scrollEnabled) return false
if (!PointerEvents.canChildrenBeTouchTarget(pointerEvents)) return false

if (ev.actionMasked == MotionEvent.ACTION_SCROLL) {
// Hover events still reach the content, which TalkBack's touch exploration relies on.
if (!scrollEnabled) return false
val vScroll = ev.getAxisValue(MotionEvent.AXIS_VSCROLL)
if (vScroll != 0f) {
enableFpsListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,11 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
}

override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean {
if (!scrollEnabled) return false
if (!PointerEvents.canChildrenBeTouchTarget(pointerEvents)) return false

if (ev.actionMasked == MotionEvent.ACTION_SCROLL) {
// Hover events still reach the content, which TalkBack's touch exploration relies on.
if (!scrollEnabled) return false
val vScroll = ev.getAxisValue(MotionEvent.AXIS_VSCROLL)
if (vScroll != 0f) {
enableFpsListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,21 @@ public open class ReactViewGroup public constructor(context: Context?) :
return super.onHoverEvent(event)
}

override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean {
// We do not dispatch the motion event if its children are not supposed to receive it
override fun onInterceptHoverEvent(event: MotionEvent): Boolean {
// We keep the hover event for this view if the children are not supposed to receive it.
if (!canChildrenBeTouchTarget(pointerEvents)) {
return true
}
return super.onInterceptHoverEvent(event)
}

override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean {
// We do not dispatch the motion event if its children are not supposed to receive it. A hover
// event still reaches a view that is a touch target, and onInterceptHoverEvent keeps it there.
if (
!canChildrenBeTouchTarget(pointerEvents) &&
!(isHoverEvent(ev) && canBeTouchTarget(pointerEvents))
) {
return false
}

Expand Down Expand Up @@ -1155,5 +1167,13 @@ public open class ReactViewGroup public constructor(context: Context?) :
private fun setViewClipped(view: View, clipped: Boolean) {
view.setTag(R.id.view_clipped, clipped)
}

private fun isHoverEvent(event: MotionEvent): Boolean =
when (event.actionMasked) {
MotionEvent.ACTION_HOVER_ENTER,
MotionEvent.ACTION_HOVER_MOVE,
MotionEvent.ACTION_HOVER_EXIT -> true
else -> false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* 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.
*/

// The deprecated APIs exercised here remain public and required by this Robolectric setup.
@file:Suppress("DEPRECATION")

package com.facebook.react.views.scroll

import android.annotation.SuppressLint
import android.util.DisplayMetrics
import android.view.InputDevice
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.soloader.SoLoader
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.mock
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

/** Verifies that a ScrollView with scrolling disabled ignores the mouse wheel but not hover. */
@SuppressLint("DeprecatedClass", "DeprecatedMethod")
@RunWith(RobolectricTestRunner::class)
@Config(shadows = [ShadowSoLoader::class])
class ReactScrollViewDisabledHoverTest {

private lateinit var themedContext: ThemedReactContext
private lateinit var uiManagerHelperMock: MockedStatic<UIManagerHelper>

@Before
fun setup() {
ReactNativeFeatureFlagsForTests.setUp()
SoLoader.setInTestMode()
val context = createTestReactApplicationContext()
themedContext = ThemedReactContext(context, context, null, -1)
DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics())
uiManagerHelperMock = mockStatic(UIManagerHelper::class.java)
uiManagerHelperMock
.`when`<EventDispatcher?> { UIManagerHelper.getEventDispatcher(any()) }
.thenReturn(mock())
}

@After
fun teardown() {
uiManagerHelperMock.close()
DisplayMetricsHolder.setScreenDisplayMetrics(null)
}

@Test
fun testScrollViewWithScrollDisabledDispatchesHoverToContent() {
val view = ReactScrollViewManager().createViewInstance(themedContext)
view.scrollEnabled = false

assertThat(hoverReachesContent(view)).isTrue()
}

@Test
fun testHorizontalScrollViewWithScrollDisabledDispatchesHoverToContent() {
val view = ReactHorizontalScrollViewManager().createViewInstance(themedContext)
view.scrollEnabled = false

assertThat(hoverReachesContent(view)).isTrue()
}

@Test
fun testNestedScrollViewWithScrollDisabledDispatchesHoverToContent() {
val view = ReactNestedScrollView(themedContext)
view.scrollEnabled = false

assertThat(hoverReachesContent(view)).isTrue()
}

@Test
fun testScrollViewWithScrollDisabledIgnoresWheel() {
val view = ReactScrollViewManager().createViewInstance(themedContext)
view.scrollEnabled = false
addContent(view)

val handled = dispatchAndRecycle(view, obtainWheelScroll(MotionEvent.AXIS_VSCROLL, -1f))

assertThat(handled).isFalse()
assertThat(view.scrollY).isZero()
}

@Test
fun testHorizontalScrollViewWithScrollDisabledIgnoresWheel() {
val view = ReactHorizontalScrollViewManager().createViewInstance(themedContext)
view.scrollEnabled = false
addContent(view)

val handled = dispatchAndRecycle(view, obtainWheelScroll(MotionEvent.AXIS_HSCROLL, 1f))

assertThat(handled).isFalse()
assertThat(view.scrollX).isZero()
}

@Test
fun testNestedScrollViewWithScrollDisabledIgnoresWheel() {
val view = ReactNestedScrollView(themedContext)
view.scrollEnabled = false
addContent(view)

val handled = dispatchAndRecycle(view, obtainWheelScroll(MotionEvent.AXIS_VSCROLL, -1f))

assertThat(handled).isFalse()
assertThat(view.scrollY).isZero()
}

private fun hoverReachesContent(view: ViewGroup): Boolean {
var hovered = false
addContent(view).setOnHoverListener { _, _ ->
hovered = true
true
}
dispatchAndRecycle(view, obtainStylusHover(MotionEvent.ACTION_HOVER_ENTER))
dispatchAndRecycle(view, obtainStylusHover(MotionEvent.ACTION_HOVER_MOVE))
return hovered
}

private fun addContent(view: ViewGroup): View {
val content = View(themedContext)
view.addView(content)
content.layout(0, 0, CONTENT_SIZE, CONTENT_SIZE)
view.layout(0, 0, VIEWPORT_SIZE, VIEWPORT_SIZE)
return content
}

private fun dispatchAndRecycle(view: View, event: MotionEvent): Boolean =
try {
view.dispatchGenericMotionEvent(event)
} finally {
event.recycle()
}

private fun obtainStylusHover(action: Int): MotionEvent {
val event = MotionEvent.obtain(0L, 0L, action, VIEWPORT_SIZE / 2f, VIEWPORT_SIZE / 2f, 0)
event.source = InputDevice.SOURCE_STYLUS
return event
}

private fun obtainWheelScroll(axis: Int, value: Float): MotionEvent {
val properties =
MotionEvent.PointerProperties().apply { toolType = MotionEvent.TOOL_TYPE_MOUSE }
val coords = MotionEvent.PointerCoords().apply { setAxisValue(axis, value) }
return MotionEvent.obtain(
0L,
0L,
MotionEvent.ACTION_SCROLL,
1,
arrayOf(properties),
arrayOf(coords),
0,
0,
1f,
1f,
0,
0,
InputDevice.SOURCE_MOUSE,
0,
)
}

private companion object {
const val VIEWPORT_SIZE = 100
const val CONTENT_SIZE = 1000
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import android.content.Context
import android.graphics.BlendMode
import android.graphics.Canvas
import android.graphics.Paint
import android.view.InputDevice
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.facebook.react.R
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
import com.facebook.react.uimanager.HasElevatedDescendantCache
import com.facebook.react.uimanager.PointerEvents
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -266,6 +269,79 @@ class ReactViewGroupTest {
assertThat(outer.hasOverlappingRendering()).isTrue()
}

@Test
fun `pointer events - a box-only view receives hover and keeps it from its children`() {
val (rvg, child) = hoverTarget(PointerEvents.BOX_ONLY)
var viewHovered = false
var childHovered = false
rvg.setOnHoverListener { _, _ ->
viewHovered = true
true
}
child.setOnHoverListener { _, _ ->
childHovered = true
true
}

dispatchHover(rvg, MotionEvent.ACTION_HOVER_ENTER)

assertThat(viewHovered).isTrue()
assertThat(childHovered).isFalse()
}

@Test
fun `pointer events - a none view and its children receive no hover`() {
val (rvg, child) = hoverTarget(PointerEvents.NONE)
var hovered = false
rvg.setOnHoverListener { _, _ ->
hovered = true
true
}
child.setOnHoverListener { _, _ ->
hovered = true
true
}

assertThat(dispatchHover(rvg, MotionEvent.ACTION_HOVER_ENTER)).isFalse()
assertThat(hovered).isFalse()
}

@Test
fun `pointer events - a box-only view keeps other generic motion events from its children`() {
val (rvg, child) = hoverTarget(PointerEvents.BOX_ONLY)
var childReceived = false
child.setOnGenericMotionListener { _, _ ->
childReceived = true
true
}
val event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_SCROLL, 50f, 50f, 0)
event.source = InputDevice.SOURCE_MOUSE

assertThat(rvg.dispatchGenericMotionEvent(event)).isFalse()
assertThat(childReceived).isFalse()
event.recycle()
}

private fun hoverTarget(pointerEvents: PointerEvents): Pair<ReactViewGroup, View> {
val rvg = ReactViewGroup(context)
rvg.pointerEvents = pointerEvents
val child = View(context)
rvg.addView(child)
child.layout(0, 0, 100, 100)
rvg.layout(0, 0, 100, 100)
return rvg to child
}

private fun dispatchHover(view: View, action: Int): Boolean {
val event = MotionEvent.obtain(0L, 0L, action, 50f, 50f, 0)
event.source = InputDevice.SOURCE_STYLUS
return try {
view.dispatchGenericMotionEvent(event)
} finally {
event.recycle()
}
}

// Mirrors the ViewManager mount/unmount hooks: attach/detach the child, then refresh the cache.
private fun mount(parent: ReactViewGroup, child: View) {
parent.addView(child)
Expand Down
Loading