mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Pull model: mount the commits of starting and stopping surfaces (#59030)
Summary: With `enableMountingCoordinatorPullModelAndroid`, a commit tells Java only its surface id. At the next mount dispatch, `FabricUIManagerBinding::pullAndExecuteTransaction` looks the surface's `MountingCoordinator` up in the binding's surface registry. A surface is not in the registry while it starts or stops: - The transaction that stops a surface is never pulled, because `stopSurfaceWithSurfaceHandler` removes the surface from the registry before `SurfaceHandler::stop()` commits the empty tree. Mounting override delegates such as LayoutAnimations and C++ Animated never see those Deletes. With `enableLayoutAnimationsOnAndroid`, a layout animation that runs when its surface stops stays in flight, and `FabricUIManager` calls `driveCxxAnimations` on every frame until another surface pulls a transaction. - A commit that a bridgeless surface makes before `startSurfaceWithSurfaceHandler` registers it is not mounted until the surface commits again. - The legacy `stopSurface(int)` holds the registry lock across `stop()`. On the UI thread, the stop commit pulls inline and blocks on the same lock, so the app freezes. iOS does not look the surface up. `RCTMountingManager` captures the `MountingCoordinator` that reported the commit in the block it dispatches to the main queue, and pulls from it. This PR does the same. `schedulerShouldRenderTransactions` keeps the reporting `MountingCoordinator` until the UI thread pulls, and `pullAndExecuteTransaction` pulls from it. Java still receives only the surface id. The surface registry, the start and stop functions, `reportMount` and the push model are unchanged. As on iOS, a surface stopped on the UI thread has its stop transaction pulled during the stop commit. When it stops on another thread, its shadow tree is destroyed and its coordinator revoked before the UI thread pulls, so that transaction is not mounted. A pull of a stopped surface can log "Executing commit after surface N was stopped!" when a mounting override delegate still returns a transaction; iOS pulls these the same way. Only one coordinator per surface id is kept. Stopping and starting the same `ReactSurface` on the same host reuses its id. If the stopped surface's commit has not been pulled yet, the restarted surface's coordinator replaces it and a warning is logged. The replaced coordinator is revoked and would pull nothing. Java does not mount such a restarted surface anyway: `MountingManager.startSurface` logs "Called startSurface more than once" and keeps the stopped surface's manager. ## Changelog: [ANDROID] [FIXED] - Pull model: mount the commits of starting and stopping surfaces, and fix a freeze when the legacy `stopSurface(int)` runs on the UI thread Pull Request resolved: https://github.com/react/react-native/pull/59030 Test Plan: New `FabricMountingManagerInstrumentationTest` cases drive a real `FabricUIManagerBinding`. The test helper runs no JavaScript and commits views to the surface's shadow tree itself. The pull-model cases run with the Props 2.0 flags and `enablePreallocatedPropsDiffOnInsertAndroid`. | Test | Without the fix | With the fix | | --- | --- | --- | | `native_stopSurfaceWithSurfaceHandler_pullModel_pullsDeletes` | fails: no Deletes are pulled | passes | | `native_stopSurface_pullModel_pullsDeletes` | hangs | passes | | `native_stopSurfaceWithSurfaceHandler_pushModel_pullsDeletes` | passes | passes | The push case is a control. The hang stops the run, so the bridgeless stop test was also run without the legacy one. With the fix, the whole class passes. A one-off check, not committed, covered the start: a bridgeless surface started on a background thread commits a view, and the start is held before `startSurfaceWithSurfaceHandler` registers the surface. On https://github.com/react/react-native/issues/59029 alone the view is not mounted while the start is held. With this PR it is mounted by the next frame. The test class has no Gradle target in the open-source repository, so I ran it on an Android emulator through a local androidTest source set: ``` ./gradlew :packages:react-native:ReactAndroid:assembleReleaseAndroidTest -PreactNativeArchitectures=arm64-v8a adb install -r -t packages/react-native/ReactAndroid/build/outputs/apk/androidTest/release/ReactAndroid-release-androidTest.apk adb shell am instrument -w -e class com.facebook.react.fabric.FabricMountingManagerInstrumentationTest \ com.facebook.react.tests.gradle/androidx.test.runner.AndroidJUnitRunner ``` I also checked the effect in RNTester with a temporary hook, not part of this PR. The Playground starts a 60 s `LayoutAnimation`, then `ReactDelegate.unloadApp()` stops the surface on the UI thread while the activity stays resumed. Props 2.0 and `enableLayoutAnimationsOnAndroid` were on: | Run | After the stop | | --- | --- | | Pull, without this PR | no `onAllAnimationsComplete`; `driveCxxAnimations` keeps running on every frame | | Pull, with this PR | `onAllAnimationsComplete` right after the stop, then no more driven frames | | Push, with this PR | `onAllAnimationsComplete` right after the stop, as before | Pulled By: bartlomiejbloniarz bartlomiejbloniarz Reviewed By: javache Differential Revision: D124966432 fbshipit-source-id: 0713c8dc007e731716aa82b068de5600bd68c33e
This commit is contained in:
1 parent
879df0514e
commit
1423ef001d
6 files changed
+352
-28
No files matched your search
+17
-23
@@ -125,29 +125,12 @@ void FabricUIManagerBinding::pullAndExecuteTransaction(SurfaceId surfaceId) {
|
||||
|
||||
std::shared_ptr<const MountingCoordinator> mountingCoordinator;
|
||||
{
|
||||
std::shared_lock lock(surfaceHandlerRegistryMutex_);
|
||||
auto iterator = surfaceHandlerRegistry_.find(surfaceId);
|
||||
if (iterator == surfaceHandlerRegistry_.end()) {
|
||||
std::scoped_lock lock(pullableMountingCoordinatorsMutex_);
|
||||
auto pullable = pullableMountingCoordinators_.extract(surfaceId);
|
||||
if (pullable.empty()) {
|
||||
return;
|
||||
}
|
||||
const auto* surfaceHandler = std::get_if<SurfaceHandler>(&iterator->second);
|
||||
jni::local_ref<SurfaceHandlerBinding::jhybridobject> javaSurfaceHandler;
|
||||
if (surfaceHandler == nullptr) {
|
||||
javaSurfaceHandler =
|
||||
std::get<jni::weak_ref<SurfaceHandlerBinding::jhybridobject>>(
|
||||
iterator->second)
|
||||
.lockLocal();
|
||||
if (javaSurfaceHandler) {
|
||||
surfaceHandler = &javaSurfaceHandler->cthis()->getSurfaceHandler();
|
||||
}
|
||||
}
|
||||
if (surfaceHandler != nullptr) {
|
||||
mountingCoordinator = surfaceHandler->getMountingCoordinator();
|
||||
}
|
||||
}
|
||||
|
||||
if (mountingCoordinator == nullptr) {
|
||||
return;
|
||||
mountingCoordinator = std::move(pullable.mapped());
|
||||
}
|
||||
|
||||
auto mountingManager = getMountingManager("pullAndExecuteTransaction");
|
||||
@@ -732,8 +715,19 @@ void FabricUIManagerBinding::schedulerShouldRenderTransactions(
|
||||
// commit thread). Just notify Java that a transaction is available; the UI
|
||||
// thread calls back into `pullAndExecuteTransaction` at its next mount
|
||||
// dispatch.
|
||||
mountingManager->onTransactionAvailable(
|
||||
mountingCoordinator->getSurfaceId());
|
||||
auto surfaceId = mountingCoordinator->getSurfaceId();
|
||||
{
|
||||
std::scoped_lock lock(pullableMountingCoordinatorsMutex_);
|
||||
auto& pullable = pullableMountingCoordinators_[surfaceId];
|
||||
if (pullable != nullptr && pullable != mountingCoordinator) {
|
||||
LOG(WARNING)
|
||||
<< "FabricUIManagerBinding::schedulerShouldRenderTransactions: "
|
||||
<< "surface " << surfaceId
|
||||
<< " was restarted while the previous surface's commit waits to be pulled";
|
||||
}
|
||||
pullable = mountingCoordinator;
|
||||
}
|
||||
mountingManager->onTransactionAvailable(surfaceId);
|
||||
} else if (ReactNativeFeatureFlags::
|
||||
enableAccumulatedUpdatesInRawPropsAndroid()) {
|
||||
auto mountingTransaction = mountingCoordinator->pullTransaction(
|
||||
|
||||
@@ -167,6 +167,12 @@ class FabricUIManagerBinding : public jni::HybridClass<FabricUIManagerBinding>,
|
||||
std::mutex pendingTransactionsMutex_;
|
||||
std::vector<MountingTransaction> pendingTransactions_;
|
||||
|
||||
// Pull model: keeps the coordinator that reported a surface's commit alive
|
||||
// until the UI thread handles the pull, as iOS does by capturing it in the
|
||||
// block it dispatches to the main queue. Java only gets the surface id.
|
||||
std::mutex pullableMountingCoordinatorsMutex_;
|
||||
std::unordered_map<SurfaceId, std::shared_ptr<const MountingCoordinator>> pullableMountingCoordinators_;
|
||||
|
||||
float pointScaleFactor_ = 1;
|
||||
|
||||
bool enableFabricLogs_{false};
|
||||
|
||||
+132
@@ -7,16 +7,21 @@
|
||||
|
||||
#include "FabricMountingManagerTestHelper.h"
|
||||
|
||||
#include <react/fabric/FabricUIManagerBinding.h>
|
||||
#include <react/featureflags/ReactNativeFeatureFlags.h>
|
||||
#include <react/featureflags/ReactNativeFeatureFlagsDefaults.h>
|
||||
#include <react/renderer/animationbackend/AnimatedProps.h>
|
||||
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
|
||||
#include <react/renderer/components/root/RootShadowNode.h>
|
||||
#include <react/renderer/components/view/ViewComponentDescriptor.h>
|
||||
#include <react/renderer/components/view/ViewProps.h>
|
||||
#include <react/renderer/core/ComponentDescriptor.h>
|
||||
#include <react/renderer/core/PropsParserContext.h>
|
||||
#include <react/renderer/core/RawProps.h>
|
||||
#include <react/renderer/mounting/MountingOverrideDelegate.h>
|
||||
#include <react/renderer/mounting/MountingTransaction.h>
|
||||
#include <react/renderer/mounting/ShadowView.h>
|
||||
#include <react/renderer/scheduler/Scheduler.h>
|
||||
#include <react/renderer/telemetry/TransactionTelemetry.h>
|
||||
|
||||
namespace facebook::react {
|
||||
@@ -31,6 +36,30 @@ static LayoutConstraints makeLayoutConstraints(
|
||||
.layoutDirection = LayoutDirection::LeftToRight};
|
||||
}
|
||||
|
||||
class FabricMountingManagerTestHelper::DeleteRecorder
|
||||
: public MountingOverrideDelegate {
|
||||
public:
|
||||
bool shouldOverridePullTransaction() const override {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<MountingTransaction> pullTransaction(
|
||||
SurfaceId surfaceId,
|
||||
MountingTransaction::Number number,
|
||||
const TransactionTelemetry& telemetry,
|
||||
ShadowViewMutationList mutations) const override {
|
||||
for (const auto& mutation : mutations) {
|
||||
if (mutation.type == ShadowViewMutation::Delete) {
|
||||
deletedTags.push_back(mutation.oldChildShadowView.tag);
|
||||
}
|
||||
}
|
||||
return MountingTransaction{
|
||||
surfaceId, number, std::move(mutations), telemetry};
|
||||
}
|
||||
|
||||
mutable std::vector<Tag> deletedTags;
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
ShadowView makeShadowView(jint surfaceId, jint tag) {
|
||||
@@ -43,6 +72,13 @@ ShadowView makeShadowView(jint surfaceId, jint tag) {
|
||||
return sv;
|
||||
}
|
||||
|
||||
void runNoJavaScript(std::function<void(jsi::Runtime& runtime)>&& /*unused*/) {}
|
||||
|
||||
std::vector<Tag> toTags(jni::alias_ref<jni::JArrayInt> tags) {
|
||||
auto region = tags->getRegion(0, static_cast<jsize>(tags->size()));
|
||||
return {region.get(), region.get() + tags->size()};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
FabricMountingManagerTestHelper::FabricMountingManagerTestHelper(
|
||||
@@ -285,6 +321,85 @@ void FabricMountingManagerTestHelper::mountPendingTransaction() {
|
||||
}
|
||||
}
|
||||
|
||||
jni::local_ref<JRuntimeExecutor::jhybridobject>
|
||||
FabricMountingManagerTestHelper::createRuntimeExecutor() {
|
||||
return JRuntimeExecutor::newObjectCxxArgs(runNoJavaScript);
|
||||
}
|
||||
|
||||
jni::local_ref<JRuntimeScheduler::jhybridobject>
|
||||
FabricMountingManagerTestHelper::createRuntimeScheduler() {
|
||||
// Work scheduled here never runs and stays queued. A queued task holds the
|
||||
// UIManager, which holds this scheduler, so both outlive the test.
|
||||
runtimeScheduler_ = std::make_shared<RuntimeScheduler>(runNoJavaScript);
|
||||
return JRuntimeScheduler::newObjectCxxArgs(runtimeScheduler_);
|
||||
}
|
||||
|
||||
void FabricMountingManagerTestHelper::registerViewComponent(
|
||||
ComponentFactory* componentFactory) {
|
||||
componentFactory->buildRegistryFunction =
|
||||
[](const EventDispatcher::Weak& eventDispatcher,
|
||||
const std::shared_ptr<const ContextContainer>& contextContainer) {
|
||||
auto providerRegistry =
|
||||
std::make_shared<ComponentDescriptorProviderRegistry>();
|
||||
providerRegistry->add(
|
||||
concreteComponentDescriptorProvider<ViewComponentDescriptor>());
|
||||
return providerRegistry->createComponentDescriptorRegistry(
|
||||
{.eventDispatcher = eventDispatcher,
|
||||
.contextContainer = contextContainer,
|
||||
.flavor = nullptr});
|
||||
};
|
||||
}
|
||||
|
||||
void FabricMountingManagerTestHelper::commitViews(
|
||||
jint surfaceId,
|
||||
jni::alias_ref<jni::JArrayInt> tags) {
|
||||
auto children =
|
||||
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>();
|
||||
for (auto tag : toTags(tags)) {
|
||||
auto props = std::make_shared<ViewProps>();
|
||||
props->collapsable = false;
|
||||
children->push_back(viewComponentDescriptor_->createShadowNode(
|
||||
{.props = props},
|
||||
viewComponentDescriptor_->createFamily(
|
||||
{.tag = tag, .surfaceId = surfaceId, .instanceHandle = nullptr})));
|
||||
}
|
||||
javaUIManager_->getBinding()
|
||||
->getScheduler()
|
||||
->getUIManager()
|
||||
->getShadowTreeRegistry()
|
||||
.visit(surfaceId, [&](const ShadowTree& shadowTree) {
|
||||
shadowTree.commit(
|
||||
[&](const RootShadowNode& oldRootShadowNode) {
|
||||
return std::make_shared<RootShadowNode>(
|
||||
oldRootShadowNode,
|
||||
ShadowNodeFragment{
|
||||
.props = ShadowNodeFragment::propsPlaceholder(),
|
||||
.children = children});
|
||||
},
|
||||
{});
|
||||
});
|
||||
}
|
||||
|
||||
void FabricMountingManagerTestHelper::recordPulledDeletes(jint surfaceId) {
|
||||
deleteRecorder_ = std::make_shared<DeleteRecorder>();
|
||||
javaUIManager_->getBinding()
|
||||
->getScheduler()
|
||||
->getUIManager()
|
||||
->getShadowTreeRegistry()
|
||||
.visit(surfaceId, [&](const ShadowTree& shadowTree) {
|
||||
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate(
|
||||
deleteRecorder_);
|
||||
});
|
||||
}
|
||||
|
||||
jni::local_ref<jni::JArrayInt>
|
||||
FabricMountingManagerTestHelper::pulledDeleteTags() {
|
||||
const auto& tags = deleteRecorder_->deletedTags;
|
||||
auto array = jni::JArrayInt::newArray(tags.size());
|
||||
array->setRegion(0, static_cast<jsize>(tags.size()), tags.data());
|
||||
return array;
|
||||
}
|
||||
|
||||
void FabricMountingManagerTestHelper::registerNatives() {
|
||||
registerHybrid({
|
||||
makeNativeMethod(
|
||||
@@ -332,6 +447,23 @@ void FabricMountingManagerTestHelper::registerNatives() {
|
||||
makeNativeMethod(
|
||||
"resetFeatureFlags",
|
||||
FabricMountingManagerTestHelper::resetFeatureFlags),
|
||||
makeNativeMethod(
|
||||
"createRuntimeExecutor",
|
||||
FabricMountingManagerTestHelper::createRuntimeExecutor),
|
||||
makeNativeMethod(
|
||||
"createRuntimeScheduler",
|
||||
FabricMountingManagerTestHelper::createRuntimeScheduler),
|
||||
makeNativeMethod(
|
||||
"registerViewComponent",
|
||||
FabricMountingManagerTestHelper::registerViewComponent),
|
||||
makeNativeMethod(
|
||||
"commitViews", FabricMountingManagerTestHelper::commitViews),
|
||||
makeNativeMethod(
|
||||
"recordPulledDeletes",
|
||||
FabricMountingManagerTestHelper::recordPulledDeletes),
|
||||
makeNativeMethod(
|
||||
"pulledDeleteTags",
|
||||
FabricMountingManagerTestHelper::pulledDeleteTags),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+17
@@ -8,11 +8,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <fbjni/fbjni.h>
|
||||
#include <react/jni/JRuntimeExecutor.h>
|
||||
#include <react/jni/JRuntimeScheduler.h>
|
||||
#include <react/jni/NativeMap.h>
|
||||
#include <react/renderer/components/view/ViewComponentDescriptor.h>
|
||||
#include <react/renderer/mounting/ShadowTree.h>
|
||||
#include <react/renderer/mounting/ShadowTreeDelegate.h>
|
||||
|
||||
#include <react/fabric/ComponentFactory.h>
|
||||
#include <react/fabric/FabricMountingManager.h>
|
||||
|
||||
#include <unordered_map>
|
||||
@@ -45,6 +48,17 @@ class FabricMountingManagerTestHelper : public jni::HybridClass<FabricMountingMa
|
||||
void overrideFeatureFlags(jboolean fixTextLayoutOnPixelDensityChange);
|
||||
void resetFeatureFlags();
|
||||
|
||||
// A FabricUIManagerBinding registered on the helper's FabricUIManager with
|
||||
// these never runs JavaScript: its surfaces mount only what the helper
|
||||
// commits.
|
||||
jni::local_ref<JRuntimeExecutor::jhybridobject> createRuntimeExecutor();
|
||||
jni::local_ref<JRuntimeScheduler::jhybridobject> createRuntimeScheduler();
|
||||
void registerViewComponent(ComponentFactory *componentFactory);
|
||||
|
||||
void commitViews(jint surfaceId, jni::alias_ref<jni::JArrayInt> tags);
|
||||
void recordPulledDeletes(jint surfaceId);
|
||||
jni::local_ref<jni::JArrayInt> pulledDeleteTags();
|
||||
|
||||
static void registerNatives();
|
||||
|
||||
private:
|
||||
@@ -65,6 +79,9 @@ class FabricMountingManagerTestHelper : public jni::HybridClass<FabricMountingMa
|
||||
std::unique_ptr<ShadowTreeDelegate> shadowTreeDelegate_;
|
||||
std::unique_ptr<ShadowTree> shadowTree_;
|
||||
std::unordered_map<Tag, std::shared_ptr<const ShadowNode>> queuedShadowNodes_;
|
||||
std::shared_ptr<RuntimeScheduler> runtimeScheduler_;
|
||||
class DeleteRecorder;
|
||||
std::shared_ptr<DeleteRecorder> deleteRecorder_;
|
||||
};
|
||||
|
||||
} // namespace facebook::react
|
||||
+161
-3
@@ -8,6 +8,7 @@
|
||||
package com.facebook.react.fabric
|
||||
|
||||
import android.graphics.Rect
|
||||
import android.view.View
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import com.facebook.react.ReactRootView
|
||||
@@ -17,6 +18,7 @@ import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReadableType
|
||||
import com.facebook.react.bridge.WritableNativeArray
|
||||
import com.facebook.react.bridge.WritableNativeMap
|
||||
import com.facebook.react.fabric.events.EventBeatManager
|
||||
import com.facebook.react.fabric.mounting.MountingManager
|
||||
import com.facebook.react.fabric.mounting.MountingManager.MountItemExecutor
|
||||
import com.facebook.react.fabric.mounting.mountitems.IntBufferBatchMountItem
|
||||
@@ -24,6 +26,7 @@ import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
|
||||
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsCxxInterop
|
||||
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults
|
||||
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
|
||||
import com.facebook.react.uimanager.DisplayMetricsHolder
|
||||
import com.facebook.react.uimanager.PixelUtil
|
||||
import com.facebook.react.uimanager.ThemedReactContext
|
||||
import com.facebook.react.uimanager.ViewManager
|
||||
@@ -54,6 +57,7 @@ import org.mockito.Mockito.`when`
|
||||
class FabricMountingManagerInstrumentationTest {
|
||||
private lateinit var mountingManager: MountingManager
|
||||
private lateinit var themedReactContext: ThemedReactContext
|
||||
|
||||
private val surfaceId = 1
|
||||
private var nativeFeatureFlagsHelper: FabricMountingManagerTestHelper? = null
|
||||
|
||||
@@ -80,9 +84,16 @@ class FabricMountingManagerInstrumentationTest {
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
nativeFeatureFlagsHelper?.resetFeatureFlags()
|
||||
ReactNativeFeatureFlags.dangerouslyReset()
|
||||
ReactNativeFeatureFlagsCxxInterop.dangerouslyReset()
|
||||
try {
|
||||
while (cleanups.isNotEmpty()) {
|
||||
cleanups.removeLast()()
|
||||
}
|
||||
} finally {
|
||||
bindingDependencies.clear()
|
||||
nativeFeatureFlagsHelper?.resetFeatureFlags()
|
||||
ReactNativeFeatureFlags.dangerouslyReset()
|
||||
ReactNativeFeatureFlagsCxxInterop.dangerouslyReset()
|
||||
}
|
||||
}
|
||||
|
||||
private fun startSurface() {
|
||||
@@ -471,4 +482,151 @@ class FabricMountingManagerInstrumentationTest {
|
||||
view.setPadding(left, top, right, bottom)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Surface lifecycle through a real FabricUIManagerBinding ----
|
||||
|
||||
/**
|
||||
* The pull model is tested with Props 2.0 (accumulated, exclusive and reconciled props, and props
|
||||
* diffed on insert); the push model with all of these flags off.
|
||||
*/
|
||||
private fun overrideCxxFlags(pullModel: Boolean) {
|
||||
ReactNativeFeatureFlagsCxxInterop.dangerouslyForceOverride(
|
||||
object : ReactNativeFeatureFlagsDefaults() {
|
||||
override fun enableMountingCoordinatorPullModelAndroid(): Boolean = pullModel
|
||||
|
||||
override fun enableAccumulatedUpdatesInRawPropsAndroid(): Boolean = pullModel
|
||||
|
||||
override fun enableExclusivePropsUpdateAndroid(): Boolean = pullModel
|
||||
|
||||
override fun enablePropsUpdateReconciliationAndroid(): Boolean = pullModel
|
||||
|
||||
override fun enablePreallocatedPropsDiffOnInsertAndroid(): Boolean = pullModel
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Run in reverse order after each test: stop the surfaces a test leaves running, then unregister
|
||||
* the bindings.
|
||||
*/
|
||||
private val cleanups = ArrayDeque<() -> Unit>()
|
||||
|
||||
/**
|
||||
* Kept alive until the bindings are unregistered, as a React instance keeps them: the native
|
||||
* binding's event beat uses the event beat manager, and the helper owns the view component
|
||||
* descriptor of the committed views.
|
||||
*/
|
||||
private val bindingDependencies = mutableListOf<Any>()
|
||||
|
||||
private fun registerBinding(fabricUIManager: FabricUIManager): FabricMountingManagerTestHelper {
|
||||
val binding = FabricUIManagerBinding()
|
||||
val helper = FabricMountingManagerTestHelper.create(fabricUIManager)
|
||||
cleanups.addLast { runOnUiThread { binding.unregister() } }
|
||||
val componentFactory = ComponentFactory()
|
||||
val eventBeatManager = EventBeatManager()
|
||||
bindingDependencies.addAll(listOf(helper, eventBeatManager))
|
||||
helper.registerViewComponent(componentFactory)
|
||||
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(themedReactContext)
|
||||
binding.register(
|
||||
helper.createRuntimeExecutor(),
|
||||
helper.createRuntimeScheduler(),
|
||||
fabricUIManager,
|
||||
eventBeatManager,
|
||||
componentFactory,
|
||||
)
|
||||
return helper
|
||||
}
|
||||
|
||||
private fun runOnUiThread(block: () -> Unit) {
|
||||
InstrumentationRegistry.getInstrumentation().runOnMainSync(block)
|
||||
}
|
||||
|
||||
private val rootMeasureSpec = View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)
|
||||
|
||||
private fun startSurfaceWithHandler(fabricUIManager: FabricUIManager): SurfaceHandlerBinding {
|
||||
val surfaceHandler = SurfaceHandlerBinding("TestModule")
|
||||
surfaceHandler.setLayoutConstraints(
|
||||
rootMeasureSpec,
|
||||
rootMeasureSpec,
|
||||
0,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
1f,
|
||||
1f,
|
||||
)
|
||||
fabricUIManager.startSurface(
|
||||
surfaceHandler,
|
||||
themedReactContext,
|
||||
ReactRootView(themedReactContext),
|
||||
)
|
||||
return surfaceHandler
|
||||
}
|
||||
|
||||
/** Starts a surface through the bridgeless path and returns its id and a function stopping it. */
|
||||
private fun startBridgelessSurface(fabricUIManager: FabricUIManager): Pair<Int, () -> Unit> {
|
||||
val surfaceHandler = startSurfaceWithHandler(fabricUIManager)
|
||||
return surfaceHandler.surfaceId to { fabricUIManager.stopSurface(surfaceHandler) }
|
||||
}
|
||||
|
||||
/** Starts a surface through the legacy path and returns its id and a function stopping it. */
|
||||
private fun startLegacySurface(fabricUIManager: FabricUIManager): Pair<Int, () -> Unit> {
|
||||
val surfaceId =
|
||||
fabricUIManager.startSurface(
|
||||
ReactRootView(themedReactContext),
|
||||
"TestModule",
|
||||
null,
|
||||
rootMeasureSpec,
|
||||
rootMeasureSpec,
|
||||
)
|
||||
return surfaceId to { fabricUIManager.stopSurface(surfaceId) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun native_stopSurfaceWithSurfaceHandler_pullModel_pullsDeletes() {
|
||||
assertStoppingSurfacePullsDeletes(pullModel = true, ::startBridgelessSurface)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun native_stopSurfaceWithSurfaceHandler_pushModel_pullsDeletes() {
|
||||
assertStoppingSurfacePullsDeletes(pullModel = false, ::startBridgelessSurface)
|
||||
}
|
||||
|
||||
/** The legacy stop also runs on the UI thread, where its stop commit pulls inline. */
|
||||
@Test
|
||||
fun native_stopSurface_pullModel_pullsDeletes() {
|
||||
assertStoppingSurfacePullsDeletes(pullModel = true, ::startLegacySurface)
|
||||
}
|
||||
|
||||
/**
|
||||
* The transaction that stops a surface deletes its views. Mounting override delegates, such as
|
||||
* LayoutAnimations and C++ Animated, must see it pulled.
|
||||
*/
|
||||
private fun assertStoppingSurfacePullsDeletes(
|
||||
pullModel: Boolean,
|
||||
startSurface: (FabricUIManager) -> Pair<Int, () -> Unit>,
|
||||
) {
|
||||
overrideCxxFlags(pullModel)
|
||||
val fabricUIManager = createFabricUIManager()
|
||||
val helper = registerBinding(fabricUIManager)
|
||||
lateinit var stopSurface: () -> Unit
|
||||
var running = true
|
||||
var mounted = false
|
||||
runOnUiThread {
|
||||
val (surfaceId, stop) = startSurface(fabricUIManager)
|
||||
stopSurface = stop
|
||||
cleanups.addLast { if (running) runOnUiThread(stop) }
|
||||
helper.recordPulledDeletes(surfaceId)
|
||||
helper.commitViews(surfaceId, intArrayOf(42, 43))
|
||||
mounted = fabricUIManager.resolveView(42) != null
|
||||
}
|
||||
assertThat(mounted).isTrue()
|
||||
|
||||
runOnUiThread {
|
||||
running = false
|
||||
stopSurface()
|
||||
}
|
||||
|
||||
assertThat(helper.pulledDeleteTags()).containsExactlyInAnyOrder(42, 43)
|
||||
}
|
||||
}
|
||||
+19
-2
@@ -9,6 +9,8 @@ package com.facebook.react.fabric
|
||||
|
||||
import com.facebook.jni.HybridClassBase
|
||||
import com.facebook.react.bridge.NativeMap
|
||||
import com.facebook.react.bridge.RuntimeExecutor
|
||||
import com.facebook.react.bridge.RuntimeScheduler
|
||||
import com.facebook.soloader.SoLoader
|
||||
|
||||
/**
|
||||
@@ -16,8 +18,8 @@ import com.facebook.soloader.SoLoader
|
||||
* around the preallocation queue, executeMount, destroyUnmountedShadowNode, and the
|
||||
* allocatedViewRegistry_ lifecycle.
|
||||
*/
|
||||
class FabricMountingManagerTestHelper private constructor(fabricUIManager: FabricUIManager) :
|
||||
HybridClassBase() {
|
||||
internal class FabricMountingManagerTestHelper
|
||||
private constructor(fabricUIManager: FabricUIManager) : HybridClassBase() {
|
||||
|
||||
init {
|
||||
initHybrid(fabricUIManager)
|
||||
@@ -67,6 +69,21 @@ class FabricMountingManagerTestHelper private constructor(fabricUIManager: Fabri
|
||||
|
||||
external fun resetFeatureFlags()
|
||||
|
||||
// A FabricUIManagerBinding registered on the helper's FabricUIManager with these never runs
|
||||
// JavaScript: its surfaces mount only what the helper commits.
|
||||
external fun createRuntimeExecutor(): RuntimeExecutor
|
||||
|
||||
external fun createRuntimeScheduler(): RuntimeScheduler
|
||||
|
||||
external fun registerViewComponent(componentFactory: ComponentFactory)
|
||||
|
||||
external fun commitViews(surfaceId: Int, tags: IntArray)
|
||||
|
||||
/** Records the Delete mutations of every transaction pulled for the surface from now on. */
|
||||
external fun recordPulledDeletes(surfaceId: Int)
|
||||
|
||||
external fun pulledDeleteTags(): IntArray
|
||||
|
||||
companion object {
|
||||
init {
|
||||
SoLoader.loadLibrary("fabricjni")
|
||||
|
||||
Reference in new issue
Block a user