From f0c2d5eddc9b75e38878a8b712cbd29179acf3c1 Mon Sep 17 00:00:00 2001 From: Phil Quitslund Date: Fri, 21 Jul 2017 15:11:59 -0700 Subject: [PATCH] Flutter create widget test template. (#11304) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Flutter create widget test template. Running `flutter create —with-widget-test` produces a test/widget_test.dart sample widget test. * Generate widget test bits in flutter create. * Path fix. * Added types. * Skip shell testing on windows. * formatting fixes * Update test sample to test the sample app. * Formatting tweak. --- .../templates/create/pubspec.yaml.tmpl | 6 ++- .../create/test/widget_test.dart.tmpl | 29 +++++++++++ .../test/commands/create_test.dart | 50 ++++++++++++++++--- 3 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 packages/flutter_tools/templates/create/test/widget_test.dart.tmpl diff --git a/packages/flutter_tools/templates/create/pubspec.yaml.tmpl b/packages/flutter_tools/templates/create/pubspec.yaml.tmpl index 90255ce96d..b6a5c89b10 100644 --- a/packages/flutter_tools/templates/create/pubspec.yaml.tmpl +++ b/packages/flutter_tools/templates/create/pubspec.yaml.tmpl @@ -4,11 +4,15 @@ description: {{description}} dependencies: flutter: sdk: flutter -{{#withDriverTest}} + dev_dependencies: + flutter_test: + sdk: flutter +{{#withDriverTest}} flutter_driver: sdk: flutter {{/withDriverTest}} + {{#withPluginHook}} {{pluginProjectName}}: path: ../ diff --git a/packages/flutter_tools/templates/create/test/widget_test.dart.tmpl b/packages/flutter_tools/templates/create/test/widget_test.dart.tmpl new file mode 100644 index 0000000000..2c2b24afa8 --- /dev/null +++ b/packages/flutter_tools/templates/create/test/widget_test.dart.tmpl @@ -0,0 +1,29 @@ +// This is a basic Flutter widget test. +// To perform an interaction with a widget in your test, use the WidgetTester utility that Flutter +// provides. For example, you can send tap and scroll gestures. You can also use WidgetTester to +// find child widgets in the widget tree, read text, and verify that the values of widget properties +// are correct. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../lib/main.dart'; + +void main() { + testWidgets('Counter increments smoke test', (WidgetTester tester) async { + // Build our app and trigger a frame. + await tester.pumpWidget(new MyApp()); + + // Verify that our counter starts at 0. + expect(find.text('0'), findsOneWidget); + expect(find.text('1'), findsNothing); + + // Tap the '+' icon and trigger a frame. + await tester.tap(find.byIcon(Icons.add)); + await tester.pump(); + + // Verify that our counter has incremented. + expect(find.text('0'), findsNothing); + expect(find.text('1'), findsOneWidget); + }); +} diff --git a/packages/flutter_tools/test/commands/create_test.dart b/packages/flutter_tools/test/commands/create_test.dart index 45558bb6a8..f8240a09e7 100644 --- a/packages/flutter_tools/test/commands/create_test.dart +++ b/packages/flutter_tools/test/commands/create_test.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io' as io; import 'package:args/command_runner.dart'; import 'package:flutter_tools/src/base/file_system.dart'; @@ -45,6 +46,7 @@ void main() { 'ios/Runner/AppDelegate.m', 'ios/Runner/main.m', 'lib/main.dart', + 'test/widget_test.dart' ], ); }); @@ -59,7 +61,7 @@ void main() { 'ios/Runner/Runner-Bridging-Header.h', 'lib/main.dart', ], - [ + unexpectedPaths: [ 'android/app/src/main/java/com/yourcompany/flutterproject/MainActivity.java', 'ios/Runner/AppDelegate.h', 'ios/Runner/AppDelegate.m', @@ -83,6 +85,7 @@ void main() { 'example/ios/Runner/main.m', 'example/lib/main.dart', ], + plugin: true, ); }); @@ -101,13 +104,14 @@ void main() { 'example/ios/Runner/Runner-Bridging-Header.h', 'example/lib/main.dart', ], - [ + unexpectedPaths: [ 'android/src/main/java/com/yourcompany/flutterproject/FlutterProjectPlugin.java', 'example/android/app/src/main/java/com/yourcompany/flutterprojectexample/MainActivity.java', 'example/ios/Runner/AppDelegate.h', 'example/ios/Runner/AppDelegate.m', 'example/ios/Runner/main.m', ], + plugin: true, ); }); @@ -119,10 +123,11 @@ void main() { 'android/src/main/java/com/bar/foo/flutterproject/FlutterProjectPlugin.java', 'example/android/app/src/main/java/com/bar/foo/flutterprojectexample/MainActivity.java', ], - [ + unexpectedPaths: [ 'android/src/main/java/com/yourcompany/flutterproject/FlutterProjectPlugin.java', 'example/android/app/src/main/java/com/yourcompany/flutterprojectexample/MainActivity.java', ], + plugin: true, ); }); @@ -163,6 +168,24 @@ void main() { } } + // TODO(pq): enable when sky_shell is available + if (!io.Platform.isWindows) { + // Verify that the sample widget test runs cleanly. + final List args = [ + fs.path.absolute(fs.path.join('bin', 'flutter_tools.dart')), + 'test', + '--no-color', + fs.path.join(projectDir.path, 'test', 'widget_test.dart'), + ]; + + final ProcessResult result = await Process.run( + fs.path.join(dartSdkPath, 'bin', 'dart'), + args, + workingDirectory: projectDir.path, + ); + expect(result.exitCode, 0); + } + // Generated Xcode settings final String xcodeConfigPath = fs.path.join('ios', 'Flutter', 'Generated.xcconfig'); expectExists(xcodeConfigPath); @@ -232,7 +255,7 @@ void main() { Future _createAndAnalyzeProject( Directory dir, List createArgs, List expectedPaths, - [List unexpectedPaths = const []]) async { + { List unexpectedPaths = const [], bool plugin = false }) async { Cache.flutterRoot = '../..'; final CreateCommand command = new CreateCommand(); final CommandRunner runner = createTestCommandRunner(command); @@ -247,14 +270,29 @@ Future _createAndAnalyzeProject( for (String path in unexpectedPaths) { expect(fs.file(fs.path.join(dir.path, path)).existsSync(), false, reason: '$path exists'); } + + if (plugin) { + _analyze(dir.path, target: fs.path.join(dir.path, 'lib', 'flutter_project.dart')); + _analyze(fs.path.join(dir.path, 'example')); + } else { + _analyze(dir.path); + } +} + +void _analyze(String workingDir, {String target}) { final String flutterToolsPath = fs.path.absolute(fs.path.join( 'bin', 'flutter_tools.dart', )); + + final List args = [flutterToolsPath, 'analyze']; + if (target != null) + args.add(target); + final ProcessResult exec = Process.runSync( '$dartSdkPath/bin/dart', - [flutterToolsPath, 'analyze'], - workingDirectory: dir.path, + args, + workingDirectory: workingDir, ); if (exec.exitCode != 0) { print(exec.stdout);