windows_version_validator_test.dart 3.87 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter 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 'package:flutter_tools/src/base/os.dart';
6 7
import 'package:flutter_tools/src/doctor_validator.dart';
import 'package:flutter_tools/src/windows/windows_version_validator.dart';
8
import 'package:test/fake.dart';
9 10 11

import '../src/common.dart';

12 13 14 15 16
/// Fake [_WindowsUtils] to use for testing
class FakeValidOperatingSystemUtils extends Fake
    implements OperatingSystemUtils {
  FakeValidOperatingSystemUtils(
      [this.name = 'Microsoft Windows [Version 11.0.22621.963]']);
17

18 19 20
  @override
  final String name;
}
21 22 23 24

/// The expected validation result object for
/// a passing windows version test
const ValidationResult validWindows10ValidationResult = ValidationResult(
25
  ValidationType.success,
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  <ValidationMessage>[],
  statusInfo: 'Installed version of Windows is version 10 or higher',
);

/// The expected validation result object for
/// a passing windows version test
const ValidationResult invalidWindowsValidationResult = ValidationResult(
  ValidationType.missing,
  <ValidationMessage>[],
  statusInfo: 'Unable to confirm if installed Windows version is 10 or greater',
);

void main() {
  testWithoutContext('Successfully running windows version check on windows 10',
      () async {
    final WindowsVersionValidator windowsVersionValidator =
        WindowsVersionValidator(
43
            operatingSystemUtils: FakeValidOperatingSystemUtils());
44 45 46 47 48 49 50 51 52

    final ValidationResult result = await windowsVersionValidator.validate();

    expect(result.type, validWindows10ValidationResult.type,
        reason: 'The ValidationResult type should be the same (installed)');
    expect(result.statusInfo, validWindows10ValidationResult.statusInfo,
        reason: 'The ValidationResult statusInfo messages should be the same');
  });

53
  testWithoutContext(
54 55
      'Successfully running windows version check on windows 10 for BR',
      () async {
56 57
    final WindowsVersionValidator windowsVersionValidator =
        WindowsVersionValidator(
58 59
            operatingSystemUtils: FakeValidOperatingSystemUtils(
                'Microsoft Windows [versão 10.0.22621.1105]'));
60 61 62

    final ValidationResult result = await windowsVersionValidator.validate();

63 64 65
    expect(result.type, validWindows10ValidationResult.type,
        reason: 'The ValidationResult type should be the same (installed)');
    expect(result.statusInfo, validWindows10ValidationResult.statusInfo,
66 67 68 69 70 71
        reason: 'The ValidationResult statusInfo messages should be the same');
  });

  testWithoutContext('Identifying a windows version before 10', () async {
    final WindowsVersionValidator windowsVersionValidator =
        WindowsVersionValidator(
72 73
            operatingSystemUtils: FakeValidOperatingSystemUtils(
                'Microsoft Windows [Version 8.0.22621.1105]'));
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    final ValidationResult result = await windowsVersionValidator.validate();

    expect(result.type, invalidWindowsValidationResult.type,
        reason: 'The ValidationResult type should be the same (missing)');
  });

  testWithoutContext('Unit testing on a regex pattern validator', () async {
    const String testStr = r'''
OS Version:                10.0.19044 N/A Build 19044
OSz Version:                10.0.19044 N/A Build 19044
OxS Version:                10.0.19044 N/A Build 19044
OS Version:                10.19044 N/A Build 19044
OS Version:                10.x.19044 N/A Build 19044
OS Version:                10.0.19044 N/A Build 19044
OS Version:                .0.19044 N/A Build 19044
90
OS 版本:          10.0.22621 暂缺 Build 22621
91 92
''';

93 94 95 96
    final RegExp regex = RegExp(
      kWindowsOSVersionSemVerPattern,
      multiLine: true,
    );
97 98
    final Iterable<RegExpMatch> matches = regex.allMatches(testStr);

99 100
    expect(matches.length, 5,
        reason: 'There should be only 5 matches for the pattern provided');
101 102
  });
}