|
| 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 | +@file:Suppress("DEPRECATION") |
| 9 | + |
| 10 | +package com.facebook.react.fabric |
| 11 | + |
| 12 | +import android.os.Looper |
| 13 | +import com.facebook.react.ReactRootView |
| 14 | +import com.facebook.react.bridge.JavaOnlyMap |
| 15 | +import com.facebook.react.bridge.ReactTestHelper |
| 16 | +import com.facebook.react.fabric.events.EventEmitterWrapper |
| 17 | +import com.facebook.react.fabric.mounting.MountingManager |
| 18 | +import com.facebook.react.fabric.mounting.MountingManager.MountItemExecutor |
| 19 | +import com.facebook.react.fabric.mounting.SurfaceMountingManager |
| 20 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 21 | +import com.facebook.react.uimanager.ThemedReactContext |
| 22 | +import com.facebook.react.uimanager.ViewManager |
| 23 | +import com.facebook.react.uimanager.ViewManagerRegistry |
| 24 | +import com.facebook.react.uimanager.events.EventCategoryDef |
| 25 | +import com.facebook.react.views.view.ReactViewManager |
| 26 | +import com.facebook.testutils.shadows.ShadowSoLoader |
| 27 | +import org.assertj.core.api.Assertions.assertThat |
| 28 | +import org.junit.Before |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | +import org.mockito.Mockito.inOrder |
| 32 | +import org.mockito.kotlin.mock |
| 33 | +import org.robolectric.RobolectricTestRunner |
| 34 | +import org.robolectric.Shadows.shadowOf |
| 35 | +import org.robolectric.annotation.Config |
| 36 | +import org.robolectric.annotation.LooperMode |
| 37 | + |
| 38 | +/** |
| 39 | + * Regression tests for issue #54636: events emitted before [SurfaceMountingManager.updateEventEmitter] |
| 40 | + * has run must reach JS in receive order, even if a later event happens to find the emitter ready. |
| 41 | + * |
| 42 | + * The fix relies on [SurfaceMountingManager.hasPendingEvents] reflecting that an enqueue lambda |
| 43 | + * has been posted to the UI thread but not yet executed, so callers can route subsequent events |
| 44 | + * through the same queue path instead of dispatching them directly. |
| 45 | + */ |
| 46 | +@RunWith(RobolectricTestRunner::class) |
| 47 | +@LooperMode(LooperMode.Mode.PAUSED) |
| 48 | +@Config(shadows = [ShadowSoLoader::class]) |
| 49 | +class SurfaceMountingManagerEventOrderingTest { |
| 50 | + private lateinit var mountingManager: MountingManager |
| 51 | + private lateinit var themedReactContext: ThemedReactContext |
| 52 | + private val surfaceId = 1 |
| 53 | + private val reactTag = 42 |
| 54 | + |
| 55 | + @Before |
| 56 | + fun setUp() { |
| 57 | + ReactNativeFeatureFlagsForTests.setUp() |
| 58 | + val reactContext = ReactTestHelper.createCatalystContextForTest() |
| 59 | + themedReactContext = ThemedReactContext(reactContext, reactContext, null, -1) |
| 60 | + mountingManager = |
| 61 | + MountingManager( |
| 62 | + ViewManagerRegistry(listOf<ViewManager<*, *>>(ReactViewManager())), |
| 63 | + MountItemExecutor {}, |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + private fun startSurfaceWithView(): SurfaceMountingManager { |
| 68 | + val rootView = ReactRootView(themedReactContext) |
| 69 | + mountingManager.startSurface(surfaceId, themedReactContext, rootView) |
| 70 | + val smm = mountingManager.getSurfaceManagerEnforced(surfaceId, "test") |
| 71 | + smm.preallocateView("RCTView", reactTag, JavaOnlyMap.of(), null, true) |
| 72 | + return smm |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * After enqueuePendingEvent posts its UI-thread lambda, hasPendingEvents must return true so |
| 77 | + * that any concurrent direct-dispatch path knows to also queue rather than overtake. |
| 78 | + */ |
| 79 | + @Test |
| 80 | + fun hasPendingEvents_isTrue_whileEnqueueLambdaIsInFlight() { |
| 81 | + val smm = startSurfaceWithView() |
| 82 | + |
| 83 | + assertThat(smm.hasPendingEvents(reactTag)).isFalse() |
| 84 | + |
| 85 | + smm.enqueuePendingEvent( |
| 86 | + reactTag, "topChange", false, null, EventCategoryDef.UNSPECIFIED, 0L) |
| 87 | + |
| 88 | + assertThat(smm.hasPendingEvents(reactTag)).isTrue() |
| 89 | + |
| 90 | + shadowOf(Looper.getMainLooper()).idle() |
| 91 | + |
| 92 | + assertThat(smm.hasPendingEvents(reactTag)).isFalse() |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * MountingManager.hasPendingEvents must mirror the SurfaceMountingManager state so callers above |
| 97 | + * the surface layer (e.g. FabricUIManager.receiveEvent) can consult it. |
| 98 | + */ |
| 99 | + @Test |
| 100 | + fun mountingManager_hasPendingEvents_mirrorsSurfaceMountingManager() { |
| 101 | + startSurfaceWithView() |
| 102 | + |
| 103 | + assertThat(mountingManager.hasPendingEvents(surfaceId, reactTag)).isFalse() |
| 104 | + |
| 105 | + mountingManager.enqueuePendingEvent( |
| 106 | + surfaceId, reactTag, "topChange", false, null, EventCategoryDef.UNSPECIFIED, 0L) |
| 107 | + |
| 108 | + assertThat(mountingManager.hasPendingEvents(surfaceId, reactTag)).isTrue() |
| 109 | + |
| 110 | + shadowOf(Looper.getMainLooper()).idle() |
| 111 | + |
| 112 | + assertThat(mountingManager.hasPendingEvents(surfaceId, reactTag)).isFalse() |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * The counter must match the number of in-flight enqueue lambdas, so that interleaved enqueue |
| 117 | + * + direct-route-to-enqueue calls are all accounted for and only fall back to false once the UI |
| 118 | + * thread has fully drained them. |
| 119 | + */ |
| 120 | + @Test |
| 121 | + fun hasPendingEvents_remainsTrue_acrossMultipleEnqueues() { |
| 122 | + val smm = startSurfaceWithView() |
| 123 | + |
| 124 | + smm.enqueuePendingEvent( |
| 125 | + reactTag, "topChange", false, null, EventCategoryDef.UNSPECIFIED, 0L) |
| 126 | + smm.enqueuePendingEvent( |
| 127 | + reactTag, "topChange", false, null, EventCategoryDef.UNSPECIFIED, 1L) |
| 128 | + smm.enqueuePendingEvent( |
| 129 | + reactTag, "topChange", false, null, EventCategoryDef.UNSPECIFIED, 2L) |
| 130 | + |
| 131 | + assertThat(smm.hasPendingEvents(reactTag)).isTrue() |
| 132 | + |
| 133 | + shadowOf(Looper.getMainLooper()).idle() |
| 134 | + |
| 135 | + assertThat(smm.hasPendingEvents(reactTag)).isFalse() |
| 136 | + } |
| 137 | + |
| 138 | + /** Tags with no enqueue activity must not report pending events. */ |
| 139 | + @Test |
| 140 | + fun hasPendingEvents_isFalse_forUnrelatedTag() { |
| 141 | + val smm = startSurfaceWithView() |
| 142 | + val otherTag = reactTag + 1 |
| 143 | + smm.preallocateView("RCTView", otherTag, JavaOnlyMap.of(), null, true) |
| 144 | + |
| 145 | + smm.enqueuePendingEvent( |
| 146 | + reactTag, "topChange", false, null, EventCategoryDef.UNSPECIFIED, 0L) |
| 147 | + |
| 148 | + assertThat(smm.hasPendingEvents(reactTag)).isTrue() |
| 149 | + assertThat(smm.hasPendingEvents(otherTag)).isFalse() |
| 150 | + |
| 151 | + shadowOf(Looper.getMainLooper()).idle() |
| 152 | + } |
| 153 | + |
| 154 | + /** Calling enqueuePendingEvent for a tag without view state must not falsely flag it. */ |
| 155 | + @Test |
| 156 | + fun hasPendingEvents_isFalse_forUnknownTag() { |
| 157 | + startSurfaceWithView() |
| 158 | + val unknownTag = reactTag + 99 |
| 159 | + |
| 160 | + mountingManager.enqueuePendingEvent( |
| 161 | + surfaceId, unknownTag, "topChange", false, null, EventCategoryDef.UNSPECIFIED, 0L) |
| 162 | + |
| 163 | + assertThat(mountingManager.hasPendingEvents(surfaceId, unknownTag)).isFalse() |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * End-to-end ordering invariant: when a caller observes [hasPendingEvents] is true and routes a |
| 168 | + * subsequent event through [enqueuePendingEvent] (instead of dispatching directly), the emitter |
| 169 | + * must observe the events in receive order. This is the contract the [FabricUIManager.receiveEvent] |
| 170 | + * fix relies on to prevent #54636. |
| 171 | + */ |
| 172 | + @Test |
| 173 | + fun enqueuePendingEvent_dispatchesInFifoOrder_whenLambdaIsInFlight() { |
| 174 | + val smm = startSurfaceWithView() |
| 175 | + val emitter: EventEmitterWrapper = mock() |
| 176 | + |
| 177 | + smm.updateEventEmitter(reactTag, emitter) |
| 178 | + |
| 179 | + shadowOf(Looper.getMainLooper()).pause() |
| 180 | + |
| 181 | + smm.enqueuePendingEvent(reactTag, "first", false, null, EventCategoryDef.UNSPECIFIED, 1L) |
| 182 | + assertThat(smm.hasPendingEvents(reactTag)).isTrue() |
| 183 | + |
| 184 | + // Caller (e.g. FabricUIManager.receiveEvent) sees a previous enqueue is still in-flight and |
| 185 | + // routes through enqueuePendingEvent rather than dispatching directly via the emitter. |
| 186 | + smm.enqueuePendingEvent(reactTag, "second", false, null, EventCategoryDef.UNSPECIFIED, 2L) |
| 187 | + |
| 188 | + shadowOf(Looper.getMainLooper()).idle() |
| 189 | + |
| 190 | + val ordered = inOrder(emitter) |
| 191 | + ordered.verify(emitter).dispatch("first", null, EventCategoryDef.UNSPECIFIED, 1L) |
| 192 | + ordered.verify(emitter).dispatch("second", null, EventCategoryDef.UNSPECIFIED, 2L) |
| 193 | + |
| 194 | + assertThat(smm.hasPendingEvents(reactTag)).isFalse() |
| 195 | + } |
| 196 | +} |
0 commit comments