windows_device_test.dart 20.1 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:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/platform.dart';
11
import 'package:flutter_tools/src/build_info.dart';
12
import 'package:flutter_tools/src/cache.dart';
13
import 'package:flutter_tools/src/device.dart';
14
import 'package:flutter_tools/src/features.dart';
15
import 'package:flutter_tools/src/project.dart';
16
import 'package:flutter_tools/src/windows/application_package.dart';
17
import 'package:flutter_tools/src/windows/uwptool.dart';
18
import 'package:flutter_tools/src/windows/windows_device.dart';
19
import 'package:flutter_tools/src/windows/windows_workflow.dart';
20
import 'package:test/fake.dart';
21

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

void main() {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  testWithoutContext('WindowsDevice defaults', () async {
    final WindowsDevice windowsDevice = setUpWindowsDevice();
    final PrebuiltWindowsApp windowsApp = PrebuiltWindowsApp(executable: 'foo');

    expect(await windowsDevice.targetPlatform, TargetPlatform.windows_x64);
    expect(windowsDevice.name, 'Windows');
    expect(await windowsDevice.installApp(windowsApp), true);
    expect(await windowsDevice.uninstallApp(windowsApp), true);
    expect(await windowsDevice.isLatestBuildInstalled(windowsApp), true);
    expect(await windowsDevice.isAppInstalled(windowsApp), true);
    expect(windowsDevice.category, Category.desktop);

    expect(windowsDevice.supportsRuntimeMode(BuildMode.debug), true);
    expect(windowsDevice.supportsRuntimeMode(BuildMode.profile), true);
    expect(windowsDevice.supportsRuntimeMode(BuildMode.release), true);
    expect(windowsDevice.supportsRuntimeMode(BuildMode.jitRelease), false);
  });

45
  testWithoutContext('WindowsUwpDevice defaults', () async {
46
    final FakeUwpTool uwptool = FakeUwpTool();
47 48 49 50 51
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
        fileSystem: fileSystem,
        uwptool: uwptool,
    );
52
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();
53

54 55 56 57 58
    final String packagePath = fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Debug_Test', 'testapp_1.2.3.4_x64_Debug.msix',
    );
    fileSystem.file(packagePath).createSync(recursive:true);
59 60
    expect(await windowsDevice.targetPlatform, TargetPlatform.windows_uwp_x64);
    expect(windowsDevice.name, 'Windows (UWP)');
61
    expect(await windowsDevice.installApp(package), true);
62
    expect(await windowsDevice.uninstallApp(package), true);
63 64
    expect(await windowsDevice.isLatestBuildInstalled(package), false);
    expect(await windowsDevice.isAppInstalled(package), false);
65 66 67 68 69 70 71 72
    expect(windowsDevice.category, Category.desktop);

    expect(windowsDevice.supportsRuntimeMode(BuildMode.debug), true);
    expect(windowsDevice.supportsRuntimeMode(BuildMode.profile), true);
    expect(windowsDevice.supportsRuntimeMode(BuildMode.release), true);
    expect(windowsDevice.supportsRuntimeMode(BuildMode.jitRelease), false);
  });

73 74 75 76
  testWithoutContext('WindowsDevices does not list devices if the workflow is unsupported', () async {
    expect(await WindowsDevices(
      windowsWorkflow: WindowsWorkflow(
        featureFlags: TestFeatureFlags(isWindowsEnabled: false),
77
        platform: FakePlatform(operatingSystem: 'windows'),
78
      ),
79
      featureFlags: TestFeatureFlags(isWindowsEnabled: false),
80 81 82 83
      operatingSystemUtils: FakeOperatingSystemUtils(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
      fileSystem: MemoryFileSystem.test(),
84
      uwptool: FakeUwpTool(),
85 86 87 88 89 90 91 92 93 94 95 96 97
    ).devices, <Device>[]);
  });

  testWithoutContext('WindowsDevices lists a devices if the workflow is supported', () async {
    expect(await WindowsDevices(
      windowsWorkflow: WindowsWorkflow(
        featureFlags: TestFeatureFlags(isWindowsEnabled: true),
        platform: FakePlatform(operatingSystem: 'windows')
      ),
      operatingSystemUtils: FakeOperatingSystemUtils(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
      fileSystem: MemoryFileSystem.test(),
98
      featureFlags: TestFeatureFlags(isWindowsEnabled: true),
99
      uwptool: FakeUwpTool(),
100 101 102
    ).devices, hasLength(1));
  });

103 104 105 106 107 108 109 110 111 112 113 114
  testWithoutContext('WindowsDevices lists a UWP Windows device if feature is enabled', () async {
    final FeatureFlags featureFlags = TestFeatureFlags(isWindowsEnabled: true, isWindowsUwpEnabled: true);
    expect(await WindowsDevices(
      windowsWorkflow: WindowsWorkflow(
        featureFlags: featureFlags,
        platform: FakePlatform(operatingSystem: 'windows')
      ),
      operatingSystemUtils: FakeOperatingSystemUtils(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
      fileSystem: MemoryFileSystem.test(),
      featureFlags: featureFlags,
115
      uwptool: FakeUwpTool(),
116 117 118
    ).devices, hasLength(2));
  });

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  testWithoutContext('WindowsDevices has windows and winuwp well known devices', () async {
    final FeatureFlags featureFlags = TestFeatureFlags(isWindowsEnabled: true, isWindowsUwpEnabled: true);
    expect(WindowsDevices(
      windowsWorkflow: WindowsWorkflow(
        featureFlags: featureFlags,
        platform: FakePlatform(operatingSystem: 'windows')
      ),
      operatingSystemUtils: FakeOperatingSystemUtils(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
      fileSystem: MemoryFileSystem.test(),
      featureFlags: featureFlags,
      uwptool: FakeUwpTool(),
    ).wellKnownIds, <String>['windows', 'winuwp']);
  });

135 136 137 138 139 140 141 142 143 144
  testWithoutContext('WindowsDevices ignores the timeout provided to discoverDevices', () async {
    final WindowsDevices windowsDevices = WindowsDevices(
      windowsWorkflow: WindowsWorkflow(
        featureFlags: TestFeatureFlags(isWindowsEnabled: true),
        platform: FakePlatform(operatingSystem: 'windows')
      ),
      operatingSystemUtils: FakeOperatingSystemUtils(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
      fileSystem: MemoryFileSystem.test(),
145
      featureFlags: TestFeatureFlags(isWindowsEnabled: true),
146
      uwptool: FakeUwpTool(),
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    );
    // Timeout ignored.
    final List<Device> devices = await windowsDevices.discoverDevices(timeout: const Duration(seconds: 10));
    expect(devices, hasLength(1));
  });

  testWithoutContext('isSupportedForProject is true with editable host app', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsDevice windowsDevice = setUpWindowsDevice(fileSystem: fileSystem);
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
    fileSystem.directory('windows').createSync();
    fileSystem.file(fileSystem.path.join('windows', 'CMakeLists.txt')).createSync();
    final FlutterProject flutterProject = setUpFlutterProject(fileSystem.currentDirectory);

    expect(windowsDevice.isSupportedForProject(flutterProject), true);
  });

  testWithoutContext('isSupportedForProject is false with no host app', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsDevice windowsDevice = setUpWindowsDevice(fileSystem: fileSystem);
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
    final FlutterProject flutterProject = setUpFlutterProject(fileSystem.currentDirectory);

    expect(windowsDevice.isSupportedForProject(flutterProject), false);
  });

  testWithoutContext('isSupportedForProject is false with no build file', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsDevice windowsDevice = setUpWindowsDevice(fileSystem: fileSystem);
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
    fileSystem.directory('windows').createSync();
    final FlutterProject flutterProject = setUpFlutterProject(fileSystem.currentDirectory);

    expect(windowsDevice.isSupportedForProject(flutterProject), false);
  });

  testWithoutContext('executablePathForDevice uses the correct package executable', () async {
    final WindowsDevice windowsDevice = setUpWindowsDevice();
188 189 190 191 192
    final FakeWindowsApp fakeApp = FakeWindowsApp();

    expect(windowsDevice.executablePathForDevice(fakeApp, BuildMode.debug), 'debug/executable');
    expect(windowsDevice.executablePathForDevice(fakeApp, BuildMode.profile), 'profile/executable');
    expect(windowsDevice.executablePathForDevice(fakeApp, BuildMode.release), 'release/executable');
193
  });
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  testWithoutContext('WinUWPDevice installs cert if not installed', () async {
    Cache.flutterRoot = '';
    final FakeUwpTool uwptool = FakeUwpTool();
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
      fileSystem: fileSystem,
      uwptool: uwptool,
    );
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();

    uwptool.hasValidSignature = false;
    final String packagePath = fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Debug_Test', 'testapp_1.2.3.4_x64_Debug.msix',
    );
    fileSystem.file(packagePath).createSync(recursive:true);
    final bool result = await windowsDevice.installApp(package);

    expect(result, isTrue);
    expect(uwptool.installCertRequests, hasLength(1));
    expect(uwptool.installAppRequests, hasLength(1));
  });

  testWithoutContext('WinUWPDevice does not install cert if not installed', () async {
    Cache.flutterRoot = '';
    final FakeUwpTool uwptool = FakeUwpTool();
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
      fileSystem: fileSystem,
      uwptool: uwptool,
    );
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();

    uwptool.hasValidSignature = true;
    final String packagePath = fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Debug_Test', 'testapp_1.2.3.4_x64_Debug.msix',
    );
    fileSystem.file(packagePath).createSync(recursive:true);
    final bool result = await windowsDevice.installApp(package);

    expect(result, isTrue);
    expect(uwptool.installCertRequests, isEmpty);
    expect(uwptool.installAppRequests, hasLength(1));
  });

  testWithoutContext('WinUWPDevice prefers installing multi-arch binaries', () async {
    Cache.flutterRoot = '';
    final FakeUwpTool uwptool = FakeUwpTool();
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
      fileSystem: fileSystem,
      uwptool: uwptool,
    );
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();

    final String singleArchPath = fileSystem.path.absolute(fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Debug_Test', 'testapp_1.2.3.4_x64_Debug.msix',
    ));
    fileSystem.file(singleArchPath).createSync(recursive:true);
    final String multiArchPath = fileSystem.path.absolute(fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_Debug_Test', 'testapp_1.2.3.4_Debug.msix',
    ));
    fileSystem.file(multiArchPath).createSync(recursive:true);
    final bool result = await windowsDevice.installApp(package);

    expect(result, isTrue);
    expect(uwptool.installAppRequests.single.packageUri, Uri.file(multiArchPath).toString());
  });

  testWithoutContext('WinUWPDevice falls back to installing single-arch binaries', () async {
    Cache.flutterRoot = '';
    final FakeUwpTool uwptool = FakeUwpTool();
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
      fileSystem: fileSystem,
      uwptool: uwptool,
    );
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();

    final String singleArchPath = fileSystem.path.absolute(fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Debug_Test', 'testapp_1.2.3.4_x64_Debug.msix',
    ));
    fileSystem.file(singleArchPath).createSync(recursive:true);
    final bool result = await windowsDevice.installApp(package);

    expect(result, isTrue);
    expect(uwptool.installAppRequests.single.packageUri, Uri.file(singleArchPath).toString());
  });

288
  testWithoutContext('WinUWPDevice can launch application if cert is installed', () async {
289 290 291 292 293 294 295 296 297
    Cache.flutterRoot = '';
    final FakeUwpTool uwptool = FakeUwpTool();
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
      fileSystem: fileSystem,
      uwptool: uwptool,
    );
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();

298 299 300 301 302 303
    uwptool.hasValidSignature = true;
    final String packagePath = fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Debug_Test', 'testapp_1.2.3.4_x64_Debug.msix',
    );
    fileSystem.file(packagePath).createSync(recursive:true);
304 305 306 307 308 309 310 311
    final LaunchResult result = await windowsDevice.startApp(
      package,
      debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
      prebuiltApplication: true,
      platformArgs: <String, Object>{},
    );

    expect(result.started, true);
312 313 314
    expect(uwptool.installCertRequests, isEmpty);
    expect(uwptool.launchAppRequests.single.packageFamily, 'PACKAGE-ID_publisher');
    expect(uwptool.launchAppRequests.single.args, <String>[
315 316 317 318 319 320 321 322
      '--observatory-port=12345',
      '--disable-service-auth-codes',
      '--enable-dart-profiling',
      '--enable-checked-mode',
      '--verify-entry-points',
    ]);
  });

323
  testWithoutContext('WinUWPDevice installs cert and can launch application if cert not installed', () async {
324 325 326 327 328 329 330 331 332
    Cache.flutterRoot = '';
    final FakeUwpTool uwptool = FakeUwpTool();
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
      fileSystem: fileSystem,
      uwptool: uwptool,
    );
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    uwptool.hasValidSignature = false;
    final String packagePath = fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Debug_Test', 'testapp_1.2.3.4_x64_Debug.msix',
    );
    fileSystem.file(packagePath).createSync(recursive:true);
    final LaunchResult result = await windowsDevice.startApp(
      package,
      debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
      prebuiltApplication: true,
      platformArgs: <String, Object>{},
    );

    expect(result.started, true);
    expect(uwptool.installCertRequests, isNotEmpty);
    expect(uwptool.launchAppRequests.single.packageFamily, 'PACKAGE-ID_publisher');
    expect(uwptool.launchAppRequests.single.args, <String>[
      '--observatory-port=12345',
      '--disable-service-auth-codes',
      '--enable-dart-profiling',
      '--enable-checked-mode',
      '--verify-entry-points',
    ]);
  });

  testWithoutContext('WinUWPDevice can launch application in release mode', () async {
    Cache.flutterRoot = '';
    final FakeUwpTool uwptool = FakeUwpTool();
    final FileSystem fileSystem = MemoryFileSystem.test();
    final WindowsUWPDevice windowsDevice = setUpWindowsUwpDevice(
      fileSystem: fileSystem,
      uwptool: uwptool,
    );
    final FakeBuildableUwpApp package = FakeBuildableUwpApp();

    final String packagePath = fileSystem.path.join(
      'build', 'winuwp', 'runner_uwp', 'AppPackages', 'testapp',
      'testapp_1.2.3.4_x64_Release_Test', 'testapp_1.2.3.4_x64_Release.msix',
    );
    fileSystem.file(packagePath).createSync(recursive:true);
373 374 375 376 377 378 379 380
    final LaunchResult result = await windowsDevice.startApp(
      package,
      debuggingOptions: DebuggingOptions.enabled(BuildInfo.release),
      prebuiltApplication: true,
      platformArgs: <String, Object>{},
    );

    expect(result.started, true);
381 382
    expect(uwptool.launchAppRequests.single.packageFamily, 'PACKAGE-ID_publisher');
    expect(uwptool.launchAppRequests.single.args, <String>[]);
383
  });
384 385
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
FlutterProject setUpFlutterProject(Directory directory) {
  final FlutterProjectFactory flutterProjectFactory = FlutterProjectFactory(
    fileSystem: directory.fileSystem,
    logger: BufferLogger.test(),
  );
  return flutterProjectFactory.fromDirectory(directory);
}

WindowsDevice setUpWindowsDevice({
  FileSystem fileSystem,
  Logger logger,
  ProcessManager processManager,
}) {
  return WindowsDevice(
    fileSystem: fileSystem ?? MemoryFileSystem.test(),
    logger: logger ?? BufferLogger.test(),
    processManager: processManager ?? FakeProcessManager.any(),
    operatingSystemUtils: FakeOperatingSystemUtils(),
  );
}
406

407 408 409 410
WindowsUWPDevice setUpWindowsUwpDevice({
  FileSystem fileSystem,
  Logger logger,
  ProcessManager processManager,
411
  UwpTool uwptool,
412 413 414 415 416 417
}) {
  return WindowsUWPDevice(
    fileSystem: fileSystem ?? MemoryFileSystem.test(),
    logger: logger ?? BufferLogger.test(),
    processManager: processManager ?? FakeProcessManager.any(),
    operatingSystemUtils: FakeOperatingSystemUtils(),
418
    uwptool: uwptool ?? FakeUwpTool(),
419 420 421
  );
}

422 423 424 425
class FakeWindowsApp extends Fake implements WindowsApp {
  @override
  String executable(BuildMode buildMode) => '${buildMode.name}/executable';
}
426 427 428 429 430 431 432 433

class FakeBuildableUwpApp extends Fake implements BuildableUwpApp {
  @override
  String get id => 'PACKAGE-ID';
  @override
  String get name => 'testapp';
  @override
  String get projectVersion => '1.2.3.4';
434 435 436

  // Test helper to get the expected package family.
  static const String packageFamily = 'PACKAGE-ID_publisher';
437 438 439
}

class FakeUwpTool implements UwpTool {
440
  bool isInstalled = false;
441
  bool hasValidSignature = false;
442
  final List<_GetPackageFamilyRequest> getPackageFamilyRequests = <_GetPackageFamilyRequest>[];
443 444 445 446
  final List<_LaunchAppRequest> launchAppRequests = <_LaunchAppRequest>[];
  final List<_InstallCertRequest> installCertRequests = <_InstallCertRequest>[];
  final List<_InstallAppRequest> installAppRequests = <_InstallAppRequest>[];
  final List<_UninstallAppRequest> uninstallAppRequests = <_UninstallAppRequest>[];
447 448 449

  @override
  Future<List<String>> listApps() async {
450
    return isInstalled ? <String>[FakeBuildableUwpApp.packageFamily] : <String>[];
451 452 453
  }

  @override
454 455 456
  Future<String/*?*/> getPackageFamilyName(String packageName) async {
    getPackageFamilyRequests.add(_GetPackageFamilyRequest(packageName));
    return isInstalled ? FakeBuildableUwpApp.packageFamily : null;
457 458 459
  }

  @override
460
  Future<int/*?*/> launchApp(String packageFamily, List<String> args) async {
461
    launchAppRequests.add(_LaunchAppRequest(packageFamily, args));
462 463
    return 42;
  }
464 465

  @override
466 467 468 469 470 471 472 473 474 475 476 477 478
  Future<bool> isSignatureValid(String packagePath) async {
    return hasValidSignature;
  }

  @override
  Future<bool> installCertificate(String certificatePath) async {
    installCertRequests.add(_InstallCertRequest(certificatePath));
    return true;
  }

  @override
  Future<bool> installApp(String packageUri, List<String> dependencyUris) async {
    installAppRequests.add(_InstallAppRequest(packageUri, dependencyUris));
479 480 481 482 483 484
    isInstalled = true;
    return true;
  }

  @override
  Future<bool> uninstallApp(String packageFamily) async {
485
    uninstallAppRequests.add(_UninstallAppRequest(packageFamily));
486 487 488
    isInstalled = false;
    return true;
  }
489 490
}

491 492
class _GetPackageFamilyRequest {
  const _GetPackageFamilyRequest(this.packageId);
493 494 495 496

  final String packageId;
}

497 498
class _LaunchAppRequest {
  const _LaunchAppRequest(this.packageFamily, this.args);
499

500
  final String packageFamily;
501 502
  final List<String> args;
}
503

504 505 506 507 508 509 510 511 512
class _InstallCertRequest {
  const _InstallCertRequest(this.certificatePath);

  final String certificatePath;
}


class _InstallAppRequest {
  const _InstallAppRequest(this.packageUri, this.dependencyUris);
513

514 515
  final String packageUri;
  final List<String> dependencyUris;
516 517
}

518 519
class _UninstallAppRequest {
  const _UninstallAppRequest(this.packageFamily);
520 521 522

  final String packageFamily;
}