Skip to content

Commit

Permalink
Fix compilation errors from mock canvas copy (#468)
Browse files Browse the repository at this point in the history
* Alter copy of mock_canvas to unblock moving to flutter_test

* Fix compilation errors

* changelog

* Update changelog
  • Loading branch information
Piinks committed Aug 3, 2023
1 parent 8c250ed commit a5e8532
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 47 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
1 change: 1 addition & 0 deletions test/indicators/capacity_indicators_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
60 changes: 30 additions & 30 deletions test/mock_canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -593,9 +593,9 @@ bool _evaluatePainter(Object? object, Canvas canvas, PaintingContext context) {
abstract class _TestRecordingCanvasMatcher extends Matcher {
@override
bool matches(Object? object, Map<dynamic, dynamic> 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;
Expand All @@ -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');
}
}
Expand All @@ -628,7 +628,7 @@ abstract class _TestRecordingCanvasMatcher extends Matcher {
}

bool _evaluatePredicates(
Iterable<RecordedInvocation> calls, StringBuffer description);
Iterable<RecordInvocation> calls, StringBuffer description);

@override
Description describeMismatch(
Expand Down Expand Up @@ -658,9 +658,9 @@ class _TestRecordingCanvasPaintsCountMatcher

@override
bool _evaluatePredicates(
Iterable<RecordedInvocation> calls, StringBuffer description) {
Iterable<RecordInvocation> 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++;
Expand All @@ -683,8 +683,8 @@ class _TestRecordingCanvasPaintsNothingMatcher

@override
bool _evaluatePredicates(
Iterable<RecordedInvocation> calls, StringBuffer description) {
final Iterable<RecordedInvocation> paintingCalls =
Iterable<RecordInvocation> calls, StringBuffer description) {
final Iterable<RecordInvocation> paintingCalls =
_filterCanvasCalls(calls);
if (paintingCalls.isEmpty) {
return true;
Expand All @@ -702,10 +702,10 @@ class _TestRecordingCanvasPaintsNothingMatcher
];

// Filters out canvas calls that are not painting anything.
static Iterable<RecordedInvocation> _filterCanvasCalls(
Iterable<RecordedInvocation> canvasCalls) {
static Iterable<RecordInvocation> _filterCanvasCalls(
Iterable<RecordInvocation> canvasCalls) {
return canvasCalls.where(
(RecordedInvocation canvasCall) =>
(RecordInvocation canvasCall) =>
!_nonPaintingOperations.contains(canvasCall.invocation.memberName),
);
}
Expand All @@ -714,9 +714,9 @@ class _TestRecordingCanvasPaintsNothingMatcher
class _TestRecordingCanvasPaintsAssertionMatcher extends Matcher {
@override
bool matches(Object? object, Map<dynamic, dynamic> 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;
Expand All @@ -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');
}
}
Expand Down Expand Up @@ -1017,7 +1017,7 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher

@override
bool _evaluatePredicates(
Iterable<RecordedInvocation> calls, StringBuffer description) {
Iterable<RecordInvocation> calls, StringBuffer description) {
if (calls.isEmpty) {
description.writeln('It painted nothing.');
return false;
Expand All @@ -1030,7 +1030,7 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher
return false;
}
final Iterator<_PaintPredicate> predicate = _predicates.iterator;
final Iterator<RecordedInvocation> call = calls.iterator..moveNext();
final Iterator<RecordInvocation> call = calls.iterator..moveNext();
try {
while (predicate.moveNext()) {
predicate.current.match(call);
Expand All @@ -1056,12 +1056,12 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher
}

abstract class _PaintPredicate {
void match(Iterator<RecordedInvocation> call);
void match(Iterator<RecordInvocation> call);

@protected
void checkMethod(Iterator<RecordedInvocation> call, Symbol symbol) {
void checkMethod(Iterator<RecordInvocation> 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;
Expand Down Expand Up @@ -1108,7 +1108,7 @@ abstract class _DrawCommandPaintPredicate extends _PaintPredicate {
String get methodName => _symbolName(symbol);

@override
void match(Iterator<RecordedInvocation> call) {
void match(Iterator<RecordInvocation> call) {
checkMethod(call, symbol);
final int actualArgumentCount =
call.current.invocation.positionalArguments.length;
Expand Down Expand Up @@ -1605,7 +1605,7 @@ class _ShadowPredicate extends _PaintPredicate {
}

@override
void match(Iterator<RecordedInvocation> call) {
void match(Iterator<RecordInvocation> call) {
checkMethod(call, symbol);
verifyArguments(call.current.invocation.positionalArguments);
call.moveNext();
Expand Down Expand Up @@ -1771,8 +1771,8 @@ class _SomethingPaintPredicate extends _PaintPredicate {
final PaintPatternPredicate predicate;

@override
void match(Iterator<RecordedInvocation> call) {
RecordedInvocation currentCall;
void match(Iterator<RecordInvocation> call) {
RecordInvocation currentCall;
bool testedAllCalls = false;
do {
if (testedAllCalls) {
Expand Down Expand Up @@ -1807,9 +1807,9 @@ class _EverythingPaintPredicate extends _PaintPredicate {
final PaintPatternPredicate predicate;

@override
void match(Iterator<RecordedInvocation> call) {
void match(Iterator<RecordInvocation> 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';
}
Expand Down Expand Up @@ -1842,7 +1842,7 @@ class _FunctionPaintPredicate extends _PaintPredicate {
final List<dynamic> arguments;

@override
void match(Iterator<RecordedInvocation> call) {
void match(Iterator<RecordInvocation> call) {
checkMethod(call, symbol);
if (call.current.invocation.positionalArguments.length !=
arguments.length) {
Expand Down Expand Up @@ -1874,7 +1874,7 @@ class _FunctionPaintPredicate extends _PaintPredicate {

class _SaveRestorePairPaintPredicate extends _PaintPredicate {
@override
void match(Iterator<RecordedInvocation> call) {
void match(Iterator<RecordInvocation> call) {
checkMethod(call, #save);
int depth = 1;
while (depth > 0) {
Expand Down
28 changes: 14 additions & 14 deletions test/recording_canvas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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.
/// ```
Expand All @@ -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<RecordedInvocation> invocations = <RecordedInvocation>[];
final List<RecordInvocation> invocations = <RecordInvocation>[];

int _saveCount = 0;

Expand All @@ -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, <dynamic>[bounds, paint]),
stack: StackTrace.current));
}
Expand All @@ -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;
Expand Down

0 comments on commit a5e8532

Please sign in to comment.