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_sender_unittest.cc
7 var test = require('test').binding;
8 var unittestBindings = require('test_environment_specific_bindings');
13 function generateData(size, pattern) {
16 var buffer = new ArrayBuffer(size);
17 var intView = new Int8Array(buffer);
18 for (var i = 0; i < size; i++) {
19 intView[i] = pattern.charCodeAt(i % pattern.length);
24 // Returns a promise to a newly created DataSender.
25 function createSender() {
27 requireAsync('content/public/renderer/service_provider'),
28 requireAsync('data_sender'),
29 requireAsync('device/serial/data_stream.mojom'),
30 ]).then(function(modules) {
31 var serviceProvider = modules[0];
32 var dataSender = modules[1];
33 var dataStream = modules[2];
34 return new dataSender.DataSender(
35 serviceProvider.connectToService(dataStream.DataSink.name),
41 // Returns a function that sends data to a provided DataSender |sender|,
42 // checks that the send completes successfully and returns a promise that will
43 // resolve to |sender|.
44 function sendAndExpectSuccess(data) {
45 return function(sender) {
46 return sender.send(data).then(function(bytesSent) {
47 test.assertEq(data.byteLength, bytesSent);
53 // Returns a function that sends data to a provided DataSender |sender|,
54 // checks that the send fails with the expected error and expected number of
55 // bytes sent, and returns a promise that will resolve to |sender|.
56 function sendAndExpectError(data, expectedError, expectedBytesSent) {
57 return function(sender) {
58 return sender.send(data).catch(function(result) {
59 test.assertEq(expectedError, result.error);
60 test.assertEq(expectedBytesSent, result.bytesSent);
66 // Returns a function that cancels sends on the provided DataSender |sender|
67 // with error |cancelReason|, returning a promise that will resolve to |sender|
68 // once the cancel completes.
69 function cancelSend(cancelReason) {
70 return function(sender) {
71 return sender.cancel(cancelReason).then(function() {
77 // Checks that attempting to start a send with |sender| fails.
78 function sendAfterClose(sender) {
79 test.assertThrows(sender.send, sender, [], 'DataSender has been closed');
82 // Checks that the provided promises resolve in order, returning the result of
84 function expectOrder(promises) {
86 function createOrderChecker(promise, expectedIndex) {
87 return promise.then(function(sender) {
88 test.assertEq(nextIndex, expectedIndex);
93 var wrappedPromises = [];
94 for (var i = 0; i < promises.length; i++) {
95 wrappedPromises.push(createOrderChecker(promises[i], i));
97 return Promise.all(wrappedPromises).then(function(results) {
102 // Serializes and deserializes the provided DataSender |sender|, returning a
103 // promise that will resolve to the newly deserialized DataSender.
104 function serializeRoundTrip(sender) {
107 requireAsync('data_sender'),
108 ]).then(function(promises) {
109 var serialized = promises[0];
110 var dataSenderModule = promises[1];
111 return dataSenderModule.DataSender.deserialize(serialized);
115 function closeSender(sender) {
120 unittestBindings.exportTests([
121 function testSend() {
122 var sender = createSender();
124 sender.then(sendAndExpectSuccess(generateData(1))),
125 sender.then(sendAndExpectSuccess(generateData(1))),
128 .then(test.succeed, test.fail);
131 function testLargeSend() {
133 .then(sendAndExpectSuccess(generateData(BUFFER_SIZE * 3, '123')))
135 .then(test.succeed, test.fail);
138 function testSendError() {
140 .then(sendAndExpectError(generateData(BUFFER_SIZE * 3, 'b'), 1, 0))
141 .then(sendAndExpectSuccess(generateData(1)))
143 .then(test.succeed, test.fail);
146 function testSendErrorPartialSuccess() {
148 .then(sendAndExpectError(generateData(BUFFER_SIZE * 3, 'b'), 1, 5))
149 .then(sendAndExpectSuccess(generateData(1)))
151 .then(test.succeed, test.fail);
154 function testSendErrorBetweenPackets() {
155 var sender = createSender();
157 sender.then(sendAndExpectError(generateData(2, 'b'), 1, 2)),
158 sender.then(sendAndExpectError(generateData(2, 'b'), 1, 0)),
160 .then(sendAndExpectSuccess(generateData(1)))
162 .then(test.succeed, test.fail);
165 function testSendErrorInSecondPacket() {
166 var sender = createSender();
168 sender.then(sendAndExpectSuccess(generateData(2, 'b'))),
169 sender.then(sendAndExpectError(generateData(2, 'b'), 1, 1)),
171 .then(sendAndExpectSuccess(generateData(1)))
173 .then(test.succeed, test.fail);
176 function testSendErrorInLargeSend() {
178 .then(sendAndExpectError(
179 generateData(BUFFER_SIZE * 3, '1234567890'), 1, 12))
180 .then(sendAndExpectSuccess(generateData(1)))
182 .then(test.succeed, test.fail);
185 function testSendErrorBeforeLargeSend() {
186 var sender = createSender();
188 sender.then(sendAndExpectError(generateData(5, 'b'), 1, 2)),
189 sender.then(sendAndExpectError(
190 generateData(BUFFER_SIZE * 3, '1234567890'), 1, 0)),
192 .then(sendAndExpectSuccess(generateData(1)))
194 .then(test.succeed, test.fail);
197 function testCancelWithoutSend() {
201 .then(test.succeed, test.fail);
204 function testCancel() {
205 var sender = createSender();
207 sender.then(sendAndExpectError(generateData(1, 'b'), 3, 0)),
208 sender.then(cancelSend(3)),
211 .then(test.succeed, test.fail);
212 sender.then(function(sender) {
214 sender.cancel, sender, [], 'Cancel already in progress');
215 test.assertThrows(sender.send, sender, [], 'Cancel in progress');
219 function testClose() {
220 var sender = createSender();
222 sender.then(sendAndExpectError(generateData(1, 'b'), FATAL_ERROR, 0)),
223 sender.then(cancelSend(3)),
227 .then(sendAfterClose)
228 .then(test.succeed, test.fail);
231 function testSendAfterSerialization() {
232 var sender = createSender().then(serializeRoundTrip);
234 sender.then(sendAndExpectSuccess(generateData(1))),
235 sender.then(sendAndExpectSuccess(generateData(1))),
238 .then(test.succeed, test.fail);
241 function testSendErrorAfterSerialization() {
243 .then(serializeRoundTrip)
244 .then(sendAndExpectError(generateData(BUFFER_SIZE * 3, 'b'), 1, 0))
245 .then(sendAndExpectSuccess(generateData(1)))
247 .then(test.succeed, test.fail);
251 function testCancelAfterSerialization() {
252 var sender = createSender().then(serializeRoundTrip);
254 sender.then(sendAndExpectError(generateData(1, 'b'), 4, 0)),
255 sender.then(cancelSend(4)),
258 .then(test.succeed, test.fail);
261 function testSerializeCancelsSendsInProgress() {
262 var sender = createSender();
264 sender.then(sendAndExpectError(generateData(1, 'b'), FATAL_ERROR, 0)),
265 sender.then(sendAndExpectError(generateData(1, 'b'), FATAL_ERROR, 0)),
266 sender.then(serializeRoundTrip),
269 .then(test.succeed, test.fail);
272 function testSerializeWaitsForCancel() {
273 var sender = createSender();
275 sender.then(sendAndExpectError(generateData(1, 'b'), 3, 0)),
276 sender.then(cancelSend(3)),
277 sender.then(serializeRoundTrip),
280 .then(test.succeed, test.fail);
283 function testSerializeAfterClose() {
286 .then(serializeRoundTrip)
287 .then(sendAfterClose)
288 .then(test.succeed, test.fail);
291 ], test.runTests, exports);