22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- import 'dart:math' as math ;
5+ import 'dart:convert' show base64Decode ;
66import 'dart:typed_data' ;
77
88import 'package:flutter/services.dart' ;
9+ import 'package:flutter/widgets.dart' ;
910import 'package:flutter_test/flutter_test.dart' ;
1011import 'package:integration_test/integration_test.dart' ;
1112
1213import 'package:wide_gamut_test/main.dart' as app;
1314
15+ /// BGRA10_XR has ~0.002 step size, so 0.01 is needed for 10-bit formats.
16+ const double _bgra10Epsilon = 0.01 ;
17+
1418// See: https://developer.apple.com/documentation/metal/mtlpixelformat/mtlpixelformatbgr10_xr.
1519double _decodeBGR10 (int x) {
1620 const max = 1.25098 ;
@@ -20,23 +24,6 @@ double _decodeBGR10(int x) {
2024 return (x * slope) + intercept;
2125}
2226
23- double _decodeHalf (int x) {
24- if (x == 0x7c00 ) {
25- return double .infinity;
26- }
27- if (x == 0xfc00 ) {
28- return - double .infinity;
29- }
30- final sign = x & 0x8000 == 0 ? 1.0 : - 1.0 ;
31- final int exponent = (x >> 10 ) & 0x1f ;
32- final int fraction = x & 0x3ff ;
33- if (exponent == 0 ) {
34- return sign * math.pow (2.0 , - 14 ) * (fraction / 1024.0 );
35- } else {
36- return sign * math.pow (2.0 , exponent - 15 ) * (1.0 + fraction / 1024.0 );
37- }
38- }
39-
4027bool _isAlmost (double x, double y, double epsilon) {
4128 return (x - y).abs () < epsilon;
4229}
@@ -49,38 +36,6 @@ double _distanceSquared(double r, double g, double b, List<double> color) {
4936
5037List <double > _deepRed = < double > [1.0931 , - 0.2268 , - 0.1501 ];
5138
52- (bool , List <double >) _findRGBAF16Color (
53- Uint8List bytes,
54- int width,
55- int height,
56- List <double > color, {
57- required double epsilon,
58- }) {
59- final byteData = ByteData .sublistView (bytes);
60- expect (bytes.lengthInBytes, width * height * 8 );
61- expect (bytes.lengthInBytes, byteData.lengthInBytes);
62- var foundColor = false ;
63- double minDistance = double .infinity;
64- var closestColor = < double > [0 , 0 , 0 ];
65- for (var i = 0 ; i < bytes.lengthInBytes; i += 8 ) {
66- final int pixel = byteData.getUint64 (i, Endian .host);
67- final double blue = _decodeHalf ((pixel >> 32 ) & 0xffff );
68- final double green = _decodeHalf ((pixel >> 16 ) & 0xffff );
69- final double red = _decodeHalf ((pixel >> 0 ) & 0xffff );
70- if (_isAlmost (red, color[0 ], epsilon) &&
71- _isAlmost (green, color[1 ], epsilon) &&
72- _isAlmost (blue, color[2 ], epsilon)) {
73- foundColor = true ;
74- }
75- final double currentDistance = _distanceSquared (red, green, blue, color);
76- if (currentDistance < minDistance) {
77- minDistance = currentDistance;
78- closestColor = < double > [red, green, blue];
79- }
80- }
81- return (foundColor, closestColor);
82- }
83-
8439(bool , List <double >) _findBGRA10Color (
8540 Uint8List bytes,
8641 int width,
@@ -113,55 +68,21 @@ List<double> _deepRed = <double>[1.0931, -0.2268, -0.1501];
11368 return (foundColor, closestColor);
11469}
11570
116- (bool , List <double >) _findBGR10Color (
117- Uint8List bytes,
118- int width,
119- int height,
120- List <double > color, {
121- required double epsilon,
122- }) {
123- final byteData = ByteData .sublistView (bytes);
124- expect (bytes.lengthInBytes, width * height * 4 );
125- expect (bytes.lengthInBytes, byteData.lengthInBytes);
126- var foundColor = false ;
127- double minDistance = double .infinity;
128- var closestColor = < double > [0 , 0 , 0 ];
129- for (var i = 0 ; i < bytes.lengthInBytes; i += 4 ) {
130- final int pixel = byteData.getUint32 (i, Endian .host);
131- final double blue = _decodeBGR10 (pixel & 0x3ff );
132- final double green = _decodeBGR10 ((pixel >> 10 ) & 0x3ff );
133- final double red = _decodeBGR10 ((pixel >> 20 ) & 0x3ff );
134- if (_isAlmost (red, color[0 ], epsilon) &&
135- _isAlmost (green, color[1 ], epsilon) &&
136- _isAlmost (blue, color[2 ], epsilon)) {
137- foundColor = true ;
138- }
139- final double currentDistance = _distanceSquared (red, green, blue, color);
140- if (currentDistance < minDistance) {
141- minDistance = currentDistance;
142- closestColor = < double > [red, green, blue];
143- }
144- }
145- return (foundColor, closestColor);
146- }
147-
148- (bool , List <double >) _findColor (List <dynamic > result, List <double > color, {double epsilon = 0.01 }) {
71+ (bool , List <double >) _findColor (List <dynamic > result, List <double > color, {double ? epsilon}) {
14972 expect (result, isNotNull);
15073 expect (result.length, 4 );
15174 final [int width, int height, String format, Uint8List bytes] = result;
152- return switch (format) {
153- 'MTLPixelFormatBGR10_XR' => _findBGR10Color (bytes, width, height, color, epsilon: epsilon),
154- 'MTLPixelFormatBGRA10_XR' => _findBGRA10Color (bytes, width, height, color, epsilon: epsilon),
155- 'MTLPixelFormatRGBA16Float' => _findRGBAF16Color (bytes, width, height, color, epsilon: epsilon),
156- _ => fail ('Unsupported pixel format: $format ' ),
157- };
75+ if (format != 'MTLPixelFormatBGRA10_XR' ) {
76+ throw UnsupportedError ('Unsupported pixel format: $format ' );
77+ }
78+ return _findBGRA10Color (bytes, width, height, color, epsilon: epsilon ?? _bgra10Epsilon);
15879}
15980
16081class _HasColor extends Matcher {
161- const _HasColor (this .color, {this .epsilon = 0.01 });
82+ const _HasColor (this .color, {this .epsilon});
16283
16384 final List <double > color;
164- final double epsilon;
85+ final double ? epsilon;
16586
16687 @override
16788 bool matches (dynamic item, Map <dynamic , dynamic > matchState) {
@@ -186,100 +107,123 @@ class _HasColor extends Matcher {
186107 Map <dynamic , dynamic > matchState,
187108 bool verbose,
188109 ) {
189- final closest = matchState['closest' ] as List <double >;
110+ final closest = matchState['closest' ] as List <double >? ;
111+ if (closest == null ) {
112+ return mismatchDescription.add ('could not find any colors (unsupported pixel format?)' );
113+ }
190114 return mismatchDescription.add ('closest color to $color was $closest ' );
191115 }
192116}
193117
118+ /// Precache the Display P3 test image so it is fully decoded before we take a
119+ /// screenshot. Must be called after [pumpAndSettle] so a [BuildContext] exists.
120+ Future <void > _precacheP3Image (WidgetTester tester) async {
121+ final BuildContext context = tester.element (find.byType (app.MyApp ));
122+ await tester.runAsync (() => precacheImage (MemoryImage (base64Decode (app.displayP3Logo)), context));
123+ }
124+
194125void main () {
195126 IntegrationTestWidgetsFlutterBinding .ensureInitialized ();
196127
197128 group ('end-to-end test' , () {
129+ const screenshotChannel = MethodChannel ('flutter/screenshot' );
198130 testWidgets ('look for display p3 deepest red' , (WidgetTester tester) async {
199131 app.run (app.Setup .image);
200- await tester.pumpAndSettle (const Duration (seconds: 2 ));
132+ await tester.pumpAndSettle ();
133+ await _precacheP3Image (tester);
134+ await tester.pumpAndSettle ();
201135
202- const channel = MethodChannel ('flutter/screenshot' );
203- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
136+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
204137 expect (result, _HasColor (_deepRed));
205138 });
206- testWidgets ('look for display p3 deepest red' , (WidgetTester tester) async {
139+ testWidgets ('look for display p3 deepest red (saveLayer) ' , (WidgetTester tester) async {
207140 app.run (app.Setup .canvasSaveLayer);
208- await tester.pumpAndSettle (const Duration (seconds: 2 ));
141+ await tester.pumpAndSettle ();
142+ await _precacheP3Image (tester);
143+ await tester.pumpAndSettle ();
209144
210- const channel = MethodChannel ('flutter/screenshot' );
211- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
145+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
146+ expect (result, _HasColor (_deepRed));
147+ });
148+ testWidgets ('p3 deepest red via codec API (ImageDescriptor)' , (WidgetTester tester) async {
149+ app.run (app.Setup .codecImage);
150+ await tester.pumpAndSettle ();
151+ // Wait for async _loadCodecImage() to complete and rebuild
152+ await tester.runAsync (() => Future <void >.delayed (const Duration (milliseconds: 500 )));
153+ await tester.pumpAndSettle ();
154+
155+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
212156 expect (result, _HasColor (_deepRed));
213157 });
214158 testWidgets ('no p3 deepest red without image' , (WidgetTester tester) async {
215159 app.run (app.Setup .none);
216- await tester.pumpAndSettle (const Duration (seconds : 2 ) );
160+ await tester.pumpAndSettle ();
217161
218- const channel = MethodChannel ('flutter/screenshot' );
219- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
162+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
220163 expect (result, isNot (_HasColor (_deepRed)));
221164 expect (result, isNot (const _HasColor (< double > [0.0 , 1.0 , 0.0 ])));
222165 });
223166 testWidgets ('p3 deepest red with blur' , (WidgetTester tester) async {
224167 app.run (app.Setup .blur);
225- await tester.pumpAndSettle (const Duration (seconds: 2 ));
168+ await tester.pumpAndSettle ();
169+ await _precacheP3Image (tester);
170+ await tester.pumpAndSettle ();
226171
227- const channel = MethodChannel ('flutter/screenshot' );
228- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
172+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
229173 expect (result, _HasColor (_deepRed));
230174 expect (result, const _HasColor (< double > [0.0 , 1.0 , 0.0 ]));
231175 });
232176 testWidgets ('draw image with wide gamut works' , (WidgetTester tester) async {
233177 app.run (app.Setup .drawnImage);
234- await tester.pumpAndSettle (const Duration (seconds: 2 ));
178+ await tester.pumpAndSettle ();
179+ // Wait for async _drawImage() to complete and rebuild
180+ await tester.runAsync (() => Future <void >.delayed (const Duration (milliseconds: 500 )));
181+ await tester.pumpAndSettle ();
235182
236- const channel = MethodChannel ('flutter/screenshot' );
237- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
183+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
238184 expect (result, const _HasColor (< double > [0.0 , 1.0 , 0.0 ]));
239185 });
240186 testWidgets ('draw container with wide gamut works' , (WidgetTester tester) async {
241187 app.run (app.Setup .container);
242- await tester.pumpAndSettle (const Duration (seconds : 2 ) );
188+ await tester.pumpAndSettle ();
243189
244- const channel = MethodChannel ('flutter/screenshot' );
245- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
190+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
246191 expect (result, _HasColor (_deepRed));
247192 });
248193
249194 testWidgets ('draw wide gamut linear gradient works' , (WidgetTester tester) async {
250195 app.run (app.Setup .linearGradient);
251- await tester.pumpAndSettle (const Duration (seconds : 2 ) );
196+ await tester.pumpAndSettle ();
252197
253- const channel = MethodChannel ('flutter/screenshot' );
254- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
255- expect (result, _HasColor (_deepRed));
198+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
199+
200+ // Gradients need larger epsilon due to pixel sampling between gradient stops.
201+ expect (result, _HasColor (_deepRed, epsilon: 0.02 ));
256202 });
257203
258204 testWidgets ('draw wide gamut radial gradient works' , (WidgetTester tester) async {
259205 app.run (app.Setup .radialGradient);
260- await tester.pumpAndSettle (const Duration (seconds : 2 ) );
206+ await tester.pumpAndSettle ();
261207
262- const channel = MethodChannel ('flutter/screenshot' );
263- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
208+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
264209 expect (result, _HasColor (_deepRed, epsilon: 0.05 ));
265210 });
266211
267212 testWidgets ('draw wide gamut conical gradient works' , (WidgetTester tester) async {
268213 app.run (app.Setup .conicalGradient);
269- await tester.pumpAndSettle (const Duration (seconds : 2 ) );
214+ await tester.pumpAndSettle ();
270215
271- const channel = MethodChannel ('flutter/screenshot' );
272- final result = await channel.invokeMethod ('test' ) as List <Object ?>;
216+ final result = await screenshotChannel.invokeMethod ('test' ) as List <Object ?>;
273217 expect (result, _HasColor (_deepRed, epsilon: 0.05 ));
274218 });
275219
276220 testWidgets ('draw wide gamut sweep gradient works' , (WidgetTester tester) async {
277221 app.run (app.Setup .sweepGradient);
278- await tester.pumpAndSettle (const Duration (seconds : 2 ) );
222+ await tester.pumpAndSettle ();
279223
280- const channel = MethodChannel ( 'flutter/screenshot' ) ;
281- final result = await channel. invokeMethod ( 'test' ) as List < Object ?>;
282- expect (result, _HasColor (_deepRed));
224+ final result = await screenshotChannel. invokeMethod ( 'test' ) as List < Object ?> ;
225+ // Sweep gradient endpoint may not be sampled exactly at a pixel center.
226+ expect (result, _HasColor (_deepRed, epsilon : 0.02 ));
283227 });
284228 });
285229}
0 commit comments