Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / test / data / data_sender_unittest.js
blob78568c90740baa68a8c0035ce3be8c7e6cbd7470
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');
10 var BUFFER_SIZE = 11;
11 var FATAL_ERROR = 2;
13 function generateData(size, pattern) {
14   if (!pattern)
15     pattern = 'a';
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);
20   }
21   return buffer;
24 function createSender() {
25   return Promise.all([
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_),
35         BUFFER_SIZE,
36         FATAL_ERROR);
37   }));
40 unittestBindings.exportTests([
41   function testSend() {
42     createSender().then(test.callbackPass(function(sender) {
43       var seen = null;
44       sender.send(generateData(1)).then(test.callbackPass(function(bytesSent) {
45         test.assertEq(1, bytesSent);
46         test.assertEq(null, seen);
47         seen = 'first';
48       }));
49       sender.send(generateData(1)).then(test.callbackPass(function(bytesSent) {
50         sender.close();
51         test.assertEq(1, bytesSent);
52         test.assertEq('first', seen);
53         seen = 'second';
54       }));
55     }));
56   },
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);
63         sender.close();
64       }));
65     }));
66   },
68   function testSendError() {
69     createSender().then(test.callbackPass(function(sender) {
70       sender.send(generateData(BUFFER_SIZE * 3, 'b')).catch(test.callbackPass(
71           function(e) {
72         test.assertEq(1, e.error);
73         test.assertEq(0, e.bytesSent);
74         sender.send(generateData(1)).then(test.callbackPass(
75             function(bytesSent) {
76           test.assertEq(1, bytesSent);
77           sender.close();
78         }));
79       }));
80     }));
81   },
83   function testSendErrorPartialSuccess() {
84     createSender().then(test.callbackPass(function(sender) {
85       sender.send(generateData(BUFFER_SIZE * 3, 'b')).catch(test.callbackPass(
86           function(e) {
87         test.assertEq(1, e.error);
88         test.assertEq(5, e.bytesSent);
89         sender.send(generateData(1)).then(test.callbackPass(
90             function(bytesSent) {
91           test.assertEq(1, bytesSent);
92           sender.close();
93         }));
94       }));
95     }));
96   },
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);
103       }));
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);
111           sender.close();
112         }));
113       }));
114     }));
115   },
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);
122       }));
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);
129           sender.close();
130         }));
131       }));
132     }));
133   },
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);
144           sender.close();
145         }));
146       }));
147     }));
148   },
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);
155       }));
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);
163           sender.close();
164         }));
165       }));
166     }));
167   },
169   function testCancelWithoutSend() {
170     createSender().then(test.callbackPass(function(sender) {
171       sender.cancel(3).then(test.callbackPass(function() {
172         sender.close();
173       }));
174     }));
175   },
177   function testCancel() {
178     createSender().then(test.callbackPass(function(sender) {
179       var seen = null;
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);
184         seen = 'send';
185       }));
186       sender.cancel(3).then(test.callbackPass(function() {
187         test.assertEq('send', seen);
188         seen = 'cancel';
189         sender.close();
190       }));
191       test.assertThrows(
192           sender.cancel, sender, [], 'Cancel already in progress');
193       test.assertThrows(sender.send, sender, [], 'Cancel in progress');
194     }));
195   },
197   function testClose() {
198     createSender().then(test.callbackPass(function(sender) {
199       var seen = null;
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);
204         seen = 'send';
205       }));
206       sender.cancel(3).then(test.callbackPass(function() {
207         test.assertEq('send', seen);
208         seen = 'cancel';
209         sender.close();
210       }));
211       sender.close();
212       test.assertThrows(
213           sender.cancel, sender, [], 'DataSender has been closed');
214       test.assertThrows(sender.send, sender, [], 'DataSender has been closed');
215     }));
216   },
218 ], test.runTests, exports);