context.dart 3.41 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 15 16
import 'package:flutter_tools/src/ios/simulators.dart';

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 51 52
    if (!overrides.containsKey(OperatingSystemUtils)) {
      MockOperatingSystemUtils os = new MockOperatingSystemUtils();
      when(os.isWindows).thenReturn(false);
      testContext[OperatingSystemUtils] = os;
    }
53

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

60 61 62 63 64
    if (Platform.isMacOS) {
      if (!overrides.containsKey(XCode))
        testContext[XCode] = new XCode();
    }

65 66 67 68 69
    return testContext.runInZone(testMethod);
  }, timeout: timeout);
}

class MockDeviceManager implements DeviceManager {
70 71
  List<Device> devices = <Device>[];

72
  @override
73
  String specifiedDeviceId;
74 75

  @override
76 77
  bool get hasSpecifiedDeviceId => specifiedDeviceId != null;

78
  @override
Ian Hickson's avatar
Ian Hickson committed
79
  Future<List<Device>> getAllConnectedDevices() => new Future<List<Device>>.value(devices);
80

81
  @override
82 83
  Future<Device> getDeviceById(String deviceId) {
    Device device = devices.firstWhere((Device device) => device.id == deviceId, orElse: () => null);
Ian Hickson's avatar
Ian Hickson committed
84
    return new Future<Device>.value(device);
85 86
  }

87
  @override
88 89 90 91 92 93 94 95 96 97 98 99 100 101
  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.
102
  @override
103
  bool get canLaunchAnything => true;
104
}
105 106 107 108 109 110 111 112 113 114

class MockSimControl extends Mock implements SimControl {
  MockSimControl() {
    when(this.getConnectedDevices()).thenReturn([]);
  }
}

class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}

class MockIOSSimulatorUtils extends Mock implements IOSSimulatorUtils {}