|
| 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.view.View |
| 13 | +import com.facebook.react.ReactRootView |
| 14 | +import com.facebook.react.bridge.JavaOnlyArray |
| 15 | +import com.facebook.react.bridge.JavaOnlyMap |
| 16 | +import com.facebook.react.bridge.ReactTestHelper |
| 17 | +import com.facebook.react.fabric.mounting.MountItemDispatcher |
| 18 | +import com.facebook.react.fabric.mounting.MountingManager |
| 19 | +import com.facebook.react.fabric.mounting.SurfaceMountingManager |
| 20 | +import com.facebook.react.fabric.mounting.mountitems.MountItem |
| 21 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 22 | +import com.facebook.react.uimanager.ThemedReactContext |
| 23 | +import com.facebook.react.uimanager.ViewManager |
| 24 | +import com.facebook.react.uimanager.ViewManagerRegistry |
| 25 | +import com.facebook.react.views.view.ReactViewManager |
| 26 | +import com.facebook.testutils.fakes.FakeBatchEventDispatchedListener |
| 27 | +import com.facebook.testutils.shadows.ShadowNativeLoader |
| 28 | +import com.facebook.testutils.shadows.ShadowNativeMap |
| 29 | +import com.facebook.testutils.shadows.ShadowPerformanceTracer |
| 30 | +import com.facebook.testutils.shadows.ShadowReadableNativeArray |
| 31 | +import com.facebook.testutils.shadows.ShadowReadableNativeMap |
| 32 | +import com.facebook.testutils.shadows.ShadowSoLoader |
| 33 | +import com.facebook.testutils.shadows.ShadowWritableNativeArray |
| 34 | +import com.facebook.testutils.shadows.ShadowWritableNativeMap |
| 35 | +import org.assertj.core.api.Assertions.assertThat |
| 36 | +import org.junit.Before |
| 37 | +import org.junit.Test |
| 38 | +import org.junit.runner.RunWith |
| 39 | +import org.robolectric.RobolectricTestRunner |
| 40 | +import org.robolectric.annotation.Config |
| 41 | +import org.robolectric.util.ReflectionHelpers |
| 42 | + |
| 43 | +/** |
| 44 | + * Synchronous props updates for a view that doesn't exist yet wait for the next batch of mount |
| 45 | + * items. The view can mount and get a newer update applied directly before that batch runs. |
| 46 | + */ |
| 47 | +@RunWith(RobolectricTestRunner::class) |
| 48 | +@Config( |
| 49 | + shadows = |
| 50 | + [ |
| 51 | + ShadowSoLoader::class, |
| 52 | + ShadowNativeLoader::class, |
| 53 | + ShadowNativeMap::class, |
| 54 | + ShadowWritableNativeMap::class, |
| 55 | + ShadowReadableNativeMap::class, |
| 56 | + ShadowWritableNativeArray::class, |
| 57 | + ShadowReadableNativeArray::class, |
| 58 | + ShadowPerformanceTracer::class, |
| 59 | + ], |
| 60 | +) |
| 61 | +class FabricUIManagerSynchronousPropsTest { |
| 62 | + private val surfaceId = 1 |
| 63 | + private val tag = 42 |
| 64 | + private lateinit var themedReactContext: ThemedReactContext |
| 65 | + private lateinit var underTest: FabricUIManager |
| 66 | + |
| 67 | + private val mountingManager: MountingManager |
| 68 | + get() = ReflectionHelpers.getField(underTest, "mMountingManager") |
| 69 | + |
| 70 | + private val dispatcher: MountItemDispatcher |
| 71 | + get() = ReflectionHelpers.getField(underTest, "mMountItemDispatcher") |
| 72 | + |
| 73 | + @Before |
| 74 | + fun setUp() { |
| 75 | + ReactNativeFeatureFlagsForTests.setUp() |
| 76 | + val reactContext = ReactTestHelper.createTestReactApplicationContext() |
| 77 | + themedReactContext = ThemedReactContext(reactContext, reactContext, null, -1) |
| 78 | + underTest = |
| 79 | + FabricUIManager( |
| 80 | + reactContext, |
| 81 | + ViewManagerRegistry(listOf<ViewManager<*, *>>(ReactViewManager())), |
| 82 | + FakeBatchEventDispatchedListener(), |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + private fun mountView(): SurfaceMountingManager { |
| 87 | + mountingManager.startSurface(surfaceId, themedReactContext, ReactRootView(themedReactContext)) |
| 88 | + val smm = mountingManager.getSurfaceManagerEnforced(surfaceId, "test") |
| 89 | + smm.preallocateView("RCTView", tag, JavaOnlyMap.of(), null, true) |
| 90 | + smm.addViewAt(surfaceId, tag, 0) |
| 91 | + return smm |
| 92 | + } |
| 93 | + |
| 94 | + private fun updateViewBatch(opacity: Double) { |
| 95 | + val method = |
| 96 | + FabricUIManager::class |
| 97 | + .java |
| 98 | + .getDeclaredMethod( |
| 99 | + "synchronouslyUpdateViewBatch", |
| 100 | + IntArray::class.java, |
| 101 | + DoubleArray::class.java, |
| 102 | + Array<Any>::class.java, |
| 103 | + ) |
| 104 | + method.isAccessible = true |
| 105 | + method.invoke( |
| 106 | + underTest, |
| 107 | + intArrayOf(CMD_START_OF_VIEW, tag, CMD_OPACITY, CMD_END_OF_VIEW), |
| 108 | + doubleArrayOf(opacity), |
| 109 | + arrayOf<Any?>(), |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun queuedUpdate_doesNotOverwriteNewerDirectUpdate() { |
| 115 | + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.3)) |
| 116 | + val smm = mountView() |
| 117 | + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.6)) |
| 118 | + |
| 119 | + dispatcher.tryDispatchMountItems() |
| 120 | + |
| 121 | + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) |
| 122 | + // The synchronous mount props override keeps the newer value over a Fabric commit. |
| 123 | + smm.updateProps(tag, JavaOnlyMap.of("opacity", 1.0)) |
| 124 | + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun queuedBatchedUpdate_doesNotOverwriteNewerDirectUpdate() { |
| 129 | + updateViewBatch(opacity = 0.3) |
| 130 | + val smm = mountView() |
| 131 | + updateViewBatch(opacity = 0.6) |
| 132 | + |
| 133 | + dispatcher.tryDispatchMountItems() |
| 134 | + |
| 135 | + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) |
| 136 | + smm.updateProps(tag, JavaOnlyMap.of("opacity", 1.0)) |
| 137 | + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + fun queuedUpdates_waitForTheMountItemThatCreatesTheView() { |
| 142 | + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.3)) |
| 143 | + lateinit var smm: SurfaceMountingManager |
| 144 | + dispatcher.addMountItem( |
| 145 | + object : MountItem { |
| 146 | + override fun execute(mountingManager: MountingManager) { |
| 147 | + smm = mountView() |
| 148 | + } |
| 149 | + |
| 150 | + override fun getSurfaceId(): Int = View.NO_ID |
| 151 | + } |
| 152 | + ) |
| 153 | + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", 0.6)) |
| 154 | + |
| 155 | + dispatcher.tryDispatchMountItems() |
| 156 | + |
| 157 | + assertThat(smm.getView(tag).alpha).isEqualTo(0.6f) |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + fun queuedUpdates_mergeInOrderAndKeepNullResets() { |
| 162 | + underTest.synchronouslyUpdateViewOnUIThread( |
| 163 | + tag, |
| 164 | + JavaOnlyMap.of("opacity", 0.3, "transform", JavaOnlyArray.of(JavaOnlyMap.of("scale", 2.0))), |
| 165 | + ) |
| 166 | + underTest.synchronouslyUpdateViewOnUIThread(tag, JavaOnlyMap.of("opacity", null)) |
| 167 | + val smm = mountView() |
| 168 | + |
| 169 | + dispatcher.tryDispatchMountItems() |
| 170 | + |
| 171 | + val view = smm.getView(tag) |
| 172 | + assertThat(view.alpha).isEqualTo(1f) |
| 173 | + assertThat(view.scaleX).isEqualTo(2f) |
| 174 | + // The reset also cleared the stored opacity, so a Fabric commit applies its own value. |
| 175 | + smm.updateProps(tag, JavaOnlyMap.of("opacity", 0.5)) |
| 176 | + assertThat(view.alpha).isEqualTo(0.5f) |
| 177 | + } |
| 178 | + |
| 179 | + private companion object { |
| 180 | + // Keep in sync with BatchedAnimatedPropsMountItem. |
| 181 | + const val CMD_START_OF_VIEW = 1 |
| 182 | + const val CMD_END_OF_VIEW = 4 |
| 183 | + const val CMD_OPACITY = 10 |
| 184 | + } |
| 185 | +} |
0 commit comments