Skip to content

Commit 7a2d1d2

Browse files
authored
Add conic gradients to View backgroundImage
## Summary Adds `conic-gradient()` support to `View`’s `backgroundImage` style on iOS and Android. Supports: - String and object syntax. - Rotation with `from <angle>`. - Center positioning with `at <position>`, including edge offsets, percentages, fractional values, and negative offsets. - Angular and percentage color stops, two-position stops, and transition hints. - Multiple background layers and existing background sizing, positioning, and repeat behavior. ### Implementation - Adds JavaScript and C++ parsing, a shared `ConicGradient` model, and native prop conversion. - Uses `CAGradientLayer` on iOS and `SweepGradient` on Android. - Renders the iOS sweep on a square layer to preserve angles on rectangular views. A clipped container retains the original tile dimensions. - Clears conflicting center defaults when explicit right or bottom offsets are supplied. - Accepts negative center offsets on Android. - Rejects malformed commas, misplaced hints, excessive stop positions, and invalid colors in conic gradient objects. - Preserves rejection of `px` strings in linear/radial object color stops and transition hints. Numeric and percentage object positions remain supported. RNTester includes 16 corner cases with string/object syntax switching, dimension swapping, and hide/show controls. ### Scope This PR does not add: - `repeating-conic-gradient()`. - CSS color-interpolation methods such as `in hsl`. - Explicit normalization for color stops outside the visible 0–360° interval. ## Changelog: [General] [Added] - Support conic gradients in View backgroundImage. ## Test Plan ### Automated checks - `yarn --cwd packages/react-native-codegen build`: passed. - `yarn build-types`, `yarn build-types --validate`, and `yarn test-generated-typescript`: passed. - `yarn flow-check`: passed with 0 errors. - ESLint and Prettier for changed JavaScript files, C++ formatting for changed files, and the repository Kotlin formatting check: passed. - C++ API snapshots: regenerated all 12 variants with `yarn cxx-api-build` using Doxygen 1.16.1 and Python 3.12.14. Generation ran in a clean source copy to exclude ignored native build headers. `yarn cxx-api-validate --snapshot-dir /Users/saeed/projects/react-native/scripts/cxx-api/api-snapshots --output-dir /private/tmp/pr58143-cxx-current-validation` passed all 12 snapshots. - Fantom: all 796 tests passed across these six suites: yarn fantom 'View-conicGradient-itest|processConicGradient-itest|processBackgroundImage-itest|processBackgroundPosition-itest|/View-itest|View-nativeCSSParsing-itest' --runInBand Coverage includes both native CSS parsing modes and both C++ prop-setter modes. - Android: all 19 tests passed, including 12 conic-gradient tests and 7 color-stop tests: ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \ --tests com.facebook.react.uimanager.style.ConicGradientTest \ --tests com.facebook.react.uimanager.style.ColorStopTest \ -Preact.internal.useHermesNightly=false -Pkotlin.incremental=false \ --console=plain --no-parallel - C++ CSS parser: all 39 `CSSBackgroundImageTest` tests passed, including five conic cases, using a standalone C++20 GoogleTest runner compiled from the current test source. ### Validation limits - Main disables Android API validation and does not register `apiDump` or `apiCheck`; both task invocations reported task-not-found. The Android API snapshot is unchanged. - Building Fantom with the installed Xcode 27 toolchain required the local CMake flag `-Wno-error=deprecated-literal-operator` for a third-party JSON header. No source or build-script workaround is included. - The iOS pixel suite and simulator/emulator visual checks below were performed before this rebase and were not rerun for the object-validation fix. ## Verified Ran it locally: yes The new tests reject `positions: ['20px']` in both linear and radial object syntax, for colored stops and transition hints. They were run with the pre-fix parser and then the fixed parser: yarn fantom processBackgroundImage-itest --runInBand --testNamePattern='px string' | Parser | Result | | --- | --- | | Before the fix | Both new regression tests failed; 77 existing tests passed. | | With the fix | All 79 tests passed. | Existing tests continue to cover numeric and percentage object stops, CSS pixel stops, and conic angular stops. ### Earlier native rendering verification RNTester was built and run on an iOS simulator and Android emulator. All 16 corner cases were checked, along with resizing, hiding, and restoring the gradient without remounting the view. All 11 iOS pixel tests passed through a focused XCTest runner linked against the production renderer and gradient utilities. Four cases failed before the rectangular-rendering fix. This was not a run of the complete RNTester XCTest suite. The screenshots below record that earlier verification. ### Visual verification Tested in RNTester on iOS and Android. The “Conic Gradient Corner Cases” example includes 16 selectable cases, string/object syntax switching, resizing, and hide/show controls. <details> <summary>Geometry and rotation</summary> The red wedge should retain its 45° angle across aspect ratios. The 90° and −270° rotations should look identical. | Case | iOS | Android | | --- | --- | --- | | Wide rectangle | <img width="1206" height="2622" alt="01-wide-string" src="https://github.com/user-attachments/assets/b2bfe482-c695-4e8c-acec-08ebf9e1a6bb" /> | <img width="1080" height="2424" alt="01-wide-string" src="https://github.com/user-attachments/assets/27f0484a-08d1-4cba-90cb-b2f6b29b9ac6" /> | | Tall rectangle | <img width="1206" height="2622" alt="02-tall-string" src="https://github.com/user-attachments/assets/78da06ab-8609-42c3-89e8-036351efdc9a" /> | <img width="1080" height="2424" alt="02-tall-string" src="https://github.com/user-attachments/assets/3b449d0f-0b8f-4e08-b303-ecdb4a454ce8" /> | | Extreme aspect ratio | <img width="1206" height="2622" alt="03-extreme-aspect-string" src="https://github.com/user-attachments/assets/aa7526ab-3302-4e55-974e-1067438e3bb3" /> | <img width="1080" height="2424" alt="03-extreme-aspect-string" src="https://github.com/user-attachments/assets/0bbb2f2f-3ce5-4fb6-befe-31f36d4f96ee" /> | | 90° rotation | <img width="1206" height="2622" alt="04-rotation-string" src="https://github.com/user-attachments/assets/ddb84d1b-461f-48ed-bb74-838f84ca416d" /> | <img width="1080" height="2424" alt="04-rotation-string" src="https://github.com/user-attachments/assets/0ab2eccf-1248-4251-91fa-f032c15ba6e1" /> | | Equivalent −270° rotation | <img width="1206" height="2622" alt="10-negative-rotation-string" src="https://github.com/user-attachments/assets/c87b3f3e-af6c-4761-a37b-4046099f3077" /> | <img width="1080" height="2424" alt="10-negative-rotation-string" src="https://github.com/user-attachments/assets/433f603f-3cb5-422e-9408-d78a055b7aff" /> | </details> <details> <summary>Center positioning</summary> The white ring marks the expected center where it falls within the view. Negative offsets place the center above and left of the view, producing a solid-blue visible area. | Case | iOS | Android | | --- | --- | --- | | Right 10px / bottom 20px - string syntax | <img width="1206" height="2622" alt="05-edge-offsets-string" src="https://github.com/user-attachments/assets/00491177-682b-4827-9f39-74163176f50b" /> | <img width="1080" height="2424" alt="05-edge-offsets-string" src="https://github.com/user-attachments/assets/7e15f9a5-d0bb-4c8a-808c-e976f99fa926" /> | | Right 10px / bottom 20px - object syntax | <img width="1206" height="2622" alt="05-edge-offsets-object" src="https://github.com/user-attachments/assets/68e007d9-3586-40f1-92c0-db168443d009" /> | <img width="1080" height="2424" alt="05-edge-offsets-object" src="https://github.com/user-attachments/assets/da66ae8c-d298-4733-abdd-f666c1977f72" /> | | Percentage offsets - string syntax | <img width="1206" height="2622" alt="06-percentage-offsets-string" src="https://github.com/user-attachments/assets/a7b35deb-f86d-4b13-8bd7-f19eac0181ee" /> | <img width="1080" height="2424" alt="06-percentage-offsets-string" src="https://github.com/user-attachments/assets/6f6e008b-207c-41d2-9dff-dc67c8b8ddb5" /> | | Percentage offsets - object syntax | <img width="1206" height="2622" alt="06-percentage-offsets-object" src="https://github.com/user-attachments/assets/e3c2849f-fb77-4438-80e0-1df26683cc6d" /> | <img width="1080" height="2424" alt="06-percentage-offsets-object" src="https://github.com/user-attachments/assets/a28b758d-fee6-4280-83ea-f962186e72ee" /> | | Zero edge offsets | <img width="1206" height="2622" alt="07-zero-offsets-string" src="https://github.com/user-attachments/assets/898d3573-3416-4ee0-b6cf-f27ba96078f3" /> | <img width="1080" height="2424" alt="07-zero-offsets-string" src="https://github.com/user-attachments/assets/a6a7c15d-ac4b-4af7-be08-9b0c96a16688" /> | | Fractional offsets | <img width="1206" height="2622" alt="08-fractional-offsets-string" src="https://github.com/user-attachments/assets/f65bd10a-403a-41a4-9de5-ac4c335b1b3f" /> | <img width="1080" height="2424" alt="08-fractional-offsets-string" src="https://github.com/user-attachments/assets/1f9b95d9-a6a5-4722-96e0-80534e86e0d1" /> | | Negative offsets - string syntax | <img width="1206" height="2622" alt="09-negative-offsets-string" src="https://github.com/user-attachments/assets/94a0868c-7308-4e24-9205-a163f34cef14" /> | <img width="1080" height="2424" alt="09-negative-offsets-string" src="https://github.com/user-attachments/assets/8eac9529-5064-471b-8ffd-f6f2d5b3b369" /> | | Negative offsets - object syntax | <img width="1206" height="2622" alt="09-negative-offsets-object" src="https://github.com/user-attachments/assets/dc847c3e-236c-463d-996e-602bdb64dc21" /> | <img width="1080" height="2424" alt="09-negative-offsets-object" src="https://github.com/user-attachments/assets/d0aba589-c70e-4d9e-abc5-6c1629ed2ec0" /> | </details> <details> <summary>Color stops, tiling, and composition</summary> Repeated rectangular tiles should retain independent, undistorted centers. The transition hint controls the blend midpoint, and transparent portions of the conic layer reveal the underlying linear gradient. | Case | iOS | Android | | --- | --- | --- | | Repeated rectangular tiles | <img width="1206" height="2622" alt="11-repeated-tiles-string" src="https://github.com/user-attachments/assets/0f81959b-9471-4a00-ae80-269903be5d94" /> | <img width="1080" height="2424" alt="11-repeated-tiles-string" src="https://github.com/user-attachments/assets/b80953c8-a25f-47ab-8143-e060864b9815" /> | | Transition hint | <img width="1206" height="2622" alt="12-transition-hint-string" src="https://github.com/user-attachments/assets/d46c62c4-ca2a-4991-81b3-5190d72de890" /> | <img width="1080" height="2424" alt="12-transition-hint-string" src="https://github.com/user-attachments/assets/61aafd3f-a13a-451d-ae43-9fb939824232" /> | | Mixed background layers | <img width="1206" height="2622" alt="13-mixed-layers-string" src="https://github.com/user-attachments/assets/ef63f472-4089-474b-80b6-c2859f4bc83c" /> | <img width="1080" height="2424" alt="13-mixed-layers-string" src="https://github.com/user-attachments/assets/c072a95e-f1f7-4b0d-822a-5874b5f8abf4" /> | </details> <details> <summary>Invalid input and zero-size views</summary> Invalid gradients should leave only the gray fallback background. A zero-size view should paint nothing and must not crash. | Case | iOS | Android | | --- | --- | --- | | Invalid trailing hint - string syntax | <img width="1206" height="2622" alt="14-invalid-hint-string" src="https://github.com/user-attachments/assets/599348af-18c3-4e2d-9d92-7ad3266c8977" /> | <img width="1080" height="2424" alt="14-invalid-hint-string" src="https://github.com/user-attachments/assets/e05b7bae-392f-4210-ac70-30f1c29d834a" /> | | Invalid trailing hint - object syntax | <img width="1206" height="2622" alt="14-invalid-hint-object" src="https://github.com/user-attachments/assets/00fc8932-e7d1-48af-89c1-a627d5281dfe" /> | <img width="1080" height="2424" alt="14-invalid-hint-object" src="https://github.com/user-attachments/assets/10ff6db7-0bf9-4402-9d3d-60298faecded" /> | | Excessive stop positions - string syntax | <img width="1206" height="2622" alt="15-invalid-stop-string" src="https://github.com/user-attachments/assets/bfc60f87-53f4-4bf3-a2a2-f4658bd5506c" /> | <img width="1080" height="2424" alt="15-invalid-stop-string" src="https://github.com/user-attachments/assets/1ff502c0-960d-43af-91ca-afa9b37e80c3" /> | | Excessive stop positions - object syntax | <img width="1206" height="2622" alt="15-invalid-stop-object" src="https://github.com/user-attachments/assets/e2a0f0f7-21b4-40a2-804d-8e7cc08a0bea" /> | <img width="1080" height="2424" alt="15-invalid-stop-object" src="https://github.com/user-attachments/assets/1abff7c3-a494-4a7f-b166-fff8b4dbfa3d" /> | | Zero-size view | <img width="1206" height="2622" alt="16-zero-size-string" src="https://github.com/user-attachments/assets/4415cab2-ea0e-4df0-9121-6b64b236b1a2" /> | <img width="1080" height="2424" alt="16-zero-size-string" src="https://github.com/user-attachments/assets/f6822728-9b46-45b5-9147-d4a5a15e5bd7" /> | </details> <details> <summary>Updates without remounting</summary> Swapping dimensions should recalculate the gradient geometry. Hiding should reveal the fallback background, and restoring should redraw the gradient correctly. | Case | iOS | Android | | --- | --- | --- | | Resized in place | <img width="1206" height="2622" alt="17-resized-in-place" src="https://github.com/user-attachments/assets/9b9d33ad-f7a5-4c2a-9b35-f07eb1a37742" /> | <img width="1080" height="2424" alt="17-resized-in-place" src="https://github.com/user-attachments/assets/3a94c6df-ef82-4d33-8ed3-57687cba450a" /> | | Gradient hidden | <img width="1206" height="2622" alt="18-hidden" src="https://github.com/user-attachments/assets/3a7c2f73-61cf-4658-9ac7-cd8607446267" /> | <img width="1080" height="2424" alt="18-hidden" src="https://github.com/user-attachments/assets/4dd3b521-91a8-4f5e-803b-f6c55877d6e7" /> | | Gradient restored | <img width="1206" height="2622" alt="19-restored" src="https://github.com/user-attachments/assets/64859516-27b2-41cc-8384-257a05d34b9c" /> | <img width="1080" height="2424" alt="19-restored" src="https://github.com/user-attachments/assets/454e4af4-4d7b-4447-913a-409a7ad0aafb" /> | </details> Differential Revision: D128673846 Pull Request resolved: #58143
1 parent 521d556 commit 7a2d1d2

42 files changed

Lines changed: 3465 additions & 152 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 378 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
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+
* @flow strict-local
8+
* @fantom_flags enableNativeCSSParsing:*
9+
* @fantom_flags enableCppPropsIteratorSetter:*
10+
* @format
11+
*/
12+
13+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
14+
15+
import type {ColorValue} from '../../../StyleSheet/StyleSheet';
16+
import type {
17+
BackgroundImageValue,
18+
RadialGradientPosition,
19+
} from '../../../StyleSheet/StyleSheetTypes';
20+
21+
import * as Fantom from '@react-native/fantom';
22+
import * as React from 'react';
23+
import {View} from 'react-native';
24+
25+
type BackgroundImage = string | ReadonlyArray<BackgroundImageValue>;
26+
27+
function mountedBackgroundImage(backgroundImage: BackgroundImage): ?string {
28+
const root = Fantom.createRoot();
29+
Fantom.runTask(() => {
30+
root.render(<View collapsable={false} style={{backgroundImage}} />);
31+
});
32+
return root.getRenderedOutput({props: ['backgroundImage']}).toJSONObject()
33+
.props.backgroundImage;
34+
}
35+
36+
const colors: ReadonlyArray<{
37+
color: ColorValue,
38+
positions?: ReadonlyArray<string>,
39+
}> = [{color: 'red'}, {color: 'blue'}];
40+
const serializedColors = 'rgba(255, 0, 0, 1), rgba(0, 0, 255, 1)';
41+
42+
describe('conic gradient native props', () => {
43+
// Assert the complete native value: matching only the right/bottom suffix
44+
// would miss stale top/left defaults, which take precedence on iOS.
45+
const positions: Array<{
46+
css: string,
47+
object: RadialGradientPosition,
48+
expected: string,
49+
}> = [
50+
{css: 'left top', object: {left: '0%', top: '0%'}, expected: '0% 0%'},
51+
{css: 'center', object: {left: '50%', top: '50%'}, expected: '50% 50%'},
52+
{
53+
css: 'top center',
54+
object: {left: '50%', top: '0%'},
55+
expected: '50% 0%',
56+
},
57+
{
58+
css: 'bottom center',
59+
object: {left: '50%', top: '100%'},
60+
expected: '50% 100%',
61+
},
62+
{
63+
css: 'center left',
64+
object: {left: '0%', top: '50%'},
65+
expected: '0% 50%',
66+
},
67+
{
68+
css: 'center right',
69+
object: {left: '100%', top: '50%'},
70+
expected: '100% 50%',
71+
},
72+
{css: '25% 75%', object: {left: '25%', top: '75%'}, expected: '25% 75%'},
73+
{
74+
css: 'left 10px top 20px',
75+
object: {left: 10, top: 20},
76+
expected: '10px 20px',
77+
},
78+
{
79+
css: 'right 10px top 20px',
80+
object: {right: 10, top: 20},
81+
expected: '20px 10px',
82+
},
83+
{
84+
css: 'left 10px bottom 20px',
85+
object: {left: 10, bottom: 20},
86+
expected: '10px 20px',
87+
},
88+
{
89+
css: 'right 10px bottom 20px',
90+
object: {right: 10, bottom: 20},
91+
expected: '10px 20px',
92+
},
93+
{
94+
css: 'bottom 20px right 10px',
95+
object: {bottom: 20, right: 10},
96+
expected: '10px 20px',
97+
},
98+
{
99+
css: 'right 0px bottom 0px',
100+
object: {right: 0, bottom: 0},
101+
expected: '0px 0px',
102+
},
103+
{
104+
css: 'right 10% bottom 20%',
105+
object: {right: '10%', bottom: '20%'},
106+
expected: '10% 20%',
107+
},
108+
{
109+
css: 'right -10px bottom -20px',
110+
object: {right: -10, bottom: -20},
111+
expected: '-10px -20px',
112+
},
113+
{
114+
css: 'right 110% bottom 120%',
115+
object: {right: '110%', bottom: '120%'},
116+
expected: '110% 120%',
117+
},
118+
{
119+
css: 'right 0.5px bottom 1.25px',
120+
object: {right: 0.5, bottom: 1.25},
121+
expected: '0.5px 1.25px',
122+
},
123+
];
124+
125+
for (const {css, object, expected} of positions) {
126+
it(`preserves string position ${css}`, () => {
127+
expect(
128+
mountedBackgroundImage(
129+
`conic-gradient(from 0deg at ${css}, red, blue)`,
130+
),
131+
).toBe(
132+
`[conic-gradient(from 0deg at ${expected} , ${serializedColors})]`,
133+
);
134+
});
135+
it(`preserves object position ${JSON.stringify(object)}`, () => {
136+
expect(
137+
mountedBackgroundImage([
138+
{type: 'conic-gradient', position: object, colorStops: colors},
139+
]),
140+
).toBe(
141+
`[conic-gradient(from 0deg at ${expected} , ${serializedColors})]`,
142+
);
143+
});
144+
}
145+
146+
for (const position of [
147+
'right 10px top',
148+
'top 20px left',
149+
'left bottom 10%',
150+
'center top 25%',
151+
'right 25% center',
152+
'top 25% center',
153+
]) {
154+
for (const from of ['', 'from 90deg ']) {
155+
const input = `conic-gradient(${from}at ${position}, red, blue)`;
156+
it(`rejects three-value position in ${input}`, () => {
157+
expect(mountedBackgroundImage(input)).toBeUndefined();
158+
});
159+
}
160+
}
161+
162+
for (const {position, expected} of [
163+
{position: {right: 10}, expected: '50% 10px'},
164+
{position: {bottom: 20}, expected: '50% 20px'},
165+
{position: {left: 10}, expected: '10px 50%'},
166+
{position: {top: 20}, expected: '50% 20px'},
167+
]) {
168+
it(`centers only the unspecified axis for ${JSON.stringify(position)}`, () => {
169+
expect(
170+
// $FlowFixMe[incompatible-type] - exercise partial positions from untyped callers.
171+
mountedBackgroundImage([
172+
{type: 'conic-gradient', position, colorStops: colors},
173+
]),
174+
).toBe(
175+
`[conic-gradient(from 0deg at ${expected} , ${serializedColors})]`,
176+
);
177+
});
178+
}
179+
180+
for (const [angle, expected] of [
181+
['0', '0'],
182+
['90deg', '90'],
183+
['100grad', '90'],
184+
['0.25turn', '90'],
185+
['-90deg', '-90'],
186+
['720deg', '720'],
187+
]) {
188+
for (const syntax of ['string', 'object']) {
189+
it(`preserves ${angle} in ${syntax} syntax`, () => {
190+
const input: BackgroundImage =
191+
syntax === 'string'
192+
? `conic-gradient(from ${angle}, red, blue)`
193+
: [{type: 'conic-gradient', from: angle, colorStops: colors}];
194+
expect(mountedBackgroundImage(input)).toBe(
195+
`[conic-gradient(from ${expected}deg at 50% 50% , ${serializedColors})]`,
196+
);
197+
});
198+
}
199+
}
200+
201+
it('expands two-position hard stops without losing their order', () => {
202+
expect(
203+
mountedBackgroundImage(
204+
'conic-gradient(red 0deg 90deg, blue 90deg 1turn)',
205+
),
206+
).toBe(
207+
'[conic-gradient(from 0deg at 50% 50% , rgba(255, 0, 0, 1) 0%, rgba(255, 0, 0, 1) 25%, rgba(0, 0, 255, 1) 25%, rgba(0, 0, 255, 1) 100%)]',
208+
);
209+
});
210+
211+
it('accepts a single two-position hard stop', () => {
212+
expect(mountedBackgroundImage('conic-gradient(red 0deg 360deg)')).toBe(
213+
'[conic-gradient(from 0deg at 50% 50% , rgba(255, 0, 0, 1) 0%, rgba(255, 0, 0, 1) 100%)]',
214+
);
215+
});
216+
217+
for (const invalid of [
218+
'conic-gradient()',
219+
'conic-gradient(red)',
220+
'conic-gradient(from 10px, red, blue)',
221+
'conic-gradient(from 1, red, blue)',
222+
'conic-gradient(from 50%, red, blue)',
223+
'conic-gradient(from, red, blue)',
224+
'conic-gradient(at, red, blue)',
225+
'conic-gradient(at right left, red, blue)',
226+
'conic-gradient(at left right, red, blue)',
227+
'conic-gradient(at right 10px left 20px, red, blue)',
228+
'conic-gradient(at 20px left, red, blue)',
229+
'conic-gradient(at 20px right, red, blue)',
230+
'conic-gradient(at top 20px, red, blue)',
231+
'conic-gradient(at bottom 20px, red, blue)',
232+
'conic-gradient(red 10px, blue)',
233+
'conic-gradient(red 20, blue)',
234+
'conic-gradient(20%, red, blue)',
235+
'conic-gradient(red, blue, 20%)',
236+
'conic-gradient(red, 20%, 30%, blue)',
237+
'conic-gradient(red,, blue)',
238+
'conic-gradient(, red, blue)',
239+
'conic-gradient(red, blue,)',
240+
'conic-gradient(red, not-a-color)',
241+
'conic-gradient(from 90deg red, blue)',
242+
'conic-gradient(at center red, blue)',
243+
]) {
244+
it(`rejects ${invalid}`, () => {
245+
expect(mountedBackgroundImage(invalid)).toBeUndefined();
246+
});
247+
}
248+
249+
for (const colorStops of [
250+
[{color: 'red', positions: ['0deg', '45deg', '90deg']}, {color: 'blue'}],
251+
[{positions: ['20%']}, {color: 'red'}, {color: 'blue'}],
252+
[{color: 'red'}, {color: 'blue'}, {positions: ['20%']}],
253+
[
254+
{color: 'red'},
255+
{positions: ['20%']},
256+
{positions: ['30%']},
257+
{color: 'blue'},
258+
],
259+
[{color: 'red'}, {positions: ['20%', '30%']}, {color: 'blue'}],
260+
[
261+
{color: 'red'},
262+
{color: 'not-a-color', positions: ['25%']},
263+
{color: 'blue'},
264+
],
265+
]) {
266+
it(`rejects malformed object stops through native props: ${JSON.stringify(colorStops)}`, () => {
267+
const input = [{type: 'conic-gradient', colorStops}];
268+
// $FlowFixMe[incompatible-type] - exercise malformed stops from untyped callers.
269+
expect(mountedBackgroundImage(input)).toBeUndefined();
270+
});
271+
}
272+
273+
for (const color of [undefined, null]) {
274+
it(`preserves a valid object hint with color ${String(color)}`, () => {
275+
const input: BackgroundImage = [
276+
{
277+
type: 'conic-gradient',
278+
colorStops: [
279+
{color: 'red'},
280+
{color, positions: ['90deg']},
281+
{color: 'blue'},
282+
],
283+
},
284+
];
285+
expect(mountedBackgroundImage(input)).toBe(
286+
'[conic-gradient(from 0deg at 50% 50% , rgba(255, 0, 0, 1), rgba(0, 0, 0, 0) 25%, rgba(0, 0, 255, 1))]',
287+
);
288+
});
289+
}
290+
291+
it('preserves layer order when conic, linear, and radial gradients are mixed', () => {
292+
expect(
293+
mountedBackgroundImage(
294+
'conic-gradient(red, blue), linear-gradient(red, blue), radial-gradient(red, blue)',
295+
),
296+
).toBe(
297+
`[conic-gradient(from 0deg at 50% 50% , ${serializedColors}), linear-gradient(180deg, ${serializedColors}), radial-gradient(ellipse farthest-corner at 50% 50% , ${serializedColors})]`,
298+
);
299+
});
300+
301+
const updates: Array<{
302+
name: string,
303+
before: BackgroundImage,
304+
after: BackgroundImage | void,
305+
expected: ?string,
306+
}> = [
307+
{
308+
name: 'replaces left/top with right/bottom',
309+
before: 'conic-gradient(at left 30px top 40px, red, blue)',
310+
after: [
311+
{
312+
type: 'conic-gradient',
313+
position: {right: 10, bottom: 20},
314+
colorStops: colors,
315+
},
316+
],
317+
expected: `[conic-gradient(from 0deg at 10px 20px , ${serializedColors})]`,
318+
},
319+
{
320+
name: 'resets edge offsets to the default center',
321+
before: 'conic-gradient(at right 10px bottom 20px, red, blue)',
322+
after: 'conic-gradient(red, blue)',
323+
expected: `[conic-gradient(from 0deg at 50% 50% , ${serializedColors})]`,
324+
},
325+
{
326+
name: 'removes a gradient with an empty list',
327+
before: 'conic-gradient(red, blue)',
328+
after: [],
329+
expected: undefined,
330+
},
331+
{
332+
name: 'removes a gradient with undefined',
333+
before: 'conic-gradient(red, blue)',
334+
after: undefined,
335+
expected: undefined,
336+
},
337+
{
338+
name: 'clears a gradient after an invalid replacement',
339+
before: 'conic-gradient(red, blue)',
340+
after: 'conic-gradient(red 10px, blue)',
341+
expected: undefined,
342+
},
343+
{
344+
name: 'replaces a conic gradient with a linear gradient',
345+
before: 'conic-gradient(red, blue)',
346+
after: 'linear-gradient(red, blue)',
347+
expected: `[linear-gradient(180deg, ${serializedColors})]`,
348+
},
349+
{
350+
name: 'recovers after invalid input',
351+
before: 'conic-gradient(red 10px, blue)',
352+
after: 'conic-gradient(red, blue)',
353+
expected: `[conic-gradient(from 0deg at 50% 50% , ${serializedColors})]`,
354+
},
355+
];
356+
for (const {name, before, after, expected} of updates) {
357+
it(name, () => {
358+
const root = Fantom.createRoot();
359+
Fantom.runTask(() => {
360+
root.render(
361+
<View collapsable={false} style={{backgroundImage: before}} />,
362+
);
363+
});
364+
Fantom.runTask(() => {
365+
root.render(
366+
<View
367+
collapsable={false}
368+
style={after === undefined ? {} : {backgroundImage: after}}
369+
/>,
370+
);
371+
});
372+
expect(
373+
root.getRenderedOutput({props: ['backgroundImage']}).toJSONObject()
374+
.props.backgroundImage,
375+
).toBe(expected);
376+
});
377+
}
378+
});

0 commit comments

Comments
 (0)