blob: b083a1e7fb87d38977a66c773ac40b765410fc25 [file] [log] [blame]
// Copyright 2014 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:isolate';
import 'dart:mojo_bindings' as bindings;
import 'dart:mojo_core' as core;
import 'dart:typed_data';
import 'package:mojo/dart/testing/expect.dart';
import 'package:mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.dart' as sample;
class ProviderImpl extends sample.Provider {
ProviderImpl(core.MojoMessagePipeEndpoint endpoint) : super(endpoint) {
super.delegate = this;
}
echoString(String a, Function responseFactory) =>
new Future.value(responseFactory(a));
echoStrings(String a, String b, Function responseFactory) =>
new Future.value(responseFactory(a, b));
echoMessagePipeHanlde(core.MojoHandle a, Function responseFactory) =>
new Future.value(responseFactory(a));
echoEnum(int a, Function responseFactory) =>
new Future.value(responseFactory(a));
}
void providerIsolate(core.MojoMessagePipeEndpoint endpoint) {
var provider = new ProviderImpl(endpoint);
provider.listen();
}
Future<bool> test() {
var pipe = new core.MojoMessagePipe();
var client = new sample.ProviderProxy(pipe.endpoints[0]);
var c = new Completer();
Isolate.spawn(providerIsolate, pipe.endpoints[1]).then((_) {
client.echoString("hello!").then((echoStringResponse) {
Expect.equals("hello!", echoStringResponse.a);
}).then((_) {
client.echoStrings("hello", "mojo!").then((echoStringsResponse) {
Expect.equals("hello", echoStringsResponse.a);
Expect.equals("mojo!", echoStringsResponse.b);
client.close();
c.complete(true);
});
});
});
return c.future;
}
Future testAwait() async {
var pipe = new core.MojoMessagePipe();
var client = new sample.ProviderProxy(pipe.endpoints[0]);
var isolate = await Isolate.spawn(providerIsolate, pipe.endpoints[1]);
var echoStringResponse = await client.echoString("hello!");
Expect.equals("hello!", echoStringResponse.a);
var echoStringsResponse = await client.echoStrings("hello", "mojo!");
Expect.equals("hello", echoStringsResponse.a);
Expect.equals("mojo!", echoStringsResponse.b);
client.close();
}
main() async {
await test();
await testAwait();
}