|
| 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.safeareaview |
| 9 | + |
| 10 | +import android.widget.FrameLayout |
| 11 | +import com.facebook.react.bridge.JavaOnlyMap |
| 12 | +import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext |
| 13 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 14 | +import com.facebook.react.uimanager.PointerEvents |
| 15 | +import com.facebook.react.uimanager.ReactStylesDiffMap |
| 16 | +import com.facebook.react.uimanager.ThemedReactContext |
| 17 | +import com.facebook.react.uimanager.TouchTargetHelper |
| 18 | +import com.facebook.react.views.view.ReactViewGroup |
| 19 | +import com.facebook.testutils.shadows.ShadowSoLoader |
| 20 | +import org.assertj.core.api.Assertions.assertThat |
| 21 | +import org.junit.Before |
| 22 | +import org.junit.Test |
| 23 | +import org.junit.runner.RunWith |
| 24 | +import org.robolectric.RobolectricTestRunner |
| 25 | +import org.robolectric.RuntimeEnvironment |
| 26 | +import org.robolectric.annotation.Config |
| 27 | + |
| 28 | +@RunWith(RobolectricTestRunner::class) |
| 29 | +@Config(shadows = [ShadowSoLoader::class]) |
| 30 | +class ReactSafeAreaViewManagerTest { |
| 31 | + |
| 32 | + private lateinit var themedContext: ThemedReactContext |
| 33 | + private lateinit var root: FrameLayout |
| 34 | + private lateinit var safeArea: ReactSafeAreaView |
| 35 | + private val manager = ReactSafeAreaViewManager() |
| 36 | + |
| 37 | + @Before |
| 38 | + fun setUp() { |
| 39 | + ReactNativeFeatureFlagsForTests.setUp() |
| 40 | + val context = createTestReactApplicationContext(RuntimeEnvironment.getApplication()) |
| 41 | + themedContext = ThemedReactContext(context, context, null, -1) |
| 42 | + |
| 43 | + root = FrameLayout(themedContext) |
| 44 | + root.id = ROOT_TAG |
| 45 | + root.measure(500, 500) |
| 46 | + root.layout(0, 0, 500, 500) |
| 47 | + |
| 48 | + val underlying = ReactViewGroup(themedContext) |
| 49 | + underlying.id = UNDERLYING_TAG |
| 50 | + root.addView(underlying) |
| 51 | + underlying.layout(0, 0, 500, 500) |
| 52 | + |
| 53 | + safeArea = ReactSafeAreaView(themedContext) |
| 54 | + safeArea.id = SAFE_AREA_TAG |
| 55 | + root.addView(safeArea) |
| 56 | + safeArea.layout(0, 300, 500, 500) |
| 57 | + |
| 58 | + val toast = ReactViewGroup(themedContext) |
| 59 | + toast.id = TOAST_TAG |
| 60 | + safeArea.addView(toast) |
| 61 | + toast.layout(100, 0, 400, 100) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun boxNone_touchOutsideChildrenReachesViewUnderneath() { |
| 66 | + setPointerEvents("box-none") |
| 67 | + |
| 68 | + assertThat(TouchTargetHelper.findTargetTagForTouch(20f, 380f, root)).isEqualTo(UNDERLYING_TAG) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun boxNone_touchOnChildStillReachesChild() { |
| 73 | + setPointerEvents("box-none") |
| 74 | + |
| 75 | + assertThat(TouchTargetHelper.findTargetTagForTouch(200f, 350f, root)).isEqualTo(TOAST_TAG) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun withoutPointerEvents_safeAreaViewReceivesTouchOutsideChildren() { |
| 80 | + assertThat(TouchTargetHelper.findTargetTagForTouch(20f, 380f, root)).isEqualTo(SAFE_AREA_TAG) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun resettingPointerEvents_restoresDefaultHitTesting() { |
| 85 | + setPointerEvents("box-none") |
| 86 | + setPointerEvents(null) |
| 87 | + |
| 88 | + assertThat(TouchTargetHelper.findTargetTagForTouch(20f, 380f, root)).isEqualTo(SAFE_AREA_TAG) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun invalidPointerEvents_restoresDefaultHitTesting() { |
| 93 | + setPointerEvents("box-none") |
| 94 | + assertThat(TouchTargetHelper.findTargetTagForTouch(20f, 380f, root)).isEqualTo(UNDERLYING_TAG) |
| 95 | + |
| 96 | + setPointerEvents("BoX-InVaLiD") |
| 97 | + |
| 98 | + assertThat(safeArea.pointerEvents).isEqualTo(PointerEvents.AUTO) |
| 99 | + assertThat(TouchTargetHelper.findTargetTagForTouch(20f, 380f, root)).isEqualTo(SAFE_AREA_TAG) |
| 100 | + assertThat(TouchTargetHelper.findTargetTagForTouch(200f, 350f, root)).isEqualTo(TOAST_TAG) |
| 101 | + } |
| 102 | + |
| 103 | + private fun setPointerEvents(value: String?) { |
| 104 | + manager.updateProperties(safeArea, ReactStylesDiffMap(JavaOnlyMap.of("pointerEvents", value))) |
| 105 | + } |
| 106 | + |
| 107 | + private companion object { |
| 108 | + const val ROOT_TAG = 1 |
| 109 | + const val UNDERLYING_TAG = 2 |
| 110 | + const val SAFE_AREA_TAG = 3 |
| 111 | + const val TOAST_TAG = 4 |
| 112 | + } |
| 113 | +} |
0 commit comments