android_studio_test.dart 6.48 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 6 7
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_studio.dart';
import 'package:flutter_tools/src/base/file_system.dart';
8
import 'package:flutter_tools/src/base/platform.dart';
9
import 'package:flutter_tools/src/globals.dart' as globals;
10
import 'package:flutter_tools/src/ios/plist_parser.dart';
11
import 'package:mockito/mockito.dart';
12

13 14
import '../../src/common.dart';
import '../../src/context.dart';
15

16 17 18
const String homeLinux = '/home/me';
const String homeMac = '/Users/me';

19 20 21 22 23 24 25 26 27 28 29
const Map<String, dynamic> macStudioInfoPlist = <String, dynamic>{
  'CFBundleGetInfoString': 'Android Studio 3.3, build AI-182.5107.16.33.5199772. Copyright JetBrains s.r.o., (c) 2000-2018',
  'CFBundleShortVersionString': '3.3',
  'CFBundleVersion': 'AI-182.5107.16.33.5199772',
  'JVMOptions': <String, dynamic>{
    'Properties': <String, dynamic>{
      'idea.paths.selector': 'AndroidStudio3.3',
      'idea.platform.prefix': 'AndroidStudio',
    },
  },
};
30

31 32 33 34
final Platform linuxPlatform = FakePlatform(
  operatingSystem: 'linux',
  environment: <String, String>{'HOME': homeLinux},
);
35

36
class MockPlistUtils extends Mock implements PlistParser {}
37

38
Platform macPlatform() {
39 40 41 42
  return FakePlatform(
    operatingSystem: 'macos',
    environment: <String, String>{'HOME': homeMac},
  );
43
}
44

45
void main() {
46
  FileSystem fileSystem;
47 48

  setUp(() {
49
    fileSystem = MemoryFileSystem.test();
50 51
  });

52 53 54 55 56 57 58
  testUsingContext('pluginsPath on Linux extracts custom paths from home dir', () {
    const String installPath = '/opt/android-studio-with-cheese-5.0';
    const String studioHome = '$homeLinux/.AndroidStudioWithCheese5.0';
    const String homeFile = '$studioHome/system/.home';
    globals.fs.directory(installPath).createSync(recursive: true);
    globals.fs.file(homeFile).createSync(recursive: true);
    globals.fs.file(homeFile).writeAsStringSync(installPath);
59

60
    final AndroidStudio studio =
61
      AndroidStudio.fromHomeDot(globals.fs.directory(studioHome));
62 63 64 65 66 67 68 69 70 71 72 73 74
    expect(studio, isNotNull);
    expect(studio.pluginsPath,
        equals('/home/me/.AndroidStudioWithCheese5.0/config/plugins'));
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
    // Custom home paths are not supported on macOS nor Windows yet,
    // so we force the platform to fake Linux here.
    Platform: () => linuxPlatform,
    FileSystemUtils: () => FileSystemUtils(
      fileSystem: fileSystem,
      platform: linuxPlatform,
    ),
75
  });
76 77

  group('pluginsPath on Mac', () {
78 79
    FileSystemUtils fsUtils;
    Platform platform;
80
    MockPlistUtils plistUtils;
81 82

    setUp(() {
83
      plistUtils = MockPlistUtils();
84 85
      platform = macPlatform();
      fsUtils = FileSystemUtils(
86
        fileSystem: fileSystem,
87 88 89 90
        platform: platform,
      );
    });

91
    testUsingContext('extracts custom paths for directly downloaded Android Studio on Mac', () {
92 93 94 95 96 97
      final String studioInApplicationPlistFolder = globals.fs.path.join(
        '/',
        'Application',
        'Android Studio.app',
        'Contents',
      );
98
      globals.fs.directory(studioInApplicationPlistFolder).createSync(recursive: true);
99

100
      final String plistFilePath = globals.fs.path.join(studioInApplicationPlistFolder, 'Info.plist');
101
      when(plistUtils.parseFile(plistFilePath)).thenReturn(macStudioInfoPlist);
102 103 104
      final AndroidStudio studio = AndroidStudio.fromMacOSBundle(
        globals.fs.directory(studioInApplicationPlistFolder)?.parent?.path,
      );
105
      expect(studio, isNotNull);
106 107 108 109 110 111
      expect(studio.pluginsPath, equals(globals.fs.path.join(
        homeMac,
        'Library',
        'Application Support',
        'AndroidStudio3.3',
      )));
112
    }, overrides: <Type, Generator>{
113
      FileSystem: () => fileSystem,
114
      FileSystemUtils: () => fsUtils,
115
      ProcessManager: () => FakeProcessManager.any(),
116 117
      // Custom home paths are not supported on macOS nor Windows yet,
      // so we force the platform to fake Linux here.
118
      Platform: () => platform,
119
      PlistParser: () => plistUtils,
120 121 122
    });

    testUsingContext('extracts custom paths for Android Studio downloaded by JetBrainsToolbox on Mac', () {
123 124 125 126 127 128 129
      final String jetbrainsStudioInApplicationPlistFolder = globals.fs.path.join(
        homeMac,
        'Application',
        'JetBrains Toolbox',
        'Android Studio.app',
        'Contents',
      );
130
      globals.fs.directory(jetbrainsStudioInApplicationPlistFolder).createSync(recursive: true);
131 132 133 134 135 136
      const Map<String, dynamic> jetbrainsInfoPlist = <String, dynamic>{
        'CFBundleLongVersionString': '3.3',
        'CFBundleShortVersionString': '3.3',
        'CFBundleVersion': '3.3',
        'JetBrainsToolboxApp': '$homeMac/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/183.5256920/Android Studio 3.3.app',
      };
137 138 139 140
      final String jetbrainsPlistFilePath = globals.fs.path.join(
        jetbrainsStudioInApplicationPlistFolder,
        'Info.plist',
      );
141
      when(plistUtils.parseFile(jetbrainsPlistFilePath)).thenReturn(jetbrainsInfoPlist);
142

143 144 145 146 147 148 149 150 151 152
      final String studioInApplicationPlistFolder = globals.fs.path.join(
        globals.fs.path.join(homeMac,'Library','Application Support'),
        'JetBrains',
        'Toolbox',
        'apps',
        'AndroidStudio',
        'ch-0',
        '183.5256920',
        globals.fs.path.join('Android Studio 3.3.app', 'Contents'),
      );
153
      globals.fs.directory(studioInApplicationPlistFolder).createSync(recursive: true);
154 155 156 157
      final String studioPlistFilePath = globals.fs.path.join(
        studioInApplicationPlistFolder,
        'Info.plist',
      );
158
      when(plistUtils.parseFile(studioPlistFilePath)).thenReturn(macStudioInfoPlist);
159

160 161 162
      final AndroidStudio studio = AndroidStudio.fromMacOSBundle(
        globals.fs.directory(jetbrainsStudioInApplicationPlistFolder)?.parent?.path,
      );
163
      expect(studio, isNotNull);
164 165 166 167 168 169
      expect(studio.pluginsPath, equals(globals.fs.path.join(
        homeMac,
        'Library',
        'Application Support',
        'AndroidStudio3.3',
      )));
170
    }, overrides: <Type, Generator>{
171
      FileSystem: () => fileSystem,
172
      FileSystemUtils: () => fsUtils,
173
      ProcessManager: () => FakeProcessManager.any(),
174 175
      // Custom home paths are not supported on macOS nor Windows yet,
      // so we force the platform to fake Linux here.
176
      Platform: () => platform,
177
      PlistParser: () => plistUtils,
178 179
    });
  });
180
}