Unverified Commit 186765b3 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] cleanups to plugin test cases (#67242)

Removes usage of global variables, expands documentation, and fixes some formatting inconsistencies. Increased test coverage for android plugin to prove it can determine the embedding version.
parent 304e2c57
...@@ -7,7 +7,6 @@ import 'package:yaml/yaml.dart'; ...@@ -7,7 +7,6 @@ import 'package:yaml/yaml.dart';
import 'base/common.dart'; import 'base/common.dart';
import 'base/file_system.dart'; import 'base/file_system.dart';
import 'globals.dart' as globals;
/// Constant for 'pluginClass' key in plugin maps. /// Constant for 'pluginClass' key in plugin maps.
const String kPluginClass = 'pluginClass'; const String kPluginClass = 'pluginClass';
...@@ -38,18 +37,22 @@ class AndroidPlugin extends PluginPlatform { ...@@ -38,18 +37,22 @@ class AndroidPlugin extends PluginPlatform {
@required this.package, @required this.package,
@required this.pluginClass, @required this.pluginClass,
@required this.pluginPath, @required this.pluginPath,
}); @required FileSystem fileSystem,
}) : _fileSystem = fileSystem;
factory AndroidPlugin.fromYaml(String name, YamlMap yaml, String pluginPath) { factory AndroidPlugin.fromYaml(String name, YamlMap yaml, String pluginPath, FileSystem fileSystem) {
assert(validate(yaml)); assert(validate(yaml));
return AndroidPlugin( return AndroidPlugin(
name: name, name: name,
package: yaml['package'] as String, package: yaml['package'] as String,
pluginClass: yaml['pluginClass'] as String, pluginClass: yaml['pluginClass'] as String,
pluginPath: pluginPath, pluginPath: pluginPath,
fileSystem: fileSystem,
); );
} }
final FileSystem _fileSystem;
static bool validate(YamlMap yaml) { static bool validate(YamlMap yaml) {
if (yaml == null) { if (yaml == null) {
return false; return false;
...@@ -91,7 +94,7 @@ class AndroidPlugin extends PluginPlatform { ...@@ -91,7 +94,7 @@ class AndroidPlugin extends PluginPlatform {
Set<String> _getSupportedEmbeddings() { Set<String> _getSupportedEmbeddings() {
assert(pluginPath != null); assert(pluginPath != null);
final Set<String> supportedEmbeddings = <String>{}; final Set<String> supportedEmbeddings = <String>{};
final String baseMainPath = globals.fs.path.join( final String baseMainPath = _fileSystem.path.join(
pluginPath, pluginPath,
'android', 'android',
'src', 'src',
...@@ -99,16 +102,16 @@ class AndroidPlugin extends PluginPlatform { ...@@ -99,16 +102,16 @@ class AndroidPlugin extends PluginPlatform {
); );
final List<String> mainClassCandidates = <String>[ final List<String> mainClassCandidates = <String>[
globals.fs.path.join( _fileSystem.path.join(
baseMainPath, baseMainPath,
'java', 'java',
package.replaceAll('.', globals.fs.path.separator), package.replaceAll('.', _fileSystem.path.separator),
'$pluginClass.java', '$pluginClass.java',
), ),
globals.fs.path.join( _fileSystem.path.join(
baseMainPath, baseMainPath,
'kotlin', 'kotlin',
package.replaceAll('.', globals.fs.path.separator), package.replaceAll('.', _fileSystem.path.separator),
'$pluginClass.kt', '$pluginClass.kt',
) )
]; ];
...@@ -116,7 +119,7 @@ class AndroidPlugin extends PluginPlatform { ...@@ -116,7 +119,7 @@ class AndroidPlugin extends PluginPlatform {
File mainPluginClass; File mainPluginClass;
bool mainClassFound = false; bool mainClassFound = false;
for (final String mainClassCandidate in mainClassCandidates) { for (final String mainClassCandidate in mainClassCandidates) {
mainPluginClass = globals.fs.file(mainClassCandidate); mainPluginClass = _fileSystem.file(mainClassCandidate);
if (mainPluginClass.existsSync()) { if (mainPluginClass.existsSync()) {
mainClassFound = true; mainClassFound = true;
break; break;
...@@ -132,15 +135,7 @@ class AndroidPlugin extends PluginPlatform { ...@@ -132,15 +135,7 @@ class AndroidPlugin extends PluginPlatform {
); );
} }
String mainClassContent; final String mainClassContent = mainPluginClass.readAsStringSync();
try {
mainClassContent = mainPluginClass.readAsStringSync();
} on FileSystemException {
throwToolExit(
"Couldn't read file ${mainPluginClass.path} even though it exists. "
'Please verify that this file has read permission and try again.'
);
}
if (mainClassContent if (mainClassContent
.contains('io.flutter.embedding.engine.plugins.FlutterPlugin')) { .contains('io.flutter.embedding.engine.plugins.FlutterPlugin')) {
supportedEmbeddings.add('2'); supportedEmbeddings.add('2');
...@@ -167,7 +162,7 @@ class IOSPlugin extends PluginPlatform { ...@@ -167,7 +162,7 @@ class IOSPlugin extends PluginPlatform {
}); });
factory IOSPlugin.fromYaml(String name, YamlMap yaml) { factory IOSPlugin.fromYaml(String name, YamlMap yaml) {
assert(validate(yaml)); assert(validate(yaml)); // TODO(jonahwilliams): https://github.com/flutter/flutter/issues/67241
return IOSPlugin( return IOSPlugin(
name: name, name: name,
classPrefix: '', classPrefix: '',
......
...@@ -68,16 +68,17 @@ class Plugin { ...@@ -68,16 +68,17 @@ class Plugin {
String name, String name,
String path, String path,
YamlMap pluginYaml, YamlMap pluginYaml,
List<String> dependencies, List<String> dependencies, {
) { @required FileSystem fileSystem,
}) {
final List<String> errors = validatePluginYaml(pluginYaml); final List<String> errors = validatePluginYaml(pluginYaml);
if (errors.isNotEmpty) { if (errors.isNotEmpty) {
throwToolExit('Invalid plugin specification $name.\n${errors.join('\n')}'); throwToolExit('Invalid plugin specification $name.\n${errors.join('\n')}');
} }
if (pluginYaml != null && pluginYaml['platforms'] != null) { if (pluginYaml != null && pluginYaml['platforms'] != null) {
return Plugin._fromMultiPlatformYaml(name, path, pluginYaml, dependencies); return Plugin._fromMultiPlatformYaml(name, path, pluginYaml, dependencies, fileSystem);
} }
return Plugin._fromLegacyYaml(name, path, pluginYaml, dependencies); return Plugin._fromLegacyYaml(name, path, pluginYaml, dependencies, fileSystem);
} }
factory Plugin._fromMultiPlatformYaml( factory Plugin._fromMultiPlatformYaml(
...@@ -85,6 +86,7 @@ class Plugin { ...@@ -85,6 +86,7 @@ class Plugin {
String path, String path,
dynamic pluginYaml, dynamic pluginYaml,
List<String> dependencies, List<String> dependencies,
FileSystem fileSystem,
) { ) {
assert (pluginYaml != null && pluginYaml['platforms'] != null, assert (pluginYaml != null && pluginYaml['platforms'] != null,
'Invalid multi-platform plugin specification $name.'); 'Invalid multi-platform plugin specification $name.');
...@@ -100,6 +102,7 @@ class Plugin { ...@@ -100,6 +102,7 @@ class Plugin {
name, name,
platformsYaml[AndroidPlugin.kConfigKey] as YamlMap, platformsYaml[AndroidPlugin.kConfigKey] as YamlMap,
path, path,
fileSystem,
); );
} }
...@@ -141,6 +144,7 @@ class Plugin { ...@@ -141,6 +144,7 @@ class Plugin {
String path, String path,
dynamic pluginYaml, dynamic pluginYaml,
List<String> dependencies, List<String> dependencies,
FileSystem fileSystem,
) { ) {
final Map<String, PluginPlatform> platforms = <String, PluginPlatform>{}; final Map<String, PluginPlatform> platforms = <String, PluginPlatform>{};
final String pluginClass = pluginYaml['pluginClass'] as String; final String pluginClass = pluginYaml['pluginClass'] as String;
...@@ -152,6 +156,7 @@ class Plugin { ...@@ -152,6 +156,7 @@ class Plugin {
package: pluginYaml['androidPackage'] as String, package: pluginYaml['androidPackage'] as String,
pluginClass: pluginClass, pluginClass: pluginClass,
pluginPath: path, pluginPath: path,
fileSystem: fileSystem,
); );
} }
...@@ -332,6 +337,7 @@ Plugin _pluginFromPackage(String name, Uri packageRoot) { ...@@ -332,6 +337,7 @@ Plugin _pluginFromPackage(String name, Uri packageRoot) {
packageRootPath, packageRootPath,
flutterConfig['plugin'] as YamlMap, flutterConfig['plugin'] as YamlMap,
dependencies == null ? <String>[] : <String>[...dependencies.keys.cast<String>()], dependencies == null ? <String>[] : <String>[...dependencies.keys.cast<String>()],
fileSystem: globals.fs,
); );
} }
......
// 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'
));
});
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,
});
});
}
// 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:flutter_tools/src/platform_plugins.dart';
import 'package:mockito/mockito.dart';
import 'package:path/path.dart' as p;
import '../src/common.dart';
import '../src/context.dart';
void main() {
group('AndroidPlugin', () {
MockFileSystem mockFileSystem;
MockPathContext pathContext;
setUp(() {
pathContext = MockPathContext();
when(pathContext.separator).thenReturn('/');
mockFileSystem = MockFileSystem();
when(mockFileSystem.path).thenReturn(pathContext);
});
testUsingContext("throws tool exit if the plugin main class can't be read", () {
when(pathContext.join('.pub_cache/plugin_a', 'android', 'src', 'main'))
.thenReturn('.pub_cache/plugin_a/android/src/main');
when(pathContext.join('.pub_cache/plugin_a/android/src/main', 'java', 'com/company', 'PluginA.java'))
.thenReturn('.pub_cache/plugin_a/android/src/main/java/com/company/PluginA.java');
when(pathContext.join('.pub_cache/plugin_a/android/src/main', 'kotlin', 'com/company', 'PluginA.kt'))
.thenReturn('.pub_cache/plugin_a/android/src/main/kotlin/com/company/PluginA.kt');
final MockFile pluginJavaMainClass = MockFile();
when(pluginJavaMainClass.existsSync()).thenReturn(true);
when(pluginJavaMainClass.readAsStringSync(encoding: anyNamed('encoding'))).thenThrow(const FileSystemException());
when(mockFileSystem.file('.pub_cache/plugin_a/android/src/main/java/com/company/PluginA.java'))
.thenReturn(pluginJavaMainClass);
final MockFile pluginKotlinMainClass = MockFile();
when(pluginKotlinMainClass.existsSync()).thenReturn(false);
when(mockFileSystem.file('.pub_cache/plugin_a/android/src/main/kotlin/com/company/PluginA.kt'))
.thenReturn(pluginKotlinMainClass);
expect(() {
AndroidPlugin(
name: 'pluginA',
package: 'com.company',
pluginClass: 'PluginA',
pluginPath: '.pub_cache/plugin_a',
).toMap();
}, throwsToolExit(
message: "Couldn't read file null even though it exists. "
'Please verify that this file has read permission and try again.'
));
}, overrides: <Type, Generator>{
FileSystem: () => mockFileSystem,
ProcessManager: () => FakeProcessManager.any(),
});
});
}
class MockFile extends Mock implements File {}
class MockFileSystem extends Mock implements FileSystem {}
class MockPathContext extends Mock implements p.Context {}
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/platform_plugins.dart'; import 'package:flutter_tools/src/platform_plugins.dart';
import 'package:flutter_tools/src/plugins.dart'; import 'package:flutter_tools/src/plugins.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
...@@ -12,209 +14,261 @@ const String _kTestPluginName = 'test_plugin_name'; ...@@ -12,209 +14,261 @@ const String _kTestPluginName = 'test_plugin_name';
const String _kTestPluginPath = 'test_plugin_path'; const String _kTestPluginPath = 'test_plugin_path';
void main() { void main() {
group('PluginParsing', () { testWithoutContext('Plugin creation from the legacy format', () {
test('Legacy Format', () { final MemoryFileSystem fileSystem = MemoryFileSystem.test();
const String pluginYamlRaw = 'androidPackage: com.flutter.dev\n' const String pluginYamlRaw = 'androidPackage: com.flutter.dev\n'
'iosPrefix: FLT\n' 'iosPrefix: FLT\n'
'pluginClass: SamplePlugin\n'; 'pluginClass: SamplePlugin\n';
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap; final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
final Plugin plugin = final Plugin plugin = Plugin.fromYaml(
Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml, const <String>[]); _kTestPluginName,
_kTestPluginPath,
final AndroidPlugin androidPlugin = pluginYaml,
plugin.platforms[AndroidPlugin.kConfigKey] as AndroidPlugin; const <String>[],
final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey] as IOSPlugin; fileSystem: fileSystem,
final String androidPluginClass = androidPlugin.pluginClass; );
final String iosPluginClass = iosPlugin.pluginClass;
final AndroidPlugin androidPlugin = plugin.platforms[AndroidPlugin.kConfigKey] as AndroidPlugin;
expect(iosPluginClass, 'SamplePlugin'); final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey] as IOSPlugin;
expect(androidPluginClass, 'SamplePlugin'); final String androidPluginClass = androidPlugin.pluginClass;
expect(iosPlugin.classPrefix, 'FLT'); final String iosPluginClass = iosPlugin.pluginClass;
expect(androidPlugin.package, 'com.flutter.dev');
}); expect(iosPluginClass, 'SamplePlugin');
expect(androidPluginClass, 'SamplePlugin');
test('Multi-platform Format', () { expect(iosPlugin.classPrefix, 'FLT');
const String pluginYamlRaw = 'platforms:\n' expect(androidPlugin.package, 'com.flutter.dev');
' android:\n' });
' package: com.flutter.dev\n'
' pluginClass: ASamplePlugin\n' testWithoutContext('Plugin creation from the multi-platform format', () {
' ios:\n' final MemoryFileSystem fileSystem = MemoryFileSystem.test();
' pluginClass: ISamplePlugin\n' const String pluginYamlRaw = 'platforms:\n'
' linux:\n' ' android:\n'
' pluginClass: LSamplePlugin\n' ' package: com.flutter.dev\n'
' macos:\n' ' pluginClass: ASamplePlugin\n'
' pluginClass: MSamplePlugin\n' ' ios:\n'
' web:\n' ' pluginClass: ISamplePlugin\n'
' pluginClass: WebSamplePlugin\n' ' linux:\n'
' fileName: web_plugin.dart\n' ' pluginClass: LSamplePlugin\n'
' windows:\n' ' macos:\n'
' pluginClass: WinSamplePlugin\n'; ' pluginClass: MSamplePlugin\n'
' web:\n'
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap; ' pluginClass: WebSamplePlugin\n'
final Plugin plugin = ' fileName: web_plugin.dart\n'
Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml, const <String>[]); ' windows:\n'
' pluginClass: WinSamplePlugin\n';
final AndroidPlugin androidPlugin =
plugin.platforms[AndroidPlugin.kConfigKey] as AndroidPlugin; final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey] as IOSPlugin; final Plugin plugin = Plugin.fromYaml(
final LinuxPlugin linuxPlugin = _kTestPluginName,
plugin.platforms[LinuxPlugin.kConfigKey] as LinuxPlugin; _kTestPluginPath,
final MacOSPlugin macOSPlugin = pluginYaml,
plugin.platforms[MacOSPlugin.kConfigKey] as MacOSPlugin; const <String>[],
final WebPlugin webPlugin = plugin.platforms[WebPlugin.kConfigKey] as WebPlugin; fileSystem: fileSystem,
final WindowsPlugin windowsPlugin = );
plugin.platforms[WindowsPlugin.kConfigKey] as WindowsPlugin;
final String androidPluginClass = androidPlugin.pluginClass; final AndroidPlugin androidPlugin = plugin.platforms[AndroidPlugin.kConfigKey] as AndroidPlugin;
final String iosPluginClass = iosPlugin.pluginClass; final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey] as IOSPlugin;
final LinuxPlugin linuxPlugin = plugin.platforms[LinuxPlugin.kConfigKey] as LinuxPlugin;
expect(iosPluginClass, 'ISamplePlugin'); final MacOSPlugin macOSPlugin = plugin.platforms[MacOSPlugin.kConfigKey] as MacOSPlugin;
expect(androidPluginClass, 'ASamplePlugin'); final WebPlugin webPlugin = plugin.platforms[WebPlugin.kConfigKey] as WebPlugin;
expect(iosPlugin.classPrefix, ''); final WindowsPlugin windowsPlugin = plugin.platforms[WindowsPlugin.kConfigKey] as WindowsPlugin;
expect(androidPlugin.package, 'com.flutter.dev'); final String androidPluginClass = androidPlugin.pluginClass;
expect(linuxPlugin.pluginClass, 'LSamplePlugin'); final String iosPluginClass = iosPlugin.pluginClass;
expect(macOSPlugin.pluginClass, 'MSamplePlugin');
expect(webPlugin.pluginClass, 'WebSamplePlugin'); expect(iosPluginClass, 'ISamplePlugin');
expect(webPlugin.fileName, 'web_plugin.dart'); expect(androidPluginClass, 'ASamplePlugin');
expect(windowsPlugin.pluginClass, 'WinSamplePlugin'); expect(iosPlugin.classPrefix, '');
}); expect(androidPlugin.package, 'com.flutter.dev');
expect(linuxPlugin.pluginClass, 'LSamplePlugin');
test('Unknown fields are allowed (allows some future compatibility)', () { expect(macOSPlugin.pluginClass, 'MSamplePlugin');
const String pluginYamlRaw = 'implements: same_plugin\n' // this should be ignored by the tool expect(webPlugin.pluginClass, 'WebSamplePlugin');
'platforms:\n' expect(webPlugin.fileName, 'web_plugin.dart');
' android:\n' expect(windowsPlugin.pluginClass, 'WinSamplePlugin');
' package: com.flutter.dev\n' });
' pluginClass: ASamplePlugin\n'
' anUnknownField: ASamplePlugin\n' // this should be ignored by the tool testWithoutContext('Plugin parsing of unknown fields are allowed (allows some future compatibility)', () {
' ios:\n' final MemoryFileSystem fileSystem = MemoryFileSystem.test();
' pluginClass: ISamplePlugin\n' const String pluginYamlRaw = 'implements: same_plugin\n' // this should be ignored by the tool
' linux:\n' 'platforms:\n'
' pluginClass: LSamplePlugin\n' ' android:\n'
' macos:\n' ' package: com.flutter.dev\n'
' pluginClass: MSamplePlugin\n' ' pluginClass: ASamplePlugin\n'
' web:\n' ' anUnknownField: ASamplePlugin\n' // this should be ignored by the tool
' pluginClass: WebSamplePlugin\n' ' ios:\n'
' fileName: web_plugin.dart\n' ' pluginClass: ISamplePlugin\n'
' windows:\n' ' linux:\n'
' pluginClass: WinSamplePlugin\n'; ' pluginClass: LSamplePlugin\n'
' macos:\n'
final dynamic pluginYaml = loadYaml(pluginYamlRaw); ' pluginClass: MSamplePlugin\n'
final Plugin plugin = ' web:\n'
Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml as YamlMap, const <String>[]); ' pluginClass: WebSamplePlugin\n'
' fileName: web_plugin.dart\n'
final AndroidPlugin androidPlugin = plugin.platforms[AndroidPlugin.kConfigKey] as AndroidPlugin; ' windows:\n'
final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey] as IOSPlugin; ' pluginClass: WinSamplePlugin\n';
final LinuxPlugin linuxPlugin = plugin.platforms[LinuxPlugin.kConfigKey] as LinuxPlugin;
final MacOSPlugin macOSPlugin = plugin.platforms[MacOSPlugin.kConfigKey] as MacOSPlugin; final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
final WebPlugin webPlugin = plugin.platforms[WebPlugin.kConfigKey] as WebPlugin; final Plugin plugin = Plugin.fromYaml(
final WindowsPlugin windowsPlugin = plugin.platforms[WindowsPlugin.kConfigKey] as WindowsPlugin; _kTestPluginName,
final String androidPluginClass = androidPlugin.pluginClass; _kTestPluginPath,
final String iosPluginClass = iosPlugin.pluginClass; pluginYaml,
const <String>[],
expect(iosPluginClass, 'ISamplePlugin'); fileSystem: fileSystem,
expect(androidPluginClass, 'ASamplePlugin'); );
expect(iosPlugin.classPrefix, '');
expect(androidPlugin.package, 'com.flutter.dev'); final AndroidPlugin androidPlugin = plugin.platforms[AndroidPlugin.kConfigKey] as AndroidPlugin;
expect(linuxPlugin.pluginClass, 'LSamplePlugin'); final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey] as IOSPlugin;
expect(macOSPlugin.pluginClass, 'MSamplePlugin'); final LinuxPlugin linuxPlugin = plugin.platforms[LinuxPlugin.kConfigKey] as LinuxPlugin;
expect(webPlugin.pluginClass, 'WebSamplePlugin'); final MacOSPlugin macOSPlugin = plugin.platforms[MacOSPlugin.kConfigKey] as MacOSPlugin;
expect(webPlugin.fileName, 'web_plugin.dart'); final WebPlugin webPlugin = plugin.platforms[WebPlugin.kConfigKey] as WebPlugin;
expect(windowsPlugin.pluginClass, 'WinSamplePlugin'); final WindowsPlugin windowsPlugin = plugin.platforms[WindowsPlugin.kConfigKey] as WindowsPlugin;
}); final String androidPluginClass = androidPlugin.pluginClass;
final String iosPluginClass = iosPlugin.pluginClass;
test('Allow for Dart-only plugins without a pluginClass', () {
/// This is currently supported only on macOS, linux, Windows. expect(iosPluginClass, 'ISamplePlugin');
const String pluginYamlRaw = 'implements: same_plugin\n' // this should be ignored by the tool expect(androidPluginClass, 'ASamplePlugin');
'platforms:\n' expect(iosPlugin.classPrefix, '');
' linux:\n' expect(androidPlugin.package, 'com.flutter.dev');
' dartPluginClass: LSamplePlugin\n' expect(linuxPlugin.pluginClass, 'LSamplePlugin');
' macos:\n' expect(macOSPlugin.pluginClass, 'MSamplePlugin');
' dartPluginClass: MSamplePlugin\n' expect(webPlugin.pluginClass, 'WebSamplePlugin');
' windows:\n' expect(webPlugin.fileName, 'web_plugin.dart');
' dartPluginClass: WinSamplePlugin\n'; expect(windowsPlugin.pluginClass, 'WinSamplePlugin');
});
final dynamic pluginYaml = loadYaml(pluginYamlRaw);
final Plugin plugin = testWithoutContext('Plugin parsing allows for Dart-only plugins without a pluginClass', () {
Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml as YamlMap, const <String>[]); final FileSystem fileSystem = MemoryFileSystem.test();
/// This is currently supported only on macOS, linux, Windows.
final LinuxPlugin linuxPlugin = plugin.platforms[LinuxPlugin.kConfigKey] as LinuxPlugin; const String pluginYamlRaw = 'implements: same_plugin\n' // this should be ignored by the tool
final MacOSPlugin macOSPlugin = plugin.platforms[MacOSPlugin.kConfigKey] as MacOSPlugin; 'platforms:\n'
final WindowsPlugin windowsPlugin = plugin.platforms[WindowsPlugin.kConfigKey] as WindowsPlugin; ' linux:\n'
' dartPluginClass: LSamplePlugin\n'
expect(linuxPlugin.pluginClass, isNull); ' macos:\n'
expect(macOSPlugin.pluginClass, isNull); ' dartPluginClass: MSamplePlugin\n'
expect(windowsPlugin.pluginClass, isNull); ' windows:\n'
expect(linuxPlugin.dartPluginClass, 'LSamplePlugin'); ' dartPluginClass: WinSamplePlugin\n';
expect(macOSPlugin.dartPluginClass, 'MSamplePlugin');
expect(windowsPlugin.dartPluginClass, 'WinSamplePlugin'); final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
}); final Plugin plugin = Plugin.fromYaml(
_kTestPluginName,
test('Legacy Format and Multi-Platform Format together is not allowed and error message contains plugin name', () { _kTestPluginPath,
const String pluginYamlRaw = 'androidPackage: com.flutter.dev\n' pluginYaml,
'platforms:\n' const <String>[],
' android:\n' fileSystem: fileSystem,
' package: com.flutter.dev\n'; );
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap; final LinuxPlugin linuxPlugin = plugin.platforms[LinuxPlugin.kConfigKey] as LinuxPlugin;
expect( final MacOSPlugin macOSPlugin = plugin.platforms[MacOSPlugin.kConfigKey] as MacOSPlugin;
() => Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml, const <String>[]), final WindowsPlugin windowsPlugin = plugin.platforms[WindowsPlugin.kConfigKey] as WindowsPlugin;
throwsToolExit(message: _kTestPluginName),
); expect(linuxPlugin.pluginClass, isNull);
}); expect(macOSPlugin.pluginClass, isNull);
expect(windowsPlugin.pluginClass, isNull);
test('A default_package field is allowed', () { expect(linuxPlugin.dartPluginClass, 'LSamplePlugin');
const String pluginYamlRaw = expect(macOSPlugin.dartPluginClass, 'MSamplePlugin');
'platforms:\n' expect(windowsPlugin.dartPluginClass, 'WinSamplePlugin');
' android:\n' });
' default_package: sample_package_android\n'
' ios:\n' testWithoutContext('Plugin parsing of legacy format and multi-platform format together is not allowed '
' default_package: sample_package_ios\n' 'and fatal error message contains plugin name', () {
' linux:\n' final FileSystem fileSystem = MemoryFileSystem.test();
' default_package: sample_package_linux\n' const String pluginYamlRaw = 'androidPackage: com.flutter.dev\n'
' macos:\n' 'platforms:\n'
' default_package: sample_package_macos\n' ' android:\n'
' web:\n' ' package: com.flutter.dev\n';
' default_package: sample_package_web\n'
' windows:\n' final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
' default_package: sample_package_windows\n';
expect(
final dynamic pluginYaml = loadYaml(pluginYamlRaw); () => Plugin.fromYaml(
final Plugin plugin = _kTestPluginName,
Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml as YamlMap, const <String>[]); _kTestPluginPath,
pluginYaml,
expect(plugin.platforms, <String, PluginPlatform> {}); const <String>[],
}); fileSystem: fileSystem,
),
test('error on empty plugin', () { throwsToolExit(message: _kTestPluginName),
const String pluginYamlRaw = ''; );
});
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
expect( testWithoutContext('Plugin parsing allows a default_package field', () {
() => Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml, const <String>[]), final FileSystem fileSystem = MemoryFileSystem.test();
throwsToolExit(message: 'Invalid "plugin" specification.'), const String pluginYamlRaw =
); 'platforms:\n'
}); ' android:\n'
' default_package: sample_package_android\n'
test('error on empty platforms', () { ' ios:\n'
const String pluginYamlRaw = 'platforms:\n'; ' default_package: sample_package_ios\n'
' linux:\n'
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap; ' default_package: sample_package_linux\n'
expect( ' macos:\n'
() => Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml, const <String>[]), ' default_package: sample_package_macos\n'
throwsToolExit(message: 'Invalid "platforms" specification.'), ' web:\n'
); ' default_package: sample_package_web\n'
}); ' windows:\n'
' default_package: sample_package_windows\n';
test('error on empty platform', () {
const String pluginYamlRaw = final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
'platforms:\n' final Plugin plugin = Plugin.fromYaml(
' android:\n'; _kTestPluginName,
_kTestPluginPath,
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap; pluginYaml,
expect( const <String>[],
() => Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml, const <String>[]), fileSystem: fileSystem,
throwsToolExit(message: 'Invalid "android" plugin specification.'), );
);
}); expect(plugin.platforms, <String, PluginPlatform>{});
});
testWithoutContext('Plugin parsing throws a fatal error on an empty plugin', () {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
final YamlMap pluginYaml = loadYaml('') as YamlMap;
expect(
() => Plugin.fromYaml(
_kTestPluginName,
_kTestPluginPath,
pluginYaml,
const <String>[],
fileSystem: fileSystem,
),
throwsToolExit(message: 'Invalid "plugin" specification.'),
);
});
testWithoutContext('Plugin parsing throws a fatal error on empty platforms', () {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
const String pluginYamlRaw = 'platforms:\n';
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
expect(
() => Plugin.fromYaml(
_kTestPluginName,
_kTestPluginPath,
pluginYaml,
const <String>[],
fileSystem: fileSystem,
),
throwsToolExit(message: 'Invalid "platforms" specification.'),
);
});
test('Plugin parsing throws a fatal error on an empty platform key', () {
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
const String pluginYamlRaw =
'platforms:\n'
' android:\n';
final YamlMap pluginYaml = loadYaml(pluginYamlRaw) as YamlMap;
expect(
() => Plugin.fromYaml(
_kTestPluginName,
_kTestPluginPath,
pluginYaml,
const <String>[],
fileSystem: fileSystem,
),
throwsToolExit(message: 'Invalid "android" plugin specification.'),
);
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment