Skip to content

Commit

Permalink
Override GoldenComperator with tolerance to fix problem on CI.
Browse files Browse the repository at this point in the history
  • Loading branch information
rawi-coding committed Mar 13, 2024
1 parent c070451 commit 33d18b5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
15 changes: 10 additions & 5 deletions test/flutter_test_config.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import 'dart:async';

import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';

import 'golden_diff_comparator.dart';
import 'test_app.dart';

Future<void> testExecutable(FutureOr<void> Function() testMain) async =>
GoldenToolkit.runWithConfiguration(
() => TestApp.setupAll().then((value) => testMain()),
config: GoldenToolkitConfiguration(skipGoldenAssertion: () => false),
);
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
final testUrl = (goldenFileComparator as LocalFileComparator).basedir;
goldenFileComparator = GoldenDiffComparator('$testUrl/test.dart');
GoldenToolkit.runWithConfiguration(
() => TestApp.setupAll().then((value) => testMain()),
config: GoldenToolkitConfiguration(skipGoldenAssertion: () => false),
);
}
30 changes: 30 additions & 0 deletions test/golden_diff_comparator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

class GoldenDiffComparator extends LocalFileComparator {
GoldenDiffComparator(
String testFile, {
this.tolerance = 0.005,
}) : super(Uri.parse(testFile));

final double tolerance;

@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
final ComparisonResult result = await GoldenFileComparator.compareLists(
imageBytes,
await getGoldenBytes(golden),
);

if (!result.passed && result.diffPercent > tolerance) {
final String error = await generateFailureOutput(result, golden, basedir);
throw FlutterError(error);
}
if (!result.passed) {
log('A tolerable difference of ${result.diffPercent * 100}% was found when comparing $golden.');
}
return result.passed || result.diffPercent <= tolerance;
}
}

0 comments on commit 33d18b5

Please sign in to comment.