From a5e85324e8831633084bfcb4169f3f0ba4bffb62 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Wed, 2 Aug 2023 20:06:40 -0500 Subject: [PATCH] Fix compilation errors from mock canvas copy (#468) * Alter copy of mock_canvas to unblock moving to flutter_test * Fix compilation errors * changelog * Update changelog --- CHANGELOG.md | 3 - test/indicators/capacity_indicators_test.dart | 1 + test/mock_canvas.dart | 60 +++++++++---------- test/recording_canvas.dart | 28 ++++----- 4 files changed, 45 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02a7bb2a..0c59bae9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,3 @@ -## [2.0.1] -* Modified mock_canvas copy from the framework to unblock move to flutter_test - ## [2.0.0] ### 🚨 Breaking Changes 🚨 * `macos_ui` has been migrated to utilize [macos_window_utils](https://pub.dev/packages/macos_window_utils) under the hood, which provides the following benefits: diff --git a/test/indicators/capacity_indicators_test.dart b/test/indicators/capacity_indicators_test.dart index 3286af26..1780a362 100644 --- a/test/indicators/capacity_indicators_test.dart +++ b/test/indicators/capacity_indicators_test.dart @@ -5,6 +5,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:macos_ui/macos_ui.dart'; +// TODO(): Remove once mock_canvas in flutter_test reaches stable. import '../mock_canvas.dart'; void main() { diff --git a/test/mock_canvas.dart b/test/mock_canvas.dart index 7c7cd5f7..ee18e4f3 100644 --- a/test/mock_canvas.dart +++ b/test/mock_canvas.dart @@ -564,7 +564,7 @@ class _MismatchedCall { const _MismatchedCall(this.message, this.callIntroduction, this.call); final String message; final String callIntroduction; - final RecordedInvocation call; + final RecordInvocation call; } bool _evaluatePainter(Object? object, Canvas canvas, PaintingContext context) { @@ -593,9 +593,9 @@ bool _evaluatePainter(Object? object, Canvas canvas, PaintingContext context) { abstract class _TestRecordingCanvasMatcher extends Matcher { @override bool matches(Object? object, Map matchState) { - final TestRecordingCanvas canvas = TestRecordingCanvas(); - final TestRecordingPaintingContext context = - TestRecordingPaintingContext(canvas); + final TestRecordCanvas canvas = TestRecordCanvas(); + final TestRecordPaintingContext context = + TestRecordPaintingContext(canvas); final StringBuffer description = StringBuffer(); String prefixMessage = 'unexpectedly failed.'; bool result = false; @@ -618,7 +618,7 @@ abstract class _TestRecordingCanvasMatcher extends Matcher { if (!result) { if (canvas.invocations.isNotEmpty) { description.write('The complete display list was:'); - for (final RecordedInvocation call in canvas.invocations) { + for (final RecordInvocation call in canvas.invocations) { description.write('\n * $call'); } } @@ -628,7 +628,7 @@ abstract class _TestRecordingCanvasMatcher extends Matcher { } bool _evaluatePredicates( - Iterable calls, StringBuffer description); + Iterable calls, StringBuffer description); @override Description describeMismatch( @@ -658,9 +658,9 @@ class _TestRecordingCanvasPaintsCountMatcher @override bool _evaluatePredicates( - Iterable calls, StringBuffer description) { + Iterable calls, StringBuffer description) { int count = 0; - for (final RecordedInvocation call in calls) { + for (final RecordInvocation call in calls) { if (call.invocation.isMethod && call.invocation.memberName == _methodName) { count++; @@ -683,8 +683,8 @@ class _TestRecordingCanvasPaintsNothingMatcher @override bool _evaluatePredicates( - Iterable calls, StringBuffer description) { - final Iterable paintingCalls = + Iterable calls, StringBuffer description) { + final Iterable paintingCalls = _filterCanvasCalls(calls); if (paintingCalls.isEmpty) { return true; @@ -702,10 +702,10 @@ class _TestRecordingCanvasPaintsNothingMatcher ]; // Filters out canvas calls that are not painting anything. - static Iterable _filterCanvasCalls( - Iterable canvasCalls) { + static Iterable _filterCanvasCalls( + Iterable canvasCalls) { return canvasCalls.where( - (RecordedInvocation canvasCall) => + (RecordInvocation canvasCall) => !_nonPaintingOperations.contains(canvasCall.invocation.memberName), ); } @@ -714,9 +714,9 @@ class _TestRecordingCanvasPaintsNothingMatcher class _TestRecordingCanvasPaintsAssertionMatcher extends Matcher { @override bool matches(Object? object, Map matchState) { - final TestRecordingCanvas canvas = TestRecordingCanvas(); - final TestRecordingPaintingContext context = - TestRecordingPaintingContext(canvas); + final TestRecordCanvas canvas = TestRecordCanvas(); + final TestRecordPaintingContext context = + TestRecordPaintingContext(canvas); final StringBuffer description = StringBuffer(); String prefixMessage = 'unexpectedly failed.'; bool result = false; @@ -738,7 +738,7 @@ class _TestRecordingCanvasPaintsAssertionMatcher extends Matcher { if (!result) { if (canvas.invocations.isNotEmpty) { description.write('The complete display list was:'); - for (final RecordedInvocation call in canvas.invocations) { + for (final RecordInvocation call in canvas.invocations) { description.write('\n * $call'); } } @@ -1017,7 +1017,7 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher @override bool _evaluatePredicates( - Iterable calls, StringBuffer description) { + Iterable calls, StringBuffer description) { if (calls.isEmpty) { description.writeln('It painted nothing.'); return false; @@ -1030,7 +1030,7 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher return false; } final Iterator<_PaintPredicate> predicate = _predicates.iterator; - final Iterator call = calls.iterator..moveNext(); + final Iterator call = calls.iterator..moveNext(); try { while (predicate.moveNext()) { predicate.current.match(call); @@ -1056,12 +1056,12 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher } abstract class _PaintPredicate { - void match(Iterator call); + void match(Iterator call); @protected - void checkMethod(Iterator call, Symbol symbol) { + void checkMethod(Iterator call, Symbol symbol) { int others = 0; - final RecordedInvocation firstCall = call.current; + final RecordInvocation firstCall = call.current; while (!call.current.invocation.isMethod || call.current.invocation.memberName != symbol) { others += 1; @@ -1108,7 +1108,7 @@ abstract class _DrawCommandPaintPredicate extends _PaintPredicate { String get methodName => _symbolName(symbol); @override - void match(Iterator call) { + void match(Iterator call) { checkMethod(call, symbol); final int actualArgumentCount = call.current.invocation.positionalArguments.length; @@ -1605,7 +1605,7 @@ class _ShadowPredicate extends _PaintPredicate { } @override - void match(Iterator call) { + void match(Iterator call) { checkMethod(call, symbol); verifyArguments(call.current.invocation.positionalArguments); call.moveNext(); @@ -1771,8 +1771,8 @@ class _SomethingPaintPredicate extends _PaintPredicate { final PaintPatternPredicate predicate; @override - void match(Iterator call) { - RecordedInvocation currentCall; + void match(Iterator call) { + RecordInvocation currentCall; bool testedAllCalls = false; do { if (testedAllCalls) { @@ -1807,9 +1807,9 @@ class _EverythingPaintPredicate extends _PaintPredicate { final PaintPatternPredicate predicate; @override - void match(Iterator call) { + void match(Iterator call) { do { - final RecordedInvocation currentCall = call.current; + final RecordInvocation currentCall = call.current; if (!currentCall.invocation.isMethod) { throw 'It called $currentCall, which was not a method, when the paint pattern expected a method call'; } @@ -1842,7 +1842,7 @@ class _FunctionPaintPredicate extends _PaintPredicate { final List arguments; @override - void match(Iterator call) { + void match(Iterator call) { checkMethod(call, symbol); if (call.current.invocation.positionalArguments.length != arguments.length) { @@ -1874,7 +1874,7 @@ class _FunctionPaintPredicate extends _PaintPredicate { class _SaveRestorePairPaintPredicate extends _PaintPredicate { @override - void match(Iterator call) { + void match(Iterator call) { checkMethod(call, #save); int depth = 1; while (depth > 0) { diff --git a/test/recording_canvas.dart b/test/recording_canvas.dart index d2bd2f33..8ff6abe4 100644 --- a/test/recording_canvas.dart +++ b/test/recording_canvas.dart @@ -7,9 +7,9 @@ import 'package:flutter/rendering.dart'; /// An [Invocation] and the [stack] trace that led to it. /// /// Used by [TestRecordingCanvas] to trace canvas calls. -class RecordedInvocation { +class RecordInvocation { /// Create a record for an invocation list. - const RecordedInvocation(this.invocation, {required this.stack}); + const RecordInvocation(this.invocation, {required this.stack}); /// The method that was called and its arguments. /// @@ -37,13 +37,13 @@ class RecordedInvocation { /// A [Canvas] for tests that records its method calls. /// -/// This class can be used in conjunction with [TestRecordingPaintingContext] +/// This class can be used in conjunction with [TestRecordPaintingContext] /// to record the [Canvas] method calls made by a renderer. For example: /// /// ```dart /// RenderBox box = tester.renderObject(find.text('ABC')); -/// TestRecordingCanvas canvas = TestRecordingCanvas(); -/// TestRecordingPaintingContext context = TestRecordingPaintingContext(canvas); +/// TestRecordCanvas canvas = TestRecordCanvas(); +/// TestRecordPaintingContext context = TestRecordPaintingContext(canvas); /// box.paint(context, Offset.zero); /// // Now test the expected canvas.invocations. /// ``` @@ -53,10 +53,10 @@ class RecordedInvocation { /// that the test requires. /// /// For simple tests, consider using the [paints] matcher, which overlays a -/// pattern matching API over [TestRecordingCanvas]. -class TestRecordingCanvas implements Canvas { +/// pattern matching API over [TestRecordCanvas]. +class TestRecordCanvas implements Canvas { /// All of the method calls on this canvas. - final List invocations = []; + final List invocations = []; int _saveCount = 0; @@ -67,13 +67,13 @@ class TestRecordingCanvas implements Canvas { void save() { _saveCount += 1; invocations - .add(RecordedInvocation(_MethodCall(#save), stack: StackTrace.current)); + .add(RecordInvocation(_MethodCall(#save), stack: StackTrace.current)); } @override void saveLayer(Rect? bounds, Paint paint) { _saveCount += 1; - invocations.add(RecordedInvocation( + invocations.add(RecordInvocation( _MethodCall(#saveLayer, [bounds, paint]), stack: StackTrace.current)); } @@ -83,20 +83,20 @@ class TestRecordingCanvas implements Canvas { _saveCount -= 1; assert(_saveCount >= 0); invocations.add( - RecordedInvocation(_MethodCall(#restore), stack: StackTrace.current)); + RecordInvocation(_MethodCall(#restore), stack: StackTrace.current)); } @override void noSuchMethod(Invocation invocation) { - invocations.add(RecordedInvocation(invocation, stack: StackTrace.current)); + invocations.add(RecordInvocation(invocation, stack: StackTrace.current)); } } /// A [PaintingContext] for tests that use [TestRecordingCanvas]. -class TestRecordingPaintingContext extends ClipContext +class TestRecordPaintingContext extends ClipContext implements PaintingContext { /// Creates a [PaintingContext] for tests that use [TestRecordingCanvas]. - TestRecordingPaintingContext(this.canvas); + TestRecordPaintingContext(this.canvas); @override final Canvas canvas;