From 33d18b562b9519e048c5a4dae36f78847e30df6d Mon Sep 17 00:00:00 2001 From: u221638 Date: Wed, 13 Mar 2024 09:09:48 +0100 Subject: [PATCH] Override GoldenComperator with tolerance to fix problem on CI. --- test/flutter_test_config.dart | 15 ++++++++++----- test/golden_diff_comparator.dart | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/golden_diff_comparator.dart diff --git a/test/flutter_test_config.dart b/test/flutter_test_config.dart index 490e7ff0..05d13062 100644 --- a/test/flutter_test_config.dart +++ b/test/flutter_test_config.dart @@ -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 testExecutable(FutureOr Function() testMain) async => - GoldenToolkit.runWithConfiguration( - () => TestApp.setupAll().then((value) => testMain()), - config: GoldenToolkitConfiguration(skipGoldenAssertion: () => false), - ); +Future testExecutable(FutureOr 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), + ); +} diff --git a/test/golden_diff_comparator.dart b/test/golden_diff_comparator.dart new file mode 100644 index 00000000..d264d0f9 --- /dev/null +++ b/test/golden_diff_comparator.dart @@ -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 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; + } +} \ No newline at end of file