Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / extensions / test / data / data_receiver_unittest.js
blobb46189a4631a65f88e86a57d4f436b0e6d61c3d4
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 BUFFER_SIZE = 10;
11 var FATAL_ERROR = 2;
13 // Returns a promise to a newly created DataReceiver.
14 function createReceiver() {
15 return Promise.all([
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);
24 });
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);
34 resolve(receiver);
36 });
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]);
48 return receiver;
49 });
50 test.assertThrows(
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);
62 return receiver;
63 });
64 test.assertThrows(
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) {
72 return Promise.all([
73 receiver.serialize(),
74 requireAsync('data_receiver'),
75 ]).then(function(promises) {
76 var serialized = promises[0];
77 var dataReceiverModule = promises[1];
78 return dataReceiverModule.DataReceiver.deserialize(serialized);
79 });
82 // Closes and returns the provided DataReceiver |receiver|.
83 function closeReceiver(receiver) {
84 receiver.close();
85 return receiver;
88 unittestBindings.exportTests([
89 function testReceive() {
90 createReceiver()
91 .then(receiveAndCheckData('a'))
92 .then(closeReceiver)
93 .then(test.succeed, test.fail);
96 function testReceiveError() {
97 createReceiver()
98 .then(receiveAndCheckError(1))
99 .then(closeReceiver)
100 .then(test.succeed, test.fail);
103 function testReceiveDataAndError() {
104 createReceiver()
105 .then(receiveAndCheckData('a'))
106 .then(receiveAndCheckError(1))
107 .then(receiveAndCheckData('b'))
108 .then(closeReceiver)
109 .then(test.succeed, test.fail);
112 function testReceiveErrorThenData() {
113 createReceiver()
114 .then(receiveAndCheckError(1))
115 .then(receiveAndCheckData('a'))
116 .then(closeReceiver)
117 .then(test.succeed, test.fail);
120 function testReceiveBeforeAndAfterSerialization() {
121 createReceiver()
122 .then(receiveAndCheckData('a'))
123 .then(serializeRoundTrip)
124 .then(receiveAndCheckData('b'))
125 .then(closeReceiver)
126 .then(test.succeed, test.fail);
129 function testReceiveErrorSerialization() {
130 createReceiver()
131 .then(waitForReceiveError)
132 .then(serializeRoundTrip)
133 .then(receiveAndCheckError(1))
134 .then(receiveAndCheckError(3))
135 .then(closeReceiver)
136 .then(test.succeed, test.fail);
139 function testReceiveDataAndErrorSerialization() {
140 createReceiver()
141 .then(waitForReceiveError)
142 .then(receiveAndCheckData('a'))
143 .then(serializeRoundTrip)
144 .then(receiveAndCheckError(1))
145 .then(receiveAndCheckData('b'))
146 .then(receiveAndCheckError(3))
147 .then(closeReceiver)
148 .then(test.succeed, test.fail);
151 function testSerializeDuringReceive() {
152 var receiver = createReceiver();
153 Promise.all([
154 receiver.then(receiveAndCheckError(FATAL_ERROR)),
155 receiver
156 .then(serializeRoundTrip)
157 .then(receiveAndCheckData('a'))
158 .then(closeReceiver)
159 ]).then(test.succeed, test.fail);
162 function testSerializeAfterClose() {
163 function receiveAfterClose(receiver) {
164 test.assertThrows(
165 receiver.receive, receiver, [], 'DataReceiver has been closed');
168 createReceiver()
169 .then(closeReceiver)
170 .then(serializeRoundTrip)
171 .then(receiveAfterClose)
172 .then(test.succeed, test.fail);
175 ], test.runTests, exports);