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 function createSender() {
26 requireAsync('content/public/renderer/service_provider'),
27 requireAsync('data_sender'),
28 requireAsync('device/serial/data_stream.mojom'),
29 ]).then(test.callbackPass(function(modules) {
30 var serviceProvider = modules[0];
31 var dataSender = modules[1];
32 var dataStream = modules[2];
33 return new dataSender.DataSender(
34 serviceProvider.connectToService(dataStream.DataSinkProxy.NAME_),
40 unittestBindings.exportTests([
42 createSender().then(test.callbackPass(function(sender) {
44 sender.send(generateData(1)).then(test.callbackPass(function(bytesSent) {
45 test.assertEq(1, bytesSent);
46 test.assertEq(null, seen);
49 sender.send(generateData(1)).then(test.callbackPass(function(bytesSent) {
51 test.assertEq(1, bytesSent);
52 test.assertEq('first', seen);
58 function testLargeSend() {
59 createSender().then(test.callbackPass(function(sender) {
60 sender.send(generateData(BUFFER_SIZE * 3, '123')).then(
61 test.callbackPass(function(bytesSent) {
62 test.assertEq(BUFFER_SIZE * 3, bytesSent);
68 function testSendError() {
69 createSender().then(test.callbackPass(function(sender) {
70 sender.send(generateData(BUFFER_SIZE * 3, 'b')).catch(test.callbackPass(
72 test.assertEq(1, e.error);
73 test.assertEq(0, e.bytesSent);
74 sender.send(generateData(1)).then(test.callbackPass(
76 test.assertEq(1, bytesSent);
83 function testSendErrorPartialSuccess() {
84 createSender().then(test.callbackPass(function(sender) {
85 sender.send(generateData(BUFFER_SIZE * 3, 'b')).catch(test.callbackPass(
87 test.assertEq(1, e.error);
88 test.assertEq(5, e.bytesSent);
89 sender.send(generateData(1)).then(test.callbackPass(
91 test.assertEq(1, bytesSent);
98 function testSendErrorBetweenPackets() {
99 createSender().then(test.callbackPass(function(sender) {
100 sender.send(generateData(2, 'b')).catch(test.callbackPass(function(e) {
101 test.assertEq(1, e.error);
102 test.assertEq(2, e.bytesSent);
104 // After an error, all sends in progress will be cancelled.
105 sender.send(generateData(2, 'b')).catch(test.callbackPass(function(e) {
106 test.assertEq(1, e.error);
107 test.assertEq(0, e.bytesSent);
108 sender.send(generateData(1)).then(test.callbackPass(
109 function(bytesSent) {
110 test.assertEq(1, bytesSent);
117 function testSendErrorInSecondPacket() {
118 createSender().then(test.callbackPass(function(sender) {
119 sender.send(generateData(2, 'b')).then(test.callbackPass(
120 function(bytesSent) {
121 test.assertEq(2, bytesSent);
123 sender.send(generateData(2, 'b')).catch(test.callbackPass(function(e) {
124 test.assertEq(1, e.error);
125 test.assertEq(1, e.bytesSent);
126 sender.send(generateData(1)).then(test.callbackPass(
127 function(bytesSent) {
128 test.assertEq(1, bytesSent);
135 function testSendErrorInLargeSend() {
136 createSender().then(test.callbackPass(function(sender) {
137 sender.send(generateData(BUFFER_SIZE * 3, '1234567890')).catch(
138 test.callbackPass(function(e) {
139 test.assertEq(1, e.error);
140 test.assertEq(12, e.bytesSent);
141 sender.send(generateData(1)).then(test.callbackPass(
142 function(bytesSent) {
143 test.assertEq(1, bytesSent);
150 function testSendErrorBeforeLargeSend() {
151 createSender().then(test.callbackPass(function(sender) {
152 sender.send(generateData(5, 'b')).catch(test.callbackPass(function(e) {
153 test.assertEq(1, e.error);
154 test.assertEq(2, e.bytesSent);
156 sender.send(generateData(BUFFER_SIZE * 3, '1234567890')).catch(
157 test.callbackPass(function(e) {
158 test.assertEq(1, e.error);
159 test.assertEq(0, e.bytesSent);
160 sender.send(generateData(1)).then(test.callbackPass(
161 function(bytesSent) {
162 test.assertEq(1, bytesSent);
169 function testCancelWithoutSend() {
170 createSender().then(test.callbackPass(function(sender) {
171 sender.cancel(3).then(test.callbackPass(function() {
177 function testCancel() {
178 createSender().then(test.callbackPass(function(sender) {
180 sender.send(generateData(1, 'b')).catch(test.callbackPass(function(e) {
181 test.assertEq(3, e.error);
182 test.assertEq(0, e.bytesSent);
183 test.assertEq(null, seen);
186 sender.cancel(3).then(test.callbackPass(function() {
187 test.assertEq('send', seen);
192 sender.cancel, sender, [], 'Cancel already in progress');
193 test.assertThrows(sender.send, sender, [], 'Cancel in progress');
197 function testClose() {
198 createSender().then(test.callbackPass(function(sender) {
200 sender.send(generateData(1, 'b')).catch(test.callbackPass(function(e) {
201 test.assertEq(FATAL_ERROR, e.error);
202 test.assertEq(0, e.bytesSent);
203 test.assertEq(null, seen);
206 sender.cancel(3).then(test.callbackPass(function() {
207 test.assertEq('send', seen);
213 sender.cancel, sender, [], 'DataSender has been closed');
214 test.assertThrows(sender.send, sender, [], 'DataSender has been closed');
218 ], test.runTests, exports);