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;
import 'package:flutter/material.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';
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() {
testWidgets('IconTheme color', (WidgetTester tester) async {
......@@ -33,7 +97,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
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 {
......@@ -55,7 +119,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
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 {
......@@ -77,7 +141,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
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 {
......@@ -100,7 +164,8 @@ void main() {
final MockCanvas canvas = MockCanvas();
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.
verify(canvas.scale(0.25, 0.25));
expect(canvas.capturedSx, 0.25);
expect(canvas.capturedSy, 0.25);
});
testWidgets('size overridesIconTheme size', (WidgetTester tester) async {
......@@ -124,7 +189,8 @@ void main() {
final MockCanvas canvas = MockCanvas();
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.
verify(canvas.scale(2.0, 2.0));
expect(canvas.capturedSx, 2);
expect(canvas.capturedSy, 2);
});
testWidgets('Semantic label', (WidgetTester tester) async {
......@@ -165,9 +231,9 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verifyInOrder(<void>[
canvas.rotate(math.pi),
canvas.translate(-48.0, -48.0),
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedRotate(math.pi),
RecordedTranslate(-48, -48),
]);
});
......@@ -189,8 +255,7 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verifyNever(canvas.rotate(any));
verifyNever(canvas.translate(any, any));
expect(canvas.invocations, isEmpty);
});
testWidgets('Inherited text direction overridden', (WidgetTester tester) async {
......@@ -212,9 +277,9 @@ void main() {
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter.paint(canvas, const Size(48.0, 48.0));
verifyInOrder(<void>[
canvas.rotate(math.pi),
canvas.translate(-48.0, -48.0),
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedRotate(math.pi),
RecordedTranslate(-48, -48),
]);
});
}
......
......@@ -7,7 +7,8 @@
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import '../flutter_test_alternative.dart' show Fake;
class TestCustomPainter extends CustomPainter {
TestCustomPainter({ this.log, this.name });
......@@ -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() {
testWidgets('Control test for custom painting', (WidgetTester tester) async {
......@@ -76,11 +91,8 @@ void main() {
painter: TestCustomPainter(log: log),
));
final RenderCustomPaint renderCustom = target.currentContext.findRenderObject() as RenderCustomPaint;
final Canvas canvas = MockCanvas();
int saveCount = 0;
when(canvas.getSaveCount()).thenAnswer((_) => saveCount++);
final PaintingContext paintingContext = MockPaintingContext();
when(paintingContext.canvas).thenReturn(canvas);
final MockPaintingContext paintingContext = MockPaintingContext();
final MockCanvas canvas = paintingContext.canvas;
FlutterError getError() {
FlutterError error;
......@@ -104,7 +116,7 @@ void main() {
' matching call to restore().\n'
));
when(canvas.getSaveCount()).thenAnswer((_) => saveCount--);
canvas.saveCountDelta = -1;
error = getError();
expect(error.toStringDeep(), equalsIgnoringHashCodes(
'FlutterError\n'
......@@ -117,11 +129,11 @@ void main() {
' saveLayer().\n'
));
when(canvas.getSaveCount()).thenAnswer((_) => saveCount += 2);
canvas.saveCountDelta = 2;
error = getError();
expect(error.toStringDeep(), contains('2 more times'));
when(canvas.getSaveCount()).thenAnswer((_) => saveCount -= 2);
canvas.saveCountDelta = -2;
error = getError();
expect(error.toStringDeep(), contains('2 more times'));
});
......
......@@ -11,8 +11,8 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import '../flutter_test_alternative.dart' show Fake;
import 'semantics_tester.dart';
final List<String> results = <String>[];
......@@ -452,60 +452,64 @@ void main() {
group('PageRouteObserver', () {
test('calls correct listeners', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware1 = MockRouteAware();
final MockRouteAware pageRouteAware1 = MockRouteAware();
final MockPageRoute route1 = MockPageRoute();
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();
observer.didPush(route2, route1);
verify(pageRouteAware1.didPushNext()).called(1);
expect(pageRouteAware1.didPushNextCount, 1);
observer.subscribe(pageRouteAware2, route2);
verify(pageRouteAware2.didPush()).called(1);
expect(pageRouteAware2.didPushCount, 1);
observer.didPop(route2, route1);
verify(pageRouteAware2.didPop()).called(1);
verify(pageRouteAware1.didPopNext()).called(1);
expect(pageRouteAware2.didPopCount, 1);
expect(pageRouteAware1.didPopNextCount, 1);
});
test('does not call listeners for non-PageRoute', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware = MockRouteAware();
final MockRouteAware pageRouteAware = MockRouteAware();
final MockPageRoute pageRoute = MockPageRoute();
final MockRoute route = MockRoute();
observer.subscribe(pageRouteAware, pageRoute);
verify(pageRouteAware.didPush());
expect(pageRouteAware.didPushCount, 1);
observer.didPush(route, pageRoute);
observer.didPop(route, pageRoute);
verifyNoMoreInteractions(pageRouteAware);
expect(pageRouteAware.didPushCount, 1);
expect(pageRouteAware.didPopCount, 0);
});
test('does not call listeners when already subscribed', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware = MockRouteAware();
final MockRouteAware pageRouteAware = MockRouteAware();
final MockPageRoute pageRoute = MockPageRoute();
observer.subscribe(pageRouteAware, pageRoute);
observer.subscribe(pageRouteAware, pageRoute);
verify(pageRouteAware.didPush()).called(1);
expect(pageRouteAware.didPushCount, 1);
});
test('does not call listeners when unsubscribed', () {
final RouteObserver<PageRoute<dynamic>> observer = RouteObserver<PageRoute<dynamic>>();
final RouteAware pageRouteAware = MockRouteAware();
final MockRouteAware pageRouteAware = MockRouteAware();
final MockPageRoute pageRoute = MockPageRoute();
final MockPageRoute nextPageRoute = MockPageRoute();
observer.subscribe(pageRouteAware, pageRoute);
observer.subscribe(pageRouteAware, nextPageRoute);
verify(pageRouteAware.didPush()).called(2);
expect(pageRouteAware.didPushCount, 2);
observer.unsubscribe(pageRouteAware);
observer.didPush(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> {
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> {
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