Skip to content

Commit a9166d6

Browse files
committed
[FTD #6] add golden test file comparer for threshold
1 parent a9e7207 commit a9166d6

5 files changed

Lines changed: 63 additions & 7 deletions

File tree

dart_test.yaml

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/src/modules/box/painter/bubble_box_painter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class BubbleBoxPainter extends CustomPainter {
3131
this.tip = const BubbleBoxTip(),
3232
});
3333
final Text text;
34-
final EdgeInsets padding; //! TODO: fix text not being centered
34+
final EdgeInsets padding;
3535
final Color bubbleColor;
3636
final double borderRadius;
3737
final BubbleBoxTip tip;
@@ -59,7 +59,7 @@ class BubbleBoxPainter extends CustomPainter {
5959
final bubbleHeight = availableHeight + padding.vertical;
6060

6161
// Calculate tail size
62-
//! TODO: extract
62+
// !TODO: extract
6363
final tailHeight = bubbleHeight * 0.25;
6464

6565
final path = Path()

test/flutter_test_config.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
import 'utils/local_file_comparer.dart';
6+
7+
const _kGoldenTestsThreshold = 0.5 / 100;
8+
9+
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
10+
if (goldenFileComparator is LocalFileComparator) {
11+
final testUrl = (goldenFileComparator as LocalFileComparator).basedir;
12+
13+
goldenFileComparator = LocalFileComparatorWithThreshold(
14+
Uri.parse('$testUrl/test.dart'),
15+
_kGoldenTestsThreshold,
16+
);
17+
} else {
18+
throw Exception(
19+
'Expected `goldenFileComparator` to be of type `LocalFileComparator`, '
20+
'but it is of type `${goldenFileComparator.runtimeType}`',
21+
);
22+
}
23+
24+
await testMain();
25+
}

test/src/widgets/text_decorator_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ void main() {
3434
matchesGoldenFile('text_decorator/box/bubble.default.png'),
3535
);
3636
},
37-
tags: ['golden'],
3837
);
3938
testWidgets(
4039
'BoxStyle.rounded renders',
@@ -57,7 +56,6 @@ void main() {
5756
matchesGoldenFile('text_decorator/box/rounded.default.png'),
5857
);
5958
},
60-
tags: ['golden'],
6159
);
6260

6361
testWidgets(
@@ -80,7 +78,6 @@ void main() {
8078
matchesGoldenFile('text_decorator/box/curled.default.png'),
8179
);
8280
},
83-
tags: ['golden'],
8481
);
8582
});
8683
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
/// Works just like [LocalFileComparator] but includes a [threshold] that, when
5+
/// exceeded, marks the test as a failure.
6+
class LocalFileComparatorWithThreshold extends LocalFileComparator {
7+
LocalFileComparatorWithThreshold(super.testFile, this.threshold) : assert(threshold >= 0 && threshold <= 1, 'Threshold must be between 0 and 1');
8+
9+
/// Threshold above which tests will be marked as failing.
10+
/// Ranges from 0 to 1, both inclusive.
11+
final double threshold;
12+
13+
@override
14+
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
15+
final result = await GoldenFileComparator.compareLists(
16+
imageBytes,
17+
await getGoldenBytes(golden),
18+
);
19+
20+
if (!result.passed && result.diffPercent <= threshold) {
21+
debugPrint(
22+
'A difference of ${result.diffPercent * 100}% was found, but it is '
23+
'acceptable since it is not greater than the threshold of '
24+
'${threshold * 100}%',
25+
);
26+
27+
return true;
28+
}
29+
30+
if (!result.passed) {
31+
final error = await generateFailureOutput(result, golden, basedir);
32+
throw FlutterError(error);
33+
}
34+
return result.passed;
35+
}
36+
}

0 commit comments

Comments
 (0)