diff --git a/packages/flutter/test/flutter_test_alternative.dart b/packages/flutter/test/flutter_test_alternative.dart index 998c4ac382..9110617c38 100644 --- a/packages/flutter/test/flutter_test_alternative.dart +++ b/packages/flutter/test/flutter_test_alternative.dart @@ -11,6 +11,7 @@ import 'package:test_api/test_api.dart' hide TypeMatcher, isInstanceOf; // ignor import 'package:test_api/test_api.dart' as test_package show TypeMatcher; // ignore: deprecated_member_use export 'package:test_api/test_api.dart' hide TypeMatcher, isInstanceOf; // ignore: deprecated_member_use +export 'package:test_api/fake.dart'; // ignore: deprecated_member_use /// A matcher that compares the type of the actual value to the type argument T. test_package.TypeMatcher isInstanceOf() => isA(); diff --git a/packages/flutter/test/foundation/consolidate_response_test.dart b/packages/flutter/test/foundation/consolidate_response_test.dart index c69aac84bb..2b30beca9b 100644 --- a/packages/flutter/test/foundation/consolidate_response_test.dart +++ b/packages/flutter/test/foundation/consolidate_response_test.dart @@ -11,43 +11,24 @@ import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/foundation.dart'; -import 'package:mockito/mockito.dart'; import '../flutter_test_alternative.dart'; +final Uint8List chunkOne = Uint8List.fromList([0, 1, 2, 3, 4, 5]); +final Uint8List chunkTwo = Uint8List.fromList([6, 7, 8, 9, 10]); void main() { group(consolidateHttpClientResponseBytes, () { - final Uint8List chunkOne = Uint8List.fromList([0, 1, 2, 3, 4, 5]); - final Uint8List chunkTwo = Uint8List.fromList([6, 7, 8, 9, 10]); MockHttpClientResponse response; setUp(() { - response = MockHttpClientResponse(); - when(response.compressionState).thenReturn(HttpClientResponseCompressionState.notCompressed); - when(response.listen( - any, - onDone: anyNamed('onDone'), - onError: anyNamed('onError'), - cancelOnError: anyNamed('cancelOnError'), - )).thenAnswer((Invocation invocation) { - final void Function(List) onData = invocation.positionalArguments[0] as void Function(List); - final void Function(Object) onError = invocation.namedArguments[#onError] as void Function(Object); - final VoidCallback onDone = invocation.namedArguments[#onDone] as VoidCallback; - final bool cancelOnError = invocation.namedArguments[#cancelOnError] as bool; - - return Stream.fromIterable( - [chunkOne, chunkTwo]).listen( - onData, - onDone: onDone, - onError: onError, - cancelOnError: cancelOnError, - ); - }); + response = MockHttpClientResponse( + chunkOne: chunkOne, + chunkTwo: chunkTwo, + ); }); test('Converts an HttpClientResponse with contentLength to bytes', () async { - when(response.contentLength) - .thenReturn(chunkOne.length + chunkTwo.length); + response.contentLength = chunkOne.length + chunkTwo.length; final List bytes = await consolidateHttpClientResponseBytes(response); @@ -55,7 +36,7 @@ void main() { }); test('Converts a compressed HttpClientResponse with contentLength to bytes', () async { - when(response.contentLength).thenReturn(chunkOne.length); + response.contentLength = chunkOne.length; final List bytes = await consolidateHttpClientResponseBytes(response); @@ -63,7 +44,7 @@ void main() { }); test('Converts an HttpClientResponse without contentLength to bytes', () async { - when(response.contentLength).thenReturn(-1); + response.contentLength = -1; final List bytes = await consolidateHttpClientResponseBytes(response); @@ -72,7 +53,7 @@ void main() { test('Notifies onBytesReceived for every chunk of bytes', () async { final int syntheticTotal = (chunkOne.length + chunkTwo.length) * 2; - when(response.contentLength).thenReturn(syntheticTotal); + response.contentLength = syntheticTotal; final List records = []; await consolidateHttpClientResponseBytes( response, @@ -90,33 +71,14 @@ void main() { }); test('forwards errors from HttpClientResponse', () async { - when(response.listen( - any, - onDone: anyNamed('onDone'), - onError: anyNamed('onError'), - cancelOnError: anyNamed('cancelOnError'), - )).thenAnswer((Invocation invocation) { - final void Function(List) onData = invocation.positionalArguments[0] as void Function(List); - final void Function(Object) onError = invocation.namedArguments[#onError] as void Function(Object); - final VoidCallback onDone = invocation.namedArguments[#onDone] as VoidCallback; - final bool cancelOnError = invocation.namedArguments[#cancelOnError] as bool; - - return Stream.fromFuture( - Future.error(Exception('Test Error'))) - .listen( - onData, - onDone: onDone, - onError: onError, - cancelOnError: cancelOnError, - ); - }); - when(response.contentLength).thenReturn(-1); + response = MockHttpClientResponse(error: Exception('Test Error')); + response.contentLength = -1; expect(consolidateHttpClientResponseBytes(response), throwsException); }); test('Propagates error to Future return value if onBytesReceived throws', () async { - when(response.contentLength).thenReturn(-1); + response.contentLength = -1; final Future> result = consolidateHttpClientResponseBytes( response, onBytesReceived: (int cumulative, int total) { @@ -133,45 +95,24 @@ void main() { final List gzippedChunkTwo = gzipped.sublist(gzipped.length ~/ 2); setUp(() { - when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); - when(response.listen( - any, - onDone: anyNamed('onDone'), - onError: anyNamed('onError'), - cancelOnError: anyNamed('cancelOnError'), - )).thenAnswer((Invocation invocation) { - final void Function(List) onData = invocation.positionalArguments[0] as void Function(List); - final void Function(Object) onError = invocation.namedArguments[#onError] as void Function(Object); - final VoidCallback onDone = invocation.namedArguments[#onDone] as VoidCallback; - final bool cancelOnError = invocation.namedArguments[#cancelOnError] as bool; - - return Stream>.fromIterable( - >[gzippedChunkOne, gzippedChunkTwo]).listen( - onData, - onDone: onDone, - onError: onError, - cancelOnError: cancelOnError, - ); - }); + response = MockHttpClientResponse(chunkOne: gzippedChunkOne, chunkTwo: gzippedChunkTwo); + response.compressionState = HttpClientResponseCompressionState.compressed; }); test('Uncompresses GZIP bytes if autoUncompress is true and response.compressionState is compressed', () async { - when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); - when(response.contentLength).thenReturn(gzipped.length); + response.contentLength = gzipped.length; final List bytes = await consolidateHttpClientResponseBytes(response); expect(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); test('returns gzipped bytes if autoUncompress is false and response.compressionState is compressed', () async { - when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); - when(response.contentLength).thenReturn(gzipped.length); + response.contentLength = gzipped.length; final List bytes = await consolidateHttpClientResponseBytes(response, autoUncompress: false); expect(bytes, gzipped); }); test('Notifies onBytesReceived with gzipped numbers', () async { - when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); - when(response.contentLength).thenReturn(gzipped.length); + response.contentLength = gzipped.length; final List records = []; await consolidateHttpClientResponseBytes( response, @@ -190,8 +131,8 @@ void main() { test('Notifies onBytesReceived with expectedContentLength of -1 if response.compressionState is decompressed', () async { final int syntheticTotal = (chunkOne.length + chunkTwo.length) * 2; - when(response.compressionState).thenReturn(HttpClientResponseCompressionState.decompressed); - when(response.contentLength).thenReturn(syntheticTotal); + response.compressionState = HttpClientResponseCompressionState.decompressed; + response.contentLength = syntheticTotal; final List records = []; await consolidateHttpClientResponseBytes( response, @@ -211,4 +152,36 @@ void main() { }); } -class MockHttpClientResponse extends Mock implements HttpClientResponse {} +class MockHttpClientResponse extends Fake implements HttpClientResponse { + MockHttpClientResponse({this.error, this.chunkOne, this.chunkTwo}); + + final dynamic error; + final List chunkOne; + final List chunkTwo; + + @override + int contentLength = 0; + + @override + HttpClientResponseCompressionState compressionState = HttpClientResponseCompressionState.notCompressed; + + @override + StreamSubscription> listen(void Function(List event) onData, {Function onError, void Function() onDone, bool cancelOnError}) { + if (error != null) { + return Stream>.fromFuture( + Future>.error(error)).listen( + onData, + onDone: onDone, + onError: onError, + cancelOnError: cancelOnError, + ); + } + return Stream>.fromIterable( + >[chunkOne, chunkTwo]).listen( + onData, + onDone: onDone, + onError: onError, + cancelOnError: cancelOnError, + ); + } +} diff --git a/packages/flutter/test/rendering/editable_gesture_test.dart b/packages/flutter/test/rendering/editable_gesture_test.dart index 5288c35966..8ef7e921bb 100644 --- a/packages/flutter/test/rendering/editable_gesture_test.dart +++ b/packages/flutter/test/rendering/editable_gesture_test.dart @@ -9,7 +9,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mockito/mockito.dart'; void main() { setUp(() => _GestureBindingSpy()); @@ -58,7 +57,7 @@ class _GestureBindingSpy extends AutomatedTestWidgetsFlutterBinding { PointerRouter get pointerRouter => _testPointerRouter; } -class FakeEditableTextState extends TextSelectionDelegate with Mock { } +class FakeEditableTextState extends TextSelectionDelegate with Fake { } class _PointerRouterSpy extends PointerRouter { int routeCount = 0; diff --git a/packages/flutter/test/widgets/basic_test.dart b/packages/flutter/test/widgets/basic_test.dart index b5995c43c1..5e9133c7f8 100644 --- a/packages/flutter/test/widgets/basic_test.dart +++ b/packages/flutter/test/widgets/basic_test.dart @@ -10,7 +10,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter/rendering.dart'; -import 'package:mockito/mockito.dart'; void main() { group('PhysicalShape', () { @@ -353,8 +352,7 @@ void main() { setUp(() { mockContext = _MockPaintingContext(); - mockCanvas = _MockCanvas(); - when(mockContext.canvas).thenReturn(mockCanvas); + mockCanvas = mockContext.canvas; }); testWidgets('ColoredBox - no size, no child', (WidgetTester tester) async { @@ -372,8 +370,10 @@ void main() { renderColoredBox.paint(mockContext, Offset.zero); - verifyNever(mockCanvas.drawRect(any, any)); - verifyNever(mockContext.paintChild(any, any)); + expect(mockCanvas.rects, isEmpty); + expect(mockCanvas.paints, isEmpty); + expect(mockContext.children, isEmpty); + expect(mockContext.offets, isEmpty); }); testWidgets('ColoredBox - no size, child', (WidgetTester tester) async { @@ -394,8 +394,10 @@ void main() { renderColoredBox.paint(mockContext, Offset.zero); - verifyNever(mockCanvas.drawRect(any, any)); - verify(mockContext.paintChild(renderSizedBox, Offset.zero)).called(1); + expect(mockCanvas.rects, isEmpty); + expect(mockCanvas.paints, isEmpty); + expect(mockContext.children.single, renderSizedBox); + expect(mockContext.offets.single, Offset.zero); }); testWidgets('ColoredBox - size, no child', (WidgetTester tester) async { @@ -405,11 +407,10 @@ void main() { renderColoredBox.paint(mockContext, Offset.zero); - final List drawRect = verify(mockCanvas.drawRect(captureAny, captureAny)).captured; - expect(drawRect.length, 2); - expect(drawRect[0], const Rect.fromLTWH(0, 0, 800, 600)); - expect(drawRect[1].color, colorToPaint); - verifyNever(mockContext.paintChild(any, any)); + expect(mockCanvas.rects.single, const Rect.fromLTWH(0, 0, 800, 600)); + expect(mockCanvas.paints.single.color, colorToPaint); + expect(mockContext.children, isEmpty); + expect(mockContext.offets, isEmpty); }); testWidgets('ColoredBox - size, child', (WidgetTester tester) async { @@ -422,11 +423,10 @@ void main() { renderColoredBox.paint(mockContext, Offset.zero); - final List drawRect = verify(mockCanvas.drawRect(captureAny, captureAny)).captured; - expect(drawRect.length, 2); - expect(drawRect[0], const Rect.fromLTWH(0, 0, 800, 600)); - expect(drawRect[1].color, colorToPaint); - verify(mockContext.paintChild(renderSizedBox, Offset.zero)).called(1); + expect(mockCanvas.rects.single, const Rect.fromLTWH(0, 0, 800, 600)); + expect(mockCanvas.paints.single.color, colorToPaint); + expect(mockContext.children.single, renderSizedBox); + expect(mockContext.offets.single, Offset.zero); }); testWidgets('ColoredBox - properties', (WidgetTester tester) async { @@ -650,5 +650,28 @@ class DoesNotHitRenderBox extends Matcher { } } -class _MockPaintingContext extends Mock implements PaintingContext {} -class _MockCanvas extends Mock implements Canvas {} +class _MockPaintingContext extends Fake implements PaintingContext { + final List children = []; + final List offets = []; + + @override + final _MockCanvas canvas = _MockCanvas(); + + @override + void paintChild(RenderObject child, Offset offset) { + children.add(child); + offets.add(offset); + } +} + +class _MockCanvas extends Fake implements Canvas { + final List rects = []; + final List paints = []; + bool didPaint = false; + + @override + void drawRect(Rect rect, Paint paint) { + rects.add(rect); + paints.add(paint); + } +} diff --git a/packages/flutter/test/widgets/container_test.dart b/packages/flutter/test/widgets/container_test.dart index e33ca2212a..b931b87c4e 100644 --- a/packages/flutter/test/widgets/container_test.dart +++ b/packages/flutter/test/widgets/container_test.dart @@ -7,7 +7,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; -import 'package:mockito/mockito.dart'; import '../rendering/mock_canvas.dart'; @@ -440,10 +439,6 @@ void main() { final RenderBox decoratedBox = tester.renderObject(find.byType(DecoratedBox).last); final PaintingContext context = _MockPaintingContext(); - final Canvas canvas = _MockCanvas(); - int saveCount = 0; - when(canvas.getSaveCount()).thenAnswer((_) => saveCount++); - when(context.canvas).thenReturn(canvas); FlutterError error; try { decoratedBox.paint(context, const Offset(0, 0)); @@ -561,5 +556,19 @@ void main() { }); } -class _MockPaintingContext extends Mock implements PaintingContext {} -class _MockCanvas extends Mock implements Canvas {} +class _MockPaintingContext extends Fake implements PaintingContext { + @override + final Canvas canvas = _MockCanvas(); +} + +class _MockCanvas extends Fake implements Canvas { + int saveCount = 0; + + @override + int getSaveCount() { + return saveCount++; + } + + @override + void drawRect(Rect rect, Paint paint) { } +} diff --git a/packages/flutter/test/widgets/image_headers_test.dart b/packages/flutter/test/widgets/image_headers_test.dart index 3cb4f4b3c1..b03a742b83 100644 --- a/packages/flutter/test/widgets/image_headers_test.dart +++ b/packages/flutter/test/widgets/image_headers_test.dart @@ -9,15 +9,11 @@ import 'dart:io'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mockito/mockito.dart'; import '../painting/image_data.dart'; void main() { final MockHttpClient client = MockHttpClient(); - final MockHttpClientRequest request = MockHttpClientRequest(); - final MockHttpClientResponse response = MockHttpClientResponse(); - final MockHttpHeaders headers = MockHttpHeaders(); testWidgets('Headers', (WidgetTester tester) async { HttpOverrides.runZoned>(() async { @@ -26,30 +22,61 @@ void main() { headers: const {'flutter': 'flutter'}, )); - verify(headers.add('flutter', 'flutter')).called(1); + expect(MockHttpHeaders.headers['flutter'], ['flutter']); }, createHttpClient: (SecurityContext _) { - when(client.getUrl(any)).thenAnswer((_) => Future.value(request)); - when(request.headers).thenReturn(headers); - when(request.close()).thenAnswer((_) => Future.value(response)); - when(response.contentLength).thenReturn(kTransparentImage.length); - when(response.statusCode).thenReturn(HttpStatus.ok); - when(response.listen(any)).thenAnswer((Invocation invocation) { - final void Function(List) onData = invocation.positionalArguments[0] as void Function(List); - final void Function() onDone = invocation.namedArguments[#onDone] as void Function(); - final void Function(Object, [ StackTrace ]) onError = invocation.namedArguments[#onError] as void Function(Object, [ StackTrace ]); - final bool cancelOnError = invocation.namedArguments[#cancelOnError] as bool; - return Stream>.fromIterable(>[kTransparentImage]).listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError); - }); return client; }); }, skip: isBrowser); // https://github.com/flutter/flutter/issues/57187 } -class MockHttpClient extends Mock implements HttpClient {} +class MockHttpClient extends Fake implements HttpClient { + @override + Future getUrl(Uri url) async { + return MockHttpClientRequest(); + } -class MockHttpClientRequest extends Mock implements HttpClientRequest {} + @override + bool autoUncompress = false; +} -class MockHttpClientResponse extends Mock implements HttpClientResponse {} +class MockHttpClientRequest extends Fake implements HttpClientRequest { + @override + final MockHttpHeaders headers = MockHttpHeaders(); -class MockHttpHeaders extends Mock implements HttpHeaders {} + @override + Future close() async { + return MockHttpClientResponse(); + } +} + +class MockHttpClientResponse extends Fake implements HttpClientResponse { + @override + int get contentLength => kTransparentImage.length; + + @override + int get statusCode => HttpStatus.ok; + + @override + HttpClientResponseCompressionState get compressionState => HttpClientResponseCompressionState.decompressed; + + @override + StreamSubscription> listen(void Function(List event) onData, {Function onError, void Function() onDone, bool cancelOnError}) { + return Stream>.fromIterable(>[kTransparentImage]).listen( + onData, + onDone: onDone, + onError: onError, + cancelOnError: cancelOnError, + ); + } +} + +class MockHttpHeaders extends Fake implements HttpHeaders { + static final Map> headers = >{}; + + @override + void add(String key, Object value, { bool preserveHeaderCase = false }) { + headers[key] ??= []; + headers[key].add(value.toString()); + } +} diff --git a/packages/flutter_test/lib/src/test_compat.dart b/packages/flutter_test/lib/src/test_compat.dart index 0ab2ba59e6..0fd1322971 100644 --- a/packages/flutter_test/lib/src/test_compat.dart +++ b/packages/flutter_test/lib/src/test_compat.dart @@ -21,6 +21,9 @@ import 'package:test_api/src/backend/state.dart'; // ignore: implementation_impo // ignore: deprecated_member_use import 'package:test_api/test_api.dart'; +// ignore: deprecated_member_use +export 'package:test_api/fake.dart' show Fake; + Declarer _localDeclarer; Declarer get _declarer { final Declarer declarer = Zone.current[#test.declarer] as Declarer;