Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: extensions/test/data/api_test/hid/api/background.js

Issue 690473002: Add API tests for the chrome.hid API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/shell/app_shell.gyp ('k') | extensions/test/data/api_test/hid/api/manifest.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 var kInvalidDeviceId = -1;
6 var kInvalidConnectionId = -1;
7
8 function getDevice(wantReportIds, callback) {
9 chrome.hid.getDevices({}, function (devices) {
10 chrome.test.assertNoLastError();
11 for (var device of devices) {
12 chrome.test.assertTrue(device.collections.length > 0);
13 var foundReportId = false;
14 for (var collection of device.collections) {
15 if (collection.reportIds.length > 0) {
16 foundReportId = true;
17 }
18 }
19 if (wantReportIds == foundReportId) {
20 callback(device);
21 return;
22 }
23 }
24 chrome.test.fail("No appropriate device found.");
25 });
26 }
27
28 function openDevice(wantReportIds, callback) {
29 getDevice(wantReportIds, function (device) {
30 chrome.hid.connect(device.deviceId, function (connection) {
31 chrome.test.assertNoLastError();
32 callback(connection.connectionId);
33 });
34 });
35 }
36
37 function openDeviceWithReportId(callback) {
38 return openDevice(true, callback);
39 }
40
41 function openDeviceWithoutReportId(callback) {
42 return openDevice(false, callback);
43 }
44
45 function arrayBufferToString(buffer) {
46 return String.fromCharCode.apply(null, new Uint8Array(buffer));
47 }
48
49 function stringToArrayBuffer(string) {
50 var buffer = new ArrayBuffer(string.length);
51 var view = new Uint8Array(buffer);
52 for (var i = 0; i < string.length; i++) {
53 view[i] = string.charCodeAt(i);
54 }
55 return buffer;
56 }
57
58 function testGetDevicesWithNoOptions() {
59 chrome.hid.getDevices({}, function (devices) {
60 chrome.test.assertNoLastError();
61 chrome.test.assertEq(2, devices.length, "Expected two enumerated devices.");
62 chrome.test.succeed("Device enumeration successful.");
63 });
64 };
65
66 function testGetDevicesWithLegacyVidAndPid() {
67 chrome.hid.getDevices({
68 'vendorId': 0x18D1,
69 'productId': 0x58F0
70 }, function (devices) {
71 chrome.test.assertNoLastError();
72 chrome.test.assertEq(2, devices.length, "Expected two enumerated devices.");
73 chrome.test.succeed("Device enumeration successful.");
74 });
75 };
76
77 function testGetDevicesWithNoFilters() {
78 chrome.hid.getDevices({ 'filters': [] }, function (devices) {
79 chrome.test.assertNoLastError();
80 chrome.test.assertEq(2, devices.length, "Expected two enumerated devices.");
81 chrome.test.succeed("Device enumeration successful.");
82 });
83 };
84
85 function testGetDevicesWithVidPidFilter() {
86 chrome.hid.getDevices({ 'filters': [
87 { 'vendorId': 0x18D1, 'productId': 0x58F0}
88 ] }, function (devices) {
89 chrome.test.assertNoLastError();
90 chrome.test.assertEq(2, devices.length, "Expected two enumerated devices.");
91 chrome.test.succeed("Device enumeration successful.");
92 });
93 };
94
95 function testGetDevicesWithUsageFilter() {
96 chrome.hid.getDevices({ 'filters': [
97 { 'usagePage': 0xFF00 } /* vendor-specified usage page */
98 ] }, function (devices) {
99 chrome.test.assertNoLastError();
100 chrome.test.assertEq(1, devices.length, "Expected one enumerated device.");
101 var device = devices[0];
102 chrome.test.assertEq(1, device.collections.length,
103 "Expected one collection.");
104 var collection = device.collections[0];
105 chrome.test.assertEq(0xFF00, collection.usagePage);
106 chrome.test.succeed("Device enumeration successful.");
107 });
108 }
109
110 function testGetDevicesWithUnauthorizedDevice() {
111 chrome.hid.getDevices({ 'filters': [
112 { 'vendorId': 0x18D1, 'productId': 0x58F1}
113 ] }, function (devices) {
114 chrome.test.assertNoLastError();
115 chrome.test.assertEq(0, devices.length, "Expected no enumerated devices.");
116 chrome.test.succeed("Device enumeration successful.");
117 });
118 };
119
120 function testConnectWithInvalidDeviceId() {
121 chrome.hid.connect(kInvalidDeviceId, function (connection) {
122 chrome.test.assertLastError("Invalid HID device ID.");
123 chrome.test.succeed("Rejected invalid device ID.");
124 });
125 };
126
127 function testConnectAndDisconnect() {
128 chrome.hid.getDevices({ 'filters': [
129 { 'vendorId': 0x18D1, 'productId': 0x58F0 }
130 ] }, function (devices) {
131 chrome.test.assertNoLastError();
132 chrome.test.assertTrue(devices.length >= 1, "Expected connectable device.");
133 chrome.hid.connect(devices[0].deviceId, function (connection) {
134 chrome.test.assertNoLastError();
135 chrome.hid.disconnect(connection.connectionId, function () {
136 chrome.test.assertNoLastError();
137 chrome.test.succeed("Opened and closed device.");
138 });
139 });
140 });
141 };
142
143 function testDisconnectWithInvalidConnectionId() {
144 chrome.hid.disconnect(kInvalidConnectionId, function () {
145 chrome.test.assertLastError("Connection not established.");
146 chrome.test.succeed("Rejected invalid connection ID.");
147 });
148 }
149
150 function testReceiveWithInvalidConnectionId() {
151 chrome.hid.receive(kInvalidConnectionId, function (reportId, data) {
152 chrome.test.assertLastError("Connection not established.");
153 chrome.test.succeed("Rejected invalid connection ID.");
154 });
155 }
156
157 function testReceiveWithReportId() {
158 openDeviceWithReportId(function (connection) {
159 chrome.hid.receive(connection, function (reportId, data) {
160 chrome.test.assertEq(1, reportId, "Expected report_id == 1.");
161 var expected = "This is a HID input report.";
162 chrome.test.assertEq(expected, arrayBufferToString(data));
163 chrome.test.succeed("Receive successful.");
164 });
165 });
166 }
167
168 function testReceiveWithoutReportId() {
169 openDeviceWithoutReportId(function (connection) {
170 chrome.hid.receive(connection, function (reportId, data) {
171 chrome.test.assertNoLastError();
172 chrome.test.assertEq(0, reportId, "Expected report_id == 0.");
173 var expected = "This is a HID input report.";
174 chrome.test.assertEq(expected, arrayBufferToString(data));
175 chrome.test.succeed("Receive successful.");
176 });
177 });
178 }
179
180 function testSendWithInvalidConnectionId() {
181 var buffer = new ArrayBuffer();
182 chrome.hid.send(kInvalidConnectionId, 0, buffer, function () {
183 chrome.test.assertLastError("Connection not established.");
184 chrome.test.succeed("Rejected invalid connection ID.");
185 });
186 }
187
188 function testSendWithReportId() {
189 openDeviceWithReportId(function (connection) {
190 var buffer = stringToArrayBuffer("This is a HID output report.");
191 chrome.hid.send(connection, 1, buffer, function () {
192 chrome.test.assertNoLastError();
193 chrome.hid.disconnect(connection);
194 chrome.test.succeed("Send successful.");
195 });
196 });
197 }
198
199 function testSendWithoutReportId() {
200 openDeviceWithoutReportId(function (connection) {
201 var buffer = stringToArrayBuffer("This is a HID output report.");
202 chrome.hid.send(connection, 0, buffer, function () {
203 chrome.test.assertNoLastError();
204 chrome.hid.disconnect(connection);
205 chrome.test.succeed("Send successful.");
206 });
207 });
208 }
209
210 function testSendWithInvalidReportId() {
211 openDeviceWithReportId(function (connection) {
212 var buffer = stringToArrayBuffer("This is a HID output report.");
213 chrome.hid.send(connection, 0, buffer, function () {
214 chrome.test.assertLastError("Transfer failed.");
215 chrome.hid.disconnect(connection);
216 chrome.test.succeed("Caught invalid report ID.");
217 });
218 });
219 }
220
221 function testSendWithUnexpectedReportId() {
222 openDeviceWithoutReportId(function (connection) {
223 var buffer = stringToArrayBuffer("This is a HID output report.");
224 chrome.hid.send(connection, 1, buffer, function () {
225 chrome.test.assertLastError("Transfer failed.");
226 chrome.hid.disconnect(connection);
227 chrome.test.succeed("Caught unexpected report ID.");
228 });
229 });
230 }
231
232 function testReceiveFeatureReportWithInvalidConnectionId() {
233 chrome.hid.receiveFeatureReport(kInvalidConnectionId, 0, function (data) {
234 chrome.test.assertLastError("Connection not established.");
235 chrome.test.succeed("Rejected invalid connection ID.");
236 });
237 }
238
239 function testReceiveFeatureReportWithReportId() {
240 openDeviceWithReportId(function (connection) {
241 chrome.hid.receiveFeatureReport(connection, 1, function (data) {
242 chrome.test.assertNoLastError();
243 var expected = "\1This is a HID feature report.";
244 chrome.test.assertEq(expected, arrayBufferToString(data));
245 chrome.test.succeed("Received feature report.");
246 });
247 });
248 }
249
250 function testReceiveFeatureReportWithoutReportId() {
251 openDeviceWithoutReportId(function (connection) {
252 chrome.hid.receiveFeatureReport(connection, 0, function (data) {
253 chrome.test.assertNoLastError();
254 var expected = "This is a HID feature report.";
255 chrome.test.assertEq(expected, arrayBufferToString(data));
256 chrome.test.succeed("Received feature report.");
257 });
258 });
259 }
260
261 function testReceiveFeatureReportWithInvalidReportId() {
262 openDeviceWithReportId(function (connection) {
263 chrome.hid.receiveFeatureReport(connection, 0, function (data) {
264 chrome.test.assertLastError("Transfer failed.");
265 chrome.test.succeed("Caught invalid report ID.");
266 });
267 });
268 }
269
270 function testReceiveFeatureReportWithUnexpectedReportId() {
271 openDeviceWithoutReportId(function (connection) {
272 chrome.hid.receiveFeatureReport(connection, 1, function (data) {
273 chrome.test.assertLastError("Transfer failed.");
274 chrome.test.succeed("Caught unexpected report ID.");
275 });
276 });
277 }
278
279 function testSendFeatureReportWithInvalidConnectionId() {
280 var buffer = new ArrayBuffer();
281 chrome.hid.sendFeatureReport(kInvalidConnectionId, 0, buffer, function () {
282 chrome.test.assertLastError("Connection not established.");
283 chrome.test.succeed("Rejected invalid connection ID.");
284 });
285 }
286
287 function testSendFeatureReportWithReportId() {
288 openDeviceWithReportId(function (connection) {
289 var buffer =
290 stringToArrayBuffer("The app is setting this HID feature report.");
291 chrome.hid.sendFeatureReport(connection, 1, buffer, function () {
292 chrome.test.assertNoLastError();
293 chrome.hid.disconnect(connection);
294 chrome.test.succeed("Send successful.");
295 });
296 });
297 }
298
299 function testSendFeatureReportWithoutReportId() {
300 openDeviceWithoutReportId(function (connection) {
301 var buffer =
302 stringToArrayBuffer("The app is setting this HID feature report.");
303 chrome.hid.sendFeatureReport(connection, 0, buffer, function () {
304 chrome.test.assertNoLastError();
305 chrome.hid.disconnect(connection);
306 chrome.test.succeed("Send successful.");
307 });
308 });
309 }
310
311 function testSendFeatureReportWithInvalidReportId() {
312 openDeviceWithReportId(function (connection) {
313 var buffer =
314 stringToArrayBuffer("The app is setting this HID feature report.");
315 chrome.hid.sendFeatureReport(connection, 0, buffer, function () {
316 chrome.test.assertLastError("Transfer failed.");
317 chrome.hid.disconnect(connection);
318 chrome.test.succeed("Caught invalid report ID.");
319 });
320 });
321 }
322
323 function testSendFeatureReportWithUnexpectedReportId() {
324 openDeviceWithoutReportId(function (connection) {
325 var buffer =
326 stringToArrayBuffer("The app is setting this HID feature report.");
327 chrome.hid.sendFeatureReport(connection, 1, buffer, function () {
328 chrome.test.assertLastError("Transfer failed.");
329 chrome.hid.disconnect(connection);
330 chrome.test.succeed("Caught unexpected report ID.");
331 });
332 });
333 }
334
335 chrome.test.runTests([
336 testGetDevicesWithNoOptions,
337 testGetDevicesWithLegacyVidAndPid,
338 testGetDevicesWithNoFilters,
339 testGetDevicesWithVidPidFilter,
340 testGetDevicesWithUsageFilter,
341 testGetDevicesWithUnauthorizedDevice,
342 testConnectWithInvalidDeviceId,
343 testConnectAndDisconnect,
344 testDisconnectWithInvalidConnectionId,
345 testReceiveWithInvalidConnectionId,
346 testReceiveWithReportId,
347 testReceiveWithoutReportId,
348 testSendWithInvalidConnectionId,
349 testSendWithReportId,
350 testSendWithoutReportId,
351 testSendWithInvalidReportId,
352 testSendWithUnexpectedReportId,
353 testReceiveFeatureReportWithInvalidConnectionId,
354 testReceiveFeatureReportWithReportId,
355 testReceiveFeatureReportWithoutReportId,
356 testReceiveFeatureReportWithInvalidReportId,
357 testReceiveFeatureReportWithUnexpectedReportId,
358 testSendFeatureReportWithInvalidConnectionId,
359 testSendFeatureReportWithReportId,
360 testSendFeatureReportWithoutReportId,
361 testSendFeatureReportWithInvalidReportId,
362 testSendFeatureReportWithUnexpectedReportId,
363 ]);
OLDNEW
« no previous file with comments | « extensions/shell/app_shell.gyp ('k') | extensions/test/data/api_test/hid/api/manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698