From 6aac2b7422a305dda1791920989eada3f48ea685 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Wed, 29 Jul 2026 08:28:17 -0700 Subject: [PATCH] Add JVM tests for the Android pull model plumbing (#57583) Summary: Pull Request resolved: https://github.com/react/react-native/pull/57583 Robolectric tests for the notification path: a notification pulls synchronously on the UI thread; one pull item is enqueued per notification and dispatched in order (redundant pulls no-op in C++); no binding is a clean no-op. Adds `ShadowFabricUIManagerBinding` and `ShadowPerformanceTracer` shadows. ## Changelog: [Internal] - Add JVM tests for the Android pull-model plumbing Reviewed By: cortinico Differential Revision: D112309051 fbshipit-source-id: 7cbf14bbe484cf0034bd659cf275a860491b8a25 --- .../fabric/FabricUIManagerPullModelTest.kt | 108 ++++++++++++++++++ .../shadows/ShadowFabricUIManagerBinding.kt | 26 +++++ .../shadows/ShadowPerformanceTracer.kt | 24 ++++ 3 files changed, 158 insertions(+) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/FabricUIManagerPullModelTest.kt create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowFabricUIManagerBinding.kt create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowPerformanceTracer.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/FabricUIManagerPullModelTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/FabricUIManagerPullModelTest.kt new file mode 100644 index 00000000000..3a2a0aed3ac --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/FabricUIManagerPullModelTest.kt @@ -0,0 +1,108 @@ +/* + * 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. + */ + +@file:Suppress("DEPRECATION") + +package com.facebook.react.fabric + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactTestHelper +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import com.facebook.react.uimanager.ViewManagerRegistry +import com.facebook.testutils.fakes.FakeBatchEventDispatchedListener +import com.facebook.testutils.shadows.ShadowFabricUIManagerBinding +import com.facebook.testutils.shadows.ShadowNativeLoader +import com.facebook.testutils.shadows.ShadowPerformanceTracer +import com.facebook.testutils.shadows.ShadowSoLoader +import org.assertj.core.api.Assertions.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.shadow.api.Shadow + +/** + * Tests for the pull-model notification path: [FabricUIManager.onTransactionAvailable] enqueues a + * PullTransactionMountItem that asks the (shadowed, JNI-backed) [FabricUIManagerBinding] to pull + * and apply the surface's pending transaction. + * + * The Robolectric test thread is the UI thread, so a notification posted from it dispatches the + * queued mount items synchronously; notifications posted from a background thread stay queued until + * the next UI-thread dispatch (in production, the dispatcher's frame callback). + */ +@RunWith(RobolectricTestRunner::class) +@Config( + shadows = + [ + ShadowSoLoader::class, + ShadowNativeLoader::class, + ShadowPerformanceTracer::class, + ShadowFabricUIManagerBinding::class, + ] +) +class FabricUIManagerPullModelTest { + + private lateinit var reactContext: ReactApplicationContext + private lateinit var underTest: FabricUIManager + private lateinit var binding: FabricUIManagerBinding + private lateinit var shadowBinding: ShadowFabricUIManagerBinding + + @Before + fun setup() { + ReactNativeFeatureFlagsForTests.setUp() + reactContext = ReactTestHelper.createCatalystContextForTest() + underTest = + FabricUIManager( + reactContext, + ViewManagerRegistry(emptyList()), + FakeBatchEventDispatchedListener(), + ) + binding = FabricUIManagerBinding() + shadowBinding = Shadow.extract(binding) + underTest.setBinding(binding) + } + + private fun runOnBackgroundThread(block: () -> Unit) { + var error: Throwable? = null + val thread = Thread { + try { + block() + } catch (t: Throwable) { + error = t + } + } + thread.start() + thread.join() + error?.let { throw it } + } + + @Test + fun onTransactionAvailable_onUiThread_pullsSynchronously() { + underTest.onTransactionAvailable(1) + + assertThat(shadowBinding.pulledSurfaceIds).containsExactly(1) + } + + @Test + fun onTransactionAvailable_enqueuesOnePullPerNotification() { + // Mirrors iOS: one transaction block per commit, in notification order. Redundant pulls + // (revisions already consumed by an earlier accumulating pull) no-op in C++, not here. + runOnBackgroundThread { + underTest.onTransactionAvailable(1) + underTest.onTransactionAvailable(2) + underTest.onTransactionAvailable(1) + } + // Notifications from a background (commit) thread are queued, not dispatched. + assertThat(shadowBinding.pulledSurfaceIds).isEmpty() + + // The next UI-thread dispatch drains them in order. + underTest.onTransactionAvailable(3) + + assertThat(shadowBinding.pulledSurfaceIds).containsExactly(1, 2, 1, 3) + } +} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowFabricUIManagerBinding.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowFabricUIManagerBinding.kt new file mode 100644 index 00000000000..a9fbbf95e9f --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowFabricUIManagerBinding.kt @@ -0,0 +1,26 @@ +/* + * 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. + */ + +package com.facebook.testutils.shadows + +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements + +/** + * [FabricUIManagerBinding] is JNI-backed and Mockito can't mock native methods, so shadow the + * methods tests need instead. The binding class is `internal`, so it is referenced by name via + * [Implements.className] rather than a compile-time class literal. + */ +@Implements(className = "com.facebook.react.fabric.FabricUIManagerBinding") +class ShadowFabricUIManagerBinding { + val pulledSurfaceIds: MutableList = mutableListOf() + + @Implementation + fun pullAndExecuteTransaction(surfaceId: Int) { + pulledSurfaceIds.add(surfaceId) + } +} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowPerformanceTracer.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowPerformanceTracer.kt new file mode 100644 index 00000000000..5f180c75a4f --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowPerformanceTracer.kt @@ -0,0 +1,24 @@ +/* + * 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. + */ + +package com.facebook.testutils.shadows + +import com.facebook.react.internal.tracing.PerformanceTracer +import kotlin.jvm.JvmStatic +import org.robolectric.annotation.Implementation +import org.robolectric.annotation.Implements + +/** + * [PerformanceTracer] is JNI-backed (react_tracingjni); shadow it so code paths that check + * [PerformanceTracer.isTracing] can run on the JVM. Tracing reports as disabled. + */ +@Implements(PerformanceTracer::class) +class ShadowPerformanceTracer { + companion object { + @JvmStatic @Implementation fun isTracing(): Boolean = false + } +}