-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathflutter_test_config.dart
63 lines (53 loc) · 2.04 KB
/
flutter_test_config.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
const _kGoldenTestsThreshold = 10 / 100; // 1% tolerance
Future<void> testExecutable(FutureOr<void> Function() testMain) async {
if (goldenFileComparator is LocalFileComparator) {
final testUrl = (goldenFileComparator as LocalFileComparator).basedir;
goldenFileComparator = LocalFileComparatorWithThreshold(
Uri.parse('$testUrl/test. dart'),
_kGoldenTestsThreshold,
);
} else {
throw Exception(
'Expected goldenFileComparator to be of type'
'LocalFileComparator'
'but it is of type ${goldenFileComparator.runtimeType}`',
);
}
await testMain();
}
/// Works just like [LocalFileComparator] but includes a [threshold] that, when
/// exceeded, marks the test as a failure.
class LocalFileComparatorWithThreshold extends LocalFileComparator {
/// Threshold above which tests will be marked as failing.
/// Ranges from 0 to 1, both inclusive.
final double threshold;
LocalFileComparatorWithThreshold(Uri testFile, this.threshold)
: assert(threshold >= 0 && threshold <= 1),
super(testFile);
/// Copy of [LocalFileComparator]'s [compare] method, except for the fact that
/// it checks if the [ComparisonResult.diffPercent] is not greater than
/// [threshold] to decide whether this test is successful or a failure.
@override
Future<bool> compare(Uint8List imageBytes, Uri golden) async {
final result = await GoldenFileComparator.compareLists(
imageBytes,
await getGoldenBytes(golden),
);
if (!result.passed && result.diffPercent <= threshold) {
// print(
// 'A difference of ${result.diffPercent * 100}% was found, but it is '
// 'acceptable since it is not greater than the threshold of '
// '${threshold * 100}%',
// );
return true;
}
if (!result.passed) {
final error = await generateFailureOutput(result, golden, basedir);
throw FlutterError(error);
}
return result.passed;
}
}