diff --git a/packages/flutter_tools/lib/src/build_runner/devfs_web.dart b/packages/flutter_tools/lib/src/build_runner/devfs_web.dart index d56bd97999..cd5c0856a5 100644 --- a/packages/flutter_tools/lib/src/build_runner/devfs_web.dart +++ b/packages/flutter_tools/lib/src/build_runner/devfs_web.dart @@ -438,7 +438,8 @@ class WebDevFS implements DevFS { Set get assetPathsToEvict => const {}; @override - Uri get baseUri => null; + Uri get baseUri => _baseUri; + Uri _baseUri; @override Future create() async { @@ -451,7 +452,8 @@ class WebDevFS implements DevFS { entrypoint, testMode: testMode, ); - return Uri.parse('http://$hostname:$port'); + _baseUri = Uri.parse('http://$hostname:$port'); + return _baseUri; } @override diff --git a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart index de14319cf0..0750c53522 100644 --- a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart +++ b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart @@ -71,7 +71,7 @@ const String kExitMessage = 'Failed to establish connection with the applicatio /// A hot-runner which handles browser specific delegation. abstract class ResidentWebRunner extends ResidentRunner { ResidentWebRunner( - this.device, { + FlutterDevice device, { String target, @required this.flutterProject, @required bool ipv6, @@ -79,14 +79,14 @@ abstract class ResidentWebRunner extends ResidentRunner { bool stayResident = true, @required this.dartDefines, }) : super( - [], + [device], target: target ?? globals.fs.path.join('lib', 'main.dart'), debuggingOptions: debuggingOptions, ipv6: ipv6, stayResident: stayResident, ); - final FlutterDevice device; + FlutterDevice get device => flutterDevices.first; final FlutterProject flutterProject; final List dartDefines; DateTime firstBuildTime; @@ -249,6 +249,12 @@ abstract class ResidentWebRunner extends ResidentRunner { } } + @override + Future stopEchoingDeviceLog() async { + // Do nothing for ResidentWebRunner + await device.stopEchoingDeviceLog(); + } + @override Future debugDumpSemanticsTreeInInverseHitTestOrder() async { try { @@ -705,4 +711,10 @@ class _ResidentWebRunner extends ResidentWebRunner { await cleanupAtFinish(); return 0; } + + @override + Future exitApp() async { + await device.exitApps(); + appFinished(); + } } diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index a747b3220f..6ea5281c56 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -22,6 +22,7 @@ import '../globals.dart' as globals; import '../project.dart'; import '../resident_runner.dart'; import '../runner/flutter_command.dart' show FlutterCommandResult; +import '../web/web_runner.dart'; import 'run.dart'; /// Runs integration (a.k.a. end-to-end) tests. @@ -142,17 +143,11 @@ class DriveCommand extends RunCommandBase { } String observatoryUri; + ResidentRunner residentRunner; final bool isWebPlatform = await device.targetPlatform == TargetPlatform.web_javascript; if (argResults['use-existing-app'] == null) { globals.printStatus('Starting application: $targetFile'); - if (isWebPlatform) { - throwToolExit( - 'Flutter Driver (web) does not support running without use-existing-app.\n' - 'Please launch your application beforehand and connects via use-existing-app.' - ); - } - if (getBuildInfo().isRelease && !isWebPlatform) { // This is because we need VM service to be able to drive the app. // For Flutter Web, testing in release mode is allowed. @@ -164,7 +159,53 @@ class DriveCommand extends RunCommandBase { ); } - final LaunchResult result = await appStarter(this); + if (isWebPlatform && getBuildInfo().isDebug) { + // TODO(angjieli): remove this once running against + // target under test_driver in debug mode is supported + throwToolExit( + 'Flutter Driver web does not support running in debug mode.\n' + '\n' + 'Use --profile mode for testing application performance.\n' + 'Use --release mode for testing correctness (with assertions).' + ); + } + + Uri webUri; + + if (isWebPlatform) { + // Start Flutter web application for current test + final FlutterProject flutterProject = FlutterProject.current(); + final FlutterDevice flutterDevice = await FlutterDevice.create( + device, + flutterProject: flutterProject, + trackWidgetCreation: boolArg('track-widget-creation'), + target: targetFile, + buildMode: getBuildMode() + ); + residentRunner = webRunnerFactory.createWebRunner( + flutterDevice, + target: targetFile, + flutterProject: flutterProject, + ipv6: ipv6, + debuggingOptions: DebuggingOptions.enabled(getBuildInfo()), + stayResident: false, + dartDefines: dartDefines, + urlTunneller: null, + ); + final Completer appStartedCompleter = Completer.sync(); + final int result = await residentRunner.run( + appStartedCompleter: appStartedCompleter, + route: route, + ); + if (result != 0) { + throwToolExit(null, exitCode: result); + } + // Wait until the app is started. + await appStartedCompleter.future; + webUri = residentRunner.uri; + } + + final LaunchResult result = await appStarter(this, webUri); if (result == null) { throwToolExit('Application failed to start. Will not run test. Quitting.', exitCode: 1); } @@ -243,6 +284,7 @@ $ex } throw Exception('Unable to run test: $error\n$stackTrace'); } finally { + await residentRunner?.exit(); await driver?.quit(); if (boolArg('keep-app-running') ?? (argResults['use-existing-app'] != null)) { globals.printStatus('Leaving the application running.'); @@ -327,14 +369,14 @@ Future findTargetDevice() async { } /// Starts the application on the device given command configuration. -typedef AppStarter = Future Function(DriveCommand command); +typedef AppStarter = Future Function(DriveCommand command, Uri webUri); AppStarter appStarter = _startApp; // (mutable for testing) void restoreAppStarter() { appStarter = _startApp; } -Future _startApp(DriveCommand command) async { +Future _startApp(DriveCommand command, Uri webUri) async { final String mainPath = findMainDartFile(command.targetFile); if (await globals.fs.type(mainPath) != FileSystemEntityType.file) { globals.printError('Tried to run $mainPath, but that file does not exist.'); @@ -360,6 +402,15 @@ Future _startApp(DriveCommand command) async { platformArgs['trace-startup'] = command.traceStartup; } + if (webUri != null) { + platformArgs['uri'] = webUri.toString(); + if (!command.getBuildInfo().isDebug) { + // For web device, startApp will be triggered twice + // and it will error out for chrome the second time. + platformArgs['no-launch-chrome'] = true; + } + } + globals.printTrace('Starting application.'); // Forward device log messages to the terminal window running the "drive" command. diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 230adcbc26..b58487e1f3 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -680,6 +680,13 @@ abstract class ResidentRunner { bool get isRunningRelease => debuggingOptions.buildInfo.isRelease; bool get supportsServiceProtocol => isRunningDebug || isRunningProfile; + // Returns the Uri of the first connected device for mobile, + // and only connected device for web. + // + // Would be null if there is no device connected or + // there is no devFS associated with the first device. + Uri get uri => flutterDevices.first?.devFS?.baseUri; + /// Returns [true] if the resident runner exited after invoking [exit()]. bool get exited => _exited; diff --git a/packages/flutter_tools/lib/src/web/web_device.dart b/packages/flutter_tools/lib/src/web/web_device.dart index cf48542e3b..eb14642f25 100644 --- a/packages/flutter_tools/lib/src/web/web_device.dart +++ b/packages/flutter_tools/lib/src/web/web_device.dart @@ -134,18 +134,20 @@ class ChromeDevice extends Device { // See [ResidentWebRunner.run] in flutter_tools/lib/src/resident_web_runner.dart // for the web initialization and server logic. final String url = platformArgs['uri'] as String; - _chrome = await chromeLauncher.launch( - url, - dataDir: globals.fs.currentDirectory - .childDirectory('.dart_tool') - .childDirectory('chrome-device'), - headless: debuggingOptions.webRunHeadless, - debugPort: debuggingOptions.webBrowserDebugPort, - ); + final bool launchChrome = platformArgs['no-launch-chrome'] != true; + if (launchChrome) { + _chrome = await chromeLauncher.launch( + url, + dataDir: globals.fs.currentDirectory + .childDirectory('.dart_tool') + .childDirectory('chrome-device'), + headless: debuggingOptions.webRunHeadless, + debugPort: debuggingOptions.webBrowserDebugPort, + ); + } - globals.logger.sendEvent('app.webLaunchUrl', {'url': url, 'launched': true}); - - return LaunchResult.succeeded(observatoryUri: null); + globals.logger.sendEvent('app.webLaunchUrl', {'url': url, 'launched': launchChrome}); + return LaunchResult.succeeded(observatoryUri: url != null ? Uri.parse(url): null); } @override @@ -268,7 +270,7 @@ class WebServerDevice extends Device { globals.printStatus('$mainPath is being served at $url', emphasis: true); } globals.logger.sendEvent('app.webLaunchUrl', {'url': url, 'launched': false}); - return LaunchResult.succeeded(observatoryUri: null); + return LaunchResult.succeeded(observatoryUri: url != null ? Uri.parse(url): null); } @override diff --git a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart index 67415e26e5..858e5366a3 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart @@ -45,7 +45,7 @@ void main() { fs.file('pubspec.yaml')..createSync(); fs.file('.packages').createSync(); setExitFunctionForTests(); - appStarter = (DriveCommand command) { + appStarter = (DriveCommand command, Uri webUri) { throw 'Unexpected call to appStarter'; }; testRunner = (List testArgs, Map environment) { @@ -91,7 +91,7 @@ void main() { testUsingContext('returns 1 when app fails to run', () async { testDeviceManager.addDevice(MockDevice()); - appStarter = expectAsync1((DriveCommand command) async => null); + appStarter = expectAsync2((DriveCommand command, Uri webUri) async => null); final String testApp = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e.dart'); final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart'); @@ -170,7 +170,7 @@ void main() { final String testApp = globals.fs.path.join(tempDir.path, 'test', 'e2e.dart'); final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart'); - appStarter = expectAsync1((DriveCommand command) async { + appStarter = expectAsync2((DriveCommand command, Uri webUri) async { return LaunchResult.succeeded(); }); testRunner = expectAsync2((List testArgs, Map environment) async { @@ -207,7 +207,7 @@ void main() { final String testApp = globals.fs.path.join(tempDir.path, 'test', 'e2e.dart'); final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart'); - appStarter = expectAsync1((DriveCommand command) async { + appStarter = expectAsync2((DriveCommand command, Uri webUri) async { return LaunchResult.succeeded(); }); testRunner = (List testArgs, Map environment) async { diff --git a/packages/flutter_tools/test/general.shard/hot_test.dart b/packages/flutter_tools/test/general.shard/hot_test.dart index b12ef6245f..ba4552b4db 100644 --- a/packages/flutter_tools/test/general.shard/hot_test.dart +++ b/packages/flutter_tools/test/general.shard/hot_test.dart @@ -181,8 +181,10 @@ void main() { FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false, buildMode: BuildMode.debug)..devFS = mockDevFs, FlutterDevice(mockHotDevice, generator: residentCompiler, trackWidgetCreation: false, buildMode: BuildMode.debug)..devFS = mockDevFs, ]; - final OperationResult result = await HotRunner(devices).restart(fullRestart: true); + final HotRunner hotRunner = HotRunner(devices); + final OperationResult result = await hotRunner.restart(fullRestart: true); // Expect hot restart was successful. + expect(hotRunner.uri, mockDevFs.baseUri); expect(result.isOk, true); expect(result.message, isNot('hotRestart not supported')); }, overrides: { @@ -216,8 +218,10 @@ void main() { final List devices = [ FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false, buildMode: BuildMode.debug)..devFS = mockDevFs, ]; - final OperationResult result = await HotRunner(devices).restart(fullRestart: true); + final HotRunner hotRunner = HotRunner(devices); + final OperationResult result = await hotRunner.restart(fullRestart: true); // Expect hot restart successful. + expect(hotRunner.uri, mockDevFs.baseUri); expect(result.isOk, true); expect(result.message, isNot('setupHotRestart failed')); }, overrides: { diff --git a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart index b153a3642d..7641d18e88 100644 --- a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart @@ -119,6 +119,7 @@ void main() { when(mockDebugConnection.uri).thenReturn('ws://127.0.0.1/abcd/'); when(mockFlutterDevice.devFS).thenReturn(mockWebDevFS); when(mockWebDevFS.sources).thenReturn([]); + when(mockWebDevFS.baseUri).thenReturn(Uri.parse('http://localhost:12345')); when(mockFlutterDevice.generator).thenReturn(mockResidentCompiler); when(mockChrome.chromeConnection).thenReturn(mockChromeConnection); when(mockChromeConnection.getTab(any)).thenAnswer((Invocation invocation) async { @@ -149,6 +150,7 @@ void main() { })); test('runner with web server device supports debugging with --start-paused', () => testbed.run(() { + _setupMocks(); when(mockFlutterDevice.device).thenReturn(WebServerDevice()); final ResidentRunner profileResidentWebRunner = DwdsWebRunnerFactory().createWebRunner( mockFlutterDevice, @@ -160,6 +162,7 @@ void main() { urlTunneller: null, ); + expect(profileResidentWebRunner.uri, mockWebDevFS.baseUri); expect(profileResidentWebRunner.debuggingEnabled, true); }));