diff --git a/packages/flutter_test/lib/src/matchers.dart b/packages/flutter_test/lib/src/matchers.dart index f92b434797a..758499f0ec8 100644 --- a/packages/flutter_test/lib/src/matchers.dart +++ b/packages/flutter_test/lib/src/matchers.dart @@ -266,6 +266,18 @@ Matcher rectMoreOrLessEquals(Rect value, { double epsilon = precisionErrorTolera return _IsWithinDistance(_rectDistance, value, epsilon); } +/// Asserts that two [Matrix4]s are equal, within some tolerated error. +/// +/// {@macro flutter.flutter_test.moreOrLessEquals} +/// +/// See also: +/// +/// * [moreOrLessEquals], which is for [double]s. +/// * [offsetMoreOrLessEquals], which is for [Offset]s. +Matcher matrixMoreOrLessEquals(Matrix4 value, { double epsilon = precisionErrorTolerance }) { + return _IsWithinDistance(_matrixDistance, value, epsilon); +} + /// Asserts that two [Offset]s are equal, within some tolerated error. /// /// {@macro flutter.flutter_test.moreOrLessEquals} @@ -1144,6 +1156,14 @@ double _rectDistance(Rect a, Rect b) { return delta; } +double _matrixDistance(Matrix4 a, Matrix4 b) { + double delta = 0.0; + for (int i = 0; i < 16; i += 1) { + delta = math.max((a[i] - b[i]).abs(), delta); + } + return delta; +} + double _sizeDistance(Size a, Size b) { // TODO(a14n): remove ignore when lint is updated, https://github.com/dart-lang/linter/issues/1843 // ignore: unnecessary_parenthesis diff --git a/packages/flutter_test/test/matchers_test.dart b/packages/flutter_test/test/matchers_test.dart index cf78dc94dd7..844a2564638 100644 --- a/packages/flutter_test/test/matchers_test.dart +++ b/packages/flutter_test/test/matchers_test.dart @@ -4,6 +4,7 @@ // flutter_ignore_for_file: golden_tag (see analyze.dart) +import 'dart:math' as math; import 'dart:typed_data'; import 'package:flutter/rendering.dart'; @@ -197,6 +198,38 @@ void main() { expect(-11.0, moreOrLessEquals(11.0, epsilon: 100.0)); }); + test('matrixMoreOrLessEquals', () { + expect( + Matrix4.rotationZ(math.pi), + matrixMoreOrLessEquals(Matrix4.fromList([ + -1, 0, 0, 0, + 0, -1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1, + ])) + ); + + expect( + Matrix4.rotationZ(math.pi), + matrixMoreOrLessEquals(Matrix4.fromList([ + -2, 0, 0, 0, + 0, -2, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1, + ]), epsilon: 2) + ); + + expect( + Matrix4.rotationZ(math.pi), + isNot(matrixMoreOrLessEquals(Matrix4.fromList([ + -2, 0, 0, 0, + 0, -2, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1, + ]))) + ); + }); + test('rectMoreOrLessEquals', () { expect( const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0),