Skip to content

Commit

Permalink
Conditonally import tolerant comparator per platform in rfw's materia…
Browse files Browse the repository at this point in the history
…l_widget_test.dart
  • Loading branch information
victoreronmosele committed Jul 18, 2024
1 parent 39cdd95 commit 1935f04
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 53 deletions.
2 changes: 2 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 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}) {}
53 changes: 0 additions & 53 deletions packages/rfw/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'dart:io' show Platform;

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

// Detects if we're running the tests on the main channel.
//
Expand All @@ -22,55 +21,3 @@ bool get isMainChannel {

// See Contributing section of README.md file.
final bool runGoldens = !kIsWeb && Platform.isLinux && isMainChannel;

/// Sets [_TolerantGoldenFileComparator] as the default golden file comparator
/// in tests.
void setUpTolerantComparator(
{required String testPath, required double precisionTolerance}) {
if (!kIsWeb) {
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);
}
}

0 comments on commit 1935f04

Please sign in to comment.