Integration test for channel communication (#9621)

This commit is contained in:
Mikkel Nygaard Ravn
2017-04-27 08:44:28 +02:00
committed by GitHub
parent 0770c3c14f
commit 70ff50f929
69 changed files with 2420 additions and 186 deletions

View File

@@ -0,0 +1,144 @@
// Copyright 2017 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:typed_data';
import 'package:flutter/services.dart';
import 'test_step.dart';
Future<TestStepResult> basicBinaryHandshake(ByteData message) async {
const BasicMessageChannel<ByteData> channel =
const BasicMessageChannel<ByteData>(
'binary-msg',
const BinaryCodec(),
);
return _basicMessageHandshake<ByteData>(
'Binary >${toString(message)}<', channel, message);
}
Future<TestStepResult> basicStringHandshake(String message) async {
const BasicMessageChannel<String> channel = const BasicMessageChannel<String>(
'string-msg',
const StringCodec(),
);
return _basicMessageHandshake<String>('String >$message<', channel, message);
}
Future<TestStepResult> basicJsonHandshake(dynamic message) async {
const BasicMessageChannel<dynamic> channel =
const BasicMessageChannel<dynamic>(
'json-msg',
const JSONMessageCodec(),
);
return _basicMessageHandshake<dynamic>('JSON >$message<', channel, message);
}
Future<TestStepResult> basicStandardHandshake(dynamic message) async {
const BasicMessageChannel<dynamic> channel =
const BasicMessageChannel<dynamic>(
'std-msg',
const StandardMessageCodec(),
);
return _basicMessageHandshake<dynamic>(
'Standard >${toString(message)}<', channel, message);
}
Future<TestStepResult> basicBinaryMessageToUnknownChannel() async {
const BasicMessageChannel<ByteData> channel =
const BasicMessageChannel<ByteData>(
'binary-unknown',
const BinaryCodec(),
);
return _basicMessageToUnknownChannel<ByteData>('Binary', channel);
}
Future<TestStepResult> basicStringMessageToUnknownChannel() async {
const BasicMessageChannel<String> channel = const BasicMessageChannel<String>(
'string-unknown',
const StringCodec(),
);
return _basicMessageToUnknownChannel<String>('String', channel);
}
Future<TestStepResult> basicJsonMessageToUnknownChannel() async {
const BasicMessageChannel<dynamic> channel =
const BasicMessageChannel<dynamic>(
'json-unknown',
const JSONMessageCodec(),
);
return _basicMessageToUnknownChannel<dynamic>('JSON', channel);
}
Future<TestStepResult> basicStandardMessageToUnknownChannel() async {
const BasicMessageChannel<dynamic> channel =
const BasicMessageChannel<dynamic>(
'std-unknown',
const StandardMessageCodec(),
);
return _basicMessageToUnknownChannel<dynamic>('Standard', channel);
}
/// Sends the specified message to the platform, doing a
/// receive message/send reply/receive reply echo handshake initiated by the
/// platform, then expecting a reply echo to the original message.
///
/// Fails, if an error occurs, or if any message seen is not deeply equal to
/// the original message.
Future<TestStepResult> _basicMessageHandshake<T>(
String description,
BasicMessageChannel<T> channel,
T message,
) async {
final List<dynamic> received = <dynamic>[];
channel.setMessageHandler((T message) async {
received.add(message);
return message;
});
dynamic messageEcho = nothing;
dynamic error = nothing;
try {
messageEcho = await channel.send(message);
} catch (e) {
error = e;
}
return resultOfHandshake(
'Basic message handshake',
description,
message,
received,
messageEcho,
error,
);
}
/// Sends a message on a channel that no one listens on.
Future<TestStepResult> _basicMessageToUnknownChannel<T>(
String description,
BasicMessageChannel<T> channel,
) async {
dynamic messageEcho = nothing;
dynamic error = nothing;
try {
messageEcho = await channel.send(null);
} catch (e) {
error = e;
}
return resultOfHandshake(
'Message on unknown channel',
description,
null,
<dynamic>[null, null],
messageEcho,
error,
);
}
String toString(dynamic message) {
if (message is ByteData)
return message.buffer
.asUint8List(message.offsetInBytes, message.lengthInBytes)
.toString();
else
return '$message';
}

View File

@@ -0,0 +1,133 @@
// Copyright 2017 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/services.dart';
import 'test_step.dart';
Future<TestStepResult> methodCallJsonSuccessHandshake(dynamic payload) async {
const MethodChannel channel =
const MethodChannel('json-method', const JSONMethodCodec());
return _methodCallSuccessHandshake(
'JSON success($payload)', channel, payload);
}
Future<TestStepResult> methodCallJsonErrorHandshake(dynamic payload) async {
const MethodChannel channel =
const MethodChannel('json-method', const JSONMethodCodec());
return _methodCallErrorHandshake('JSON error($payload)', channel, payload);
}
Future<TestStepResult> methodCallJsonNotImplementedHandshake() async {
const MethodChannel channel =
const MethodChannel('json-method', const JSONMethodCodec());
return _methodCallNotImplementedHandshake('JSON notImplemented()', channel);
}
Future<TestStepResult> methodCallStandardSuccessHandshake(
dynamic payload) async {
const MethodChannel channel =
const MethodChannel('std-method', const StandardMethodCodec());
return _methodCallSuccessHandshake(
'Standard success($payload)', channel, payload);
}
Future<TestStepResult> methodCallStandardErrorHandshake(dynamic payload) async {
const MethodChannel channel =
const MethodChannel('std-method', const StandardMethodCodec());
return _methodCallErrorHandshake(
'Standard error($payload)', channel, payload);
}
Future<TestStepResult> methodCallStandardNotImplementedHandshake() async {
const MethodChannel channel =
const MethodChannel('std-method', const StandardMethodCodec());
return _methodCallNotImplementedHandshake(
'Standard notImplemented()', channel);
}
Future<TestStepResult> _methodCallSuccessHandshake(
String description,
MethodChannel channel,
dynamic arguments,
) async {
final List<dynamic> received = <dynamic>[];
channel.setMethodCallHandler((MethodCall call) async {
received.add(call.arguments);
return call.arguments;
});
dynamic result = nothing;
dynamic error = nothing;
try {
result = await channel.invokeMethod('success', arguments);
} catch (e) {
error = e;
}
return resultOfHandshake(
'Method call success handshake',
description,
arguments,
received,
result,
error,
);
}
Future<TestStepResult> _methodCallErrorHandshake(
String description,
MethodChannel channel,
dynamic arguments,
) async {
final List<dynamic> received = <dynamic>[];
channel.setMethodCallHandler((MethodCall call) async {
received.add(call.arguments);
throw new PlatformException(
code: 'error', message: null, details: arguments);
});
dynamic errorDetails = nothing;
dynamic error = nothing;
try {
error = await channel.invokeMethod('error', arguments);
} on PlatformException catch (e) {
errorDetails = e.details;
} catch (e) {
error = e;
}
return resultOfHandshake(
'Method call error handshake',
description,
arguments,
received,
errorDetails,
error,
);
}
Future<TestStepResult> _methodCallNotImplementedHandshake(
String description,
MethodChannel channel,
) async {
final List<dynamic> received = <dynamic>[];
channel.setMethodCallHandler((MethodCall call) async {
received.add(call.arguments);
throw new MissingPluginException();
});
dynamic result = nothing;
dynamic error = nothing;
try {
error = await channel.invokeMethod('notImplemented');
} on MissingPluginException {
result = null;
} catch (e) {
error = e;
}
return resultOfHandshake(
'Method call not implemented handshake',
description,
null,
received,
result,
error,
);
}

View File

@@ -0,0 +1,168 @@
// Copyright 2017 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:typed_data';
import 'package:flutter/material.dart';
enum TestStatus { ok, pending, failed, complete }
typedef Future<TestStepResult> TestStep();
const String nothing = '-';
/// Result of a test step checking a nested communication handshake
/// between the Flutter app and the platform:
///
/// - The Flutter app sends a message to the platform.
/// - The platform, on receipt, echos the message back to Flutter in a separate message.
/// - The Flutter app records the incoming message echo and replies.
/// - The platform, on receipt of reply, echos the reply back to Flutter in a separate message.
/// - The Flutter app records the incoming reply echo.
/// - The platform finally replies to the original message with another echo.
class TestStepResult {
static const TextStyle bold = const TextStyle(fontWeight: FontWeight.bold);
static const TestStepResult complete = const TestStepResult(
'Test complete',
nothing,
TestStatus.complete,
);
const TestStepResult(
this.name,
this.description,
this.status, {
this.messageSent = nothing,
this.messageEcho = nothing,
this.messageReceived = nothing,
this.replyEcho = nothing,
this.error = nothing,
});
factory TestStepResult.fromSnapshot(AsyncSnapshot<TestStepResult> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return const TestStepResult('Not started', nothing, TestStatus.ok);
case ConnectionState.waiting:
return const TestStepResult('Executing', nothing, TestStatus.pending);
case ConnectionState.done:
if (snapshot.hasData) {
return snapshot.data;
} else {
final TestStepResult result = snapshot.error;
return result;
}
break;
default:
throw 'Unsupported state ${snapshot.connectionState}';
}
}
final String name;
final String description;
final TestStatus status;
final dynamic messageSent;
final dynamic messageEcho;
final dynamic messageReceived;
final dynamic replyEcho;
final dynamic error;
Widget asWidget(BuildContext context) {
return new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text('Step: $name', style: bold),
new Text(description),
const Text(' '),
new Text('Msg sent: ${_toString(messageSent)}'),
new Text('Msg rvcd: ${_toString(messageReceived)}'),
new Text('Reply echo: ${_toString(replyEcho)}'),
new Text('Msg echo: ${_toString(messageEcho)}'),
new Text('Error: ${_toString(error)}'),
const Text(' '),
new Text(
status.toString().substring('TestStatus.'.length),
key: new ValueKey<String>(
status == TestStatus.pending ? 'nostatus' : 'status'),
style: bold,
),
],
);
}
}
Future<TestStepResult> resultOfHandshake(
String name,
String description,
dynamic message,
List<dynamic> received,
dynamic messageEcho,
dynamic error,
) async {
assert(message != nothing);
while (received.length < 2) received.add(nothing);
TestStatus status;
if (!_deepEquals(messageEcho, message) ||
received.length != 2 ||
!_deepEquals(received[0], message) ||
!_deepEquals(received[1], message)) {
status = TestStatus.failed;
} else if (error != nothing) {
status = TestStatus.failed;
} else {
status = TestStatus.ok;
}
return new TestStepResult(
name,
description,
status,
messageSent: message,
messageEcho: messageEcho,
messageReceived: received[0],
replyEcho: received[1],
error: error,
);
}
String _toString(dynamic message) {
if (message is ByteData)
return message.buffer
.asUint8List(message.offsetInBytes, message.lengthInBytes)
.toString();
else
return '$message';
}
bool _deepEquals(dynamic a, dynamic b) {
if (a == b) return true;
if (a is double && a.isNaN) return b is double && b.isNaN;
if (a is ByteData) return b is ByteData && _deepEqualsByteData(a, b);
if (a is List) return b is List && _deepEqualsList(a, b);
if (a is Map) return b is Map && _deepEqualsMap(a, b);
return false;
}
bool _deepEqualsByteData(ByteData a, ByteData b) {
return _deepEqualsList(
a.buffer.asUint8List(a.offsetInBytes, a.lengthInBytes),
b.buffer.asUint8List(b.offsetInBytes, b.lengthInBytes),
);
}
bool _deepEqualsList(List<dynamic> a, List<dynamic> b) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
if (!_deepEquals(a[i], b[i])) return false;
}
return true;
}
bool _deepEqualsMap(Map<dynamic, dynamic> a, Map<dynamic, dynamic> b) {
if (a.length != b.length) return false;
for (dynamic key in a.keys) {
if (!b.containsKey(key) || !_deepEquals(a[key], b[key])) return false;
}
return true;
}