dart_plugin_registry_tests.dart 6.71 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 The Flutter 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 'dart:async';
import 'dart:convert';
import 'dart:io';

9
import 'package:path/path.dart' as path;
10

11 12 13 14
import '../framework/framework.dart';
import '../framework/task_result.dart';
import '../framework/utils.dart';

15
TaskFunction dartPluginRegistryTest({
16 17
  String? deviceIdOverride,
  Map<String, String>? environment,
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
}) {
  final Directory tempDir = Directory.systemTemp
      .createTempSync('flutter_devicelab_dart_plugin_test.');
  return () async {
    try {
      section('Create implementation plugin');
      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>[
            '--template=plugin',
            '--org',
            'io.flutter.devicelab',
            '--platforms',
            'macos',
33
            'aplugin_platform_implementation',
34 35 36 37 38 39 40
          ],
          environment: environment,
        );
      });

      final File pluginMain = File(path.join(
        tempDir.absolute.path,
41
        'aplugin_platform_implementation',
42
        'lib',
43
        'aplugin_platform_implementation.dart',
44 45 46 47 48 49 50
      ));
      if (!pluginMain.existsSync()) {
        return TaskResult.failure('${pluginMain.path} does not exist');
      }

      // Patch plugin main dart file.
      await pluginMain.writeAsString('''
51
class ApluginPlatformInterfaceMacOS {
52
  static void registerWith() {
53
    print('ApluginPlatformInterfaceMacOS.registerWith() was called');
54 55 56 57 58 59 60
  }
}
''', flush: true);

      // Patch plugin main pubspec file.
      final File pluginImplPubspec = File(path.join(
        tempDir.absolute.path,
61
        'aplugin_platform_implementation',
62 63 64 65
        'pubspec.yaml',
      ));
      String pluginImplPubspecContent = await pluginImplPubspec.readAsString();
      pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst(
66 67 68
        '        pluginClass: ApluginPlatformImplementationPlugin',
        '        pluginClass: ApluginPlatformImplementationPlugin\n'
            '        dartPluginClass: ApluginPlatformInterfaceMacOS\n',
69 70 71
      );
      pluginImplPubspecContent = pluginImplPubspecContent.replaceFirst(
          '    platforms:\n',
72
          '    implements: aplugin_platform_interface\n'
73 74 75 76 77 78 79 80 81 82 83 84 85 86
              '    platforms:\n');
      await pluginImplPubspec.writeAsString(pluginImplPubspecContent,
          flush: true);

      section('Create interface plugin');
      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>[
            '--template=plugin',
            '--org',
            'io.flutter.devicelab',
            '--platforms',
            'macos',
87
            'aplugin_platform_interface',
88 89 90 91 92 93
          ],
          environment: environment,
        );
      });
      final File pluginInterfacePubspec = File(path.join(
        tempDir.absolute.path,
94
        'aplugin_platform_interface',
95 96 97 98 99 100
        'pubspec.yaml',
      ));
      String pluginInterfacePubspecContent =
          await pluginInterfacePubspec.readAsString();
      pluginInterfacePubspecContent =
          pluginInterfacePubspecContent.replaceFirst(
101 102
              '        pluginClass: ApluginPlatformInterfacePlugin',
              '        default_package: aplugin_platform_implementation\n');
103 104 105 106
      pluginInterfacePubspecContent =
          pluginInterfacePubspecContent.replaceFirst(
              'dependencies:',
              'dependencies:\n'
107 108
                  '  aplugin_platform_implementation:\n'
                  '    path: ../aplugin_platform_implementation\n');
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
      await pluginInterfacePubspec.writeAsString(pluginInterfacePubspecContent,
          flush: true);

      section('Create app');

      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>[
            '--template=app',
            '--org',
            'io.flutter.devicelab',
            '--platforms',
            'macos',
            'app',
          ],
          environment: environment,
        );
      });

      final File appPubspec = File(path.join(
        tempDir.absolute.path,
        'app',
        'pubspec.yaml',
      ));
      String appPubspecContent = await appPubspec.readAsString();
      appPubspecContent = appPubspecContent.replaceFirst(
          'dependencies:',
          'dependencies:\n'
138 139
              '  aplugin_platform_interface:\n'
              '    path: ../aplugin_platform_interface\n');
140 141 142 143
      await appPubspec.writeAsString(appPubspecContent, flush: true);

      section('Flutter run for macos');

144
      late Process run;
145
      await inDirectory(path.join(tempDir.path, 'app'), () async {
146
        run = await startProcess(
147 148 149
          path.join(flutterDirectory.path, 'bin', 'flutter'),
          flutterCommandArgs('run', <String>['-d', 'macos', '-v']),
        );
150 151 152 153 154 155 156 157
      });

      Completer<void> registryExecutedCompleter = Completer<void>();
      final StreamSubscription<void> stdoutSub = run.stdout
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
        .listen((String line) {
          if (line.contains('ApluginPlatformInterfaceMacOS.registerWith() was called')) {
158 159 160 161 162
            registryExecutedCompleter.complete();
          }
          print('stdout: $line');
        });

163 164 165 166 167 168
      final StreamSubscription<void> stderrSub = run.stderr
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
        .listen((String line) {
          print('stderr: $line');
        });
169

170 171
      final Future<void> stdoutDone = stdoutSub.asFuture<void>();
      final Future<void> stderrDone = stderrSub.asFuture<void>();
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      Future<void> waitForStreams() {
        return Future.wait<void>(<Future<void>>[stdoutDone, stderrDone]);
      }

      Future<void> waitOrExit(Future<void> future) async {
        final dynamic result = await Future.any<dynamic>(
          <Future<dynamic>>[
            future,
            run.exitCode,
          ],
        );
        if (result is int) {
          await waitForStreams();
          throw 'process exited with code $result';
        }
      }

      section('Wait for registry execution');
      await waitOrExit(registryExecutedCompleter.future);

      // Hot restart.
      run.stdin.write('R');
195 196
      await run.stdin.flush();
      await run.stdin.close();
197

198
      registryExecutedCompleter = Completer<void>();
199 200 201 202 203
      section('Wait for registry execution after hot restart');
      await waitOrExit(registryExecutedCompleter.future);

      run.kill();

204
      section('Wait for stdout/stderr streams');
205 206 207 208
      await waitForStreams();

      unawaited(stdoutSub.cancel());
      unawaited(stderrSub.cancel());
209 210 211 212 213 214 215

      return TaskResult.success(null);
    } finally {
      rmTree(tempDir);
    }
  };
}