macos_test.dart 7.8 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
import 'package:file/memory.dart';
6
import 'package:file_testing/file_testing.dart';
7
import 'package:flutter_tools/src/artifacts.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9 10
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/build_info.dart';
11
import 'package:flutter_tools/src/build_system/build_system.dart';
12
import 'package:flutter_tools/src/build_system/targets/assets.dart';
13
import 'package:flutter_tools/src/build_system/targets/common.dart';
14
import 'package:flutter_tools/src/build_system/targets/macos.dart';
15
import 'package:flutter_tools/src/convert.dart';
16

17
import '../../../src/common.dart';
18
import '../../../src/context.dart';
19
import '../../../src/fake_process_manager.dart';
20

21
void main() {
22
  Environment environment;
23 24 25
  FileSystem fileSystem;
  Artifacts artifacts;
  FakeProcessManager processManager;
26

27
  setUp(() {
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    processManager = FakeProcessManager.any();
    artifacts = Artifacts.test();
    fileSystem = MemoryFileSystem.test();
    environment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'debug',
        kTargetPlatform: 'darwin-x64',
      },
      inputs: <String, String>{},
      artifacts: artifacts,
      processManager: processManager,
      logger: BufferLogger.test(),
      fileSystem: fileSystem,
      engineVersion: '2'
    );
44
  });
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  testUsingContext('Copies files to correct cache directory', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      const FakeCommand(
        command: <String>[
          'cp',
          '-R',
          'Artifact.flutterMacOSFramework.debug',
          '/FlutterMacOS.framework',
        ],
      ),
    ]);
    environment = Environment.test(
      fileSystem.currentDirectory,
      defines: <String, String>{
        kBuildMode: 'debug',
        kTargetPlatform: 'darwin-x64',
      },
      inputs: <String, String>{},
      artifacts: artifacts,
      processManager: processManager,
      logger: BufferLogger.test(),
      fileSystem: fileSystem,
      engineVersion: '2'
    );

    final Directory cacheDirectory = fileSystem.directory(
      artifacts.getArtifactPath(
        Artifact.flutterMacOSFramework,
        mode: BuildMode.debug,
      ))
      ..createSync();
    cacheDirectory.childFile('dummy').createSync();
    environment.buildDir.createSync(recursive: true);
    environment.outputDir.createSync(recursive: true);

81
    await const DebugUnpackMacOS().build(environment);
82

83 84 85 86 87
    expect(processManager.hasRemainingExpectations, false);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
88

89 90 91 92 93 94 95 96 97
  testUsingContext('debug macOS application fails if App.framework missing', () async {
    fileSystem.directory(
      artifacts.getArtifactPath(
        Artifact.flutterMacOSFramework,
        mode: BuildMode.debug,
      ))
      .createSync();
    final String inputKernel = fileSystem.path.join(environment.buildDir.path, 'app.dill');
    fileSystem.file(inputKernel)
98 99 100
      ..createSync(recursive: true)
      ..writeAsStringSync('testing');

101
    expect(() async => await const DebugMacOSBundleFlutterAssets().build(environment),
Dan Field's avatar
Dan Field committed
102
        throwsException);
103 104 105 106
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
107

108 109 110 111 112 113 114
  testUsingContext('debug macOS application creates correctly structured framework', () async {
    fileSystem.directory(
      artifacts.getArtifactPath(
        Artifact.flutterMacOSFramework,
        mode: BuildMode.debug,
      ))
      .createSync();
115
    environment.inputs[kBundleSkSLPath] = 'bundle.sksl';
116 117 118 119 120 121 122 123 124 125 126 127 128
    fileSystem.file(
      artifacts.getArtifactPath(
        Artifact.vmSnapshotData,
        platform: TargetPlatform.darwin_x64,
        mode: BuildMode.debug,
      )).createSync(recursive: true);
    fileSystem.file(
      artifacts.getArtifactPath(
        Artifact.isolateSnapshotData,
        platform: TargetPlatform.darwin_x64,
        mode: BuildMode.debug,
      )).createSync(recursive: true);
    fileSystem.file('${environment.buildDir.path}/App.framework/App')
129 130
      .createSync(recursive: true);
    // sksl bundle
131
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
132 133 134 135 136 137 138 139
      <String, Object>{
        'engineRevision': '2',
        'platform': 'ios',
        'data': <String, Object>{
          'A': 'B',
        }
      }
    ));
140

141
    final String inputKernel = '${environment.buildDir.path}/app.dill';
142
    fileSystem.file(inputKernel)
143 144 145
      ..createSync(recursive: true)
      ..writeAsStringSync('testing');

146
    await const DebugMacOSBundleFlutterAssets().build(environment);
147

148
    expect(fileSystem.file(
149 150 151
      'App.framework/Versions/A/Resources/flutter_assets/kernel_blob.bin').readAsStringSync(),
      'testing',
    );
152
    expect(fileSystem.file(
153 154 155
      'App.framework/Versions/A/Resources/Info.plist').readAsStringSync(),
      contains('io.flutter.flutter.app'),
    );
156
    expect(fileSystem.file(
157 158 159
      'App.framework/Versions/A/Resources/flutter_assets/vm_snapshot_data'),
      exists,
    );
160
    expect(fileSystem.file(
161 162 163 164
      'App.framework/Versions/A/Resources/flutter_assets/isolate_snapshot_data'),
      exists,
    );

165
    final File skslFile = fileSystem.file('App.framework/Versions/A/Resources/flutter_assets/io.flutter.shaders.json');
166 167 168

    expect(skslFile, exists);
    expect(skslFile.readAsStringSync(), '{"data":{"A":"B"}}');
169 170 171 172
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
173

174 175
  testUsingContext('release/profile macOS application has no blob or precompiled runtime', () async {
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/vm_isolate_snapshot.bin')
176
      .createSync(recursive: true);
177
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/isolate_snapshot.bin')
178
      .createSync(recursive: true);
179
    fileSystem.file('${environment.buildDir.path}/App.framework/App')
180 181
      .createSync(recursive: true);

182
    await const ProfileMacOSBundleFlutterAssets().build(environment..defines[kBuildMode] = 'profile');
183

184
    expect(fileSystem.file(
185 186 187
      'App.framework/Versions/A/Resources/flutter_assets/kernel_blob.bin'),
      isNot(exists),
    );
188
    expect(fileSystem.file(
189 190 191
      'App.framework/Versions/A/Resources/flutter_assets/vm_snapshot_data'),
      isNot(exists),
    );
192
    expect(fileSystem.file(
193 194 195
      'App.framework/Versions/A/Resources/flutter_assets/isolate_snapshot_data'),
      isNot(exists),
    );
196 197 198 199
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
200

201 202
  testUsingContext('release/profile macOS application updates when App.framework updates', () async {
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/vm_isolate_snapshot.bin')
203
      .createSync(recursive: true);
204
    fileSystem.file('bin/cache/artifacts/engine/darwin-x64/isolate_snapshot.bin')
205
      .createSync(recursive: true);
206
    final File inputFramework = fileSystem.file(fileSystem.path.join(environment.buildDir.path, 'App.framework', 'App'))
207 208
      ..createSync(recursive: true)
      ..writeAsStringSync('ABC');
209 210

    await const ProfileMacOSBundleFlutterAssets().build(environment..defines[kBuildMode] = 'profile');
211
    final File outputFramework = fileSystem.file(fileSystem.path.join(environment.outputDir.path, 'App.framework', 'App'));
212 213 214 215 216 217 218

    expect(outputFramework.readAsStringSync(), 'ABC');

    inputFramework.writeAsStringSync('DEF');
    await const ProfileMacOSBundleFlutterAssets().build(environment..defines[kBuildMode] = 'profile');

    expect(outputFramework.readAsStringSync(), 'DEF');
219 220 221 222
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => processManager,
  });
223
}