|
| 1 | +import { |
| 2 | + describe, |
| 3 | + it, |
| 4 | + expect, |
| 5 | + render, |
| 6 | + waitFor, |
| 7 | + cleanup, |
| 8 | +} from 'react-native-harness'; |
| 9 | +import { View } from 'react-native'; |
| 10 | +import { useEffect, useMemo } from 'react'; |
| 11 | +import { NitroModules } from 'react-native-nitro-modules'; |
| 12 | +import { scheduleOnRN, scheduleOnUI } from 'react-native-worklets'; |
| 13 | + |
| 14 | +import { |
| 15 | + Fit, |
| 16 | + RiveView, |
| 17 | + RiveFileFactory, |
| 18 | + useRiveFile, |
| 19 | + useViewModelInstance, |
| 20 | +} from '@rive-app/react-native'; |
| 21 | +import type { RiveWorkletBridge } from '@rive-app/react-native'; |
| 22 | + |
| 23 | +const DATABINDING = require('../assets/rive/databinding.riv'); |
| 24 | +const BOUNCING_BALL = require('../assets/rive/bouncing_ball.riv'); |
| 25 | + |
| 26 | +function expectDefined<T>(value: T): asserts value is NonNullable<T> { |
| 27 | + expect(value).toBeDefined(); |
| 28 | +} |
| 29 | + |
| 30 | +// Note: installWorkletDispatcher is already called in App.tsx at startup. |
| 31 | +// UI thread listeners are designed for Rive-driven value changes (animation/data binding), |
| 32 | +// not for JS-thread value changes. Testing the full UI thread listener flow requires |
| 33 | +// a Rive file with animation that drives property changes. |
| 34 | + |
| 35 | +describe('Worklet Bridge', () => { |
| 36 | + it('RiveWorkletBridge HybridObject can be created', () => { |
| 37 | + const bridge = |
| 38 | + NitroModules.createHybridObject<RiveWorkletBridge>('RiveWorkletBridge'); |
| 39 | + expect(bridge).toBeDefined(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('property can be boxed for worklet use', async () => { |
| 43 | + const file = await RiveFileFactory.fromSource(DATABINDING, undefined); |
| 44 | + const vm = file.viewModelByName('Person'); |
| 45 | + expectDefined(vm); |
| 46 | + const instance = vm.createInstanceByName('Gordon'); |
| 47 | + expectDefined(instance); |
| 48 | + |
| 49 | + const property = instance.numberProperty('age'); |
| 50 | + expectDefined(property); |
| 51 | + |
| 52 | + // Verify boxing works (required for passing to worklets) |
| 53 | + const boxedProperty = NitroModules.box(property); |
| 54 | + expect(boxedProperty).toBeDefined(); |
| 55 | + |
| 56 | + // Verify the property value can be read |
| 57 | + expect(property.value).toBe(30); // Gordon's age is 30 |
| 58 | + }); |
| 59 | + |
| 60 | + // TODO: for some reason those wont run in harness environment |
| 61 | + if (!global.RN_HARNESS) { |
| 62 | + it('JS thread listener is called when Rive animation changes value', async () => { |
| 63 | + // Listeners are notified when Rive animation/data binding changes values. |
| 64 | + // This test uses bouncing_ball.riv which has an animation that drives ypos. |
| 65 | + |
| 66 | + let receivedValue: number | undefined; |
| 67 | + |
| 68 | + function ListenerTestComponent({ |
| 69 | + onResult, |
| 70 | + }: { |
| 71 | + onResult: (value: number) => void; |
| 72 | + }) { |
| 73 | + const { riveFile } = useRiveFile(BOUNCING_BALL); |
| 74 | + const instance = useViewModelInstance(riveFile); |
| 75 | + |
| 76 | + const property = useMemo( |
| 77 | + () => instance?.numberProperty('ypos'), |
| 78 | + [instance] |
| 79 | + ); |
| 80 | + |
| 81 | + useEffect( |
| 82 | + () => |
| 83 | + property?.addListener((value) => { |
| 84 | + onResult(value); |
| 85 | + }), |
| 86 | + [property, onResult] |
| 87 | + ); |
| 88 | + |
| 89 | + if (!riveFile || !instance) { |
| 90 | + return <View />; |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <RiveView |
| 95 | + style={{ width: 100, height: 100 }} |
| 96 | + autoPlay={true} |
| 97 | + dataBind={instance} |
| 98 | + fit={Fit.Contain} |
| 99 | + file={riveFile} |
| 100 | + /> |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + await render( |
| 106 | + <ListenerTestComponent |
| 107 | + onResult={(value) => { |
| 108 | + receivedValue = value; |
| 109 | + }} |
| 110 | + /> |
| 111 | + ); |
| 112 | + |
| 113 | + await waitFor( |
| 114 | + () => { |
| 115 | + expect(receivedValue).toBeDefined(); |
| 116 | + expect(typeof receivedValue).toBe('number'); |
| 117 | + }, |
| 118 | + { timeout: 5000 } |
| 119 | + ); |
| 120 | + } finally { |
| 121 | + cleanup(); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it('UI thread listener is called when Rive animation changes value', async () => { |
| 126 | + // Same as above but listener is registered via scheduleOnUI, so callback runs on UI thread |
| 127 | + |
| 128 | + type ListenerResult = { |
| 129 | + calledOnUIThread: boolean; |
| 130 | + receivedValue: number; |
| 131 | + }; |
| 132 | + |
| 133 | + let result: ListenerResult | undefined; |
| 134 | + |
| 135 | + function UIThreadListenerTestComponent({ |
| 136 | + onResult, |
| 137 | + }: { |
| 138 | + onResult: (r: ListenerResult) => void; |
| 139 | + }) { |
| 140 | + const { riveFile } = useRiveFile(BOUNCING_BALL); |
| 141 | + const instance = useViewModelInstance(riveFile); |
| 142 | + |
| 143 | + const property = useMemo( |
| 144 | + () => instance?.numberProperty('ypos'), |
| 145 | + [instance] |
| 146 | + ); |
| 147 | + |
| 148 | + useEffect(() => { |
| 149 | + if (!property) return; |
| 150 | + |
| 151 | + const boxedProperty = NitroModules.box(property); |
| 152 | + |
| 153 | + const reportResult = (r: ListenerResult) => { |
| 154 | + onResult(r); |
| 155 | + }; |
| 156 | + |
| 157 | + scheduleOnUI(() => { |
| 158 | + 'worklet'; |
| 159 | + const prop = boxedProperty.unbox(); |
| 160 | + prop.addListener((value: number) => { |
| 161 | + 'worklet'; |
| 162 | + // Check if we're on UI thread |
| 163 | + const isOnUIThread = |
| 164 | + typeof global._WORKLET !== 'undefined' && |
| 165 | + global._WORKLET === true; |
| 166 | + scheduleOnRN(reportResult, { |
| 167 | + calledOnUIThread: isOnUIThread, |
| 168 | + receivedValue: value, |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + return () => { |
| 174 | + property.removeListeners(); |
| 175 | + }; |
| 176 | + }, [property, onResult]); |
| 177 | + |
| 178 | + if (!riveFile || !instance) { |
| 179 | + return <View />; |
| 180 | + } |
| 181 | + |
| 182 | + return ( |
| 183 | + <RiveView |
| 184 | + style={{ width: 100, height: 100 }} |
| 185 | + autoPlay={true} |
| 186 | + dataBind={instance} |
| 187 | + fit={Fit.Contain} |
| 188 | + file={riveFile} |
| 189 | + /> |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + try { |
| 194 | + await render( |
| 195 | + <UIThreadListenerTestComponent |
| 196 | + onResult={(r) => { |
| 197 | + result = r; |
| 198 | + }} |
| 199 | + /> |
| 200 | + ); |
| 201 | + |
| 202 | + // Wait for animation to trigger the listener (bouncing ball should change ypos quickly) |
| 203 | + await waitFor( |
| 204 | + () => { |
| 205 | + expect(result).toBeDefined(); |
| 206 | + expect(result!.calledOnUIThread).toBe(true); |
| 207 | + expect(typeof result!.receivedValue).toBe('number'); |
| 208 | + }, |
| 209 | + { timeout: 5000 } |
| 210 | + ); |
| 211 | + } finally { |
| 212 | + cleanup(); |
| 213 | + } |
| 214 | + }); |
| 215 | + } |
| 216 | +}); |
0 commit comments