diff --git a/packages/flutter_tools/test/integration/expression_evaluation_test.dart b/packages/flutter_tools/test/integration/expression_evaluation_test.dart index 6610ee6047..15a6c4ace7 100644 --- a/packages/flutter_tools/test/integration/expression_evaluation_test.dart +++ b/packages/flutter_tools/test/integration/expression_evaluation_test.dart @@ -11,6 +11,7 @@ import 'package:vm_service_lib/vm_service_lib.dart'; import '../src/common.dart'; import 'test_data/basic_project.dart'; +import 'test_data/tests_project.dart'; import 'test_driver.dart'; import 'test_utils.dart'; @@ -79,6 +80,47 @@ void main() { await evaluateComplexReturningExpressions(_flutter); }); }, timeout: const Timeout.factor(6)); + + group('flutter test expression evaluation', () { + Directory tempDir; + final TestsProject _project = TestsProject(); + FlutterTestTestDriver _flutter; + + setUp(() async { + tempDir = createResolvedTempDirectorySync(); + await _project.setUpIn(tempDir); + _flutter = FlutterTestTestDriver(tempDir); + }); + + tearDown(() async { + await _flutter.quit(); + tryToDelete(tempDir); + }); + + test('can evaluate trivial expressions in a test', () async { + await _flutter.test( + withDebugger: true, + beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine), + ); + await evaluateTrivialExpressions(_flutter); + }); + + test('can evaluate complex expressions in a test', () async { + await _flutter.test( + withDebugger: true, + beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine), + ); + await evaluateComplexExpressions(_flutter); + }); + + test('can evaluate expressions returning complex objects in a test', () async { + await _flutter.test( + withDebugger: true, + beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine), + ); + await evaluateComplexReturningExpressions(_flutter); + }); + }, timeout: const Timeout.factor(6)); } Future evaluateTrivialExpressions(FlutterTestDriver flutter) async { diff --git a/packages/flutter_tools/test/integration/test_data/tests_project.dart b/packages/flutter_tools/test/integration/test_data/tests_project.dart new file mode 100644 index 0000000000..f5067aff49 --- /dev/null +++ b/packages/flutter_tools/test/integration/test_data/tests_project.dart @@ -0,0 +1,55 @@ +// Copyright 2018 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + + +import 'package:file/file.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; + +import '../test_utils.dart'; +import 'project.dart'; + +class TestsProject extends Project { + @override + final String pubspec = ''' + name: test + environment: + sdk: ">=2.0.0-dev.68.0 <3.0.0" + + dependencies: + flutter: + sdk: flutter + + dev_dependencies: + flutter_test: + sdk: flutter + '''; + + @override + String get main => '// Unused'; + + final String testContent = r''' + import 'package:flutter_test/flutter_test.dart'; + + void main() { + testWidgets('Hello world test', (WidgetTester tester) async { + expect(true, isTrue); // BREAKPOINT + }); + } + '''; + + @override + Future setUpIn(Directory dir) { + this.dir = dir; + writeFile(testFilePath, testContent); + return super.setUpIn(dir); + } + + String get testFilePath => fs.path.join(dir.path, 'test', 'test.dart'); + + @override + Uri get breakpointUri => Uri.file(testFilePath); + + @override + int get breakpointLine => lineContaining(main, '// BREAKPOINT'); +} diff --git a/packages/flutter_tools/test/integration/test_driver.dart b/packages/flutter_tools/test/integration/test_driver.dart index 18a9e84b4c..bb10ec9097 100644 --- a/packages/flutter_tools/test/integration/test_driver.dart +++ b/packages/flutter_tools/test/integration/test_driver.dart @@ -55,6 +55,7 @@ abstract class FlutterTestDriver { Future _setupProcess( List args, { + String script, bool withDebugger = false, bool pauseOnExceptions = false, File pidFile, @@ -66,6 +67,9 @@ abstract class FlutterTestDriver { if (pidFile != null) { args.addAll(['--pid-file', pidFile.path]); } + if (script != null) { + args.add(script); + } _debugPrint('Spawning flutter $args in ${_projectFolder.path}'); const ProcessManager _processManager = LocalProcessManager(); @@ -93,6 +97,32 @@ abstract class FlutterTestDriver { _stderr.stream.listen(_debugPrint); } + Future connectToVmService({bool pauseOnExceptions = false}) async { + _vmService = await vmServiceConnectUri(_vmServiceWsUri.toString()); + _vmService.onSend.listen((String s) => _debugPrint('==> $s')); + _vmService.onReceive.listen((String s) => _debugPrint('<== $s')); + await Future.wait(>[ + _vmService.streamListen('Isolate'), + _vmService.streamListen('Debug'), + ]); + + // On hot restarts, the isolate ID we have for the Flutter thread will + // exit so we need to invalidate our cached ID. + _vmService.onIsolateEvent.listen((Event event) { + if (event.kind == EventKind.kIsolateExit && event.isolate.id == _flutterIsolateId) { + _flutterIsolateId = null; + } + }); + + // Because we start paused, resume so the app is in a "running" state as + // expected by tests. Tests will reload/restart as required if they need + // to hit breakpoints, etc. + await waitForPause(); + if (pauseOnExceptions) { + await _vmService.setExceptionPauseMode(await _getFlutterIsolateId(), ExceptionPauseMode.kUnhandled); + } + } + Future quit() => _killGracefully(); Future _killGracefully() async { @@ -326,12 +356,14 @@ class FlutterRunTestDriver extends FlutterTestDriver { @override Future _setupProcess( List args, { + String script, bool withDebugger = false, bool pauseOnExceptions = false, File pidFile, }) async { await super._setupProcess( args, + script: script, withDebugger: withDebugger, pauseOnExceptions: pauseOnExceptions, pidFile: pidFile, @@ -353,30 +385,7 @@ class FlutterRunTestDriver extends FlutterTestDriver { timeout: appStartTimeout); final String wsUriString = debugPort['params']['wsUri']; _vmServiceWsUri = Uri.parse(wsUriString); - _vmService = - await vmServiceConnectUri(_vmServiceWsUri.toString()); - _vmService.onSend.listen((String s) => _debugPrint('==> $s')); - _vmService.onReceive.listen((String s) => _debugPrint('<== $s')); - await Future.wait(>[ - _vmService.streamListen('Isolate'), - _vmService.streamListen('Debug'), - ]); - - // On hot restarts, the isolate ID we have for the Flutter thread will - // exit so we need to invalidate our cached ID. - _vmService.onIsolateEvent.listen((Event event) { - if (event.kind == EventKind.kIsolateExit && event.isolate.id == _flutterIsolateId) { - _flutterIsolateId = null; - } - }); - - // Because we start paused, resume so the app is in a "running" state as - // expected by tests. Tests will reload/restart as required if they need - // to hit breakpoints, etc. - await waitForPause(); - if (pauseOnExceptions) { - await _vmService.setExceptionPauseMode(await _getFlutterIsolateId(), ExceptionPauseMode.kUnhandled); - } + await connectToVmService(pauseOnExceptions: pauseOnExceptions); await resume(wait: false); } @@ -496,6 +505,81 @@ class FlutterRunTestDriver extends FlutterTestDriver { } } +class FlutterTestTestDriver extends FlutterTestDriver { + FlutterTestTestDriver(Directory _projectFolder, {String logPrefix}): + super(_projectFolder, logPrefix: logPrefix); + + Future test({ + String testFile = 'test/test.dart', + bool withDebugger = false, + bool pauseOnExceptions = false, + File pidFile, + Future Function() beforeStart, + }) async { + await _setupProcess([ + 'test', + '--machine', + '-d', + 'flutter-tester' + ], script: testFile, withDebugger: withDebugger, pauseOnExceptions: pauseOnExceptions, pidFile: pidFile); + } + + @override + Future _setupProcess( + List args, { + String script, + bool withDebugger = false, + bool pauseOnExceptions = false, + File pidFile, + Future Function() beforeStart, + }) async { + await super._setupProcess( + args, + script: script, + withDebugger: withDebugger, + pauseOnExceptions: pauseOnExceptions, + pidFile: pidFile, + ); + + // Stash the PID so that we can terminate the VM more reliably than using + // _proc.kill() (because _proc is a shell, because `flutter` is a shell + // script). + final Map version = await _waitForJson(); + _procPid = version['pid']; + + if (withDebugger) { + final Map startedProcess = await _waitFor(event: 'test.startedProcess', timeout: appStartTimeout); + final String vmServiceHttpString = startedProcess['params']['observatoryUri']; + _vmServiceWsUri = Uri.parse(vmServiceHttpString).replace(scheme: 'ws', path: '/ws'); + await connectToVmService(pauseOnExceptions: pauseOnExceptions); + // Allow us to run code before we start, eg. to set up breakpoints. + if (beforeStart != null) { + await beforeStart(); + } + await resume(wait: false); + } + } + + Future> _waitForJson({ + Duration timeout, + }) async { + return _timeoutWithMessages>( + () => _stdout.stream.map>(_parseJsonResponse).first, + timeout: timeout, + message: 'Did not receive any JSON.', + ); + } + + Map _parseJsonResponse(String line) { + try { + return json.decode(line); + } catch (e) { + // Not valid JSON, so likely some other output. + return null; + } + } +} + Stream transformToLines(Stream> byteStream) { return byteStream.transform(utf8.decoder).transform(const LineSplitter()); }