Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / test / data / data_receiver_unittest.js
blob1bdda9663e687c01e1f70bdfb5c0496b34eef0ff
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');
10 var fatalErrorValue = 2;
11 var bufferSize = 10;
13 function createReceiver() {
14   return Promise.all([
15     requireAsync('content/public/renderer/service_provider'),
16     requireAsync('data_receiver'),
17     requireAsync('device/serial/data_stream.mojom'),
18   ]).then(test.callbackPass(function(modules) {
19     var serviceProvider = modules[0];
20     var dataReceiver = modules[1];
21     var dataStream = modules[2];
22     return new dataReceiver.DataReceiver(
23         serviceProvider.connectToService(dataStream.DataSourceProxy.NAME_),
24         bufferSize,
25         fatalErrorValue);
26   }));
29 unittestBindings.exportTests([
30   function testReceive() {
31     createReceiver().then(test.callbackPass(function(receiver) {
32       receiver.receive().then(test.callbackPass(function(data) {
33         test.assertEq(1, data.byteLength);
34         test.assertEq('a'.charCodeAt(0), new Int8Array(data)[0]);
35         receiver.close();
36       }));
37       test.assertThrows(
38           receiver.receive, receiver, [], 'Receive already in progress.');
39     }));
40   },
42   function testReceiveError() {
43     createReceiver().then(test.callbackPass(function(receiver) {
44       receiver.receive().catch(test.callbackPass(function(error) {
45         test.assertEq(1, error.error);
46         receiver.close();
47       }));
48     }));
49   },
51   function testReceiveDataAndError() {
52     createReceiver().then(test.callbackPass(function(receiver) {
53       receiver.receive().then(test.callbackPass(function(data) {
54         test.assertEq(1, data.byteLength);
55         test.assertEq('a'.charCodeAt(0), new Int8Array(data)[0]);
56         return receiver.receive();
57       })).catch(test.callbackPass(function(error) {
58         test.assertEq(1, error.error);
59         return receiver.receive();
60       })).then(test.callbackPass(function(data) {
61         test.assertEq(1, data.byteLength);
62         test.assertEq('b'.charCodeAt(0), new Int8Array(data)[0]);
63         receiver.close();
64       }));
65     }));
66   },
68   function testReceiveErrorThenData() {
69     createReceiver().then(test.callbackPass(function(receiver) {
70       receiver.receive().catch(test.callbackPass(function(error) {
71         test.assertEq(1, error.error);
72         return receiver.receive();
73       })).then(test.callbackPass(function(data) {
74         test.assertEq(1, data.byteLength);
75         test.assertEq('a'.charCodeAt(0), new Int8Array(data)[0]);
76         receiver.close();
77       }));
78     }));
79   },
81   function testSourceShutdown() {
82     createReceiver().then(test.callbackPass(function(receiver) {
83       receiver.receive().catch(test.callbackPass(function(error) {
84         test.assertEq(fatalErrorValue, error.error);
85         receiver.close();
86       }));
87     }));
88   },
90 ], test.runTests, exports);