windows_test.dart 9.89 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
// @dart = 2.8

7
import 'package:file/memory.dart';
8
import 'package:file_testing/file_testing.dart';
9
import 'package:flutter_tools/src/artifacts.dart';
10
import 'package:flutter_tools/src/base/file_system.dart';
11 12
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
13
import 'package:flutter_tools/src/build_info.dart';
14
import 'package:flutter_tools/src/build_system/build_system.dart';
15
import 'package:flutter_tools/src/build_system/depfile.dart';
16
import 'package:flutter_tools/src/build_system/targets/assets.dart';
17
import 'package:flutter_tools/src/build_system/targets/common.dart';
18
import 'package:flutter_tools/src/build_system/targets/windows.dart';
19
import 'package:flutter_tools/src/convert.dart';
20

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

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

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

50 51 52 53 54 55 56 57 58 59 60
    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',
61
      '$windowsDesktopPath\\flutter_texture_registrar.h',
62 63 64 65 66 67 68
      '$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) {
69 70 71
      fileSystem.file(path).createSync(recursive: true);
    }
    fileSystem.directory('windows').createSync();
72

73
    await const UnpackWindows().build(environment);
74

75 76 77 78 79 80 81 82 83 84
    // 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);
85
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_texture_registrar.h'), exists);
86
    expect(fileSystem.file(r'C:\windows\flutter\ephemeral\flutter_windows.h'), exists);
87 88
    expect(fileSystem.file('C:\\windows\\flutter\\ephemeral\\$icuData'), exists);
    expect(fileSystem.file('C:\\windows\\flutter\\ephemeral\\$windowsCppClientWrapper\\foo'), exists);
89 90 91 92 93 94 95 96 97 98 99 100 101 102

    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>[
103 104 105 106 107 108 109
      '$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',
110
      '$windowsDesktopPath\\flutter_texture_registrar.h',
111 112 113
      '$windowsDesktopPath\\flutter_windows.h',
      icuData,
      '$windowsCppClientWrapper\\foo',
114 115 116 117 118 119 120 121 122
    ]));
    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',
123
      r'C:\windows\flutter\ephemeral\flutter_texture_registrar.h',
124
      r'C:\windows\flutter\ephemeral\flutter_windows.h',
125 126
      'C:\\windows\\flutter\\ephemeral\\$icuData',
      'C:\\windows\\flutter\\ephemeral\\$windowsCppClientWrapper\\foo',
127 128 129 130
    ]));
  });

  // AssetBundleFactory still uses context injection
131 132 133 134 135 136
  FileSystem fileSystem;

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

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

    environment.buildDir.childFile('app.dill').createSync(recursive: true);
154 155 156 157 158 159 160 161 162 163
    // sksl bundle
    fileSystem.file('bundle.sksl').writeAsStringSync(json.encode(
      <String, Object>{
        'engineRevision': '2',
        'platform': 'ios',
        'data': <String, Object>{
          'A': 'B',
        }
      }
    ));
164 165 166 167 168 169

    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);
170
    expect(fileSystem.file(r'C:\flutter_assets\AssetManifest.json'), exists);
171 172
    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"}}');
173 174 175
  }, overrides: <Type, Generator>{
    FileSystem: () => fileSystem,
    ProcessManager: () => FakeProcessManager.any(),
176
  });
177 178 179 180

  testUsingContext('ProfileBundleWindowsAssets creates correct bundle structure', () async {
    final Environment environment = Environment.test(
      fileSystem.currentDirectory,
181
      artifacts: Artifacts.test(),
182 183 184 185 186 187 188 189 190 191
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      defines: <String, String>{
        kBuildMode: 'profile',
      }
    );

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

192
    await const WindowsAotBundle(AotElfProfile(TargetPlatform.windows_x64)).build(environment);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    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,
208
      artifacts: Artifacts.test(),
209 210 211 212 213 214 215 216 217 218
      processManager: FakeProcessManager.any(),
      fileSystem: fileSystem,
      logger: BufferLogger.test(),
      defines: <String, String>{
        kBuildMode: 'release',
      }
    );

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

219
    await const WindowsAotBundle(AotElfRelease(TargetPlatform.windows_x64)).build(environment);
220 221 222 223 224 225 226 227 228 229 230
    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(),
  });
231
}