diff --git a/dev/devicelab/lib/tasks/plugin_tests.dart b/dev/devicelab/lib/tasks/plugin_tests.dart index 45681da086..c706ada5ed 100644 --- a/dev/devicelab/lib/tasks/plugin_tests.dart +++ b/dev/devicelab/lib/tasks/plugin_tests.dart @@ -23,8 +23,8 @@ TaskFunction combine(List tasks) { }; } -/// Defines task that creates new Flutter project, adds a plugin, and then -/// builds the specified [buildTarget]. +/// Defines task that creates new Flutter project, adds a local and remote +/// plugin, and then builds the specified [buildTarget]. class PluginTest { PluginTest(this.buildTarget, this.options); @@ -32,19 +32,32 @@ class PluginTest { final List options; Future call() async { - section('Create Flutter project'); - final Directory tempDir = Directory.systemTemp.createTempSync('flutter_devicelab_plugin_test.'); + final Directory tempDir = + Directory.systemTemp.createTempSync('flutter_devicelab_plugin_test.'); try { - final FlutterProject project = await FlutterProject.create(tempDir, options); + section('Create plugin'); + final _FlutterProject plugin = await _FlutterProject.create( + tempDir, options, + name: 'plugintest', template: 'plugin'); + section('Test plugin'); + await plugin.test(); + section('Create Flutter app'); + final _FlutterProject app = await _FlutterProject.create(tempDir, options, + name: 'plugintestapp', template: 'app'); try { if (buildTarget == 'ios') - await prepareProvisioningCertificates(project.rootPath); - section('Add plugin'); - await project.addPlugin('path_provider'); - section('Build'); - await project.build(buildTarget); + await prepareProvisioningCertificates(app.rootPath); + section('Add plugins'); + await app.addPlugin('plugintest', + pluginPath: path.join('..', 'plugintest')); + await app.addPlugin('path_provider'); + section('Build app'); + await app.build(buildTarget); + section('Test app'); + await app.test(); } finally { - await project.delete(); + await plugin.delete(); + await app.delete(); } return TaskResult.success(null); } catch (e) { @@ -55,32 +68,48 @@ class PluginTest { } } -class FlutterProject { - FlutterProject(this.parent, this.name); +class _FlutterProject { + _FlutterProject(this.parent, this.name); final Directory parent; final String name; - static Future create(Directory directory, List options) async { + String get rootPath => path.join(parent.path, name); + + Future addPlugin(String plugin, {String pluginPath}) async { + final File pubspec = File(path.join(rootPath, 'pubspec.yaml')); + String content = await pubspec.readAsString(); + final String dependency = + pluginPath != null ? '$plugin:\n path: $pluginPath' : '$plugin:'; + content = content.replaceFirst( + '\ndependencies:\n', + '\ndependencies:\n $dependency\n', + ); + await pubspec.writeAsString(content, flush: true); + } + + Future test() async { + await inDirectory(Directory(rootPath), () async { + await flutter('test'); + }); + } + + static Future<_FlutterProject> create( + Directory directory, List options, + {String name, String template}) async { await inDirectory(directory, () async { await flutter( 'create', - options: ['--template=app', '--org', 'io.flutter.devicelab', ...options, 'plugintest'], + options: [ + '--template=$template', + '--org', + 'io.flutter.devicelab', + ...options, + name + ], ); }); - return FlutterProject(directory, 'plugintest'); - } - - String get rootPath => path.join(parent.path, name); - - Future addPlugin(String plugin) async { - final File pubspec = File(path.join(rootPath, 'pubspec.yaml')); - String content = await pubspec.readAsString(); - content = content.replaceFirst( - '\ndependencies:\n', - '\ndependencies:\n $plugin:\n', - ); - await pubspec.writeAsString(content, flush: true); + return _FlutterProject(directory, name); } Future build(String target) async { @@ -93,11 +122,11 @@ class FlutterProject { if (Platform.isWindows) { // A running Gradle daemon might prevent us from deleting the project // folder on Windows. - await exec( - path.absolute(path.join(rootPath, 'android', 'gradlew.bat')), - ['--stop'], - canFail: true, - ); + final String wrapperPath = + path.absolute(path.join(rootPath, 'android', 'gradlew.bat')); + if (File(wrapperPath).existsSync()) { + await exec(wrapperPath, ['--stop'], canFail: true); + } // TODO(ianh): Investigating if flakiness is timing dependent. await Future.delayed(const Duration(seconds: 10)); } diff --git a/packages/flutter_tools/templates/plugin/test/projectName_test.dart.tmpl b/packages/flutter_tools/templates/plugin/test/projectName_test.dart.tmpl index cfcb38436d..5eff1d468b 100644 --- a/packages/flutter_tools/templates/plugin/test/projectName_test.dart.tmpl +++ b/packages/flutter_tools/templates/plugin/test/projectName_test.dart.tmpl @@ -5,6 +5,8 @@ import 'package:{{projectName}}/{{projectName}}.dart'; void main() { const MethodChannel channel = MethodChannel('{{projectName}}'); + TestWidgetsFlutterBinding.ensureInitialized(); + setUp(() { channel.setMockMethodCallHandler((MethodCall methodCall) async { return '42';