plugin_dependencies_test.dart 9.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:convert';
6 7 8
import 'dart:io';

import 'package:flutter_devicelab/framework/framework.dart';
9
import 'package:flutter_devicelab/framework/task_result.dart';
10 11 12
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;

13 14
final String platformLineSep = Platform.isWindows ? '\r\n': '\n';

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/// Tests that a plugin A can depend on platform code from a plugin B
/// as long as plugin B is defined as a pub dependency of plugin A.
///
/// This test fails when `flutter build apk` fails and the stderr from this command
/// contains "Unresolved reference: plugin_b".
Future<void> main() async {
  await task(() async {

    section('Find Java');

    final String javaHome = await findJavaHome();
    if (javaHome == null) {
      return TaskResult.failure('Could not find Java');
    }

    print('\nUsing JAVA_HOME=$javaHome');

    final Directory tempDir = Directory.systemTemp.createTempSync('flutter_plugin_dependencies.');
    try {

      section('Create plugin A');

      final Directory pluginADirectory = Directory(path.join(tempDir.path, 'plugin_a'));
      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>[
            '--org',
            'io.flutter.devicelab.plugin_a',
            '--template=plugin',
45
            '--platforms=android,ios',
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
            pluginADirectory.path,
          ],
        );
      });

      section('Create plugin B');

      final Directory pluginBDirectory = Directory(path.join(tempDir.path, 'plugin_b'));
      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>[
            '--org',
            'io.flutter.devicelab.plugin_b',
            '--template=plugin',
61
            '--platforms=android,ios',
62 63 64 65 66
            pluginBDirectory.path,
          ],
        );
      });

67 68 69 70 71 72 73 74 75 76
      section('Create plugin C without android/ directory');

      final Directory pluginCDirectory = Directory(path.join(tempDir.path, 'plugin_c'));
      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>[
            '--org',
            'io.flutter.devicelab.plugin_c',
            '--template=plugin',
77
            '--platforms=ios',
78 79 80 81 82
            pluginCDirectory.path,
          ],
        );
      });

83 84 85 86 87
      checkDirectoryNotExists(path.join(
        pluginCDirectory.path,
        'android',
      ));

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      final File pluginCpubspec = File(path.join(pluginCDirectory.path, 'pubspec.yaml'));
      await pluginCpubspec.writeAsString('''
name: plugin_c
version: 0.0.1

flutter:
  plugin:
    platforms:
      ios:
        pluginClass: Plugin_cPlugin

dependencies:
  flutter:
    sdk: flutter

environment:
  sdk: ">=2.0.0-dev.28.0 <3.0.0"
105
  flutter: ">=1.5.0"
106 107
''', flush: true);

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
      section('Create plugin D without ios/ directory');

      final Directory pluginDDirectory = Directory(path.join(tempDir.path, 'plugin_d'));
      await inDirectory(tempDir, () async {
        await flutter(
          'create',
          options: <String>[
            '--org',
            'io.flutter.devicelab.plugin_d',
            '--template=plugin',
            '--platforms=android',
            pluginDDirectory.path,
          ],
        );
      });

      checkDirectoryNotExists(path.join(
        pluginDDirectory.path,
        'ios',
      ));

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      section('Write dummy Kotlin code in plugin B');

      final File pluginBKotlinClass = File(path.join(
        pluginBDirectory.path,
        'android',
        'src',
        'main',
        'kotlin',
        'DummyPluginBClass.kt',
      ));

      await pluginBKotlinClass.writeAsString('''
package io.flutter.devicelab.plugin_b

public class DummyPluginBClass {
  companion object {
    fun dummyStaticMethod() {
    }
  }
}
''', flush: true);

151
      section('Make plugin A depend on plugin B, C, and D');
152

153 154 155
      final File pluginApubspec = File(path.join(pluginADirectory.path, 'pubspec.yaml'));
      String pluginApubspecContent = await pluginApubspec.readAsString();
      pluginApubspecContent = pluginApubspecContent.replaceFirst(
156 157 158 159 160 161 162 163
        '${platformLineSep}dependencies:$platformLineSep',
        '${platformLineSep}dependencies:$platformLineSep'
        '  plugin_b:$platformLineSep'
        '    path: ${pluginBDirectory.path}$platformLineSep'
        '  plugin_c:$platformLineSep'
        '    path: ${pluginCDirectory.path}$platformLineSep'
        '  plugin_d:$platformLineSep'
        '    path: ${pluginDDirectory.path}$platformLineSep',
164
      );
165
      await pluginApubspec.writeAsString(pluginApubspecContent, flush: true);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

      section('Write Kotlin code in plugin A that references Kotlin code from plugin B');

      final File pluginAKotlinClass = File(path.join(
        pluginADirectory.path,
        'android',
        'src',
        'main',
        'kotlin',
        'DummyPluginAClass.kt',
      ));

      await pluginAKotlinClass.writeAsString('''
package io.flutter.devicelab.plugin_a

import io.flutter.devicelab.plugin_b.DummyPluginBClass

public class DummyPluginAClass {
  constructor() {
    // Call a method from plugin b.
    DummyPluginBClass.dummyStaticMethod();
  }
}
''', flush: true);

      section('Verify .flutter-plugins-dependencies');

      final Directory exampleApp = Directory(path.join(pluginADirectory.path, 'example'));

      await inDirectory(exampleApp, () async {
        await flutter(
          'packages',
          options: <String>['get'],
        );
      });

      final File flutterPluginsDependenciesFile =
          File(path.join(exampleApp.path, '.flutter-plugins-dependencies'));

      if (!flutterPluginsDependenciesFile.existsSync()) {
206
        return TaskResult.failure("${flutterPluginsDependenciesFile.path} doesn't exist");
207 208 209
      }

      final String flutterPluginsDependenciesFileContent = flutterPluginsDependenciesFile.readAsStringSync();
210 211 212 213 214 215

      final Map<String, dynamic> jsonContent = json.decode(flutterPluginsDependenciesFileContent) as Map<String, dynamic>;

      // Verify the dependencyGraph object is valid. The rest of the contents of this file are not relevant to the
      // dependency graph and are tested by unit tests.
      final List<dynamic> dependencyGraph = jsonContent['dependencyGraph'] as List<dynamic>;
216
      const String kExpectedPluginsDependenciesContent =
217 218
        '['
          '{'
219
            '"name":"plugin_a",'
220
            '"dependencies":["plugin_b","plugin_c","plugin_d"]'
221 222
          '},'
          '{'
223 224
            '"name":"plugin_b",'
            '"dependencies":[]'
225 226
          '},'
          '{'
227 228
            '"name":"plugin_c",'
            '"dependencies":[]'
229 230 231 232
          '},'
          '{'
            '"name":"plugin_d",'
            '"dependencies":[]'
233 234 235 236
          '}'
        ']';
      final String graphString = json.encode(dependencyGraph);
      if (graphString != kExpectedPluginsDependenciesContent) {
237 238
        return TaskResult.failure(
          'Unexpected file content in ${flutterPluginsDependenciesFile.path}: '
239
          'Found "$graphString" instead of "$kExpectedPluginsDependenciesContent"'
240 241 242
        );
      }

243
      section('Build plugin A example Android app');
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

      final StringBuffer stderr = StringBuffer();
      await inDirectory(exampleApp, () async {
        await evalFlutter(
          'build',
          options: <String>['apk', '--target-platform', 'android-arm'],
          canFail: true,
          stderr: stderr,
        );
      });

      if (stderr.toString().contains('Unresolved reference: plugin_b')) {
        return TaskResult.failure('plugin_a cannot reference plugin_b');
      }

      final bool pluginAExampleApk = exists(File(path.join(
        pluginADirectory.path,
        'example',
        'build',
        'app',
        'outputs',
        'apk',
        'release',
        'app-release.apk',
      )));

      if (!pluginAExampleApk) {
        return TaskResult.failure('Failed to build plugin A example APK');
      }

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      if (Platform.isMacOS) {
        section('Build plugin A example iOS app');

        await inDirectory(exampleApp, () async {
          await evalFlutter(
            'build',
            options: <String>[
              'ios',
              '--no-codesign',
            ],
          );
        });

        final Directory appBundle = Directory(path.join(
          pluginADirectory.path,
          'example',
          'build',
          'ios',
          'iphoneos',
          'Runner.app',
        ));

        if (!exists(appBundle)) {
          return TaskResult.failure('Failed to build plugin A example iOS app');
        }

        checkDirectoryExists(path.join(
          appBundle.path,
          'Frameworks',
          'plugin_a.framework',
        ));
        checkDirectoryExists(path.join(
          appBundle.path,
          'Frameworks',
          'plugin_b.framework',
        ));
        checkDirectoryExists(path.join(
          appBundle.path,
          'Frameworks',
          'plugin_c.framework',
        ));

        // Plugin D is Android only and should not be embedded.
        checkDirectoryNotExists(path.join(
          appBundle.path,
          'Frameworks',
          'plugin_d.framework',
        ));
      }

324 325 326 327 328 329 330 331 332 333
      return TaskResult.success(null);
    } on TaskResult catch (taskResult) {
      return taskResult;
    } catch (e) {
      return TaskResult.failure(e.toString());
    } finally {
      rmTree(tempDir);
    }
  });
}