Skip to content

Commit

Permalink
[rfw] Increase tolerance for material widget tests (#7148)
Browse files Browse the repository at this point in the history
This PR introduces a tolerance for rfw material widget tests to fix the golden test error in flutter/flutter#151611.

Part of a fix for flutter/flutter#151659.
  • Loading branch information
victoreronmosele authored Oct 7, 2024
1 parent 4afc383 commit 3890cee
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/rfw/test/material_widgets_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:rfw/formats.dart' show parseLibraryFile;
import 'package:rfw/rfw.dart';

import 'tolerant_comparator.dart'
if (dart.library.js_interop) 'tolerant_comparator_web.dart';
import 'utils.dart';

void main() {
Expand All @@ -22,6 +24,13 @@ void main() {
..update(materialName, createMaterialWidgets());
}

setUpAll(() {
setUpTolerantComparator(
testPath: 'test/material_widget_test.dart',
precisionTolerance: 0.00002,
);
});

testWidgets('Material widgets', (WidgetTester tester) async {
final Runtime runtime = setupRuntime();
final DynamicContent data = DynamicContent();
Expand Down
56 changes: 56 additions & 0 deletions packages/rfw/test/tolerant_comparator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

/// Sets [_TolerantGoldenFileComparator] as the default golden file comparator
/// in tests.
void setUpTolerantComparator(
{required String testPath, required double precisionTolerance}) {
final GoldenFileComparator oldComparator = goldenFileComparator;
final _TolerantGoldenFileComparator newComparator =
_TolerantGoldenFileComparator(Uri.parse(testPath),
precisionTolerance: precisionTolerance);

goldenFileComparator = newComparator;

addTearDown(() => goldenFileComparator = oldComparator);
}

class _TolerantGoldenFileComparator extends LocalFileComparator {
_TolerantGoldenFileComparator(
super.testFile, {
required double precisionTolerance,
}) : assert(
0 <= precisionTolerance && precisionTolerance <= 1,
'precisionTolerance must be between 0 and 1',
),
_precisionTolerance = precisionTolerance;

/// How much the golden image can differ from the test image.
///
/// It is expected to be between 0 and 1. Where 0 is no difference (the same image)
/// and 1 is the maximum difference (completely different images).
final double _precisionTolerance;

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

final bool passed =
result.passed || result.diffPercent <= _precisionTolerance;
if (passed) {
result.dispose();
return true;
}

final String error = await generateFailureOutput(result, golden, basedir);
result.dispose();
throw FlutterError(error);
}
}
6 changes: 6 additions & 0 deletions packages/rfw/test/tolerant_comparator_web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

void setUpTolerantComparator(
{required String testPath, required double precisionTolerance}) {}

0 comments on commit 3890cee

Please sign in to comment.