From 8beee479af1a8e157a9f8dd619c7d0d33cae6bf5 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 24 May 2021 11:59:03 -0700 Subject: [PATCH] [flutter_tools] remove more mocks from resident_runner tests (#83155) --- .../general.shard/resident_runner_test.dart | 282 ++++++------------ 1 file changed, 87 insertions(+), 195 deletions(-) diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 3aeb68ead8..8aca175550 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -11,6 +11,7 @@ import 'package:file/memory.dart'; import 'package:file_testing/file_testing.dart'; import 'package:flutter_tools/src/application_package.dart'; import 'package:flutter_tools/src/artifacts.dart'; +import 'package:flutter_tools/src/asset.dart'; import 'package:flutter_tools/src/base/command_help.dart'; import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/dds.dart'; @@ -143,11 +144,10 @@ void main() { Testbed testbed; MockFlutterDevice mockFlutterDevice; MockVMService mockVMService; - MockDevFS mockDevFS; + FakeDevFS mockDevFS; ResidentRunner residentRunner; FakeDevice mockDevice; FakeVmServiceHost fakeVmServiceHost; - MockDevtoolsLauncher mockDevtoolsLauncher; setUp(() { testbed = Testbed(setup: () { @@ -169,15 +169,7 @@ void main() { mockFlutterDevice = MockFlutterDevice(); mockDevice = FakeDevice(); mockVMService = MockVMService(); - mockDevFS = MockDevFS(); - mockDevtoolsLauncher = MockDevtoolsLauncher(); - - // DevFS Mocks - when(mockDevFS.lastCompiled).thenReturn(DateTime(2000)); - when(mockDevFS.sources).thenReturn([]); - when(mockDevFS.baseUri).thenReturn(Uri()); - when(mockDevFS.destroy()).thenAnswer((Invocation invocation) async { }); - when(mockDevFS.assetPathsToEvict).thenReturn({}); + mockDevFS = FakeDevFS(); // FlutterDevice Mocks. when(mockFlutterDevice.updateDevFS( invalidatedFiles: anyNamed('invalidatedFiles'), @@ -242,9 +234,7 @@ void main() { expect((await connectionInfo).baseUri, 'foo://bar'); expect(futureAppStart.isCompleted, true); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner suppresses errors for the initial compilation', () => testbed.run(() async { globals.fs.file(globals.fs.path.join('lib', 'main.dart')) @@ -294,9 +284,7 @@ void main() { fs: anyNamed('fs'), )).called(1); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); // Regression test for https://github.com/flutter/flutter/issues/60613 testUsingContext('ResidentRunner calls appFailedToStart if initial compilation fails', () => testbed.run(() async { @@ -438,9 +426,7 @@ void main() { fs: anyNamed('fs'), )).called(1); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner can attach to device successfully with --fast-start', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -512,9 +498,7 @@ void main() { expect((await connectionInfo).baseUri, 'foo://bar'); expect(futureAppStart.isCompleted, true); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner can handle an RPC exception from hot reload', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -560,9 +544,7 @@ void main() { expect(fakeVmServiceHost.hasRemainingExpectations, false); }, overrides: { Usage: () => TestUsage(), - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner fails its operation if the device initialization is not complete', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -632,9 +614,7 @@ void main() { expect(fakeVmServiceHost.hasRemainingExpectations, false); }, overrides: { Usage: () => TestUsage(), - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner reports hot reload event with null safety analytics', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -693,7 +673,6 @@ void main() { )); expect(fakeVmServiceHost.hasRemainingExpectations, false); }, overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, Usage: () => TestUsage(), })); @@ -755,9 +734,7 @@ void main() { expect(result.code, 0); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner reports error with missing entrypoint file', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -833,9 +810,7 @@ void main() { expect(testLogger.errorText, contains('The entrypoint file (i.e. the file with main())')); expect(result.fatal, false); expect(result.code, 0); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner resets compilation time on reload reject', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -914,10 +889,8 @@ void main() { expect(result.fatal, false); expect(result.message, contains('Reload rejected: Failed to hot reload')); // contains error message from reload report. expect(result.code, 1); - verify(mockDevFS.resetLastCompiled()).called(1); // compilation time is reset. - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + expect(mockDevFS.lastCompiled, null); + })); testUsingContext('ResidentRunner can send target platform to analytics from hot reload', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -981,9 +954,7 @@ void main() { expect(event.parameters.hotEventTargetPlatform, getNameForTargetPlatform(TargetPlatform.android_arm)); }, overrides: { Usage: () => TestUsage(), - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner can perform fast reassemble', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1044,27 +1015,11 @@ void main() { target: 'main.dart', devtoolsHandler: createNoOpHandler, ); - when(mockDevFS.update( - mainUri: anyNamed('mainUri'), - target: anyNamed('target'), - bundle: anyNamed('bundle'), - firstBuildTime: anyNamed('firstBuildTime'), - bundleFirstUpload: anyNamed('bundleFirstUpload'), - generator: anyNamed('generator'), - fullRestart: anyNamed('fullRestart'), - dillOutputPath: anyNamed('dillOutputPath'), - trackWidgetCreation: anyNamed('trackWidgetCreation'), - projectRootPath: anyNamed('projectRootPath'), - pathToReload: anyNamed('pathToReload'), - invalidatedFiles: anyNamed('invalidatedFiles'), - packageConfig: anyNamed('packageConfig'), - )).thenAnswer((Invocation invocation) async { - return UpdateFSReport( - success: true, - fastReassembleClassName: 'FOO', - invalidatedSourcesCount: 1, - ); - }); + mockDevFS.nextUpdateReport = UpdateFSReport( + success: true, + fastReassembleClassName: 'FOO', + invalidatedSourcesCount: 1, + ); final Completer futureConnectionInfo = Completer.sync(); final Completer futureAppStart = Completer.sync(); @@ -1090,9 +1045,7 @@ void main() { ProjectFileInvalidator: () => FakeProjectFileInvalidator(), Usage: () => TestUsage(), FeatureFlags: () => TestFeatureFlags(isSingleWidgetReloadEnabled: true), - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner can send target platform to analytics from full restart', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1153,9 +1106,7 @@ void main() { expect(fakeVmServiceHost.hasRemainingExpectations, false); }, overrides: { Usage: () => TestUsage(), - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner can remove breakpoints from paused isolate during hot restart', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1222,9 +1173,7 @@ void main() { expect(result.isOk, true); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner will alternative the name of the dill file uploaded for a hot restart', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1347,9 +1296,7 @@ void main() { await residentRunner.restart(fullRestart: true); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner Can handle an RPC exception from hot restart', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1395,9 +1342,7 @@ void main() { expect(fakeVmServiceHost.hasRemainingExpectations, false); }, overrides: { Usage: () => TestUsage(), - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ResidentRunner uses temp directory when there is no output dill path', () => testbed.run(() { fakeVmServiceHost = FakeVmServiceHost(requests: []); @@ -1684,21 +1629,6 @@ void main() { FlutterVersion: () => FakeFlutterVersion(engineRevision: 'abcdefg') })); - testUsingContext('ResidentRunner ignores DevToolsLauncher when attaching with enableDevTools: false', () => testbed.run(() async { - fakeVmServiceHost = FakeVmServiceHost(requests: [ - listViews, - listViews, - setAssetBundlePath, - ]); - final Future result = residentRunner.attach(enableDevTools: false); - expect(await result, 0); - - // Verify DevTools was served. - verifyNever(mockDevtoolsLauncher.serve()); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); - testUsingContext('ResidentRunner ignores DevtoolsLauncher when attaching with enableDevTools: false - cold mode', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ listViews, @@ -1723,12 +1653,7 @@ void main() { final Future result = residentRunner.attach(enableDevTools: false); expect(await result, 0); - - // Verify DevTools was served. - verifyNever(mockDevtoolsLauncher.serve()); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('FlutterDevice will not exit a paused isolate', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1906,9 +1831,7 @@ void main() { expect(fakeVmServiceHost.hasRemainingExpectations, false); expect(await globals.fs.file('foo').readAsString(), testUri.toString()); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('HotRunner copies compiled app.dill to cache during startup', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1936,9 +1859,7 @@ void main() { await residentRunner.run(enableDevTools: true); expect(await globals.fs.file(globals.fs.path.join('build', 'cache.dill')).readAsString(), 'ABC'); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('HotRunner copies compiled app.dill to cache during startup with dart defines', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -1974,9 +1895,7 @@ void main() { expect(await globals.fs.file(globals.fs.path.join( 'build', '187ef4436122d1cc2f40dc2b92f0eba0.cache.dill')).readAsString(), 'ABC'); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('HotRunner copies compiled app.dill to cache during startup with null safety', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -2012,9 +1931,7 @@ void main() { expect(await globals.fs.file(globals.fs.path.join( 'build', '3416d3007730479552122f01c01e326d.cache.dill')).readAsString(), 'ABC'); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('HotRunner does not copy app.dill if a dillOutputPath is given', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -2043,9 +1960,7 @@ void main() { await residentRunner.run(enableDevTools: true); expect(globals.fs.file(globals.fs.path.join('build', 'cache.dill')), isNot(exists)); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('HotRunner copies compiled app.dill to cache during startup with --track-widget-creation', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -2078,9 +1993,7 @@ void main() { await residentRunner.run(enableDevTools: true); expect(await globals.fs.file(globals.fs.path.join('build', 'cache.dill.track.dill')).readAsString(), 'ABC'); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('HotRunner calls device dispose', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -2107,9 +2020,7 @@ void main() { await residentRunner.run(); expect(mockDevice.disposed, true); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('HotRunner handles failure to write vmservice file', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ @@ -2139,9 +2050,7 @@ void main() { expect(fakeVmServiceHost.hasRemainingExpectations, false); }, overrides: { FileSystem: () => ThrowingForwardingFileSystem(MemoryFileSystem.test()), - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('ColdRunner writes vm service file when providing debugging option', () => testbed.run(() async { @@ -2168,9 +2077,7 @@ void main() { expect(await globals.fs.file('foo').readAsString(), testUri.toString()); expect(fakeVmServiceHost.hasRemainingExpectations, false); - }), overrides: { - DevtoolsLauncher: () => mockDevtoolsLauncher, - }); + })); testUsingContext('FlutterDevice uses dartdevc configuration when targeting web', () async { fakeVmServiceHost = FakeVmServiceHost(requests: []); @@ -2231,7 +2138,7 @@ void main() { Artifacts: () => Artifacts.test(), FileSystem: () => MemoryFileSystem.test(), ProcessManager: () => FakeProcessManager.any(), - }, skip: true); // TODO(jonahwilliams): null safe autodetection does not work on the web. + }); testUsingContext('FlutterDevice passes flutter-widget-cache flag when feature is enabled', () async { fakeVmServiceHost = FakeVmServiceHost(requests: []); @@ -2282,69 +2189,6 @@ void main() { FeatureFlags: () => TestFeatureFlags(isExperimentalInvalidationStrategyEnabled: true) }); - testUsingContext('connect sets up log reader', () => testbed.run(() async { - fakeVmServiceHost = FakeVmServiceHost(requests: []); - final MockDevice mockDevice = MockDevice(); - final MockDartDevelopmentService mockDds = MockDartDevelopmentService(); - final MockDeviceLogReader mockLogReader = MockDeviceLogReader(); - final Completer noopCompleter = Completer(); - when(mockDevice.getLogReader(app: anyNamed('app'))).thenReturn(mockLogReader); - when(mockDevice.dds).thenReturn(mockDds); - when(mockDds.startDartDevelopmentService(any, any, any, any, logger: anyNamed('logger'))).thenReturn(null); - when(mockDds.uri).thenReturn(Uri.parse('http://localhost:8181')); - when(mockDds.done).thenAnswer((_) => noopCompleter.future); - - final TestFlutterDevice flutterDevice = TestFlutterDevice( - mockDevice, - observatoryUris: Stream.value(testUri), - ); - - await flutterDevice.connect(allowExistingDdsInstance: true); - verify(mockLogReader.connectedVMService = mockVMService); - }, overrides: { - VMServiceConnector: () => (Uri httpUri, { - ReloadSources reloadSources, - Restart restart, - CompileExpression compileExpression, - GetSkSLMethod getSkSLMethod, - PrintStructuredErrorLogMethod printStructuredErrorLogMethod, - io.CompressionOptions compression, - Device device, - Logger logger, - }) async => mockVMService, - })); - - testUsingContext('FlutterDevice handles existing DDS instance', () => testbed.run(() async { - fakeVmServiceHost = FakeVmServiceHost(requests: []); - final MockDevice mockDevice = MockDevice(); - final MockDartDevelopmentService mockDds = MockDartDevelopmentService(); - final MockDeviceLogReader mockLogReader = MockDeviceLogReader(); - final Completer noopCompleter = Completer(); - when(mockDevice.getLogReader(app: anyNamed('app'))).thenReturn(mockLogReader); - when(mockDevice.dds).thenReturn(mockDds); - when(mockDds.startDartDevelopmentService(any, any, any, any, logger: anyNamed('logger'))).thenThrow(FakeDartDevelopmentServiceException()); - when(mockDds.uri).thenReturn(Uri.parse('http://localhost:1234')); - when(mockDds.done).thenAnswer((_) => noopCompleter.future); - - final TestFlutterDevice flutterDevice = TestFlutterDevice( - mockDevice, - observatoryUris: Stream.value(testUri), - ); - await flutterDevice.connect(allowExistingDdsInstance: true); - verify(mockLogReader.connectedVMService = mockVMService); - }, overrides: { - VMServiceConnector: () => (Uri httpUri, { - ReloadSources reloadSources, - Restart restart, - CompileExpression compileExpression, - GetSkSLMethod getSkSLMethod, - PrintStructuredErrorLogMethod printStructuredErrorLogMethod, - io.CompressionOptions compression, - Device device, - Logger logger, - }) async => mockVMService, - })); - testUsingContext('Handle existing VM service clients DDS error', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: []); final FakeDevice mockDevice = FakeDevice() @@ -2454,13 +2298,9 @@ void main() { } class MockFlutterDevice extends Mock implements FlutterDevice {} -class MockDartDevelopmentService extends Mock implements DartDevelopmentService {} class MockVMService extends Mock implements FlutterVmService {} class MockDevFS extends Mock implements DevFS {} -class MockDeviceLogReader extends Mock implements DeviceLogReader {} -class MockDevtoolsLauncher extends Mock implements DevtoolsLauncher {} class MockResidentCompiler extends Mock implements ResidentCompiler {} -class MockDevice extends Mock implements Device {} class FakeDartDevelopmentServiceException implements dds.DartDevelopmentServiceException { FakeDartDevelopmentServiceException({this.message = defaultMessage}); @@ -2643,3 +2483,55 @@ class FakeDevice extends Fake implements Device { @override DevicePortForwarder portForwarder = const NoOpDevicePortForwarder(); } + +class FakeDevFS extends Fake implements DevFS { + @override + DateTime lastCompiled = DateTime(2000); + + @override + PackageConfig lastPackageConfig = PackageConfig.empty; + + @override + List sources = []; + + @override + Uri baseUri = Uri(); + + @override + Future destroy() async { } + + @override + Set assetPathsToEvict = {}; + + UpdateFSReport nextUpdateReport = UpdateFSReport(success: true); + + @override + Future create() async { + return Uri(); + } + + @override + void resetLastCompiled() { + lastCompiled = null; + } + + @override + Future update({ + @required Uri mainUri, + @required ResidentCompiler generator, + @required bool trackWidgetCreation, + @required String pathToReload, + @required List invalidatedFiles, + @required PackageConfig packageConfig, + @required String dillOutputPath, + DevFSWriter devFSWriter, + String target, + AssetBundle bundle, + DateTime firstBuildTime, + bool bundleFirstUpload = false, + bool fullRestart = false, + String projectRootPath, + }) async { + return nextUpdateReport; + } +}