build_test.dart 40.1 KB
Newer Older
1 2 3 4
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6

7
import 'package:archive/archive.dart';
8
import 'package:file/memory.dart';
9
import 'package:flutter_tools/src/android/android_sdk.dart';
10
import 'package:flutter_tools/src/artifacts.dart';
11
import 'package:flutter_tools/src/build_info.dart';
12
import 'package:flutter_tools/src/base/build.dart';
13
import 'package:flutter_tools/src/base/context.dart';
14
import 'package:flutter_tools/src/base/file_system.dart';
15
import 'package:flutter_tools/src/base/io.dart';
16
import 'package:flutter_tools/src/base/logger.dart';
17
import 'package:flutter_tools/src/base/process.dart';
18
import 'package:flutter_tools/src/ios/mac.dart';
19 20
import 'package:flutter_tools/src/version.dart';
import 'package:mockito/mockito.dart';
21

22
import '../src/common.dart';
23 24
import '../src/context.dart';

25
class MockFlutterVersion extends Mock implements FlutterVersion {}
26
class MockAndroidSdk extends Mock implements AndroidSdk {}
27
class MockArtifacts extends Mock implements Artifacts {}
28
class MockXcode extends Mock implements Xcode {}
29

30 31
class _FakeGenSnapshot implements GenSnapshot {
  _FakeGenSnapshot({
32
    this.succeed = true,
33 34 35
  });

  final bool succeed;
36
  Map<String, String> outputs = <String, String>{};
37
  int _callCount = 0;
38 39 40
  SnapshotType _snapshotType;
  String _depfilePath;
  List<String> _additionalArgs;
41 42 43

  int get callCount => _callCount;

44 45 46 47 48 49
  SnapshotType get snapshotType => _snapshotType;

  String get depfilePath => _depfilePath;

  List<String> get additionalArgs => _additionalArgs;

50 51 52 53
  @override
  Future<int> run({
    SnapshotType snapshotType,
    String depfilePath,
54
    IOSArch iosArch,
55
    Iterable<String> additionalArgs = const <String>[],
56 57
  }) async {
    _callCount += 1;
58 59 60
    _snapshotType = snapshotType;
    _depfilePath = depfilePath;
    _additionalArgs = additionalArgs.toList();
61 62 63

    if (!succeed)
      return 1;
64 65 66
    outputs.forEach((String filePath, String fileContent) {
      fs.file(filePath).writeAsString(fileContent);
    });
67 68 69 70
    return 0;
  }
}

71
void main() {
72 73 74
  group('SnapshotType', () {
    test('throws, if build mode is null', () {
      expect(
75
        () => SnapshotType(TargetPlatform.android_x64, null),
76 77 78 79
        throwsA(anything),
      );
    });
    test('does not throw, if target platform is null', () {
80
      expect(SnapshotType(null, BuildMode.release), isNotNull);
81 82
    });
  });
83

84 85 86 87 88 89
  group('Snapshotter - iOS AOT', () {
    const String kSnapshotDart = 'snapshot.dart';
    String skyEnginePath;

    _FakeGenSnapshot genSnapshot;
    MemoryFileSystem fs;
90
    AOTSnapshotter snapshotter;
91
    AOTSnapshotter snapshotterWithTimings;
92
    MockAndroidSdk mockAndroidSdk;
93 94
    MockArtifacts mockArtifacts;
    MockXcode mockXcode;
95
    BufferLogger bufferLogger;
96 97

    setUp(() async {
98
      fs = MemoryFileSystem();
99 100 101
      fs.file(kSnapshotDart).createSync();
      fs.file('.packages').writeAsStringSync('sky_engine:file:///flutter/bin/cache/pkg/sky_engine/lib/');

102
      skyEnginePath = fs.path.fromUri(Uri.file('/flutter/bin/cache/pkg/sky_engine'));
103 104 105 106 107 108
      fs.directory(fs.path.join(skyEnginePath, 'lib', 'ui')).createSync(recursive: true);
      fs.directory(fs.path.join(skyEnginePath, 'sdk_ext')).createSync(recursive: true);
      fs.file(fs.path.join(skyEnginePath, '.packages')).createSync();
      fs.file(fs.path.join(skyEnginePath, 'lib', 'ui', 'ui.dart')).createSync();
      fs.file(fs.path.join(skyEnginePath, 'sdk_ext', 'vmservice_io.dart')).createSync();

109 110
      genSnapshot = _FakeGenSnapshot();
      snapshotter = AOTSnapshotter();
111
      snapshotterWithTimings = AOTSnapshotter(reportTimings: true);
112 113 114
      mockAndroidSdk = MockAndroidSdk();
      mockArtifacts = MockArtifacts();
      mockXcode = MockXcode();
115
      bufferLogger = BufferLogger();
116
      for (BuildMode mode in BuildMode.values) {
117 118
        when(mockArtifacts.getArtifactPath(Artifact.snapshotDart,
            platform: anyNamed('platform'), mode: mode)).thenReturn(kSnapshotDart);
119 120 121 122
      }
    });

    final Map<Type, Generator> contextOverrides = <Type, Generator>{
123
      AndroidSdk: () => mockAndroidSdk,
124 125 126 127
      Artifacts: () => mockArtifacts,
      FileSystem: () => fs,
      GenSnapshot: () => genSnapshot,
      Xcode: () => mockXcode,
128
      Logger: () => bufferLogger,
129 130
    };

131
    testUsingContext('iOS debug AOT snapshot is invalid', () async {
132
      final String outputPath = fs.path.join('build', 'foo');
133
      expect(await snapshotter.build(
134 135
        platform: TargetPlatform.ios,
        buildMode: BuildMode.debug,
136
        mainPath: 'main.dill',
137 138
        packagesPath: '.packages',
        outputPath: outputPath,
139
        buildSharedLibrary: false,
140
      ), isNot(equals(0)));
141 142
    }, overrides: contextOverrides);

143
    testUsingContext('Android arm debug AOT snapshot is invalid', () async {
144 145 146 147 148 149 150
      final String outputPath = fs.path.join('build', 'foo');
      expect(await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.debug,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
151
        buildSharedLibrary: false,
152 153 154
      ), isNot(0));
    }, overrides: contextOverrides);

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    testUsingContext('Android arm64 debug AOT snapshot is invalid', () async {
      final String outputPath = fs.path.join('build', 'foo');
      expect(await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.debug,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
      ), isNot(0));
    }, overrides: contextOverrides);

    testUsingContext('builds iOS armv7 profile AOT snapshot', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'snapshot_assembly.S'): '',
      };

177 178 179
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.ios,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
        iosArch: IOSArch.armv7,
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.ios);
      expect(genSnapshot.snapshotType.mode, BuildMode.profile);
      expect(genSnapshot.additionalArgs, <String>[
196
        '--deterministic',
197 198 199 200 201 202 203 204 205
        '--snapshot_kind=app-aot-assembly',
        '--assembly=${fs.path.join(outputPath, 'snapshot_assembly.S')}',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

    testUsingContext('builds iOS arm64 profile AOT snapshot', () async {
206
      fs.file('main.dill').writeAsStringSync('binary magic');
207 208 209 210 211 212 213 214

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'snapshot_assembly.S'): '',
      };

215 216 217
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
218

219
      final int genSnapshotExitCode = await snapshotter.build(
220 221
        platform: TargetPlatform.ios,
        buildMode: BuildMode.profile,
222
        mainPath: 'main.dill',
223 224
        packagesPath: '.packages',
        outputPath: outputPath,
225
        buildSharedLibrary: false,
226
        iosArch: IOSArch.arm64,
227 228 229 230 231 232 233
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.ios);
      expect(genSnapshot.snapshotType.mode, BuildMode.profile);
      expect(genSnapshot.additionalArgs, <String>[
234
        '--deterministic',
235 236 237 238 239 240
        '--snapshot_kind=app-aot-assembly',
        '--assembly=${fs.path.join(outputPath, 'snapshot_assembly.S')}',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

241 242 243 244 245 246 247 248 249 250 251 252 253
    testUsingContext('builds Android arm profile AOT snapshot', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'vm_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'vm_snapshot_instr'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

254 255 256
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
      expect(genSnapshot.snapshotType.mode, BuildMode.profile);
      expect(genSnapshot.additionalArgs, <String>[
272
        '--deterministic',
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        '--snapshot_kind=app-aot-blobs',
        '--vm_snapshot_data=build/foo/vm_snapshot_data',
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--vm_snapshot_instructions=build/foo/vm_snapshot_instr',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

    testUsingContext('builds Android arm64 profile AOT snapshot', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'vm_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'vm_snapshot_instr'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

297 298 299
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm64);
      expect(genSnapshot.snapshotType.mode, BuildMode.profile);
      expect(genSnapshot.additionalArgs, <String>[
315
        '--deterministic',
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        '--snapshot_kind=app-aot-blobs',
        '--vm_snapshot_data=build/foo/vm_snapshot_data',
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--vm_snapshot_instructions=build/foo/vm_snapshot_instr',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

    testUsingContext('builds iOS release armv7 AOT snapshot', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'snapshot_assembly.S'): '',
      };

335 336 337
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.ios,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
        iosArch: IOSArch.armv7,
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.ios);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
354
        '--deterministic',
355 356 357 358 359 360 361 362 363
        '--snapshot_kind=app-aot-assembly',
        '--assembly=${fs.path.join(outputPath, 'snapshot_assembly.S')}',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

    testUsingContext('builds iOS release arm64 AOT snapshot', () async {
364
      fs.file('main.dill').writeAsStringSync('binary magic');
365 366 367 368 369 370 371 372

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'snapshot_assembly.S'): '',
      };

373 374 375
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
376

377
      final int genSnapshotExitCode = await snapshotter.build(
378 379
        platform: TargetPlatform.ios,
        buildMode: BuildMode.release,
380
        mainPath: 'main.dill',
381 382
        packagesPath: '.packages',
        outputPath: outputPath,
383
        buildSharedLibrary: false,
384
        iosArch: IOSArch.arm64,
385 386 387 388 389 390 391
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.ios);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
392
        '--deterministic',
393 394 395 396 397
        '--snapshot_kind=app-aot-assembly',
        '--assembly=${fs.path.join(outputPath, 'snapshot_assembly.S')}',
        'main.dill',
      ]);
    }, overrides: contextOverrides);
398 399 400 401

    testUsingContext('returns failure if buildSharedLibrary is true but no NDK is found', () async {
      final String outputPath = fs.path.join('build', 'foo');

402
      when(mockAndroidSdk.ndk).thenReturn(null);
403 404 405 406 407 408 409 410 411 412 413 414 415

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: true,
      );

      expect(genSnapshotExitCode, isNot(0));
      expect(genSnapshot.callCount, 0);
    }, overrides: contextOverrides);
416 417 418 419 420 421 422 423 424 425 426 427 428 429

    testUsingContext('builds Android arm release AOT snapshot', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'vm_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'vm_snapshot_instr'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

430 431 432
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
448
        '--deterministic',
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
        '--snapshot_kind=app-aot-blobs',
        '--vm_snapshot_data=build/foo/vm_snapshot_data',
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--vm_snapshot_instructions=build/foo/vm_snapshot_instr',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

    testUsingContext('builds Android arm64 release AOT snapshot', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'vm_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'vm_snapshot_instr'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

473 474 475
      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm64);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
491
        '--deterministic',
492 493 494 495 496 497 498 499 500
        '--snapshot_kind=app-aot-blobs',
        '--vm_snapshot_data=build/foo/vm_snapshot_data',
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--vm_snapshot_instructions=build/foo/vm_snapshot_instr',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    testUsingContext('reports timing', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'vm_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'vm_snapshot_instr'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

      final RunResult successResult = RunResult(ProcessResult(1, 0, '', ''), <String>['command name', 'arguments...']);
      when(xcode.cc(any)).thenAnswer((_) => Future<RunResult>.value(successResult));
      when(xcode.clang(any)).thenAnswer((_) => Future<RunResult>.value(successResult));

      final int genSnapshotExitCode = await snapshotterWithTimings.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
        buildSharedLibrary: false,
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(bufferLogger.statusText, matches(RegExp(r'gen_snapshot\(RunTime\): \d+ ms.')));
    }, overrides: contextOverrides);
531
  });
532

533
  group('Snapshotter - JIT', () {
534
    const String kTrace = 'trace.txt';
535 536
    const String kEngineVmSnapshotData = 'engine_vm_snapshot_data';
    const String kEngineIsolateSnapshotData = 'engine_isolate_snapshot_data';
537 538 539

    _FakeGenSnapshot genSnapshot;
    MemoryFileSystem fs;
540
    JITSnapshotter snapshotter;
541 542 543 544
    MockAndroidSdk mockAndroidSdk;
    MockArtifacts mockArtifacts;

    setUp(() async {
545
      fs = MemoryFileSystem();
546
      fs.file(kTrace).createSync();
547 548
      fs.file(kEngineVmSnapshotData).createSync();
      fs.file(kEngineIsolateSnapshotData).createSync();
549

550
      genSnapshot = _FakeGenSnapshot();
551
      snapshotter = JITSnapshotter();
552 553
      mockAndroidSdk = MockAndroidSdk();
      mockArtifacts = MockArtifacts();
554 555

      for (BuildMode mode in BuildMode.values) {
556 557
        when(mockArtifacts.getArtifactPath(Artifact.vmSnapshotData,
            platform: anyNamed('platform'), mode: mode))
558
            .thenReturn(kEngineVmSnapshotData);
559 560
        when(mockArtifacts.getArtifactPath(Artifact.isolateSnapshotData,
            platform: anyNamed('platform'), mode: mode))
561 562
            .thenReturn(kEngineIsolateSnapshotData);
      }
563 564 565 566 567 568 569 570 571
    });

    final Map<Type, Generator> contextOverrides = <Type, Generator>{
      AndroidSdk: () => mockAndroidSdk,
      Artifacts: () => mockArtifacts,
      FileSystem: () => fs,
      GenSnapshot: () => genSnapshot,
    };

572
    testUsingContext('iOS debug JIT snapshot is invalid', () async {
573 574 575 576 577 578 579
      final String outputPath = fs.path.join('build', 'foo');
      expect(await snapshotter.build(
        platform: TargetPlatform.ios,
        buildMode: BuildMode.debug,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
580
        compilationTraceFilePath: kTrace,
581
        createPatch: false,
582 583 584
      ), isNot(equals(0)));
    }, overrides: contextOverrides);

585
    testUsingContext('builds Android arm debug JIT snapshot', () async {
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.debug,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
602
        compilationTraceFilePath: kTrace,
603
        createPatch: false,
604 605 606 607 608 609 610
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
      expect(genSnapshot.snapshotType.mode, BuildMode.debug);
      expect(genSnapshot.additionalArgs, <String>[
611
        '--deterministic',
612
        '--enable_asserts',
613 614 615 616
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
617 618 619 620 621 622 623 624
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

625
    testUsingContext('builds Android arm64 debug JIT snapshot', () async {
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.debug,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
642
        compilationTraceFilePath: kTrace,
643
        createPatch: false,
644 645 646 647 648 649 650
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm64);
      expect(genSnapshot.snapshotType.mode, BuildMode.debug);
      expect(genSnapshot.additionalArgs, <String>[
651
        '--deterministic',
652
        '--enable_asserts',
653 654 655 656
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
657 658 659 660 661 662
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

663
    testUsingContext('iOS release JIT snapshot is invalid', () async {
664 665 666 667 668 669 670
      final String outputPath = fs.path.join('build', 'foo');
      expect(await snapshotter.build(
        platform: TargetPlatform.ios,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
671
        compilationTraceFilePath: kTrace,
672
        createPatch: false,
673 674 675
      ), isNot(equals(0)));
    }, overrides: contextOverrides);

676
    testUsingContext('builds Android arm profile JIT snapshot', () async {
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
693
        compilationTraceFilePath: kTrace,
694
        createPatch: false,
695 696 697 698 699 700 701
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
      expect(genSnapshot.snapshotType.mode, BuildMode.profile);
      expect(genSnapshot.additionalArgs, <String>[
702
        '--deterministic',
703 704 705 706
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
707 708 709 710 711 712 713 714
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

715
    testUsingContext('builds Android arm64 profile JIT snapshot', () async {
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.profile,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
732
        compilationTraceFilePath: kTrace,
733
        createPatch: false,
734 735 736 737 738 739 740
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm64);
      expect(genSnapshot.snapshotType.mode, BuildMode.profile);
      expect(genSnapshot.additionalArgs, <String>[
741
        '--deterministic',
742 743 744 745
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
746 747 748 749 750 751
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

752
    testUsingContext('iOS release JIT snapshot is invalid', () async {
753 754 755 756 757 758 759
      final String outputPath = fs.path.join('build', 'foo');
      expect(await snapshotter.build(
        platform: TargetPlatform.ios,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
760
        compilationTraceFilePath: kTrace,
761
        createPatch: false,
762 763 764
      ), isNot(equals(0)));
    }, overrides: contextOverrides);

765
    testUsingContext('builds Android arm release JIT snapshot', () async {
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
782
        compilationTraceFilePath: kTrace,
783
        createPatch: false,
784 785 786 787 788 789 790
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
791
        '--deterministic',
792 793 794 795
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
796 797 798 799 800 801 802 803
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

804
    testUsingContext('builds Android arm64 release JIT snapshot', () async {
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
      fs.file('main.dill').writeAsStringSync('binary magic');

      final String outputPath = fs.path.join('build', 'foo');
      fs.directory(outputPath).createSync(recursive: true);

      genSnapshot.outputs = <String, String>{
        fs.path.join(outputPath, 'isolate_snapshot_data'): '',
        fs.path.join(outputPath, 'isolate_snapshot_instr'): '',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm64,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: outputPath,
821
        compilationTraceFilePath: kTrace,
822
        createPatch: false,
823 824 825 826 827 828 829
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm64);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
830
        '--deterministic',
831 832 833 834
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
835 836
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--isolate_snapshot_instructions=build/foo/isolate_snapshot_instr',
837 838 839 840
        'main.dill',
      ]);
    }, overrides: contextOverrides);

841
    testUsingContext('builds Android release JIT dynamic patch - existing snapshot', () async {
842 843
      fs.file('main.dill').writeAsStringSync('binary magic');

844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
      final Archive baselineApk = Archive()
          ..addFile(ArchiveFile('assets/flutter_assets/isolate_snapshot_instr',
            'isolateSnapshotInstr'.length, 'isolateSnapshotInstr'.codeUnits))
          ..addFile(ArchiveFile('assets/flutter_assets/vm_snapshot_data',
            'engineVmSnapshotData'.length, 'engineVmSnapshotData'.codeUnits));

      fs.file('.baseline/100.apk')
          ..createSync(recursive: true)
          ..writeAsBytesSync(ZipEncoder().encode(baselineApk), flush: true);

      fs.file('engine_vm_snapshot_data')
          ..createSync(recursive: true)
          ..writeAsStringSync('engineVmSnapshotData', flush: true);

      fs.file('build/foo/isolate_snapshot_instr')
          ..createSync(recursive: true)
          ..writeAsStringSync('isolateSnapshotInstr', flush: true);
861 862

      genSnapshot.outputs = <String, String>{
863 864
        'build/foo/isolate_snapshot_data': '',
        'build/foo/snapshot.d': 'build/foo/vm_snapshot_data : ',
865 866 867 868 869 870 871
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
872
        outputPath: 'build/foo',
873
        compilationTraceFilePath: kTrace,
874
        createPatch: true,
875
        buildNumber: '100',
876
        baselineDir: '.baseline',
877 878 879 880 881 882 883
      );

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
884
        '--deterministic',
885 886 887 888 889 890 891 892
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--reused_instructions=build/foo/isolate_snapshot_instr',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
893 894 895 896
        'main.dill',
      ]);
    }, overrides: contextOverrides);

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    testUsingContext('builds Android release JIT dynamic patch - extracts snapshot', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final Archive baselineApk = Archive()
        ..addFile(ArchiveFile('assets/flutter_assets/isolate_snapshot_instr',
            'isolateSnapshotInstr'.length, 'isolateSnapshotInstr'.codeUnits))
        ..addFile(ArchiveFile('assets/flutter_assets/vm_snapshot_data',
            'engineVmSnapshotData'.length, 'engineVmSnapshotData'.codeUnits));

      fs.file('.baseline/100.apk')
        ..createSync(recursive: true)
        ..writeAsBytesSync(ZipEncoder().encode(baselineApk), flush: true);

      fs.file('engine_vm_snapshot_data')
        ..createSync(recursive: true)
        ..writeAsStringSync('engineVmSnapshotData', flush: true);

      genSnapshot.outputs = <String, String>{
        'build/foo/isolate_snapshot_data': '',
        'build/foo/snapshot.d': 'build/foo/vm_snapshot_data : ',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: 'build/foo',
        compilationTraceFilePath: kTrace,
        createPatch: true,
927
        buildNumber: '100',
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
        baselineDir: '.baseline',
      );

      // The file was extracted from baseline APK.
      expect(fs.file('build/foo/isolate_snapshot_instr').existsSync(), true);
      expect(fs.file('build/foo/isolate_snapshot_instr').readAsStringSync(), 'isolateSnapshotInstr');

      expect(genSnapshotExitCode, 0);
      expect(genSnapshot.callCount, 1);
      expect(genSnapshot.snapshotType.platform, TargetPlatform.android_arm);
      expect(genSnapshot.snapshotType.mode, BuildMode.release);
      expect(genSnapshot.additionalArgs, <String>[
        '--deterministic',
        '--snapshot_kind=app-jit',
        '--load_compilation_trace=$kTrace',
        '--load_vm_snapshot_data=$kEngineVmSnapshotData',
        '--load_isolate_snapshot_data=$kEngineIsolateSnapshotData',
        '--isolate_snapshot_data=build/foo/isolate_snapshot_data',
        '--reused_instructions=build/foo/isolate_snapshot_instr',
        '--no-sim-use-hardfp',
        '--no-use-integer-division',
        'main.dill',
      ]);
    }, overrides: contextOverrides);

    testUsingContext('builds Android release JIT dynamic patch - mismatched snapshot 1', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final Archive baselineApk = Archive()
        ..addFile(ArchiveFile('assets/flutter_assets/isolate_snapshot_instr',
            'isolateSnapshotInstr'.length, 'isolateSnapshotInstr'.codeUnits))
        ..addFile(ArchiveFile('assets/flutter_assets/vm_snapshot_data',
            'engineVmSnapshotData'.length, 'engineVmSnapshotData'.codeUnits));

      fs.file('.baseline/100.apk')
        ..createSync(recursive: true)
        ..writeAsBytesSync(ZipEncoder().encode(baselineApk), flush: true);

      fs.file('engine_vm_snapshot_data')
        ..createSync(recursive: true)
        ..writeAsStringSync('mismatchedEngineVmSnapshotData', flush: true);

      fs.file('build/foo/isolate_snapshot_instr')
        ..createSync(recursive: true)
        ..writeAsStringSync('isolateSnapshotInstr', flush: true);

      genSnapshot.outputs = <String, String>{
        'build/foo/isolate_snapshot_data': '',
        'build/foo/snapshot.d': 'build/foo/vm_snapshot_data : ',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: 'build/foo',
        compilationTraceFilePath: kTrace,
        createPatch: true,
987
        buildNumber: '100',
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
        baselineDir: '.baseline',
      );

      expect(genSnapshotExitCode, 1);
      expect(genSnapshot.callCount, 0);

    }, overrides: contextOverrides);

    testUsingContext('builds Android release JIT dynamic patch - mismatched snapshot 2', () async {
      fs.file('main.dill').writeAsStringSync('binary magic');

      final Archive baselineApk = Archive()
        ..addFile(ArchiveFile('assets/flutter_assets/isolate_snapshot_instr',
            'isolateSnapshotInstr'.length, 'isolateSnapshotInstr'.codeUnits))
        ..addFile(ArchiveFile('assets/flutter_assets/vm_snapshot_data',
            'engineVmSnapshotData'.length, 'engineVmSnapshotData'.codeUnits));

      fs.file('.baseline/100.apk')
        ..createSync(recursive: true)
        ..writeAsBytesSync(ZipEncoder().encode(baselineApk), flush: true);

      fs.file('engine_vm_snapshot_data')
        ..createSync(recursive: true)
        ..writeAsStringSync('engineVmSnapshotData', flush: true);

      fs.file('build/foo/isolate_snapshot_instr')
        ..createSync(recursive: true)
        ..writeAsStringSync('mismatchedIsolateSnapshotInstr', flush: true);

      genSnapshot.outputs = <String, String>{
        'build/foo/isolate_snapshot_data': '',
        'build/foo/snapshot.d': 'build/foo/vm_snapshot_data : ',
      };

      final int genSnapshotExitCode = await snapshotter.build(
        platform: TargetPlatform.android_arm,
        buildMode: BuildMode.release,
        mainPath: 'main.dill',
        packagesPath: '.packages',
        outputPath: 'build/foo',
        compilationTraceFilePath: kTrace,
        createPatch: true,
1030
        buildNumber: '100',
1031 1032 1033 1034 1035 1036 1037 1038
        baselineDir: '.baseline',
      );

      expect(genSnapshotExitCode, 1);
      expect(genSnapshot.callCount, 0);

    }, overrides: contextOverrides);

1039
  });
1040
}