Skip to content

Commit

Permalink
- added TolerantComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
ps9310 committed Jun 11, 2024
1 parent b3b6425 commit 92f458d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
Binary file added golden/arrow_down.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added golden/arrow_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added golden/arrow_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added golden/arrow_up.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dev_dependencies:
build_runner: ^2.4.10
flutter_test:
sdk: flutter
image: ^4.2.0
mockito: ^5.4.4
path: ^1.9.0
zds_analysis: ^1.0.0
Expand Down
8 changes: 8 additions & 0 deletions test/src/components/tooltip/tooltip_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as p;
import 'package:zeta_flutter/zeta_flutter.dart';

import '../../../test_utils/test_app.dart';
import '../../../test_utils/tolerant_comparator.dart';

void main() {
setUpAll(() {
final testUri = Uri.parse(p.join(Directory.current.path, 'golden').replaceAll(r'\', '/'));
goldenFileComparator = TolerantComparator(testUri, tolerance: 0.01);
});

group('ZetaTooltip Widget Tests', () {
testWidgets('renders with default properties', (WidgetTester tester) async {
await tester.pumpWidget(
Expand Down
67 changes: 67 additions & 0 deletions test/test_utils/tolerant_comparator.dart
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;
}
}

0 comments on commit 92f458d

Please sign in to comment.