Unverified Commit c2e85e67 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[null-safety] remove more usage of mockito from the framework tests (#62936)

parent 191c7cbe
...@@ -8,11 +8,75 @@ import 'dart:math' as math show pi; ...@@ -8,11 +8,75 @@ import 'dart:math' as math show pi;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import '../flutter_test_alternative.dart' show Fake;
import '../widgets/semantics_tester.dart'; import '../widgets/semantics_tester.dart';
class MockCanvas extends Mock implements Canvas {} class MockCanvas extends Fake implements Canvas {
Path capturedPath;
Paint capturedPaint;
@override
void drawPath(Path path, Paint paint) {
capturedPath = path;
capturedPaint = paint;
}
double capturedSx;
double capturedSy;
@override
void scale(double sx, [double sy]) {
capturedSx = sx;
capturedSy = sy;
}
final List<RecordedCanvasCall> invocations = <RecordedCanvasCall>[];
@override
void rotate(double radians) {
invocations.add(RecordedRotate(radians));
}
@override
void translate(double dx, double dy) {
invocations.add(RecordedTranslate(dx, dy));
}
}
@immutable
abstract class RecordedCanvasCall {
const RecordedCanvasCall();
}
class RecordedRotate extends RecordedCanvasCall {
const RecordedRotate(this.radians);
final double radians;
@override
bool operator ==(Object other) {
return other is RecordedRotate && other.radians == radians;
}
@override
int get hashCode => radians.hashCode;
}
class RecordedTranslate extends RecordedCanvasCall {
const RecordedTranslate(this.dx, this.dy);
final double dx;
final double dy;
@override
bool operator ==(Object other) {
return other is RecordedTranslate && other.dx == dx && other.dy == dy;
}
@override
int get hashCode => hashValues(dx, dy);
}
void main() { void main() {
testWidgets('IconTheme color', (WidgetTester tester) async { testWidgets('IconTheme color', (WidgetTester tester) async {
...@@ -33,7 +97,7 @@ void main() { ...@@ -33,7 +97,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint)); final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0)); customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verify(canvas.drawPath(any, argThat(hasColor(0xFF666666)))); expect(canvas.capturedPaint, hasColor(0xFF666666));
}); });
testWidgets('IconTheme opacity', (WidgetTester tester) async { testWidgets('IconTheme opacity', (WidgetTester tester) async {
...@@ -55,7 +119,7 @@ void main() { ...@@ -55,7 +119,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint)); final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0)); customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verify(canvas.drawPath(any, argThat(hasColor(0x80666666)))); expect(canvas.capturedPaint, hasColor(0x80666666));
}); });
testWidgets('color overrides IconTheme color', (WidgetTester tester) async { testWidgets('color overrides IconTheme color', (WidgetTester tester) async {
...@@ -77,7 +141,7 @@ void main() { ...@@ -77,7 +141,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint)); final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0)); customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verify(canvas.drawPath(any, argThat(hasColor(0xFF0000FF)))); expect(canvas.capturedPaint, hasColor(0xFF0000FF));
}); });
testWidgets('IconTheme size', (WidgetTester tester) async { testWidgets('IconTheme size', (WidgetTester tester) async {
...@@ -100,7 +164,8 @@ void main() { ...@@ -100,7 +164,8 @@ void main() {
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(12.0, 12.0)); customPaint.painter.paint(canvas, const Size(12.0, 12.0));
// arrow_menu default size is 48x48 so we expect it to be scaled by 0.25. // arrow_menu default size is 48x48 so we expect it to be scaled by 0.25.
verify(canvas.scale(0.25, 0.25)); expect(canvas.capturedSx, 0.25);
expect(canvas.capturedSy, 0.25);
}); });
testWidgets('size overridesIconTheme size', (WidgetTester tester) async { testWidgets('size overridesIconTheme size', (WidgetTester tester) async {
...@@ -124,7 +189,8 @@ void main() { ...@@ -124,7 +189,8 @@ void main() {
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(12.0, 12.0)); customPaint.painter.paint(canvas, const Size(12.0, 12.0));
// arrow_menu default size is 48x48 so we expect it to be scaled by 2. // arrow_menu default size is 48x48 so we expect it to be scaled by 2.
verify(canvas.scale(2.0, 2.0)); expect(canvas.capturedSx, 2);
expect(canvas.capturedSy, 2);
}); });
testWidgets('Semantic label', (WidgetTester tester) async { testWidgets('Semantic label', (WidgetTester tester) async {
...@@ -165,9 +231,9 @@ void main() { ...@@ -165,9 +231,9 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint)); final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0)); customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verifyInOrder(<void>[ expect(canvas.invocations, const <RecordedCanvasCall>[
canvas.rotate(math.pi), RecordedRotate(math.pi),
canvas.translate(-48.0, -48.0), RecordedTranslate(-48, -48),
]); ]);
}); });
...@@ -189,8 +255,7 @@ void main() { ...@@ -189,8 +255,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint)); final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0)); customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verifyNever(canvas.rotate(any)); expect(canvas.invocations, isEmpty);
verifyNever(canvas.translate(any, any));
}); });
testWidgets('Inherited text direction overridden', (WidgetTester tester) async { testWidgets('Inherited text direction overridden', (WidgetTester tester) async {
...@@ -212,9 +277,9 @@ void main() { ...@@ -212,9 +277,9 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint)); final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas(); final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0)); customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verifyInOrder(<void>[ expect(canvas.invocations, const <RecordedCanvasCall>[
canvas.rotate(math.pi), RecordedRotate(math.pi),
canvas.translate(-48.0, -48.0), RecordedTranslate(-48, -48),
]); ]);
}); });
} }
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import '../flutter_test_alternative.dart' show Fake;
class TestCustomPainter extends CustomPainter { class TestCustomPainter extends CustomPainter {
TestCustomPainter({ this.log, this.name }); TestCustomPainter({ this.log, this.name });
...@@ -39,9 +40,23 @@ class TestCustomPainterWithCustomSemanticsBuilder extends TestCustomPainter { ...@@ -39,9 +40,23 @@ class TestCustomPainterWithCustomSemanticsBuilder extends TestCustomPainter {
}; };
} }
class MockCanvas extends Mock implements Canvas {} class MockCanvas extends Fake implements Canvas {
int saveCount = 0;
int saveCountDelta = 1;
class MockPaintingContext extends Mock implements PaintingContext {} @override
int getSaveCount() {
return saveCount += saveCountDelta;
}
@override
void save() { }
}
class MockPaintingContext extends Fake implements PaintingContext {
@override
final MockCanvas canvas = MockCanvas();
}
void main() { void main() {
testWidgets('Control test for custom painting', (WidgetTester tester) async { testWidgets('Control test for custom painting', (WidgetTester tester) async {
...@@ -76,11 +91,8 @@ void main() { ...@@ -76,11 +91,8 @@ void main() {
painter: TestCustomPainter(log: log), painter: TestCustomPainter(log: log),
)); ));
final RenderCustomPaint renderCustom = target.currentContext.findRenderObject() as RenderCustomPaint; final RenderCustomPaint renderCustom = target.currentContext.findRenderObject() as RenderCustomPaint;
final Canvas canvas = MockCanvas(); final MockPaintingContext paintingContext = MockPaintingContext();
int saveCount = 0; final MockCanvas canvas = paintingContext.canvas;
when(canvas.getSaveCount()).thenAnswer((_) => saveCount++);
final PaintingContext paintingContext = MockPaintingContext();
when(paintingContext.canvas).thenReturn(canvas);
FlutterError getError() { FlutterError getError() {
FlutterError error; FlutterError error;
...@@ -104,7 +116,7 @@ void main() { ...@@ -104,7 +116,7 @@ void main() {
' matching call to restore().\n' ' matching call to restore().\n'
)); ));
when(canvas.getSaveCount()).thenAnswer((_) => saveCount--); canvas.saveCountDelta = -1;
error = getError(); error = getError();
expect(error.toStringDeep(), equalsIgnoringHashCodes( expect(error.toStringDeep(), equalsIgnoringHashCodes(
'FlutterError\n' 'FlutterError\n'
...@@ -117,11 +129,11 @@ void main() { ...@@ -117,11 +129,11 @@ void main() {
' saveLayer().\n' ' saveLayer().\n'
)); ));
when(canvas.getSaveCount()).thenAnswer((_) => saveCount += 2); canvas.saveCountDelta = 2;
error = getError(); error = getError();
expect(error.toStringDeep(), contains('2 more times')); expect(error.toStringDeep(), contains('2 more times'));
when(canvas.getSaveCount()).thenAnswer((_) => saveCount -= 2); canvas.saveCountDelta = -2;
error = getError(); error = getError();
expect(error.toStringDeep(), contains('2 more times')); expect(error.toStringDeep(), contains('2 more times'));
}); });
......
...@@ -11,8 +11,8 @@ import 'package:flutter/material.dart'; ...@@ -11,8 +11,8 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import '../flutter_test_alternative.dart' show Fake;
import 'semantics_tester.dart'; import 'semantics_tester.dart';
final List<String> results = <String>[]; final List<String> results = <String>[];
...@@ -452,60 +452,64 @@ void main() { ...@@ -452,60 +452,64 @@ void main() {
group('PageRouteObserver', () { group('PageRouteObserver', () {
test('calls correct listeners', () { test('calls correct listeners', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>(); final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware1 = MockRouteAware(); final MockRouteAware pageRouteAware1 = MockRouteAware();
final MockPageRoute route1 = MockPageRoute(); final MockPageRoute route1 = MockPageRoute();
observer.subscribe(pageRouteAware1, route1); observer.subscribe(pageRouteAware1, route1);
verify(pageRouteAware1.didPush()).called(1); expect(pageRouteAware1.didPushCount, 1);
final RouteAware pageRouteAware2 = MockRouteAware(); final MockRouteAware pageRouteAware2 = MockRouteAware();
final MockPageRoute route2 = MockPageRoute(); final MockPageRoute route2 = MockPageRoute();
observer.didPush(route2, route1); observer.didPush(route2, route1);
verify(pageRouteAware1.didPushNext()).called(1); expect(pageRouteAware1.didPushNextCount, 1);
observer.subscribe(pageRouteAware2, route2); observer.subscribe(pageRouteAware2, route2);
verify(pageRouteAware2.didPush()).called(1); expect(pageRouteAware2.didPushCount, 1);
observer.didPop(route2, route1); observer.didPop(route2, route1);
verify(pageRouteAware2.didPop()).called(1); expect(pageRouteAware2.didPopCount, 1);
verify(pageRouteAware1.didPopNext()).called(1); expect(pageRouteAware1.didPopNextCount, 1);
}); });
test('does not call listeners for non-PageRoute', () { test('does not call listeners for non-PageRoute', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>(); final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware = MockRouteAware(); final MockRouteAware pageRouteAware = MockRouteAware();
final MockPageRoute pageRoute = MockPageRoute(); final MockPageRoute pageRoute = MockPageRoute();
final MockRoute route = MockRoute(); final MockRoute route = MockRoute();
observer.subscribe(pageRouteAware, pageRoute); observer.subscribe(pageRouteAware, pageRoute);
verify(pageRouteAware.didPush()); expect(pageRouteAware.didPushCount, 1);
observer.didPush(route, pageRoute); observer.didPush(route, pageRoute);
observer.didPop(route, pageRoute); observer.didPop(route, pageRoute);
verifyNoMoreInteractions(pageRouteAware);
expect(pageRouteAware.didPushCount, 1);
expect(pageRouteAware.didPopCount, 0);
}); });
test('does not call listeners when already subscribed', () { test('does not call listeners when already subscribed', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>(); final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware = MockRouteAware(); final MockRouteAware pageRouteAware = MockRouteAware();
final MockPageRoute pageRoute = MockPageRoute(); final MockPageRoute pageRoute = MockPageRoute();
observer.subscribe(pageRouteAware, pageRoute); observer.subscribe(pageRouteAware, pageRoute);
observer.subscribe(pageRouteAware, pageRoute); observer.subscribe(pageRouteAware, pageRoute);
verify(pageRouteAware.didPush()).called(1); expect(pageRouteAware.didPushCount, 1);
}); });
test('does not call listeners when unsubscribed', () { test('does not call listeners when unsubscribed', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>(); final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware = MockRouteAware(); final MockRouteAware pageRouteAware = MockRouteAware();
final MockPageRoute pageRoute = MockPageRoute(); final MockPageRoute pageRoute = MockPageRoute();
final MockPageRoute nextPageRoute = MockPageRoute(); final MockPageRoute nextPageRoute = MockPageRoute();
observer.subscribe(pageRouteAware, pageRoute); observer.subscribe(pageRouteAware, pageRoute);
observer.subscribe(pageRouteAware, nextPageRoute); observer.subscribe(pageRouteAware, nextPageRoute);
verify(pageRouteAware.didPush()).called(2); expect(pageRouteAware.didPushCount, 2);
observer.unsubscribe(pageRouteAware); observer.unsubscribe(pageRouteAware);
observer.didPush(nextPageRoute, pageRoute); observer.didPush(nextPageRoute, pageRoute);
observer.didPop(nextPageRoute, pageRoute); observer.didPop(nextPageRoute, pageRoute);
verifyNoMoreInteractions(pageRouteAware);
expect(pageRouteAware.didPushCount, 2);
expect(pageRouteAware.didPopCount, 0);
}); });
}); });
...@@ -1790,11 +1794,36 @@ class ModifiedReverseTransitionDurationRoute<T> extends MaterialPageRoute<T> { ...@@ -1790,11 +1794,36 @@ class ModifiedReverseTransitionDurationRoute<T> extends MaterialPageRoute<T> {
final Duration reverseTransitionDuration; final Duration reverseTransitionDuration;
} }
class MockPageRoute extends Mock implements PageRoute<dynamic> { } class MockPageRoute extends Fake implements PageRoute<dynamic> { }
class MockRoute extends Fake implements Route<dynamic> { }
class MockRouteAware extends Fake implements RouteAware {
int didPushCount = 0;
int didPushNextCount = 0;
int didPopCount = 0;
int didPopNextCount = 0;
class MockRoute extends Mock implements Route<dynamic> { } @override
void didPush() {
didPushCount += 1;
}
class MockRouteAware extends Mock implements RouteAware { } @override
void didPushNext() {
didPushNextCount += 1;
}
@override
void didPop() {
didPopCount += 1;
}
@override
void didPopNext() {
didPopNextCount += 1;
}
}
class TestPageRouteBuilder extends PageRouteBuilder<void> { class TestPageRouteBuilder extends PageRouteBuilder<void> {
TestPageRouteBuilder({RoutePageBuilder pageBuilder}) : super(pageBuilder: pageBuilder); TestPageRouteBuilder({RoutePageBuilder pageBuilder}) : super(pageBuilder: pageBuilder);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment