-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'dart:io'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:image/image.dart' as img; | ||
|
||
class TolerantComparator extends LocalFileComparator { | ||
TolerantComparator(super.testFile, {this.tolerance = 0.0}); | ||
|
||
final double tolerance; | ||
|
||
@override | ||
Future<bool> compare(Uint8List imageBytes, Uri golden) async { | ||
final goldenFile = File.fromUri(golden); | ||
if (!goldenFile.existsSync()) { | ||
goldenFile | ||
..createSync(recursive: true) | ||
..writeAsBytesSync(imageBytes); | ||
return true; | ||
} | ||
|
||
final goldenBytes = goldenFile.readAsBytesSync(); | ||
final testImage = img.decodeImage(imageBytes); | ||
final goldenImage = img.decodeImage(goldenBytes); | ||
|
||
if (testImage == null || goldenImage == null) { | ||
return false; | ||
} | ||
|
||
return _compareImages(testImage, goldenImage); | ||
} | ||
|
||
bool _compareImages(img.Image testImage, img.Image goldenImage) { | ||
if (testImage.width != goldenImage.width || testImage.height != goldenImage.height) { | ||
return false; | ||
} | ||
|
||
int diffPixels = 0; | ||
for (int y = 0; y < testImage.height; y++) { | ||
for (int x = 0; x < testImage.width; x++) { | ||
final testPixel = testImage.getPixel(x, y); | ||
final goldenPixel = goldenImage.getPixel(x, y); | ||
|
||
if (!_isPixelWithinTolerance(testPixel, goldenPixel)) { | ||
diffPixels++; | ||
} | ||
} | ||
} | ||
|
||
final diffPercentage = diffPixels / (testImage.width * testImage.height); | ||
return diffPercentage <= tolerance; | ||
} | ||
|
||
bool _isPixelWithinTolerance(img.Pixel testPixel, img.Pixel goldenPixel) { | ||
final tr = testPixel.r; | ||
final tg = testPixel.g; | ||
final tb = testPixel.b; | ||
|
||
final gr = goldenPixel.r; | ||
final gg = goldenPixel.g; | ||
final gb = goldenPixel.b; | ||
|
||
return (tr - gr).abs() <= 255 * tolerance && | ||
(tg - gg).abs() <= 255 * tolerance && | ||
(tb - gb).abs() <= 255 * tolerance; | ||
} | ||
} |