mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
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
This commit is contained in:
1 parent
9da1a01153
commit
6aac2b7422
3 files changed
+158
No files matched your search
+108
@@ -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)
|
||||
}
|
||||
}
|
||||
+26
@@ -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<Int> = mutableListOf()
|
||||
|
||||
@Implementation
|
||||
fun pullAndExecuteTransaction(surfaceId: Int) {
|
||||
pulledSurfaceIds.add(surfaceId)
|
||||
}
|
||||
}
|
||||
+24
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user