-
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
1 parent
9659e70
commit 9bd772c
Showing
17 changed files
with
137 additions
and
76 deletions.
There are no files selected for viewing
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
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
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
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
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 was deleted.
Oops, something went wrong.
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; | ||
} | ||
} |