workflow_test.dart 3.55 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
import 'package:flutter_tools/src/features.dart';
7 8
import 'package:flutter_tools/src/web/chrome.dart';
import 'package:flutter_tools/src/web/workflow.dart';
9
import 'package:flutter_tools/src/globals.dart' as globals;
10 11
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
12
import 'package:platform/platform.dart';
13

14 15 16
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/testbed.dart';
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

void main() {
  group('WebWorkflow', () {
    Testbed testbed;
    MockPlatform notSupported;
    MockPlatform windows;
    MockPlatform linux;
    MockPlatform macos;
    MockProcessManager mockProcessManager;
    WebWorkflow workflow;

    setUpAll(() {
      notSupported = MockPlatform(linux: false, windows: false, macos: false);
      windows = MockPlatform(windows: true);
      linux = MockPlatform(linux: true);
      macos = MockPlatform(macos: true);
      workflow = const WebWorkflow();
      mockProcessManager = MockProcessManager();
      testbed = Testbed(setup: () async {
36
        globals.fs.file('chrome').createSync();
37 38
        when(mockProcessManager.canRun('chrome')).thenReturn(true);
      }, overrides: <Type, Generator>{
39
        FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        ProcessManager: () => mockProcessManager,
      });
    });

    test('Applies on Linux', () => testbed.run(() {
      expect(workflow.appliesToHostPlatform, true);
      expect(workflow.canLaunchDevices, true);
      expect(workflow.canListDevices, true);
      expect(workflow.canListEmulators, false);
    }, overrides: <Type, Generator>{
      Platform: () => linux,
    }));

    test('Applies on macOS', () => testbed.run(() {
      expect(workflow.appliesToHostPlatform, true);
      expect(workflow.canLaunchDevices, true);
      expect(workflow.canListDevices, true);
      expect(workflow.canListEmulators, false);
    }, overrides: <Type, Generator>{
      Platform: () => macos,
    }));

    test('Applies on Windows', () => testbed.run(() {
      expect(workflow.appliesToHostPlatform, true);
      expect(workflow.canLaunchDevices, true);
      expect(workflow.canListDevices, true);
      expect(workflow.canListEmulators, false);
    }, overrides: <Type, Generator>{
      Platform: () => windows,
    }));

    test('does not apply on other platforms', () => testbed.run(() {
      expect(workflow.appliesToHostPlatform, false);
    }, overrides: <Type, Generator>{
      Platform: () => notSupported,
    }));

77
    test('does not apply if feature flag is disabled', () => testbed.run(() {
78 79 80 81 82 83
      expect(workflow.appliesToHostPlatform, false);
      expect(workflow.canLaunchDevices, false);
      expect(workflow.canListDevices, false);
      expect(workflow.canListEmulators, false);
    }, overrides: <Type, Generator>{
      Platform: () => macos,
84
      FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
85 86 87 88 89 90 91
    }));
  });
}

class MockProcessManager extends Mock implements ProcessManager {}

class MockPlatform extends Mock implements Platform {
92 93 94 95 96 97 98 99
  MockPlatform({
    this.windows = false,
    this.macos = false,
    this.linux = false,
    this.environment = const <String, String>{
      kChromeEnvironment: 'chrome',
    },
  });
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

  final bool windows;
  final bool macos;
  final bool linux;

  @override
  final Map<String, String> environment;

  @override
  bool get isLinux => linux;

  @override
  bool get isMacOS => macos;

  @override
  bool get isWindows => windows;
}