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('data_receiver'),
17 requireAsync('device/serial/data_receiver_test_factory'),
18 ]).then(function(modules) {
19 var dataReceiver = modules[0];
20 var factory = modules[1];
21 var receiver = factory.create();
22 return new dataReceiver.DataReceiver(receiver.source, receiver.client,
23 BUFFER_SIZE, FATAL_ERROR);
27 // Returns a promise that will resolve to |receiver| when it has received an
28 // error from its DataSource.
29 function waitForReceiveError(receiver) {
30 return new Promise(function(resolve, reject) {
31 var onError = receiver.onError;
32 receiver.onError = function() {
33 $Function.apply(onError, receiver, arguments);
39 // Returns a function that receives data from a provided DataReceiver
40 // |receiver|, checks that it matches the expected data and returns a promise
41 // that will resolve to |receiver|.
42 function receiveAndCheckData(expectedData) {
43 return function(receiver) {
44 return receiver.receive().then(function(data) {
45 test.assertEq(expectedData.length, data.byteLength);
46 for (var i = 0; i < expectedData.length; i++)
47 test.assertEq(expectedData.charCodeAt(i), new Int8Array(data)[i]);
51 receiver.receive, receiver, [], 'Receive already in progress.');
55 // Returns a function that attempts to receive data from a provided DataReceiver
56 // |receiver|, checks that the correct error is reported and returns a promise
57 // that will resolve to |receiver|.
58 function receiveAndCheckError(expectedError) {
59 return function(receiver) {
60 return receiver.receive().catch(function(error) {
61 test.assertEq(expectedError, error.error);
65 receiver.receive, receiver, [], 'Receive already in progress.');
69 // Serializes and deserializes the provided DataReceiver |receiver|, returning
70 // a promise that will resolve to the newly deserialized DataReceiver.
71 function serializeRoundTrip(receiver) {
74 requireAsync('data_receiver'),
75 ]).then(function(promises) {
76 var serialized = promises[0];
77 var dataReceiverModule = promises[1];
78 return dataReceiverModule.DataReceiver.deserialize(serialized);
82 // Closes and returns the provided DataReceiver |receiver|.
83 function closeReceiver(receiver) {
88 unittestBindings.exportTests([
89 function testReceive() {
91 .then(receiveAndCheckData('a'))
93 .then(test.succeed, test.fail);
96 function testReceiveError() {
98 .then(receiveAndCheckError(1))
100 .then(test.succeed, test.fail);
103 function testReceiveDataAndError() {
105 .then(receiveAndCheckData('a'))
106 .then(receiveAndCheckError(1))
107 .then(receiveAndCheckData('b'))
109 .then(test.succeed, test.fail);
112 function testReceiveErrorThenData() {
114 .then(receiveAndCheckError(1))
115 .then(receiveAndCheckData('a'))
117 .then(test.succeed, test.fail);
120 function testReceiveBeforeAndAfterSerialization() {
122 .then(receiveAndCheckData('a'))
123 .then(serializeRoundTrip)
124 .then(receiveAndCheckData('b'))
126 .then(test.succeed, test.fail);
129 function testReceiveErrorSerialization() {
131 .then(waitForReceiveError)
132 .then(serializeRoundTrip)
133 .then(receiveAndCheckError(1))
134 .then(receiveAndCheckError(3))
136 .then(test.succeed, test.fail);
139 function testReceiveDataAndErrorSerialization() {
141 .then(waitForReceiveError)
142 .then(receiveAndCheckData('a'))
143 .then(serializeRoundTrip)
144 .then(receiveAndCheckError(1))
145 .then(receiveAndCheckData('b'))
146 .then(receiveAndCheckError(3))
148 .then(test.succeed, test.fail);
151 function testSerializeDuringReceive() {
152 var receiver = createReceiver();
154 receiver.then(receiveAndCheckError(FATAL_ERROR)),
156 .then(serializeRoundTrip)
157 .then(receiveAndCheckData('a'))
159 ]).then(test.succeed, test.fail);
162 function testSerializeAfterClose() {
163 function receiveAfterClose(receiver) {
165 receiver.receive, receiver, [], 'DataReceiver has been closed');
170 .then(serializeRoundTrip)
171 .then(receiveAfterClose)
172 .then(test.succeed, test.fail);
175 ], test.runTests, exports);