|
| 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 | + * @fantom_flags useSharedAnimatedBackend:true enableAnimatedPropsInLayoutReads:true updateRuntimeShadowNodeReferencesOnCommitThread:* |
| 8 | + * @flow strict-local |
| 9 | + * @format |
| 10 | + */ |
| 11 | + |
| 12 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 13 | + |
| 14 | +import type {HostInstance} from 'react-native'; |
| 15 | + |
| 16 | +import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags'; |
| 17 | +import * as Fantom from '@react-native/fantom'; |
| 18 | +import nullthrows from 'nullthrows'; |
| 19 | +import * as React from 'react'; |
| 20 | +import {createRef, useLayoutEffect, useState} from 'react'; |
| 21 | +import {Animated, Easing, View, useAnimatedValue} from 'react-native'; |
| 22 | + |
| 23 | +// Deferred start outputs the initial value on the first animation frame and |
| 24 | +// re-anchors timing on the second, which delays the animation by one frame. |
| 25 | +const DEFERRED_START_MS = |
| 26 | + ReactNativeFeatureFlags.animatedDeferStartOfTimingAnimations() ? 16 : 0; |
| 27 | + |
| 28 | +type Layout = {pageY: number, height: number}; |
| 29 | + |
| 30 | +function measure(element: HostInstance): Layout { |
| 31 | + let layout: ?Layout; |
| 32 | + element.measure((x, y, width, height, pageX, pageY) => { |
| 33 | + layout = {pageY, height}; |
| 34 | + }); |
| 35 | + return nullthrows(layout); |
| 36 | +} |
| 37 | + |
| 38 | +test('layout reads include the transform of a running native-driven animation', () => { |
| 39 | + let _translateY; |
| 40 | + const childRef = createRef<HostInstance>(); |
| 41 | + |
| 42 | + function MyApp() { |
| 43 | + const translateY = useAnimatedValue(0); |
| 44 | + _translateY = translateY; |
| 45 | + return ( |
| 46 | + <Animated.View style={{transform: [{translateY}]}}> |
| 47 | + <View ref={childRef} style={{width: 100, height: 50}} /> |
| 48 | + </Animated.View> |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + const root = Fantom.createRoot(); |
| 53 | + |
| 54 | + Fantom.runTask(() => { |
| 55 | + root.render(<MyApp />); |
| 56 | + }); |
| 57 | + |
| 58 | + const childElement = nullthrows(childRef.current); |
| 59 | + |
| 60 | + expect(measure(childElement).pageY).toBe(0); |
| 61 | + |
| 62 | + let animation; |
| 63 | + Fantom.runTask(() => { |
| 64 | + animation = Animated.timing(_translateY, { |
| 65 | + toValue: 100, |
| 66 | + duration: 1000, |
| 67 | + easing: Easing.linear, |
| 68 | + useNativeDriver: true, |
| 69 | + }); |
| 70 | + animation.start(); |
| 71 | + }); |
| 72 | + |
| 73 | + Fantom.unstable_produceFramesForDuration(500 + DEFERRED_START_MS); |
| 74 | + |
| 75 | + // Nothing was committed to the shadow tree since the animation started. |
| 76 | + expect(measure(childElement).pageY).toBeCloseTo(50, 0.001); |
| 77 | + expect(measure(childElement).height).toBe(50); |
| 78 | + expect(childElement.getBoundingClientRect().y).toBeCloseTo(50, 0.001); |
| 79 | + |
| 80 | + Fantom.runTask(() => { |
| 81 | + animation?.stop(); |
| 82 | + }); |
| 83 | + Fantom.runWorkLoop(); |
| 84 | +}); |
| 85 | + |
| 86 | +test('layout reads include the animated props applied after a re-render', () => { |
| 87 | + let _translateY; |
| 88 | + let _setHeight; |
| 89 | + const childRef = createRef<HostInstance>(); |
| 90 | + |
| 91 | + function MyApp() { |
| 92 | + const translateY = useAnimatedValue(0); |
| 93 | + const [height, setHeight] = useState(50); |
| 94 | + _translateY = translateY; |
| 95 | + _setHeight = setHeight; |
| 96 | + return ( |
| 97 | + <Animated.View style={{transform: [{translateY}]}}> |
| 98 | + <View ref={childRef} style={{width: 100, height}} /> |
| 99 | + </Animated.View> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + const root = Fantom.createRoot(); |
| 104 | + |
| 105 | + Fantom.runTask(() => { |
| 106 | + root.render(<MyApp />); |
| 107 | + }); |
| 108 | + |
| 109 | + const childElement = nullthrows(childRef.current); |
| 110 | + |
| 111 | + let animation; |
| 112 | + Fantom.runTask(() => { |
| 113 | + animation = Animated.timing(_translateY, { |
| 114 | + toValue: 100, |
| 115 | + duration: 1000, |
| 116 | + easing: Easing.linear, |
| 117 | + useNativeDriver: true, |
| 118 | + }); |
| 119 | + animation.start(); |
| 120 | + }); |
| 121 | + |
| 122 | + Fantom.unstable_produceFramesForDuration(500 + DEFERRED_START_MS); |
| 123 | + |
| 124 | + Fantom.runTask(() => { |
| 125 | + _setHeight(80); |
| 126 | + }); |
| 127 | + |
| 128 | + expect(measure(childElement).pageY).toBeCloseTo(50, 0.001); |
| 129 | + expect(measure(childElement).height).toBe(80); |
| 130 | + |
| 131 | + Fantom.unstable_produceFramesForDuration(250); |
| 132 | + |
| 133 | + expect(measure(childElement).pageY).toBeCloseTo(75, 0.001); |
| 134 | + |
| 135 | + Fantom.runTask(() => { |
| 136 | + animation?.stop(); |
| 137 | + }); |
| 138 | + Fantom.runWorkLoop(); |
| 139 | +}); |
| 140 | + |
| 141 | +test('layout reads after a commit in the same task include the commit and the animated props', () => { |
| 142 | + let _translateY; |
| 143 | + const childRef = createRef<HostInstance>(); |
| 144 | + |
| 145 | + function MyApp(props: { |
| 146 | + height: number, |
| 147 | + onRender?: () => void, |
| 148 | + onLayoutEffect?: () => void, |
| 149 | + }) { |
| 150 | + const {height, onRender, onLayoutEffect} = props; |
| 151 | + const translateY = useAnimatedValue(0); |
| 152 | + _translateY = translateY; |
| 153 | + onRender?.(); |
| 154 | + useLayoutEffect(() => { |
| 155 | + onLayoutEffect?.(); |
| 156 | + }); |
| 157 | + return ( |
| 158 | + <Animated.View style={{transform: [{translateY}]}}> |
| 159 | + <View ref={childRef} style={{width: 100, height}} /> |
| 160 | + </Animated.View> |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + const root = Fantom.createRoot(); |
| 165 | + |
| 166 | + Fantom.runTask(() => { |
| 167 | + root.render(<MyApp height={50} />); |
| 168 | + }); |
| 169 | + |
| 170 | + const childElement = nullthrows(childRef.current); |
| 171 | + |
| 172 | + let animation; |
| 173 | + Fantom.runTask(() => { |
| 174 | + animation = Animated.timing(_translateY, { |
| 175 | + toValue: 100, |
| 176 | + duration: 1000, |
| 177 | + easing: Easing.linear, |
| 178 | + useNativeDriver: true, |
| 179 | + }); |
| 180 | + animation.start(); |
| 181 | + }); |
| 182 | + |
| 183 | + Fantom.unstable_produceFramesForDuration(500 + DEFERRED_START_MS); |
| 184 | + |
| 185 | + let layoutInRender: ?Layout; |
| 186 | + let layoutInLayoutEffect: ?Layout; |
| 187 | + Fantom.runTask(() => { |
| 188 | + root.render( |
| 189 | + <MyApp |
| 190 | + height={80} |
| 191 | + onRender={() => { |
| 192 | + layoutInRender = measure(childElement); |
| 193 | + }} |
| 194 | + onLayoutEffect={() => { |
| 195 | + layoutInLayoutEffect = measure(childElement); |
| 196 | + }} |
| 197 | + />, |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + // Render and the layout effect run in the same task, so the read in render |
| 202 | + // captures the revision before the commit. |
| 203 | + expect(nullthrows(layoutInRender).height).toBe(50); |
| 204 | + expect(nullthrows(layoutInRender).pageY).toBeCloseTo(50, 0.001); |
| 205 | + expect(nullthrows(layoutInLayoutEffect).height).toBe(80); |
| 206 | + expect(nullthrows(layoutInLayoutEffect).pageY).toBeCloseTo(50, 0.001); |
| 207 | + |
| 208 | + Fantom.runTask(() => { |
| 209 | + animation?.stop(); |
| 210 | + }); |
| 211 | + Fantom.runWorkLoop(); |
| 212 | +}); |
0 commit comments