context.dart 4.3 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<Device> getDeviceById(String deviceId) {
    Device device = devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null);
Ian Hickson's avatar
Ian Hickson committed
87
    return new Future<Device>.value(device);
88 89
  }

90
  @override
91 92 93 94 95 96 97 98 99 100 101 102 103 104
  Future<List<Device>> getDevices() async {
    if (specifiedDeviceId == null) {
      return getAllConnectedDevices();
    } else {
      Device device = await getDeviceById(specifiedDeviceId);
      return device == null ? <Device>[] : <Device>[device];
    }
  }

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

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

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

class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}

class MockIOSSimulatorUtils extends Mock implements IOSSimulatorUtils {}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

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

  @override
  bool get enabled => true;

  @override
  set enabled(bool value) { }

  @override
  void sendCommand(String command) { }

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

  @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();
146 147 148

  @override
  void printUsage() { }
149 150 151 152 153 154 155 156 157 158 159
}

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

  @override
  final String event;

  @override
  void finish() { }
}