1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// 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.
import 'dart:async';
import 'dart:convert';
import 'dart:io' as io show IOSink;
import 'package:flutter_tools/src/android/android_device.dart';
import 'package:flutter_tools/src/android/android_sdk.dart' show AndroidSdk;
import 'package:flutter_tools/src/application_package.dart';
import 'package:flutter_tools/src/base/file_system.dart' hide IOSink;
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/devfs.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/ios/devices.dart';
import 'package:flutter_tools/src/ios/simulators.dart';
import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import 'package:test/test.dart';
class MockApplicationPackageStore extends ApplicationPackageStore {
MockApplicationPackageStore() : super(
android: new AndroidApk(
id: 'io.flutter.android.mock',
apkPath: '/mock/path/to/android/SkyShell.apk',
launchActivity: 'io.flutter.android.mock.MockActivity'
),
iOS: new BuildableIOSApp(
appDirectory: '/mock/path/to/iOS/SkyShell.app',
projectBundleId: 'io.flutter.ios.mock'
)
);
}
/// An SDK installation with several SDK levels (19, 22, 23).
class MockAndroidSdk extends Mock implements AndroidSdk {
static Directory createSdkDirectory({
bool withAndroidN: false,
String withNdkDir,
bool withNdkSysroot: false,
bool withSdkManager: true,
}) {
final Directory dir = fs.systemTempDirectory.createTempSync('android-sdk');
_createSdkFile(dir, 'platform-tools/adb');
_createSdkFile(dir, 'build-tools/19.1.0/aapt');
_createSdkFile(dir, 'build-tools/22.0.1/aapt');
_createSdkFile(dir, 'build-tools/23.0.2/aapt');
if (withAndroidN)
_createSdkFile(dir, 'build-tools/24.0.0-preview/aapt');
_createSdkFile(dir, 'platforms/android-22/android.jar');
_createSdkFile(dir, 'platforms/android-23/android.jar');
if (withAndroidN) {
_createSdkFile(dir, 'platforms/android-N/android.jar');
_createSdkFile(dir, 'platforms/android-N/build.prop', contents: _buildProp);
}
if (withSdkManager)
_createSdkFile(dir, 'tools/bin/sdkmanager');
if (withNdkDir != null) {
final String ndkCompiler = fs.path.join(
'ndk-bundle',
'toolchains',
'arm-linux-androideabi-4.9',
'prebuilt',
withNdkDir,
'bin',
'arm-linux-androideabi-gcc');
_createSdkFile(dir, ndkCompiler);
}
if (withNdkSysroot) {
final String armPlatform =
fs.path.join('ndk-bundle', 'platforms', 'android-9', 'arch-arm');
_createDir(dir, armPlatform);
}
return dir;
}
static void _createSdkFile(Directory dir, String filePath, { String contents }) {
final File file = dir.childFile(filePath);
file.createSync(recursive: true);
if (contents != null) {
file.writeAsStringSync(contents, flush: true);
}
}
static void _createDir(Directory dir, String path) {
final Directory directory = fs.directory(fs.path.join(dir.path, path));
directory.createSync(recursive: true);
}
static const String _buildProp = r'''
ro.build.version.incremental=1624448
ro.build.version.sdk=24
ro.build.version.codename=REL
''';
}
/// A strategy for creating Process objects from a list of commands.
typedef Process ProcessFactory(List<String> command);
/// A ProcessManager that starts Processes by delegating to a ProcessFactory.
class MockProcessManager implements ProcessManager {
ProcessFactory processFactory = (List<String> commands) => new MockProcess();
bool succeed = true;
List<String> commands;
@override
bool canRun(dynamic command, { String workingDirectory }) => succeed;
@override
Future<Process> start(
List<dynamic> command, {
String workingDirectory,
Map<String, String> environment,
bool includeParentEnvironment: true,
bool runInShell: false,
ProcessStartMode mode: ProcessStartMode.NORMAL,
}) {
if (!succeed) {
final String executable = command[0];
final List<String> arguments = command.length > 1 ? command.sublist(1) : <String>[];
throw new ProcessException(executable, arguments);
}
commands = command;
return new Future<Process>.value(processFactory(command));
}
@override
dynamic noSuchMethod(Invocation invocation) => null;
}
/// A process that exits successfully with no output and ignores all input.
class MockProcess extends Mock implements Process {
MockProcess({
this.pid: 1,
Future<int> exitCode,
Stream<List<int>> stdin,
this.stdout: const Stream<List<int>>.empty(),
this.stderr: const Stream<List<int>>.empty(),
}) : exitCode = exitCode ?? new Future<int>.value(0),
stdin = stdin ?? new MemoryIOSink();
@override
final int pid;
@override
final Future<int> exitCode;
@override
final io.IOSink stdin;
@override
final Stream<List<int>> stdout;
@override
final Stream<List<int>> stderr;
}
/// A process that prompts the user to proceed, then asynchronously writes
/// some lines to stdout before it exits.
class PromptingProcess implements Process {
Future<Null> showPrompt(String prompt, List<String> outputLines) async {
_stdoutController.add(utf8.encode(prompt));
final List<int> bytesOnStdin = await _stdin.future;
// Echo stdin to stdout.
_stdoutController.add(bytesOnStdin);
if (bytesOnStdin[0] == utf8.encode('y')[0]) {
for (final String line in outputLines)
_stdoutController.add(utf8.encode('$line\n'));
}
await _stdoutController.close();
}
final StreamController<List<int>> _stdoutController = new StreamController<List<int>>();
final CompleterIOSink _stdin = new CompleterIOSink();
@override
Stream<List<int>> get stdout => _stdoutController.stream;
@override
Stream<List<int>> get stderr => const Stream<List<int>>.empty();
@override
IOSink get stdin => _stdin;
@override
Future<int> get exitCode async {
await _stdoutController.done;
return 0;
}
@override
dynamic noSuchMethod(Invocation invocation) => null;
}
/// An IOSink that completes a future with the first line written to it.
class CompleterIOSink extends MemoryIOSink {
final Completer<List<int>> _completer = new Completer<List<int>>();
Future<List<int>> get future => _completer.future;
@override
void add(List<int> data) {
if (!_completer.isCompleted)
_completer.complete(data);
super.add(data);
}
}
/// An IOSink that collects whatever is written to it.
class MemoryIOSink implements IOSink {
@override
Encoding encoding = utf8;
final List<List<int>> writes = <List<int>>[];
@override
void add(List<int> data) {
writes.add(data);
}
@override
Future<Null> addStream(Stream<List<int>> stream) {
final Completer<Null> completer = new Completer<Null>();
stream.listen((List<int> data) {
add(data);
}).onDone(() => completer.complete(null));
return completer.future;
}
@override
void writeCharCode(int charCode) {
add(<int>[charCode]);
}
@override
void write(Object obj) {
add(encoding.encode('$obj'));
}
@override
void writeln([Object obj = '']) {
add(encoding.encode('$obj\n'));
}
@override
void writeAll(Iterable<dynamic> objects, [String separator = '']) {
bool addSeparator = false;
for (dynamic object in objects) {
if (addSeparator) {
write(separator);
}
write(object);
addSeparator = true;
}
}
@override
void addError(dynamic error, [StackTrace stackTrace]) {
throw new UnimplementedError();
}
@override
Future<Null> get done => close();
@override
Future<Null> close() async => null;
@override
Future<Null> flush() async => null;
}
/// A Stdio that collects stdout and supports simulated stdin.
class MockStdio extends Stdio {
final MemoryIOSink _stdout = new MemoryIOSink();
final StreamController<List<int>> _stdin = new StreamController<List<int>>();
@override
IOSink get stdout => _stdout;
@override
Stream<List<int>> get stdin => _stdin.stream;
void simulateStdin(String line) {
_stdin.add(utf8.encode('$line\n'));
}
List<String> get writtenToStdout => _stdout.writes.map(_stdout.encoding.decode).toList();
}
class MockPollingDeviceDiscovery extends PollingDeviceDiscovery {
final List<Device> _devices = <Device>[];
final StreamController<Device> _onAddedController = new StreamController<Device>.broadcast();
final StreamController<Device> _onRemovedController = new StreamController<Device>.broadcast();
MockPollingDeviceDiscovery() : super('mock');
@override
Future<List<Device>> pollingGetDevices() async => _devices;
@override
bool get supportsPlatform => true;
@override
bool get canListAnything => true;
void addDevice(MockAndroidDevice device) {
_devices.add(device);
_onAddedController.add(device);
}
@override
Future<List<Device>> get devices async => _devices;
@override
Stream<Device> get onAdded => _onAddedController.stream;
@override
Stream<Device> get onRemoved => _onRemovedController.stream;
}
class MockAndroidDevice extends Mock implements AndroidDevice {
@override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.android_arm;
@override
bool isSupported() => true;
}
class MockIOSDevice extends Mock implements IOSDevice {
@override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios;
@override
bool isSupported() => true;
}
class MockIOSSimulator extends Mock implements IOSSimulator {
@override
Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios;
@override
bool isSupported() => true;
}
class MockDeviceLogReader extends DeviceLogReader {
@override
String get name => 'MockLogReader';
final StreamController<String> _linesController = new StreamController<String>.broadcast();
@override
Stream<String> get logLines => _linesController.stream;
void addLine(String line) => _linesController.add(line);
void dispose() {
_linesController.close();
}
}
void applyMocksToCommand(FlutterCommand command) {
command
..applicationPackages = new MockApplicationPackageStore();
}
/// Common functionality for tracking mock interaction
class BasicMock {
final List<String> messages = <String>[];
void expectMessages(List<String> expectedMessages) {
final List<String> actualMessages = new List<String>.from(messages);
messages.clear();
expect(actualMessages, unorderedEquals(expectedMessages));
}
bool contains(String match) {
print('Checking for `$match` in:');
print(messages);
final bool result = messages.contains(match);
messages.clear();
return result;
}
}
class MockDevFSOperations extends BasicMock implements DevFSOperations {
Map<Uri, DevFSContent> devicePathToContent = <Uri, DevFSContent>{};
@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
Future<dynamic> writeFile(String fsName, Uri deviceUri, DevFSContent content) async {
messages.add('writeFile $fsName $deviceUri');
devicePathToContent[deviceUri] = content;
}
@override
Future<dynamic> deleteFile(String fsName, Uri deviceUri) async {
messages.add('deleteFile $fsName $deviceUri');
devicePathToContent.remove(deviceUri);
}
}