context.dart 4.37 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
6
import 'dart:io';
7 8 9

import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/os.dart';
11
import 'package:flutter_tools/src/device.dart';
12 13
import 'package:flutter_tools/src/doctor.dart';
import 'package:flutter_tools/src/ios/mac.dart';
14
import 'package:flutter_tools/src/ios/simulators.dart';
15
import 'package:flutter_tools/src/usage.dart';
16
import 'package:mockito/mockito.dart';
17 18
import 'package:test/test.dart';

19 20 21
/// Return the test logger. This assumes that the current Logger is a BufferLogger.
BufferLogger get testLogger => context[Logger];

22 23 24
MockDeviceManager get testDeviceManager => context[DeviceManager];
MockDoctor get testDoctor => context[Doctor];

25 26 27 28
void testUsingContext(String description, dynamic testMethod(), {
  Timeout timeout,
  Map<Type, dynamic> overrides: const <Type, dynamic>{}
}) {
29 30 31
  test(description, () {
    AppContext testContext = new AppContext();

32 33 34 35 36 37 38 39 40
    overrides.forEach((Type type, dynamic value) {
      testContext[type] = value;
    });

    if (!overrides.containsKey(Logger))
      testContext[Logger] = new BufferLogger();

    if (!overrides.containsKey(DeviceManager))
      testContext[DeviceManager] = new MockDeviceManager();
41

42
    if (!overrides.containsKey(Doctor))
43
      testContext[Doctor] = new MockDoctor();
44

45 46 47
    if (!overrides.containsKey(SimControl))
      testContext[SimControl] = new MockSimControl();

48 49 50
    if (!overrides.containsKey(Usage))
      testContext[Usage] = new MockUsage();

51 52 53 54 55
    if (!overrides.containsKey(OperatingSystemUtils)) {
      MockOperatingSystemUtils os = new MockOperatingSystemUtils();
      when(os.isWindows).thenReturn(false);
      testContext[OperatingSystemUtils] = os;
    }
56

57 58 59 60 61
    if (!overrides.containsKey(IOSSimulatorUtils)) {
      MockIOSSimulatorUtils mock = new MockIOSSimulatorUtils();
      when(mock.getAttachedDevices()).thenReturn(<IOSSimulator>[]);
      testContext[IOSSimulatorUtils] = mock;
    }
62

63 64 65 66 67
    if (Platform.isMacOS) {
      if (!overrides.containsKey(XCode))
        testContext[XCode] = new XCode();
    }

68 69 70 71 72
    return testContext.runInZone(testMethod);
  }, timeout: timeout);
}

class MockDeviceManager implements DeviceManager {
73 74
  List<Device> devices = <Device>[];

75
  @override
76
  String specifiedDeviceId;
77 78

  @override
79 80
  bool get hasSpecifiedDeviceId => specifiedDeviceId != null;

81
  @override
Ian Hickson's avatar
Ian Hickson committed
82
  Future<List<Device>> getAllConnectedDevices() => new Future<List<Device>>.value(devices);
83

84
  @override
85 86
  Future<List<Device>> getDevicesById(String deviceId) async {
    return devices.where((Device device) => device.id == deviceId).toList();
87 88
  }

89
  @override
90 91 92 93
  Future<List<Device>> getDevices() async {
    if (specifiedDeviceId == null) {
      return getAllConnectedDevices();
    } else {
94
      return getDevicesById(specifiedDeviceId);
95 96 97 98 99 100 101 102
    }
  }

  void addDevice(Device device) => devices.add(device);
}

class MockDoctor extends Doctor {
  // True for testing.
103
  @override
104
  bool get canLaunchAnything => true;
105
}
106 107 108

class MockSimControl extends Mock implements SimControl {
  MockSimControl() {
109
    when(this.getConnectedDevices()).thenReturn(<SimDevice>[]);
110 111 112 113 114 115
  }
}

class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}

class MockIOSSimulatorUtils extends Mock implements IOSSimulatorUtils {}
116 117 118 119 120

class MockUsage implements Usage {
  @override
  bool get isFirstRun => false;

121 122 123 124 125 126
  @override
  bool get suppressAnalytics => false;

  @override
  set suppressAnalytics(bool value) { }

127 128 129 130 131 132 133 134 135 136 137 138
  @override
  bool get enabled => true;

  @override
  set enabled(bool value) { }

  @override
  void sendCommand(String command) { }

  @override
  void sendEvent(String category, String parameter) { }

139 140 141
  @override
  void sendTiming(String category, String variableName, Duration duration) { }

142 143 144 145 146 147 148 149 150 151 152
  @override
  UsageTimer startTimer(String event) => new _MockUsageTimer(event);

  @override
  void sendException(dynamic exception, StackTrace trace) { }

  @override
  Stream<Map<String, dynamic>> get onSend => null;

  @override
  Future<Null> ensureAnalyticsSent() => new Future<Null>.value();
153 154 155

  @override
  void printUsage() { }
156 157 158 159 160 161 162 163 164 165 166
}

class _MockUsageTimer implements UsageTimer {
  _MockUsageTimer(this.event);

  @override
  final String event;

  @override
  void finish() { }
}