windows_test.dart 9.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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/base/platform.dart';
11
import 'package:flutter_tools/src/build_info.dart';
12
import 'package:flutter_tools/src/build_system/build_system.dart';
13
import 'package:flutter_tools/src/build_system/depfile.dart';
14
import 'package:flutter_tools/src/build_system/targets/assets.dart';
15
import 'package:flutter_tools/src/build_system/targets/common.dart';
16
import 'package:flutter_tools/src/build_system/targets/windows.dart';
17
import 'package:flutter_tools/src/convert.dart';
18

19
import '../../../src/common.dart';
20
import '../../../src/context.dart';
21
import '../../../src/fake_process_manager.dart';
22 23 24 25 26 27

final Platform kWindowsPlatform = FakePlatform(
  operatingSystem: 'windows',
  environment: <String, String>{},
);

28
void main() {
29
  testWithoutContext('UnpackWindows copies files to the correct cache directory', () async {
30
    final Artifacts artifacts = Artifacts.test();
31 32
    final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
    final Environment environment = Environment.test(
33 34 35 36 37
      fileSystem.currentDirectory,
      artifacts: artifacts,
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
38 39
      defines: <String, String>{
        kBuildMode: 'debug',
40
      },
41
    );
42 43 44 45 46 47
    final DepfileService depfileService = DepfileService(
      logger: BufferLogger.test(),
      fileSystem: fileSystem,
    );
    environment.buildDir.createSync(recursive: true);

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    final String windowsDesktopPath = artifacts.getArtifactPath(Artifact.windowsDesktopPath, platform: TargetPlatform.windows_x64, mode: BuildMode.debug);
    final String windowsCppClientWrapper = artifacts.getArtifactPath(Artifact.windowsCppClientWrapper, platform: TargetPlatform.windows_x64, mode: BuildMode.debug);
    final String icuData = artifacts.getArtifactPath(Artifact.icuData, platform: TargetPlatform.windows_x64);
    final List<String> requiredFiles = <String>[
      '$windowsDesktopPath\\flutter_export.h',
      '$windowsDesktopPath\\flutter_messenger.h',
      '$windowsDesktopPath\\flutter_windows.dll',
      '$windowsDesktopPath\\flutter_windows.dll.exp',
      '$windowsDesktopPath\\flutter_windows.dll.lib',
      '$windowsDesktopPath\\flutter_windows.dll.pdb',
      '$windowsDesktopPath\\flutter_plugin_registrar.h',
      '$windowsDesktopPath\\flutter_windows.h',
      icuData,
      '$windowsCppClientWrapper\\foo',
      r'C:\packages\flutter_tools\lib\src\build_system\targets\windows.dart',
    ];

    for (final String path in requiredFiles) {
66 67 68
      fileSystem.file(path).createSync(recursive: true);
    }
    fileSystem.directory('windows').createSync();
69

70
    await const UnpackWindows().build(environment);
71

72 73 74 75 76 77 78 79 80 81 82
    // Output files are copied correctly.
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_export.h'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_messenger.h'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_windows.dll'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_windows.dll.exp'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_windows.dll.lib'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_windows.dll.pdb'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_export.h'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_messenger.h'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_plugin_registrar.h'), exists);
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_windows.h'), exists);
83 84
    expect(fileSystem.file('C:\\windows\\flutter\\ephemeral\\$icuData'), exists);
    expect(fileSystem.file('C:\\windows\\flutter\\ephemeral\\$windowsCppClientWrapper\\foo'), exists);
85 86 87 88 89 90 91 92 93 94 95 96 97 98

    final File outputDepfile = environment.buildDir
      .childFile('windows_engine_sources.d');

    // Depfile is created correctly.
    expect(outputDepfile, exists);

    final List<String> inputPaths = depfileService.parse(outputDepfile)
      .inputs.map((File file) => file.path).toList();
    final List<String> outputPaths = depfileService.parse(outputDepfile)
      .outputs.map((File file) => file.path).toList();

    // Depfile has expected sources.
    expect(inputPaths, unorderedEquals(<String>[
99 100 101 102 103 104 105 106 107 108
      '$windowsDesktopPath\\flutter_export.h',
      '$windowsDesktopPath\\flutter_messenger.h',
      '$windowsDesktopPath\\flutter_windows.dll',
      '$windowsDesktopPath\\flutter_windows.dll.exp',
      '$windowsDesktopPath\\flutter_windows.dll.lib',
      '$windowsDesktopPath\\flutter_windows.dll.pdb',
      '$windowsDesktopPath\\flutter_plugin_registrar.h',
      '$windowsDesktopPath\\flutter_windows.h',
      icuData,
      '$windowsCppClientWrapper\\foo',
109 110 111 112 113 114 115 116 117 118
    ]));
    expect(outputPaths, unorderedEquals(<String>[
      r'C:\windows\flutter\ephemeral\flutter_export.h',
      r'C:\windows\flutter\ephemeral\flutter_messenger.h',
      r'C:\windows\flutter\ephemeral\flutter_windows.dll',
      r'C:\windows\flutter\ephemeral\flutter_windows.dll.exp',
      r'C:\windows\flutter\ephemeral\flutter_windows.dll.lib',
      r'C:\windows\flutter\ephemeral\flutter_windows.dll.pdb',
      r'C:\windows\flutter\ephemeral\flutter_plugin_registrar.h',
      r'C:\windows\flutter\ephemeral\flutter_windows.h',
119 120
      'C:\\windows\\flutter\\ephemeral\\$icuData',
      'C:\\windows\\flutter\\ephemeral\\$windowsCppClientWrapper\\foo',
121 122 123 124
    ]));
  });

  // AssetBundleFactory still uses context injection
125 126 127 128 129 130
  FileSystem fileSystem;

  setUp(() {
    fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows);
  });

131 132 133
  testUsingContext('DebugBundleWindowsAssets creates correct bundle structure', () async {
    final Environment environment = Environment.test(
      fileSystem.currentDirectory,
134
      artifacts: Artifacts.test(),
135 136 137 138 139
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      defines: <String, String>{
        kBuildMode: 'debug',
140 141 142 143 144
      },
      inputs: <String, String>{
        kBundleSkSLPath: 'bundle.sksl',
      },
      engineVersion: '2',
145 146 147
    );

    environment.buildDir.childFile('app.dill').createSync(recursive: true);
148 149 150 151 152 153 154 155 156 157
    // sksl bundle
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
      <String, Object>{
        'engineRevision': '2',
        'platform': 'ios',
        'data': <String, Object>{
          'A': 'B',
        }
      }
    ));
158 159 160 161 162 163

    await const DebugBundleWindowsAssets().build(environment);

    // Depfile is created and dill is copied.
    expect(environment.buildDir.childFile('flutter_assets.d'), exists);
    expect(fileSystem.file(r'C:\flutter_assets\kernel_blob.bin'), exists);
164
    expect(fileSystem.file(r'C:\flutter_assets\AssetManifest.json'), exists);
165 166
    expect(fileSystem.file(r'C:\flutter_assets\io.flutter.shaders.json'), exists);
    expect(fileSystem.file(r'C:\flutter_assets\io.flutter.shaders.json').readAsStringSync(), '{"data":{"A":"B"}}');
167 168 169
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
170
  });
171 172 173 174

  testUsingContext('ProfileBundleWindowsAssets creates correct bundle structure', () async {
    final Environment environment = Environment.test(
      fileSystem.currentDirectory,
175
      artifacts: Artifacts.test(),
176 177 178 179 180 181 182 183 184 185
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      defines: <String, String>{
        kBuildMode: 'profile',
      }
    );

    environment.buildDir.childFile('app.so').createSync(recursive: true);

186
    await const WindowsAotBundle(AotElfProfile(TargetPlatform.windows_x64)).build(environment);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    await const ProfileBundleWindowsAssets().build(environment);

    // Depfile is created and so is copied.
    expect(environment.buildDir.childFile('flutter_assets.d'), exists);
    expect(fileSystem.file(r'C:\windows\app.so'), exists);
    expect(fileSystem.file(r'C:\flutter_assets\kernel_blob.bin').existsSync(), false);
    expect(fileSystem.file(r'C:\flutter_assets\AssetManifest.json'), exists);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });

  testUsingContext('ReleaseBundleWindowsAssets creates correct bundle structure', () async {
    final Environment environment = Environment.test(
      fileSystem.currentDirectory,
202
      artifacts: Artifacts.test(),
203 204 205 206 207 208 209 210 211 212
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      defines: <String, String>{
        kBuildMode: 'release',
      }
    );

    environment.buildDir.childFile('app.so').createSync(recursive: true);

213
    await const WindowsAotBundle(AotElfRelease(TargetPlatform.windows_x64)).build(environment);
214 215 216 217 218 219 220 221 222 223 224
    await const ReleaseBundleWindowsAssets().build(environment);

    // Depfile is created and so is copied.
    expect(environment.buildDir.childFile('flutter_assets.d'), exists);
    expect(fileSystem.file(r'C:\windows\app.so'), exists);
    expect(fileSystem.file(r'C:\flutter_assets\kernel_blob.bin').existsSync(), false);
    expect(fileSystem.file(r'C:\flutter_assets\AssetManifest.json'), exists);
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
  });
225
}