1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Tests launched by extensions/renderer/api/serial/data_receiver_unittest.cc
7 var test = require('test').binding;
8 var unittestBindings = require('test_environment_specific_bindings');
13 // Returns a promise to a newly created DataReceiver.
14 function createReceiver() {
16 requireAsync('content/public/renderer/service_provider'),
17 requireAsync('data_receiver'),
18 requireAsync('device/serial/data_stream.mojom'),
19 ]).then(function(modules) {
20 var serviceProvider = modules[0];
21 var dataReceiver = modules[1];
22 var dataStream = modules[2];
23 return new dataReceiver.DataReceiver(
24 serviceProvider.connectToService(dataStream.DataSource.name),
30 // Returns a promise that will resolve to |receiver| when it has received an
31 // error from its DataSource.
32 function waitForReceiveError(receiver) {
33 return new Promise(function(resolve, reject) {
34 var onError = receiver.onError;
35 receiver.onError = function() {
36 $Function.apply(onError, receiver, arguments);
42 // Returns a function that receives data from a provided DataReceiver
43 // |receiver|, checks that it matches the expected data and returns a promise
44 // that will resolve to |receiver|.
45 function receiveAndCheckData(expectedData) {
46 return function(receiver) {
47 return receiver.receive().then(function(data) {
48 test.assertEq(expectedData.length, data.byteLength);
49 for (var i = 0; i < expectedData.length; i++)
50 test.assertEq(expectedData.charCodeAt(i), new Int8Array(data)[i]);
54 receiver.receive, receiver, [], 'Receive already in progress.');
58 // Returns a function that attempts to receive data from a provided DataReceiver
59 // |receiver|, checks that the correct error is reported and returns a promise
60 // that will resolve to |receiver|.
61 function receiveAndCheckError(expectedError) {
62 return function(receiver) {
63 return receiver.receive().catch(function(error) {
64 test.assertEq(expectedError, error.error);
68 receiver.receive, receiver, [], 'Receive already in progress.');
72 // Serializes and deserializes the provided DataReceiver |receiver|, returning
73 // a promise that will resolve to the newly deserialized DataReceiver.
74 function serializeRoundTrip(receiver) {
77 requireAsync('data_receiver'),
78 ]).then(function(promises) {
79 var serialized = promises[0];
80 var dataReceiverModule = promises[1];
81 return dataReceiverModule.DataReceiver.deserialize(serialized);
85 // Closes and returns the provided DataReceiver |receiver|.
86 function closeReceiver(receiver) {
91 unittestBindings.exportTests([
92 function testReceive() {
94 .then(receiveAndCheckData('a'))
96 .then(test.succeed, test.fail);
99 function testReceiveError() {
101 .then(receiveAndCheckError(1))
103 .then(test.succeed, test.fail);
106 function testReceiveDataAndError() {
108 .then(receiveAndCheckData('a'))
109 .then(receiveAndCheckError(1))
110 .then(receiveAndCheckData('b'))
112 .then(test.succeed, test.fail);
115 function testReceiveErrorThenData() {
117 .then(receiveAndCheckError(1))
118 .then(receiveAndCheckData('a'))
120 .then(test.succeed, test.fail);
123 function testReceiveBeforeAndAfterSerialization() {
125 .then(receiveAndCheckData('a'))
126 .then(serializeRoundTrip)
127 .then(receiveAndCheckData('b'))
129 .then(test.succeed, test.fail);
132 function testReceiveErrorSerialization() {
134 .then(waitForReceiveError)
135 .then(serializeRoundTrip)
136 .then(receiveAndCheckError(1))
137 .then(receiveAndCheckError(3))
139 .then(test.succeed, test.fail);
142 function testReceiveDataAndErrorSerialization() {
144 .then(waitForReceiveError)
145 .then(receiveAndCheckData('a'))
146 .then(serializeRoundTrip)
147 .then(receiveAndCheckError(1))
148 .then(receiveAndCheckData('b'))
149 .then(receiveAndCheckError(3))
151 .then(test.succeed, test.fail);
154 function testSerializeDuringReceive() {
155 var receiver = createReceiver();
157 receiver.then(receiveAndCheckError(FATAL_ERROR)),
159 .then(serializeRoundTrip)
160 .then(receiveAndCheckData('a'))
162 ]).then(test.succeed, test.fail);
165 function testSerializeAfterClose() {
166 function receiveAfterClose(receiver) {
168 receiver.receive, receiver, [], 'DataReceiver has been closed');
173 .then(serializeRoundTrip)
174 .then(receiveAfterClose)
175 .then(test.succeed, test.fail);
178 ], test.runTests, exports);