plugins_test.dart 5.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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.

// @dart = 2.8

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:file_testing/file_testing.dart';
import 'package:flutter_tools/src/base/template.dart';
11
import 'package:flutter_tools/src/flutter_plugins.dart';
12 13 14 15 16 17
import 'package:flutter_tools/src/isolated/mustache_template.dart';
import 'package:flutter_tools/src/platform_plugins.dart';
import 'package:flutter_tools/src/plugins.dart';
import 'package:flutter_tools/src/project.dart';

import '../../src/common.dart';
18
import '../../src/fakes.dart';
19 20 21

const TemplateRenderer renderer = MustacheTemplateRenderer();

22 23 24 25 26 27 28 29 30 31 32 33 34 35
const String kPluginDependencies = r'''
{
  "info":"This is a generated file; do not edit or check into version control.",
  "plugins":{
    "windows":[
      {
        "name":"example","path":"C:\\\\example\\\\",
        "dependencies":[]
      }
    ]
  }
}
''';

36 37
void main() {

38
  testWithoutContext('Win32 injects Win32 plugins', () async {
39 40 41 42 43 44 45 46
    final FileSystem fileSystem = MemoryFileSystem.test();
    setUpProject(fileSystem);
    final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);

    await writeWindowsPluginFiles(flutterProject, <Plugin>[
      Plugin(
        name: 'test',
        path: 'foo',
47 48
        defaultPackagePlatforms: const <String, String>{},
        pluginDartClassPlatforms: const <String, String>{},
49 50 51 52 53 54
        platforms: const <String, PluginPlatform>{
          WindowsPlugin.kConfigKey: WindowsPlugin(
            name: 'test',
            pluginClass: 'Foo',
            variants: <PluginPlatformVariant>{PluginPlatformVariant.win32},
          )},
55
        dependencies: <String>[],
56
        isDirectDependency: true,
57 58 59 60 61 62 63 64 65 66 67 68
      ),
    ], renderer);

    final Directory managed = flutterProject.windows.managedDirectory;
    expect(flutterProject.windows.generatedPluginCmakeFile, exists);
    expect(managed.childFile('generated_plugin_registrant.h'), exists);
    expect(
      managed.childFile('generated_plugin_registrant.cc').readAsStringSync(),
      contains('#include <test/foo.h>'),
    );
  });

69
  testWithoutContext('UWP injects plugins marked as UWP-compatible', () async {
70 71 72 73 74 75 76 77
    final FileSystem fileSystem = MemoryFileSystem.test();
    setUpProject(fileSystem);
    final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);

    await writeWindowsUwpPluginFiles(flutterProject, <Plugin>[
      Plugin(
        name: 'test',
        path: 'foo',
78 79
        defaultPackagePlatforms: const <String, String>{},
        pluginDartClassPlatforms: const <String, String>{},
80 81 82 83 84 85
        platforms: const <String, PluginPlatform>{
          WindowsPlugin.kConfigKey: WindowsPlugin(
            name: 'test',
            pluginClass: 'Foo',
            variants: <PluginPlatformVariant>{PluginPlatformVariant.winuwp},
          )},
86
        dependencies: <String>[],
87
        isDirectDependency: true,
88 89 90 91 92 93 94 95 96 97 98
      ),
    ], renderer);

    final Directory managed = flutterProject.windowsUwp.managedDirectory;
    expect(flutterProject.windowsUwp.generatedPluginCmakeFile, exists);
    expect(managed.childFile('generated_plugin_registrant.h'), exists);
    expect(
      managed.childFile('generated_plugin_registrant.cc').readAsStringSync(),
      contains('#include <test/foo.h>'),
    );
  });
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
  testWithoutContext('UWP does not inject Win32-only plugins', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    setUpProject(fileSystem);
    final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);

    await writeWindowsUwpPluginFiles(flutterProject, <Plugin>[
      Plugin(
        name: 'test',
        path: 'foo',
        defaultPackagePlatforms: const <String, String>{},
        pluginDartClassPlatforms: const <String, String>{},
        platforms: const <String, PluginPlatform>{
          WindowsPlugin.kConfigKey: WindowsPlugin(
            name: 'test',
            pluginClass: 'Foo',
            variants: <PluginPlatformVariant>{PluginPlatformVariant.win32},
          )},
        dependencies: <String>[],
        isDirectDependency: true,
      ),
    ], renderer);

    final Directory managed = flutterProject.windowsUwp.managedDirectory;
    expect(flutterProject.windowsUwp.generatedPluginCmakeFile, exists);
    expect(managed.childFile('generated_plugin_registrant.h'), exists);
    expect(
      managed.childFile('generated_plugin_registrant.cc').readAsStringSync(),
      isNot(contains('#include <test/foo.h>')),
    );
  });

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  testWithoutContext('Symlink injection treats UWP as Win32', () {
    final FileSystem fileSystem = MemoryFileSystem.test();
    setUpProject(fileSystem);
    final FlutterProject flutterProject = FlutterProject.fromDirectoryTest(fileSystem.currentDirectory);
    flutterProject.flutterPluginsDependenciesFile.writeAsStringSync(kPluginDependencies);

    createPluginSymlinks(
      flutterProject,
      featureFlagsOverride: TestFeatureFlags(isWindowsUwpEnabled: true),
    );

    expect(flutterProject.windowsUwp.pluginSymlinkDirectory, exists);

    final Link link = flutterProject.windowsUwp.pluginSymlinkDirectory.listSync().single as Link;

    expect(link.path, '/winuwp/flutter/ephemeral/.plugin_symlinks/example');
    expect(link.targetSync(), r'C:\\example\\');
  });
149 150 151 152
}

void setUpProject(FileSystem fileSystem) {
  fileSystem.file('pubspec.yaml').createSync();
153 154
  fileSystem.file('winuwp/CMakeLists.txt')
    .createSync(recursive: true);
155 156 157 158
  fileSystem.file('winuwp/project_version')
    ..createSync(recursive: true)
    ..writeAsStringSync('0');
}