From 7ef603cc0c8e6d341d4a749687568c39da1ecea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 19 Nov 2026 10:04:59 -0800 Subject: [PATCH] Add a Fantom control for native root constraints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: ## Context Fantom can create roots with a chosen viewport size, but its existing native `TesterAppDelegate::updateSurfaceConstraints` method was not exposed to JavaScript tests. Tests of native host resizing need to change root constraints without another React render or flushing pending JS work. ## Changes - Add a thin `root.updateSurfaceConstraints(width, height)` binding to the existing tester method, forwarding the root’s device pixel ratio. - Add root-view tests for growing and shrinking native constraints, percentage-sized descendants, stable host refs, deferred JS work, and attempts to resize a destroyed root. - Document the control. Do not add context tracking or a queue-validation API. Changelog: [Internal] Differential Revision: D133247484 --- .../testing/fantom/specs/NativeFantom.js | 6 ++ .../react-native-fantom/__docs__/README.md | 4 ++ .../src/__tests__/Fantom-itest.js | 65 +++++++++++++++++++ private/react-native-fantom/src/index.js | 16 +++++ .../tester/src/NativeFantom.cpp | 13 ++++ .../tester/src/NativeFantom.h | 7 ++ 6 files changed, 111 insertions(+) diff --git a/packages/react-native/src/private/testing/fantom/specs/NativeFantom.js b/packages/react-native/src/private/testing/fantom/specs/NativeFantom.js index d4ef3a3e39ca..5b0807484325 100644 --- a/packages/react-native/src/private/testing/fantom/specs/NativeFantom.js +++ b/packages/react-native/src/private/testing/fantom/specs/NativeFantom.js @@ -84,6 +84,12 @@ interface Spec extends TurboModule { viewportOffsetY?: number, ) => RootTag; stopSurface: (surfaceId: RootTag) => void; + updateSurfaceConstraints: ( + surfaceId: RootTag, + viewportWidth: number, + viewportHeight: number, + devicePixelRatio: number, + ) => void; enqueueNativeEvent: ( shadowNode: unknown /* ShadowNode */, type: string, diff --git a/private/react-native-fantom/__docs__/README.md b/private/react-native-fantom/__docs__/README.md index 830e6d96426d..29f428dc6f48 100644 --- a/private/react-native-fantom/__docs__/README.md +++ b/private/react-native-fantom/__docs__/README.md @@ -113,6 +113,10 @@ values. If another root is still alive and the new metrics differ from the current global dimensions, Fantom applies the new values and emits a warning. Call `root.destroy()` when a root is no longer needed. +`root.updateSurfaceConstraints(width, height)` changes native root constraints +without rendering React or flushing pending work. Use it to test root and +relative child layout when a native host resizes the surface. + Each root also uses its `fontScale` for native text and text-input measurement, including line heights and wrapping. `allowFontScaling={false}` opts a component out, and `maxFontSizeMultiplier` caps its scale. Existing roots retain their own diff --git a/private/react-native-fantom/src/__tests__/Fantom-itest.js b/private/react-native-fantom/src/__tests__/Fantom-itest.js index 211e09022d47..8f435e2356b8 100644 --- a/private/react-native-fantom/src/__tests__/Fantom-itest.js +++ b/private/react-native-fantom/src/__tests__/Fantom-itest.js @@ -50,6 +50,71 @@ function getActualViewportDimensions(root: Root): { } describe('Fantom', () => { + describe('root.updateSurfaceConstraints', () => { + it('resizes the root and percentage-sized descendants without another React render', () => { + const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 80}); + const nodeRef = createRef(); + Fantom.runTask(() => { + root.render( + , + ); + }); + const node = nullthrows(nodeRef.current); + Fantom.runTask(() => { + const documentElement = nullthrows(root.document.documentElement); + expect(documentElement.getBoundingClientRect().width).toBe(100); + expect(node.getBoundingClientRect().width).toBe(50); + }); + + root.updateSurfaceConstraints(200, 160); + Fantom.runTask(() => { + const documentElement = nullthrows(root.document.documentElement); + expect(documentElement.getBoundingClientRect().width).toBe(200); + expect(documentElement.getBoundingClientRect().height).toBe(160); + expect(node.getBoundingClientRect().width).toBe(100); + expect(node.getBoundingClientRect().height).toBe(40); + expect(nodeRef.current).toBe(node); + }); + + root.updateSurfaceConstraints(80, 40); + Fantom.runTask(() => { + const documentElement = nullthrows(root.document.documentElement); + expect(documentElement.getBoundingClientRect().width).toBe(80); + expect(documentElement.getBoundingClientRect().height).toBe(40); + expect(node.getBoundingClientRect().width).toBe(40); + expect(node.getBoundingClientRect().height).toBe(10); + }); + root.destroy(); + }); + + it('does not flush pending JS work when updating native constraints', () => { + const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 80}); + Fantom.runTask(() => root.render()); + let didRunTask = false; + Fantom.scheduleTask(() => { + didRunTask = true; + expect( + nullthrows(root.document.documentElement).getBoundingClientRect() + .width, + ).toBe(200); + }); + + root.updateSurfaceConstraints(200, 160); + expect(didRunTask).toBe(false); + Fantom.runWorkLoop(); + expect(didRunTask).toBe(true); + root.destroy(); + }); + + it('rejects resizing a destroyed root', () => { + const root = Fantom.createRoot(); + root.destroy(); + expect(() => root.updateSurfaceConstraints(200, 160)).toThrow( + 'Cannot resize a destroyed root.', + ); + }); + }); + describe('scheduleTask', () => { it('does not run task immediately', () => { let didRun = false; diff --git a/private/react-native-fantom/src/index.js b/private/react-native-fantom/src/index.js index 943472e42572..2f5711616e0e 100644 --- a/private/react-native-fantom/src/index.js +++ b/private/react-native-fantom/src/index.js @@ -184,6 +184,22 @@ class Root { return NativeFantom.takeMountingManagerLogs(this.#surfaceId); } + /** + * Changes the root's native surface constraints without rendering React, + * updating Dimensions, or flushing pending work. + */ + updateSurfaceConstraints(width: number, height: number): void { + if (this.#destroyed) { + throw new Error('Cannot resize a destroyed root.'); + } + NativeFantom.updateSurfaceConstraints( + this.#surfaceId, + width, + height, + this.#devicePixelRatio, + ); + } + destroy() { // TODO: check for leaks. NativeFantom.stopSurface(this.#surfaceId); diff --git a/private/react-native-fantom/tester/src/NativeFantom.cpp b/private/react-native-fantom/tester/src/NativeFantom.cpp index 0567b1746af1..487dbea094c7 100644 --- a/private/react-native-fantom/tester/src/NativeFantom.cpp +++ b/private/react-native-fantom/tester/src/NativeFantom.cpp @@ -60,6 +60,19 @@ void NativeFantom::stopSurface(jsi::Runtime& /*runtime*/, SurfaceId surfaceId) { appDelegate_.stopSurface(surfaceId); } +void NativeFantom::updateSurfaceConstraints( + jsi::Runtime& /*runtime*/, + SurfaceId surfaceId, + double viewportWidth, + double viewportHeight, + double devicePixelRatio) { + appDelegate_.updateSurfaceConstraints( + surfaceId, + static_cast(viewportWidth), + static_cast(viewportHeight), + static_cast(devicePixelRatio)); +} + void NativeFantom::produceFramesForDuration( jsi::Runtime& /*runtime*/, double milliseconds) { diff --git a/private/react-native-fantom/tester/src/NativeFantom.h b/private/react-native-fantom/tester/src/NativeFantom.h index b26aa851c17e..90d677146703 100644 --- a/private/react-native-fantom/tester/src/NativeFantom.h +++ b/private/react-native-fantom/tester/src/NativeFantom.h @@ -89,6 +89,13 @@ class NativeFantom : public NativeFantomCxxSpec { void stopSurface(jsi::Runtime &runtime, SurfaceId surfaceId); + void updateSurfaceConstraints( + jsi::Runtime &runtime, + SurfaceId surfaceId, + double viewportWidth, + double viewportHeight, + double devicePixelRatio); + void produceFramesForDuration(jsi::Runtime &runtime, double milliseconds); void flushMessageQueue(jsi::Runtime &runtime);