@@ -12,6 +12,7 @@ import 'package:litetest/litetest.dart';
1212import 'package:path/path.dart' as path;
1313import 'package:vector_math/vector_math_64.dart' ;
1414
15+ import 'goldens.dart' ;
1516import 'impeller_enabled.dart' ;
1617
1718typedef CanvasCallback = void Function (Canvas canvas);
@@ -123,59 +124,9 @@ void testNoCrashes() {
123124 });
124125}
125126
126- /// @returns true When the images are reasonably similar.
127- /// @todo Make the search actually fuzzy to a certain degree.
128- Future <bool > fuzzyCompareImages (Image golden, Image img) async {
129- if (golden.width != img.width || golden.height != img.height) {
130- return false ;
131- }
132- int getPixel (ByteData data, int x, int y) => data.getUint32 ((x + y * golden.width) * 4 );
133- final ByteData goldenData = (await golden.toByteData ())! ;
134- final ByteData imgData = (await img.toByteData ())! ;
135- for (int y = 0 ; y < golden.height; y++ ) {
136- for (int x = 0 ; x < golden.width; x++ ) {
137- if (getPixel (goldenData, x, y) != getPixel (imgData, x, y)) {
138- return false ;
139- }
140- }
141- }
142- return true ;
143- }
144-
145- Future <void > saveTestImage (Image image, String filename) async {
146- final String imagesPath = path.join ('flutter' , 'testing' , 'resources' );
147- final ByteData pngData = (await image.toByteData (format: ImageByteFormat .png))! ;
148- final String outPath = path.join (imagesPath, filename);
149- File (outPath).writeAsBytesSync (pngData.buffer.asUint8List ());
150- print ('wrote: $outPath ' );
151- }
152-
153- /// @returns true When the images are reasonably similar.
154- Future <bool > fuzzyGoldenImageCompare (
155- Image image, String goldenImageName) async {
156- final String imagesPath = path.join ('flutter' , 'testing' , 'resources' );
157- final File file = File (path.join (imagesPath, goldenImageName));
158-
159- bool areEqual = false ;
160-
161- if (file.existsSync ()) {
162- final Uint8List goldenData = await file.readAsBytes ();
163-
164- final Codec codec = await instantiateImageCodec (goldenData);
165- final FrameInfo frame = await codec.getNextFrame ();
166- expect (frame.image.height, equals (image.height));
167- expect (frame.image.width, equals (image.width));
168-
169- areEqual = await fuzzyCompareImages (frame.image, image);
170- }
127+ void main () async {
128+ final ImageComparer comparer = await ImageComparer .create ();
171129
172- if (! areEqual) {
173- saveTestImage (image, 'found_$goldenImageName ' );
174- }
175- return areEqual;
176- }
177-
178- void main () {
179130 testNoCrashes ();
180131
181132 test ('Simple .toImage' , () async {
@@ -190,11 +141,8 @@ void main() {
190141 }, 100 , 100 );
191142 expect (image.width, equals (100 ));
192143 expect (image.height, equals (100 ));
193-
194- final bool areEqual =
195- await fuzzyGoldenImageCompare (image, 'canvas_test_toImage.png' );
196- expect (areEqual, true );
197- }, skip: impellerEnabled);
144+ await comparer.addGoldenImage (image, 'canvas_test_toImage.png' );
145+ });
198146
199147 Gradient makeGradient () {
200148 return Gradient .linear (
@@ -212,10 +160,8 @@ void main() {
212160 expect (image.width, equals (100 ));
213161 expect (image.height, equals (100 ));
214162
215- final bool areEqual =
216- await fuzzyGoldenImageCompare (image, 'canvas_test_dithered_gradient.png' );
217- expect (areEqual, true );
218- }, skip: ! Platform .isLinux || impellerEnabled); // https://github.com/flutter/flutter/issues/53784
163+ await comparer.addGoldenImage (image, 'canvas_test_dithered_gradient.png' );
164+ });
219165
220166 test ('Null values allowed for drawAtlas methods' , () async {
221167 final Image image = await createImage (100 , 100 );
@@ -302,12 +248,8 @@ void main() {
302248 });
303249 }, width, height);
304250
305- final bool areEqual = await fuzzyCompareImages (incrementalMatrixImage, combinedMatrixImage);
251+ final bool areEqual = await comparer. fuzzyCompareImages (incrementalMatrixImage, combinedMatrixImage);
306252
307- if (! areEqual) {
308- saveTestImage (incrementalMatrixImage, 'incremental_3D_transform_test_image.png' );
309- saveTestImage (combinedMatrixImage, 'combined_3D_transform_test_image.png' );
310- }
311253 expect (areEqual, true );
312254 });
313255
@@ -348,10 +290,8 @@ void main() {
348290 expect (image.width, equals (200 ));
349291 expect (image.height, equals (250 ));
350292
351- final bool areEqual =
352- await fuzzyGoldenImageCompare (image, 'dotted_path_effect_mixed_with_stroked_geometry.png' );
353- expect (areEqual, true );
354- }, skip: ! Platform .isLinux || impellerEnabled); // https://github.com/flutter/flutter/issues/53784
293+ await comparer.addGoldenImage (image, 'dotted_path_effect_mixed_with_stroked_geometry.png' );
294+ });
355295
356296 test ('Gradients with matrices in Paragraphs render correctly' , () async {
357297 final Image image = await toImage ((Canvas canvas) {
@@ -400,10 +340,8 @@ void main() {
400340 expect (image.width, equals (600 ));
401341 expect (image.height, equals (400 ));
402342
403- final bool areEqual =
404- await fuzzyGoldenImageCompare (image, 'text_with_gradient_with_matrix.png' );
405- expect (areEqual, true );
406- }, skip: ! Platform .isLinux || impellerEnabled); // https://github.com/flutter/flutter/issues/53784
343+ await comparer.addGoldenImage (image, 'text_with_gradient_with_matrix.png' );
344+ });
407345
408346 test ('toImageSync - too big' , () async {
409347 PictureRecorder recorder = PictureRecorder ();
@@ -602,8 +540,8 @@ void main() {
602540 final Image tofuImage = await drawText ('>\b <' );
603541
604542 // The tab's image should be identical to the space's image but not the tofu's image.
605- final bool tabToSpaceComparison = await fuzzyCompareImages (tabImage, spaceImage);
606- final bool tabToTofuComparison = await fuzzyCompareImages (tabImage, tofuImage);
543+ final bool tabToSpaceComparison = await comparer. fuzzyCompareImages (tabImage, spaceImage);
544+ final bool tabToTofuComparison = await comparer. fuzzyCompareImages (tabImage, tofuImage);
607545
608546 expect (tabToSpaceComparison, isTrue);
609547 expect (tabToTofuComparison, isFalse);
0 commit comments