mocks.dart 4.29 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
import 'dart:async';
6

7
import 'package:flutter_tools/src/android/android_device.dart';
8
import 'package:flutter_tools/src/application_package.dart';
9
import 'package:flutter_tools/src/build_info.dart';
10
import 'package:flutter_tools/src/devfs.dart';
11
import 'package:flutter_tools/src/device.dart';
12 13
import 'package:flutter_tools/src/ios/devices.dart';
import 'package:flutter_tools/src/ios/simulators.dart';
14
import 'package:flutter_tools/src/runner/flutter_command.dart';
15
import 'package:mockito/mockito.dart';
16
import 'package:test/test.dart';
17 18 19

class MockApplicationPackageStore extends ApplicationPackageStore {
  MockApplicationPackageStore() : super(
Adam Barth's avatar
Adam Barth committed
20 21
    android: new AndroidApk(
      id: 'io.flutter.android.mock',
22
      apkPath: '/mock/path/to/android/SkyShell.apk',
Adam Barth's avatar
Adam Barth committed
23 24
      launchActivity: 'io.flutter.android.mock.MockActivity'
    ),
25
    iOS: new BuildableIOSApp(
26
      appDirectory: '/mock/path/to/iOS/SkyShell.app',
27
      projectBundleId: 'io.flutter.ios.mock'
28 29
    )
  );
30 31
}

32
class MockPollingDeviceDiscovery extends PollingDeviceDiscovery {
33 34 35
  final List<Device> _devices = <Device>[];
  final StreamController<Device> _onAddedController = new StreamController<Device>.broadcast();
  final StreamController<Device> _onRemovedController = new StreamController<Device>.broadcast();
36 37 38 39

  MockPollingDeviceDiscovery() : super('mock');

  @override
40
  Future<List<Device>> pollingGetDevices() async => _devices;
41 42 43 44

  @override
  bool get supportsPlatform => true;

45 46 47
  @override
  bool get canListAnything => true;

48 49 50 51 52 53 54
  void addDevice(MockAndroidDevice device) {
    _devices.add(device);

    _onAddedController.add(device);
  }

  @override
55
  Future<List<Device>> get devices async => _devices;
56 57 58 59 60 61 62 63

  @override
  Stream<Device> get onAdded => _onAddedController.stream;

  @override
  Stream<Device> get onRemoved => _onRemovedController.stream;
}

64
class MockAndroidDevice extends Mock implements AndroidDevice {
65
  @override
66
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.android_arm;
67 68

  @override
69
  bool isSupported() => true;
70 71 72
}

class MockIOSDevice extends Mock implements IOSDevice {
73
  @override
74
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios;
75 76

  @override
77
  bool isSupported() => true;
78 79 80
}

class MockIOSSimulator extends Mock implements IOSSimulator {
81
  @override
82
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios;
83 84

  @override
85
  bool isSupported() => true;
86 87
}

88
class MockDeviceLogReader extends DeviceLogReader {
89
  @override
90 91
  String get name => 'MockLogReader';

Devon Carew's avatar
Devon Carew committed
92
  final StreamController<String> _linesController = new StreamController<String>.broadcast();
93

94
  @override
Devon Carew's avatar
Devon Carew committed
95
  Stream<String> get logLines => _linesController.stream;
96

Devon Carew's avatar
Devon Carew committed
97
  void addLine(String line) => _linesController.add(line);
98 99 100 101

  void dispose() {
    _linesController.close();
  }
102 103
}

104
void applyMocksToCommand(FlutterCommand command) {
105
  command
106
    ..applicationPackages = new MockApplicationPackageStore();
107
}
108

109 110
/// Common functionality for tracking mock interaction
class BasicMock {
111
  final List<String> messages = <String>[];
112

113
  void expectMessages(List<String> expectedMessages) {
114
    final List<String> actualMessages = new List<String>.from(messages);
115 116 117 118
    messages.clear();
    expect(actualMessages, unorderedEquals(expectedMessages));
  }

119
  bool contains(String match) {
120 121
    print('Checking for `$match` in:');
    print(messages);
122
    final bool result = messages.contains(match);
123 124 125
    messages.clear();
    return result;
  }
126
}
127

128
class MockDevFSOperations extends BasicMock implements DevFSOperations {
129
  Map<Uri, DevFSContent> devicePathToContent = <Uri, DevFSContent>{};
130

131 132 133 134 135 136 137 138 139 140 141 142
  @override
  Future<Uri> create(String fsName) async {
    messages.add('create $fsName');
    return Uri.parse('file:///$fsName');
  }

  @override
  Future<dynamic> destroy(String fsName) async {
    messages.add('destroy $fsName');
  }

  @override
143 144 145
  Future<dynamic> writeFile(String fsName, Uri deviceUri, DevFSContent content) async {
    messages.add('writeFile $fsName $deviceUri');
    devicePathToContent[deviceUri] = content;
146 147
  }

148
  @override
149 150 151
  Future<dynamic> deleteFile(String fsName, Uri deviceUri) async {
    messages.add('deleteFile $fsName $deviceUri');
    devicePathToContent.remove(deviceUri);
152 153
  }
}