flutter/packages/flutter_tools/test/daemon_test.dart

186 lines
7.1 KiB
Dart
Raw Normal View History

// 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:io';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/logger.dart';
2015-11-10 21:18:34 +00:00
import 'package:flutter_tools/src/commands/daemon.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/doctor.dart';
import 'package:flutter_tools/src/globals.dart';
import 'package:flutter_tools/src/ios/mac.dart';
import 'package:test/test.dart';
import 'src/context.dart';
import 'src/mocks.dart';
void main() {
2016-02-13 20:00:41 +00:00
Daemon daemon;
AppContext appContext;
NotifyingLogger notifyingLogger;
void _testUsingContext(String description, dynamic testMethod()) {
test(description, () {
return appContext.runInZone(testMethod);
});
}
2016-02-13 20:00:41 +00:00
group('daemon', () {
setUp(() {
appContext = new AppContext();
notifyingLogger = new NotifyingLogger();
2016-11-29 19:22:48 +00:00
appContext.setVariable(Logger, notifyingLogger);
appContext.setVariable(Doctor, new Doctor());
if (Platform.isMacOS)
2016-11-29 19:22:48 +00:00
appContext.setVariable(XCode, new XCode());
appContext.setVariable(DeviceManager, new MockDeviceManager());
});
tearDown(() {
if (daemon != null)
return daemon.shutdown();
notifyingLogger.dispose();
});
2016-02-13 20:00:41 +00:00
_testUsingContext('daemon.version', () async {
2016-03-12 08:32:34 +00:00
StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
notifyingLogger: notifyingLogger
);
2016-03-12 08:32:34 +00:00
commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.version'});
Map<String, dynamic> response = await responses.stream.where(_notEvent).first;
expect(response['id'], 0);
expect(response['result'], isNotEmpty);
expect(response['result'] is String, true);
responses.close();
commands.close();
});
2016-02-13 20:00:41 +00:00
_testUsingContext('daemon.logMessage', () {
return appContext.runInZone(() async {
2016-03-12 08:32:34 +00:00
StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
notifyingLogger: notifyingLogger
);
printError('daemon.logMessage test');
Map<String, dynamic> response = await responses.stream.where((Map<String, dynamic> map) {
return map['event'] == 'daemon.logMessage' && map['params']['level'] == 'error';
}).first;
expect(response['id'], isNull);
expect(response['event'], 'daemon.logMessage');
Map<String, String> logMessage = response['params'];
expect(logMessage['level'], 'error');
expect(logMessage['message'], 'daemon.logMessage test');
responses.close();
commands.close();
});
2016-01-25 21:15:01 +00:00
});
2016-02-13 20:00:41 +00:00
_testUsingContext('daemon.shutdown', () async {
2016-03-12 08:32:34 +00:00
StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
notifyingLogger: notifyingLogger
);
commands.add(<String, dynamic>{'id': 0, 'method': 'daemon.shutdown'});
return daemon.onExit.then((int code) {
responses.close();
commands.close();
expect(code, 0);
});
});
_testUsingContext('daemon.start', () async {
DaemonCommand command = new DaemonCommand();
applyMocksToCommand(command);
StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
daemonCommand: command,
notifyingLogger: notifyingLogger
);
commands.add(<String, dynamic>{ 'id': 0, 'method': 'app.start' });
Map<String, dynamic> response = await responses.stream.where(_notEvent).first;
expect(response['id'], 0);
expect(response['error'], contains('deviceId is required'));
responses.close();
commands.close();
});
_testUsingContext('daemon.restart', () async {
DaemonCommand command = new DaemonCommand();
applyMocksToCommand(command);
StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
daemonCommand: command,
notifyingLogger: notifyingLogger
);
commands.add(<String, dynamic>{ 'id': 0, 'method': 'app.restart' });
Map<String, dynamic> response = await responses.stream.where(_notEvent).first;
expect(response['id'], 0);
expect(response['error'], contains('appId is required'));
responses.close();
commands.close();
});
2016-03-03 21:31:05 +00:00
_testUsingContext('daemon.stop', () async {
DaemonCommand command = new DaemonCommand();
applyMocksToCommand(command);
2016-03-12 08:32:34 +00:00
StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
daemonCommand: command,
notifyingLogger: notifyingLogger
);
2016-03-03 21:31:05 +00:00
commands.add(<String, dynamic>{ 'id': 0, 'method': 'app.stop' });
2016-03-12 08:32:34 +00:00
Map<String, dynamic> response = await responses.stream.where(_notEvent).first;
expect(response['id'], 0);
expect(response['error'], contains('appId is required'));
responses.close();
commands.close();
});
2016-02-13 20:00:41 +00:00
_testUsingContext('device.getDevices', () async {
2016-03-12 08:32:34 +00:00
StreamController<Map<String, dynamic>> commands = new StreamController<Map<String, dynamic>>();
StreamController<Map<String, dynamic>> responses = new StreamController<Map<String, dynamic>>();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
notifyingLogger: notifyingLogger
);
commands.add(<String, dynamic>{'id': 0, 'method': 'device.getDevices'});
2016-03-12 08:32:34 +00:00
Map<String, dynamic> response = await responses.stream.where(_notEvent).first;
expect(response['id'], 0);
expect(response['result'], isList);
responses.close();
commands.close();
});
});
}
2016-01-25 21:15:01 +00:00
bool _notEvent(Map<String, dynamic> map) => map['event'] == null;