introduce globals scoped to the app's context

This commit is contained in:
Devon Carew
2016-02-06 19:19:50 -08:00
parent b2c710dd8d
commit 2dbceafbc6
42 changed files with 315 additions and 178 deletions

View File

@@ -5,11 +5,13 @@
import 'package:flutter_tools/src/android/device_android.dart';
import 'package:test/test.dart';
import 'src/test_context.dart';
main() => defineTests();
defineTests() {
group('android_device', () {
test('stores the requested id', () {
testUsingContext('stores the requested id', () {
String deviceId = '1234';
AndroidDevice device = new AndroidDevice(deviceId);
expect(device.id, equals(deviceId));

View File

@@ -2,9 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/context.dart' hide context;
import 'package:flutter_tools/src/base/globals.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:test/test.dart';
main() => defineTests();
@@ -12,55 +12,45 @@ main() => defineTests();
defineTests() {
group('DeviceManager', () {
test('error', () async {
MockContext mockContext = new MockContext();
AppContext context = new AppContext();
BufferLogger mockLogger = new BufferLogger();
context[Logger] = mockLogger;
runZoned(() {
context.runInZone(() {
printError('foo bar');
}, zoneValues: {'context': mockContext});
});
expect(mockContext.errorText, 'foo bar\n');
expect(mockContext.statusText, '');
expect(mockContext.traceText, '');
expect(mockLogger.errorText, 'foo bar\n');
expect(mockLogger.statusText, '');
expect(mockLogger.traceText, '');
});
test('status', () async {
MockContext mockContext = new MockContext();
AppContext context = new AppContext();
BufferLogger mockLogger = new BufferLogger();
context[Logger] = mockLogger;
runZoned(() {
context.runInZone(() {
printStatus('foo bar');
}, zoneValues: {'context': mockContext});
});
expect(mockContext.errorText, '');
expect(mockContext.statusText, 'foo bar\n');
expect(mockContext.traceText, '');
expect(mockLogger.errorText, '');
expect(mockLogger.statusText, 'foo bar\n');
expect(mockLogger.traceText, '');
});
test('trace', () async {
MockContext mockContext = new MockContext();
AppContext context = new AppContext();
BufferLogger mockLogger = new BufferLogger();
context[Logger] = mockLogger;
runZoned(() {
context.runInZone(() {
printTrace('foo bar');
}, zoneValues: {'context': mockContext});
});
expect(mockContext.errorText, '');
expect(mockContext.statusText, '');
expect(mockContext.traceText, 'foo bar\n');
expect(mockLogger.errorText, '');
expect(mockLogger.statusText, '');
expect(mockLogger.traceText, 'foo bar\n');
});
});
}
class MockContext implements AppContext {
bool verbose = false;
StringBuffer _error = new StringBuffer();
StringBuffer _status = new StringBuffer();
StringBuffer _trace = new StringBuffer();
String get errorText => _error.toString();
String get statusText => _status.toString();
String get traceText => _trace.toString();
void printError(String message, [StackTrace stackTrace]) => _error.writeln(message);
void printStatus(String message) => _status.writeln(message);
void printTrace(String message) => _trace.writeln(message);
}

View File

@@ -11,6 +11,8 @@ import 'package:flutter_tools/src/commands/create.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'src/test_context.dart';
main() => defineTests();
defineTests() {
@@ -31,7 +33,7 @@ defineTests() {
// TODO(devoncarew): https://github.com/flutter/flutter/issues/1709
if (Platform.isLinux) {
// Verify that we create a project that is well-formed.
test('flutter-simple', () async {
testUsingContext('flutter-simple', () async {
ArtifactStore.flutterRoot = '../..';
CreateCommand command = new CreateCommand();
CommandRunner runner = new CommandRunner('test_flutter', '')

View File

@@ -5,6 +5,8 @@
import 'dart:async';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/globals.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/commands/daemon.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
@@ -16,10 +18,13 @@ main() => defineTests();
defineTests() {
group('daemon', () {
Daemon daemon;
NotifyingAppContext appContext;
AppContext appContext;
NotifyingLogger notifyingLogger;
setUp(() {
appContext = new NotifyingAppContext();
appContext = new AppContext();
notifyingLogger = new NotifyingLogger();
appContext[Logger] = notifyingLogger;
});
tearDown(() {
@@ -33,7 +38,7 @@ defineTests() {
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
appContext: appContext
notifyingLogger: notifyingLogger
);
commands.add({'id': 0, 'method': 'daemon.version'});
Map response = await responses.stream.where(_notEvent).first;
@@ -43,13 +48,13 @@ defineTests() {
});
test('daemon.logMessage', () {
return runZoned(() async {
return appContext.runInZone(() async {
StreamController<Map<String, dynamic>> commands = new StreamController();
StreamController<Map<String, dynamic>> responses = new StreamController();
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
appContext: appContext
notifyingLogger: notifyingLogger
);
printError('daemon.logMessage test');
Map<String, dynamic> response = await responses.stream.where((Map<String, dynamic> map) {
@@ -60,7 +65,7 @@ defineTests() {
Map<String, String> logMessage = response['params'];
expect(logMessage['level'], 'error');
expect(logMessage['message'], 'daemon.logMessage test');
}, zoneValues: {'context': appContext});
});
});
test('daemon.shutdown', () async {
@@ -69,7 +74,7 @@ defineTests() {
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
appContext: appContext
notifyingLogger: notifyingLogger
);
commands.add({'id': 0, 'method': 'daemon.shutdown'});
return daemon.onExit.then((int code) {
@@ -87,7 +92,7 @@ defineTests() {
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
daemonCommand: command,
appContext: appContext
notifyingLogger: notifyingLogger
);
MockDeviceStore mockDevices = command.devices;
@@ -113,7 +118,7 @@ defineTests() {
daemon = new Daemon(
commands.stream,
(Map<String, dynamic> result) => responses.add(result),
appContext: appContext
notifyingLogger: notifyingLogger
);
commands.add({'id': 0, 'method': 'device.getDevices'});
Map response = await responses.stream.where(_notEvent).first;

View File

@@ -5,11 +5,13 @@
import 'package:flutter_tools/src/device.dart';
import 'package:test/test.dart';
import 'src/test_context.dart';
main() => defineTests();
defineTests() {
group('DeviceManager', () {
test('getDevices', () async {
testUsingContext('getDevices', () async {
// Test that DeviceManager.getDevices() doesn't throw.
DeviceManager deviceManager = new DeviceManager();
List<Device> devices = await deviceManager.getDevices();

View File

@@ -33,7 +33,7 @@ defineTests() {
CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command);
runner.run(['install']).then((int code) => expect(code, equals(0)));
return runner.run(['install']).then((int code) => expect(code, equals(0)));
});
test('returns 0 when iOS is connected and ready for an install', () {
@@ -55,7 +55,7 @@ defineTests() {
CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command);
runner.run(['install']).then((int code) => expect(code, equals(0)));
return runner.run(['install']).then((int code) => expect(code, equals(0)));
});
});
}

View File

@@ -10,12 +10,13 @@ import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
import 'src/mocks.dart';
import 'src/test_context.dart';
main() => defineTests();
defineTests() {
group('list', () {
test('returns 0 when called', () {
testUsingContext('returns 0 when called', () {
final String mockCommand = Platform.isWindows ? 'cmd /c echo' : 'echo';
ListCommand command = new ListCommand();
@@ -36,9 +37,8 @@ defineTests() {
// Instead, cause the test to run the echo command.
when(mockDevices.iOSSimulator.xcrunPath).thenReturn(mockCommand);
CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command);
runner.run(['list']).then((int code) => expect(code, equals(0)));
CommandRunner runner = new CommandRunner('test_flutter', '')..addCommand(command);
return runner.run(['list']).then((int code) => expect(code, equals(0)));
});
});
}

View File

@@ -9,12 +9,13 @@ import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
import 'src/mocks.dart';
import 'src/test_context.dart';
main() => defineTests();
defineTests() {
group('listen', () {
test('returns 0 when no device is connected', () {
testUsingContext('returns 0 when no device is connected', () {
ListenCommand command = new ListenCommand(singleRun: true);
applyMocksToCommand(command);
MockDeviceStore mockDevices = command.devices;
@@ -24,7 +25,7 @@ defineTests() {
when(mockDevices.iOSSimulator.isConnected()).thenReturn(false);
CommandRunner runner = new FlutterCommandRunner()..addCommand(command);
runner.run(['listen']).then((int code) => expect(code, equals(0)));
return runner.run(['listen']).then((int code) => expect(code, equals(0)));
});
});
}

View File

@@ -8,16 +8,17 @@ import 'package:flutter_tools/src/runner/flutter_command_runner.dart';
import 'package:test/test.dart';
import 'src/mocks.dart';
import 'src/test_context.dart';
main() => defineTests();
defineTests() {
group('logs', () {
test('fail with a bad device id', () {
testUsingContext('fail with a bad device id', () {
LogsCommand command = new LogsCommand();
applyMocksToCommand(command);
CommandRunner runner = new FlutterCommandRunner()..addCommand(command);
runner.run(<String>['-d', 'abc123', 'logs']).then((int code) {
return runner.run(<String>['-d', 'abc123', 'logs']).then((int code) {
expect(code, equals(1));
});
});

View File

@@ -0,0 +1,30 @@
// 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';
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:test/test.dart';
void testUsingContext(String description, dynamic testMethod(), { Timeout timeout }) {
test(description, () {
AppContext testContext = new AppContext();
testContext[Logger] = new BufferLogger();
testContext[DeviceManager] = new MockDeviceManager();
return testContext.runInZone(testMethod);
}, timeout: timeout);
}
class MockDeviceManager implements DeviceManager {
String specifiedDeviceId;
bool get hasSpecifiedDeviceId => specifiedDeviceId != null;
Future<List<Device>> getAllConnectedDevices() => new Future.value(<Device>[]);
Future<Device> getDeviceById(String deviceId) => new Future.value(null);
Future<List<Device>> getDevices() => getAllConnectedDevices();
}

View File

@@ -29,7 +29,7 @@ defineTests() {
CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command);
runner.run(['stop']).then((int code) => expect(code, equals(0)));
return runner.run(['stop']).then((int code) => expect(code, equals(0)));
});
test('returns 0 when iOS is connected and ready to be stopped', () {
@@ -48,7 +48,7 @@ defineTests() {
CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command);
runner.run(['stop']).then((int code) => expect(code, equals(0)));
return runner.run(['stop']).then((int code) => expect(code, equals(0)));
});
});
}

View File

@@ -8,12 +8,13 @@ import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
import 'src/mocks.dart';
import 'src/test_context.dart';
main() => defineTests();
defineTests() {
group('trace', () {
test('returns 1 when no Android device is connected', () {
testUsingContext('returns 1 when no Android device is connected', () {
TraceCommand command = new TraceCommand();
applyMocksToCommand(command);
MockDeviceStore mockDevices = command.devices;
@@ -22,7 +23,7 @@ defineTests() {
CommandRunner runner = new CommandRunner('test_flutter', '')
..addCommand(command);
runner.run(['trace']).then((int code) => expect(code, equals(1)));
return runner.run(['trace']).then((int code) => expect(code, equals(1)));
});
});
}