platform_plugins_test.dart 2.63 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
// 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);
    });

26
    testUsingContext("throws tool exit if the plugin main class can't be read", () {
27 28 29 30 31 32 33 34 35 36 37
      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);
38
      when(pluginJavaMainClass.readAsStringSync(encoding: anyNamed('encoding'))).thenThrow(const FileSystemException());
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
      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(
55
        message: "Couldn't read file null even though it exists. "
56 57 58 59 60 61 62 63 64 65 66 67
                 '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 {}