|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:path/path.dart' as path; |
| 10 | +import 'package:flutter_devicelab/framework/framework.dart'; |
| 11 | +import 'package:flutter_devicelab/framework/task_result.dart'; |
| 12 | +import 'package:flutter_devicelab/framework/utils.dart'; |
| 13 | + |
| 14 | +TaskFunction dartPluginRegistryTest({ |
| 15 | + String deviceIdOverride, |
| 16 | + Map<String, String> environment, |
| 17 | +}) { |
| 18 | + final Directory tempDir = Directory.systemTemp |
| 19 | + .createTempSync('flutter_devicelab_dart_plugin_test.'); |
| 20 | + return () async { |
| 21 | + try { |
| 22 | + section('Create implementation plugin'); |
| 23 | + await inDirectory(tempDir, () async { |
| 24 | + await flutter( |
| 25 | + 'create', |
| 26 | + options: <String>[ |
| 27 | + '--template=plugin', |
| 28 | + '--org', |
| 29 | + 'io.flutter.devicelab', |
| 30 | + '--platforms', |
| 31 | + 'macos', |
| 32 | + 'plugin_platform_implementation', |
| 33 | + ], |
| 34 | + environment: environment, |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + final File pluginMain = File(path.join( |
| 39 | + tempDir.absolute.path, |
| 40 | + 'plugin_platform_implementation', |
| 41 | + 'lib', |
| 42 | + 'plugin_platform_implementation.dart', |
| 43 | + )); |
| 44 | + if (!pluginMain.existsSync()) { |
| 45 | + return TaskResult.failure('${pluginMain.path} does not exist'); |
| 46 | + } |
| 47 | + |
| 48 | + // Patch plugin main dart file. |
| 49 | + await pluginMain.writeAsString(''' |
| 50 | +class PluginPlatformInterfaceMacOS { |
| 51 | + static void registerWith() { |
| 52 | + print('PluginPlatformInterfaceMacOS.registerWith() was called'); |
| 53 | + } |
| 54 | +} |
| 55 | +''', flush: true); |
| 56 | + |
| 57 | + // Patch plugin main pubspec file. |
| 58 | + final File pluginImplPubspec = File(path.join( |
| 59 | + tempDir.absolute.path, |
| 60 | + 'plugin_platform_implementation', |
| 61 | + 'pubspec.yaml', |
| 62 | + )); |
| 63 | + String pluginImplPubspecContent = await pluginImplPubspec.readAsString(); |
| 64 | + pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst( |
| 65 | + ' pluginClass: PluginPlatformImplementationPlugin', |
| 66 | + ' pluginClass: PluginPlatformImplementationPlugin\n' |
| 67 | + ' dartPluginClass: PluginPlatformInterfaceMacOS\n', |
| 68 | + ); |
| 69 | + pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst( |
| 70 | + ' platforms:\n', |
| 71 | + ' implements: plugin_platform_interface\n' |
| 72 | + ' platforms:\n'); |
| 73 | + await pluginImplPubspec.writeAsString(pluginImplPubspecContent, |
| 74 | + flush: true); |
| 75 | + |
| 76 | + section('Create interface plugin'); |
| 77 | + await inDirectory(tempDir, () async { |
| 78 | + await flutter( |
| 79 | + 'create', |
| 80 | + options: <String>[ |
| 81 | + '--template=plugin', |
| 82 | + '--org', |
| 83 | + 'io.flutter.devicelab', |
| 84 | + '--platforms', |
| 85 | + 'macos', |
| 86 | + 'plugin_platform_interface', |
| 87 | + ], |
| 88 | + environment: environment, |
| 89 | + ); |
| 90 | + }); |
| 91 | + final File pluginInterfacePubspec = File(path.join( |
| 92 | + tempDir.absolute.path, |
| 93 | + 'plugin_platform_interface', |
| 94 | + 'pubspec.yaml', |
| 95 | + )); |
| 96 | + String pluginInterfacePubspecContent = |
| 97 | + await pluginInterfacePubspec.readAsString(); |
| 98 | + pluginInterfacePubspecContent = |
| 99 | + pluginInterfacePubspecContent.replaceFirst( |
| 100 | + ' pluginClass: PluginPlatformInterfacePlugin', |
| 101 | + ' default_package: plugin_platform_implementation\n'); |
| 102 | + pluginInterfacePubspecContent = |
| 103 | + pluginInterfacePubspecContent.replaceFirst( |
| 104 | + 'dependencies:', |
| 105 | + 'dependencies:\n' |
| 106 | + ' plugin_platform_implementation:\n' |
| 107 | + ' path: ../plugin_platform_implementation\n'); |
| 108 | + await pluginInterfacePubspec.writeAsString(pluginInterfacePubspecContent, |
| 109 | + flush: true); |
| 110 | + |
| 111 | + section('Create app'); |
| 112 | + |
| 113 | + await inDirectory(tempDir, () async { |
| 114 | + await flutter( |
| 115 | + 'create', |
| 116 | + options: <String>[ |
| 117 | + '--template=app', |
| 118 | + '--org', |
| 119 | + 'io.flutter.devicelab', |
| 120 | + '--platforms', |
| 121 | + 'macos', |
| 122 | + 'app', |
| 123 | + ], |
| 124 | + environment: environment, |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + final File appPubspec = File(path.join( |
| 129 | + tempDir.absolute.path, |
| 130 | + 'app', |
| 131 | + 'pubspec.yaml', |
| 132 | + )); |
| 133 | + String appPubspecContent = await appPubspec.readAsString(); |
| 134 | + appPubspecContent = appPubspecContent.replaceFirst( |
| 135 | + 'dependencies:', |
| 136 | + 'dependencies:\n' |
| 137 | + ' plugin_platform_interface:\n' |
| 138 | + ' path: ../plugin_platform_interface\n'); |
| 139 | + await appPubspec.writeAsString(appPubspecContent, flush: true); |
| 140 | + |
| 141 | + section('Flutter run for macos'); |
| 142 | + |
| 143 | + await inDirectory(path.join(tempDir.path, 'app'), () async { |
| 144 | + final Process run = await startProcess( |
| 145 | + path.join(flutterDirectory.path, 'bin', 'flutter'), |
| 146 | + flutterCommandArgs('run', <String>['-d', 'macos', '-v']), |
| 147 | + environment: null, |
| 148 | + ); |
| 149 | + Completer<void> registryExecutedCompleter = Completer<void>(); |
| 150 | + final StreamSubscription<void> subscription = run.stdout |
| 151 | + .transform<String>(utf8.decoder) |
| 152 | + .transform<String>(const LineSplitter()) |
| 153 | + .listen((String line) { |
| 154 | + if (line.contains( |
| 155 | + 'PluginPlatformInterfaceMacOS.registerWith() was called')) { |
| 156 | + registryExecutedCompleter.complete(); |
| 157 | + } |
| 158 | + print('stdout: $line'); |
| 159 | + }); |
| 160 | + |
| 161 | + section('Wait for registry execution'); |
| 162 | + await registryExecutedCompleter.future |
| 163 | + .timeout(const Duration(minutes: 1)); |
| 164 | + |
| 165 | + // Hot restart. |
| 166 | + run.stdin.write('R'); |
| 167 | + registryExecutedCompleter = Completer<void>(); |
| 168 | + |
| 169 | + section('Wait for registry execution after hot restart'); |
| 170 | + await registryExecutedCompleter.future |
| 171 | + .timeout(const Duration(minutes: 1)); |
| 172 | + |
| 173 | + subscription.cancel(); |
| 174 | + run.kill(); |
| 175 | + }); |
| 176 | + return TaskResult.success(null); |
| 177 | + } finally { |
| 178 | + rmTree(tempDir); |
| 179 | + } |
| 180 | + }; |
| 181 | +} |
0 commit comments