android_plugin_test.dart 5.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// 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 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/platform_plugins.dart';

import '../src/common.dart';

void main() {
  testWithoutContext('AndroidPlugin throws tool exit if the plugin main class can not be found', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final AndroidPlugin androidPlugin = AndroidPlugin(
      name: 'pluginA',
      package: 'com.company',
      pluginClass: 'PluginA',
      pluginPath: '.pub_cache/plugin_a',
      fileSystem: fileSystem,
    );

    expect(() => androidPlugin.toMap(), throwsToolExit(
      message: "The plugin `pluginA` doesn't have a main class defined in "
      '.pub_cache/plugin_a/android/src/main/java/com/company/PluginA.java '
      'or .pub_cache/plugin_a/android/src/main/kotlin/com/company/PluginA.kt'
    ));
  });

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  testWithoutContext('AndroidPlugin does not validate the main class for Dart-only plugins', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final AndroidPlugin androidPlugin = AndroidPlugin(
      name: 'pluginA',
      dartPluginClass: 'PluginA',
      pluginPath: '.pub_cache/plugin_a',
      fileSystem: fileSystem,
    );

    expect(androidPlugin.toMap(), <String, Object>{
      'name': 'pluginA',
      'dartPluginClass': 'PluginA',
      'supportsEmbeddingV1': false,
      'supportsEmbeddingV2': false,
    });
  });

  testWithoutContext('AndroidPlugin does not validate the main class for default_package', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final AndroidPlugin androidPlugin = AndroidPlugin(
      name: 'pluginA',
      defaultPackage: 'plugin_a_android',
      pluginPath: '.pub_cache/plugin_a',
      fileSystem: fileSystem,
    );

    expect(androidPlugin.toMap(), <String, Object>{
      'name': 'pluginA',
      'default_package': 'plugin_a_android',
      'supportsEmbeddingV1': false,
      'supportsEmbeddingV2': false,
    });
  });

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  testWithoutContext('AndroidPlugin parses embedding version 2 from the Java search path', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final AndroidPlugin androidPlugin = AndroidPlugin(
      name: 'pluginA',
      package: 'com.company',
      pluginClass: 'PluginA',
      pluginPath: '.pub_cache/plugin_a',
      fileSystem: fileSystem,
    );

    fileSystem.file('.pub_cache/plugin_a/android/src/main/java/com/company/PluginA.java')
      ..createSync(recursive: true)
      ..writeAsStringSync('io.flutter.embedding.engine.plugins.FlutterPlugin');

    expect(androidPlugin.toMap(), <String, Object>{
      'name': 'pluginA',
      'package': 'com.company',
      'class': 'PluginA',
      'supportsEmbeddingV1': false,
      'supportsEmbeddingV2': true,
    });
  });

  testWithoutContext('AndroidPlugin parses embedding version 1 from the Java search path', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final AndroidPlugin androidPlugin = AndroidPlugin(
      name: 'pluginA',
      package: 'com.company',
      pluginClass: 'PluginA',
      pluginPath: '.pub_cache/plugin_a',
      fileSystem: fileSystem,
    );

    fileSystem.file('.pub_cache/plugin_a/android/src/main/java/com/company/PluginA.java')
      ..createSync(recursive: true)
      ..writeAsStringSync('some.other.string');

    expect(androidPlugin.toMap(), <String, Object>{
      'name': 'pluginA',
      'package': 'com.company',
      'class': 'PluginA',
      'supportsEmbeddingV1': true,
      'supportsEmbeddingV2': false,
    });
  });

  testWithoutContext('AndroidPlugin parses embedding version 2 from the Kotlin search path', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final AndroidPlugin androidPlugin = AndroidPlugin(
      name: 'pluginA',
      package: 'com.company',
      pluginClass: 'PluginA',
      pluginPath: '.pub_cache/plugin_a',
      fileSystem: fileSystem,
    );

    fileSystem.file('.pub_cache/plugin_a/android/src/main/kotlin/com/company/PluginA.kt')
      ..createSync(recursive: true)
      ..writeAsStringSync('io.flutter.embedding.engine.plugins.FlutterPlugin');

    expect(androidPlugin.toMap(), <String, Object>{
      'name': 'pluginA',
      'package': 'com.company',
      'class': 'PluginA',
      'supportsEmbeddingV1': false,
      'supportsEmbeddingV2': true,
    });
  });

  testWithoutContext('AndroidPlugin parses embedding version 1 from the Kotlin search path', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final AndroidPlugin androidPlugin = AndroidPlugin(
      name: 'pluginA',
      package: 'com.company',
      pluginClass: 'PluginA',
      pluginPath: '.pub_cache/plugin_a',
      fileSystem: fileSystem,
    );

    fileSystem.file('.pub_cache/plugin_a/android/src/main/kotlin/com/company/PluginA.kt')
      ..createSync(recursive: true)
      ..writeAsStringSync('some.other.string');

    expect(androidPlugin.toMap(), <String, Object>{
      'name': 'pluginA',
      'package': 'com.company',
      'class': 'PluginA',
      'supportsEmbeddingV1': true,
      'supportsEmbeddingV2': false,
    });
  });
}