1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Tests launched by extensions/renderer/api/serial/data_receiver_unittest.cc
var test = require('test').binding;
var unittestBindings = require('test_environment_specific_bindings');
var BUFFER_SIZE = 10;
var FATAL_ERROR = 2;
// Returns a promise to a newly created DataReceiver.
function createReceiver() {
return Promise.all([
requireAsync('data_receiver'),
requireAsync('device/serial/data_receiver_test_factory'),
]).then(function(modules) {
var dataReceiver = modules[0];
var factory = modules[1];
var receiver = factory.create();
return new dataReceiver.DataReceiver(receiver.source, receiver.client,
BUFFER_SIZE, FATAL_ERROR);
});
}
// Returns a promise that will resolve to |receiver| when it has received an
// error from its DataSource.
function waitForReceiveError(receiver) {
return new Promise(function(resolve, reject) {
var onError = receiver.onError;
receiver.onError = function() {
$Function.apply(onError, receiver, arguments);
resolve(receiver);
};
});
}
// Returns a function that receives data from a provided DataReceiver
// |receiver|, checks that it matches the expected data and returns a promise
// that will resolve to |receiver|.
function receiveAndCheckData(expectedData) {
return function(receiver) {
return receiver.receive().then(function(data) {
test.assertEq(expectedData.length, data.byteLength);
for (var i = 0; i < expectedData.length; i++)
test.assertEq(expectedData.charCodeAt(i), new Int8Array(data)[i]);
return receiver;
});
test.assertThrows(
receiver.receive, receiver, [], 'Receive already in progress.');
};
}
// Returns a function that attempts to receive data from a provided DataReceiver
// |receiver|, checks that the correct error is reported and returns a promise
// that will resolve to |receiver|.
function receiveAndCheckError(expectedError) {
return function(receiver) {
return receiver.receive().catch(function(error) {
test.assertEq(expectedError, error.error);
return receiver;
});
test.assertThrows(
receiver.receive, receiver, [], 'Receive already in progress.');
};
}
// Serializes and deserializes the provided DataReceiver |receiver|, returning
// a promise that will resolve to the newly deserialized DataReceiver.
function serializeRoundTrip(receiver) {
return Promise.all([
receiver.serialize(),
requireAsync('data_receiver'),
]).then(function(promises) {
var serialized = promises[0];
var dataReceiverModule = promises[1];
return dataReceiverModule.DataReceiver.deserialize(serialized);
});
}
// Closes and returns the provided DataReceiver |receiver|.
function closeReceiver(receiver) {
receiver.close();
return receiver;
}
unittestBindings.exportTests([
function testReceive() {
createReceiver()
.then(receiveAndCheckData('a'))
.then(closeReceiver)
.then(test.succeed, test.fail);
},
function testReceiveError() {
createReceiver()
.then(receiveAndCheckError(1))
.then(closeReceiver)
.then(test.succeed, test.fail);
},
function testReceiveDataAndError() {
createReceiver()
.then(receiveAndCheckData('a'))
.then(receiveAndCheckError(1))
.then(receiveAndCheckData('b'))
.then(closeReceiver)
.then(test.succeed, test.fail);
},
function testReceiveErrorThenData() {
createReceiver()
.then(receiveAndCheckError(1))
.then(receiveAndCheckData('a'))
.then(closeReceiver)
.then(test.succeed, test.fail);
},
function testReceiveBeforeAndAfterSerialization() {
createReceiver()
.then(receiveAndCheckData('a'))
.then(serializeRoundTrip)
.then(receiveAndCheckData('b'))
.then(closeReceiver)
.then(test.succeed, test.fail);
},
function testReceiveErrorSerialization() {
createReceiver()
.then(waitForReceiveError)
.then(serializeRoundTrip)
.then(receiveAndCheckError(1))
.then(receiveAndCheckError(3))
.then(closeReceiver)
.then(test.succeed, test.fail);
},
function testReceiveDataAndErrorSerialization() {
createReceiver()
.then(waitForReceiveError)
.then(receiveAndCheckData('a'))
.then(serializeRoundTrip)
.then(receiveAndCheckError(1))
.then(receiveAndCheckData('b'))
.then(receiveAndCheckError(3))
.then(closeReceiver)
.then(test.succeed, test.fail);
},
function testSerializeDuringReceive() {
var receiver = createReceiver();
Promise.all([
receiver.then(receiveAndCheckError(FATAL_ERROR)),
receiver
.then(serializeRoundTrip)
.then(receiveAndCheckData('a'))
.then(closeReceiver)
]).then(test.succeed, test.fail);
},
function testSerializeAfterClose() {
function receiveAfterClose(receiver) {
test.assertThrows(
receiver.receive, receiver, [], 'DataReceiver has been closed');
}
createReceiver()
.then(closeReceiver)
.then(serializeRoundTrip)
.then(receiveAfterClose)
.then(test.succeed, test.fail);
},
], test.runTests, exports);
|