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

Side by Side Diff: third_party/WebKit/LayoutTests/usb/resources/webusb-test.js

Issue 2821723002: Move classes in the device.usb Mojo namespace to device.mojom (Closed)
Patch Set: mcasas@ nits Created 3 years, 8 months 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
OLDNEW
1 'use strict'; 1 'use strict';
2 2
3 // This polyfil library implements the following WebIDL: 3 // This polyfil library implements the following WebIDL:
4 // 4 //
5 // partial interface USB { 5 // partial interface USB {
6 // [SameObject] readonly attribute USBTest test; 6 // [SameObject] readonly attribute USBTest test;
7 // } 7 // }
8 // 8 //
9 // interface USBTest { 9 // interface USBTest {
10 // attribute EventHandler ondeviceclose; 10 // attribute EventHandler ondeviceclose;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 interface_name: alternate.interfaceName, 117 interface_name: alternate.interfaceName,
118 endpoints: [] 118 endpoints: []
119 }; 119 };
120 alternate.endpoints.forEach(endpoint => { 120 alternate.endpoints.forEach(endpoint => {
121 var endpointInfo = { 121 var endpointInfo = {
122 endpoint_number: endpoint.endpointNumber, 122 endpoint_number: endpoint.endpointNumber,
123 packet_size: endpoint.packetSize, 123 packet_size: endpoint.packetSize,
124 }; 124 };
125 switch (endpoint.direction) { 125 switch (endpoint.direction) {
126 case "in": 126 case "in":
127 endpointInfo.direction = mojo.device.TransferDirection.INBOUND; 127 endpointInfo.direction = mojo.device.UsbTransferDirection.INBOUND;
128 break; 128 break;
129 case "out": 129 case "out":
130 endpointInfo.direction = mojo.device.TransferDirection.OUTBOUND; 130 endpointInfo.direction = mojo.device.UsbTransferDirection.OUTBOUND;
131 break; 131 break;
132 } 132 }
133 switch (endpoint.type) { 133 switch (endpoint.type) {
134 case "bulk": 134 case "bulk":
135 endpointInfo.type = mojo.device.EndpointType.BULK; 135 endpointInfo.type = mojo.device.UsbEndpointType.BULK;
136 break; 136 break;
137 case "interrupt": 137 case "interrupt":
138 endpointInfo.type = mojo.device.EndpointType.INTERRUPT; 138 endpointInfo.type = mojo.device.UsbEndpointType.INTERRUPT;
139 break; 139 break;
140 case "isochronous": 140 case "isochronous":
141 endpointInfo.type = mojo.device.EndpointType.ISOCHRONOUS; 141 endpointInfo.type = mojo.device.UsbEndpointType.ISOCHRONOUS;
142 break; 142 break;
143 } 143 }
144 alternateInfo.endpoints.push(endpointInfo); 144 alternateInfo.endpoints.push(endpointInfo);
145 }); 145 });
146 interfaceInfo.alternates.push(alternateInfo); 146 interfaceInfo.alternates.push(alternateInfo);
147 }); 147 });
148 configInfo.interfaces.push(interfaceInfo); 148 configInfo.interfaces.push(interfaceInfo);
149 }); 149 });
150 deviceInfo.configurations.push(configInfo); 150 deviceInfo.configurations.push(configInfo);
151 }); 151 });
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 return Promise.resolve({ 190 return Promise.resolve({
191 value: this.currentConfiguration_.configuration_value }); 191 value: this.currentConfiguration_.configuration_value });
192 } else { 192 } else {
193 return Promise.resolve({ value: 0 }); 193 return Promise.resolve({ value: 0 });
194 } 194 }
195 } 195 }
196 196
197 open() { 197 open() {
198 assert_false(this.opened_); 198 assert_false(this.opened_);
199 this.opened_ = true; 199 this.opened_ = true;
200 return Promise.resolve({ error: mojo.device.OpenDeviceError.OK }); 200 return Promise.resolve({ error: mojo.device.UsbOpenDeviceError.OK });
201 } 201 }
202 202
203 close() { 203 close() {
204 assert_true(this.opened_); 204 assert_true(this.opened_);
205 this.opened_ = false; 205 this.opened_ = false;
206 return Promise.resolve(); 206 return Promise.resolve();
207 } 207 }
208 208
209 setConfiguration(value) { 209 setConfiguration(value) {
210 assert_true(this.opened_); 210 assert_true(this.opened_);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 assert_true(this.opened_); 262 assert_true(this.opened_);
263 assert_false(this.currentConfiguration_ == null, 'device configured'); 263 assert_false(this.currentConfiguration_ == null, 'device configured');
264 // TODO(reillyg): Assert that endpoint is valid. 264 // TODO(reillyg): Assert that endpoint is valid.
265 return Promise.resolve({ success: true }); 265 return Promise.resolve({ success: true });
266 } 266 }
267 267
268 controlTransferIn(params, length, timeout) { 268 controlTransferIn(params, length, timeout) {
269 assert_true(this.opened_); 269 assert_true(this.opened_);
270 assert_false(this.currentConfiguration_ == null, 'device configured'); 270 assert_false(this.currentConfiguration_ == null, 'device configured');
271 return Promise.resolve({ 271 return Promise.resolve({
272 status: mojo.device.TransferStatus.OK, 272 status: mojo.device.UsbTransferStatus.OK,
273 data: [length >> 8, length & 0xff, params.request, params.value >> 8, 273 data: [length >> 8, length & 0xff, params.request, params.value >> 8,
274 params.value & 0xff, params.index >> 8, params.index & 0xff] 274 params.value & 0xff, params.index >> 8, params.index & 0xff]
275 }); 275 });
276 } 276 }
277 277
278 controlTransferOut(params, data, timeout) { 278 controlTransferOut(params, data, timeout) {
279 assert_true(this.opened_); 279 assert_true(this.opened_);
280 assert_false(this.currentConfiguration_ == null, 'device configured'); 280 assert_false(this.currentConfiguration_ == null, 'device configured');
281 return Promise.resolve({ 281 return Promise.resolve({
282 status: mojo.device.TransferStatus.OK, 282 status: mojo.device.UsbTransferStatus.OK,
283 bytesWritten: data.byteLength 283 bytesWritten: data.byteLength
284 }); 284 });
285 } 285 }
286 286
287 genericTransferIn(endpointNumber, length, timeout) { 287 genericTransferIn(endpointNumber, length, timeout) {
288 assert_true(this.opened_); 288 assert_true(this.opened_);
289 assert_false(this.currentConfiguration_ == null, 'device configured'); 289 assert_false(this.currentConfiguration_ == null, 'device configured');
290 // TODO(reillyg): Assert that endpoint is valid. 290 // TODO(reillyg): Assert that endpoint is valid.
291 let data = new Array(length); 291 let data = new Array(length);
292 for (let i = 0; i < length; ++i) 292 for (let i = 0; i < length; ++i)
293 data[i] = i & 0xff; 293 data[i] = i & 0xff;
294 return Promise.resolve({ 294 return Promise.resolve({
295 status: mojo.device.TransferStatus.OK, 295 status: mojo.device.UsbTransferStatus.OK,
296 data: data 296 data: data
297 }); 297 });
298 } 298 }
299 299
300 genericTransferOut(endpointNumber, data, timeout) { 300 genericTransferOut(endpointNumber, data, timeout) {
301 assert_true(this.opened_); 301 assert_true(this.opened_);
302 assert_false(this.currentConfiguration_ == null, 'device configured'); 302 assert_false(this.currentConfiguration_ == null, 'device configured');
303 // TODO(reillyg): Assert that endpoint is valid. 303 // TODO(reillyg): Assert that endpoint is valid.
304 return Promise.resolve({ 304 return Promise.resolve({
305 status: mojo.device.TransferStatus.OK, 305 status: mojo.device.UsbTransferStatus.OK,
306 bytesWritten: data.byteLength 306 bytesWritten: data.byteLength
307 }); 307 });
308 } 308 }
309 309
310 isochronousTransferIn(endpointNumber, packetLengths, timeout) { 310 isochronousTransferIn(endpointNumber, packetLengths, timeout) {
311 assert_true(this.opened_); 311 assert_true(this.opened_);
312 assert_false(this.currentConfiguration_ == null, 'device configured'); 312 assert_false(this.currentConfiguration_ == null, 'device configured');
313 // TODO(reillyg): Assert that endpoint is valid. 313 // TODO(reillyg): Assert that endpoint is valid.
314 let data = new Array(packetLengths.reduce((a, b) => a + b, 0)); 314 let data = new Array(packetLengths.reduce((a, b) => a + b, 0));
315 let dataOffset = 0; 315 let dataOffset = 0;
316 let packets = new Array(packetLengths.length); 316 let packets = new Array(packetLengths.length);
317 for (let i = 0; i < packetLengths.length; ++i) { 317 for (let i = 0; i < packetLengths.length; ++i) {
318 for (let j = 0; j < packetLengths[i]; ++j) 318 for (let j = 0; j < packetLengths[i]; ++j)
319 data[dataOffset++] = j & 0xff; 319 data[dataOffset++] = j & 0xff;
320 packets[i] = { 320 packets[i] = {
321 length: packetLengths[i], 321 length: packetLengths[i],
322 transferred_length: packetLengths[i], 322 transferred_length: packetLengths[i],
323 status: mojo.device.TransferStatus.OK 323 status: mojo.device.UsbTransferStatus.OK
324 }; 324 };
325 } 325 }
326 return Promise.resolve({ data: data, packets: packets }); 326 return Promise.resolve({ data: data, packets: packets });
327 } 327 }
328 328
329 isochronousTransferOut(endpointNumber, data, packetLengths, timeout) { 329 isochronousTransferOut(endpointNumber, data, packetLengths, timeout) {
330 assert_true(this.opened_); 330 assert_true(this.opened_);
331 assert_false(this.currentConfiguration_ == null, 'device configured'); 331 assert_false(this.currentConfiguration_ == null, 'device configured');
332 // TODO(reillyg): Assert that endpoint is valid. 332 // TODO(reillyg): Assert that endpoint is valid.
333 let packets = new Array(packetLengths.length); 333 let packets = new Array(packetLengths.length);
334 for (let i = 0; i < packetLengths.length; ++i) { 334 for (let i = 0; i < packetLengths.length; ++i) {
335 packets[i] = { 335 packets[i] = {
336 length: packetLengths[i], 336 length: packetLengths[i],
337 transferred_length: packetLengths[i], 337 transferred_length: packetLengths[i],
338 status: mojo.device.TransferStatus.OK 338 status: mojo.device.UsbTransferStatus.OK
339 }; 339 };
340 } 340 }
341 return Promise.resolve({ packets: packets }); 341 return Promise.resolve({ packets: packets });
342 } 342 }
343 } 343 }
344 344
345 class FakeDeviceManager { 345 class FakeDeviceManager {
346 constructor() { 346 constructor() {
347 this.bindingSet_ = 347 this.bindingSet_ =
348 new mojo.bindings.BindingSet(mojo.deviceManager.DeviceManager); 348 new mojo.bindings.BindingSet(mojo.deviceManager.UsbDeviceManager);
349 this.devices_ = new Map(); 349 this.devices_ = new Map();
350 this.client_ = null; 350 this.client_ = null;
351 } 351 }
352 352
353 addBinding(handle) { 353 addBinding(handle) {
354 this.bindingSet_.addBinding(this, handle); 354 this.bindingSet_.addBinding(this, handle);
355 } 355 }
356 356
357 addDevice(info) { 357 addDevice(info) {
358 let device = { 358 let device = {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 this.devices_.forEach(device => { 392 this.devices_.forEach(device => {
393 devices.push(fakeDeviceInitToDeviceInfo(device.guid, device.info)); 393 devices.push(fakeDeviceInitToDeviceInfo(device.guid, device.info));
394 }); 394 });
395 return Promise.resolve({ results: devices }); 395 return Promise.resolve({ results: devices });
396 } 396 }
397 397
398 getDevice(guid, request) { 398 getDevice(guid, request) {
399 let device = this.devices_.get(guid); 399 let device = this.devices_.get(guid);
400 if (device) { 400 if (device) {
401 let binding = new mojo.bindings.Binding( 401 let binding = new mojo.bindings.Binding(
402 mojo.device.Device, new FakeDevice(device.info), request); 402 mojo.device.UsbDevice, new FakeDevice(device.info), request);
403 binding.setConnectionErrorHandler(() => { 403 binding.setConnectionErrorHandler(() => {
404 if (g_closeListener) 404 if (g_closeListener)
405 g_closeListener(guid); 405 g_closeListener(guid);
406 }); 406 });
407 device.bindingArray.push(binding); 407 device.bindingArray.push(binding);
408 } else { 408 } else {
409 request.close(); 409 request.close();
410 } 410 }
411 } 411 }
412 412
413 setClient(client) { 413 setClient(client) {
414 this.client_ = client; 414 this.client_ = client;
415 } 415 }
416 } 416 }
417 417
418 class FakeChooserService { 418 class FakeChooserService {
419 constructor() { 419 constructor() {
420 this.bindingSet_ = new mojo.bindings.BindingSet( 420 this.bindingSet_ = new mojo.bindings.BindingSet(
421 mojo.chooserService.ChooserService); 421 mojo.chooserService.UsbChooserService);
422 this.chosenDevice_ = null; 422 this.chosenDevice_ = null;
423 this.lastFilters_ = null; 423 this.lastFilters_ = null;
424 } 424 }
425 425
426 addBinding(handle) { 426 addBinding(handle) {
427 this.bindingSet_.addBinding(this, handle); 427 this.bindingSet_.addBinding(this, handle);
428 } 428 }
429 429
430 setChosenDevice(guid) { 430 setChosenDevice(guid) {
431 this.chosenDeviceGuid_ = guid; 431 this.chosenDeviceGuid_ = guid;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 deviceManager: deviceManager, 471 deviceManager: deviceManager,
472 device: device, 472 device: device,
473 bindings: bindings, 473 bindings: bindings,
474 core: core, 474 core: core,
475 router: router, 475 router: router,
476 support: support 476 support: support
477 }; 477 };
478 478
479 g_deviceManager = new FakeDeviceManager(); 479 g_deviceManager = new FakeDeviceManager();
480 mojo.frameInterfaces.addInterfaceOverrideForTesting( 480 mojo.frameInterfaces.addInterfaceOverrideForTesting(
481 mojo.deviceManager.DeviceManager.name, 481 mojo.deviceManager.UsbDeviceManager.name,
482 handle => g_deviceManager.addBinding(handle)); 482 handle => g_deviceManager.addBinding(handle));
483 483
484 g_chooserService = new FakeChooserService(); 484 g_chooserService = new FakeChooserService();
485 mojo.frameInterfaces.addInterfaceOverrideForTesting( 485 mojo.frameInterfaces.addInterfaceOverrideForTesting(
486 mojo.chooserService.ChooserService.name, 486 mojo.chooserService.UsbChooserService.name,
487 handle => g_chooserService.addBinding(handle)); 487 handle => g_chooserService.addBinding(handle));
488 488
489 addEventListener('unload', () => { 489 addEventListener('unload', () => {
490 mojo.frameInterfaces.clearInterfaceOverridesForTesting(); 490 mojo.frameInterfaces.clearInterfaceOverridesForTesting();
491 }); 491 });
492 492
493 resolve(); 493 resolve();
494 }); 494 });
495 }); 495 });
496 } 496 }
497 497
498 return g_initializePromise; 498 return g_initializePromise;
499 } 499 }
500 500
501 attachToWindow(otherWindow) { 501 attachToWindow(otherWindow) {
502 if (!g_deviceManager || !g_chooserService) 502 if (!g_deviceManager || !g_chooserService)
503 throw new Error('Call initialize() before attachToWindow().'); 503 throw new Error('Call initialize() before attachToWindow().');
504 504
505 return new Promise(resolve => { 505 return new Promise(resolve => {
506 otherWindow.gin.define( 506 otherWindow.gin.define(
507 'WebUSB Test Frame Attach', [ 507 'WebUSB Test Frame Attach', [
508 'content/public/renderer/frame_interfaces' 508 'content/public/renderer/frame_interfaces'
509 ], frameInterfaces => { 509 ], frameInterfaces => {
510 frameInterfaces.addInterfaceOverrideForTesting( 510 frameInterfaces.addInterfaceOverrideForTesting(
511 mojo.deviceManager.DeviceManager.name, 511 mojo.deviceManager.UsbDeviceManager.name,
512 handle => g_deviceManager.addBinding(handle)); 512 handle => g_deviceManager.addBinding(handle));
513 frameInterfaces.addInterfaceOverrideForTesting( 513 frameInterfaces.addInterfaceOverrideForTesting(
514 mojo.chooserService.ChooserService.name, 514 mojo.chooserService.UsbChooserService.name,
515 handle => g_chooserService.addBinding(handle)); 515 handle => g_chooserService.addBinding(handle));
516 resolve(); 516 resolve();
517 }); 517 });
518 }); 518 });
519 } 519 }
520 520
521 addFakeDevice(deviceInit) { 521 addFakeDevice(deviceInit) {
522 if (!g_deviceManager) 522 if (!g_deviceManager)
523 throw new Error('Call initialize() before addFakeDevice().'); 523 throw new Error('Call initialize() before addFakeDevice().');
524 524
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 556
557 g_deviceManager.removeAllDevices(); 557 g_deviceManager.removeAllDevices();
558 g_chooserService.setChosenDevice(null); 558 g_chooserService.setChosenDevice(null);
559 g_closeListener = null; 559 g_closeListener = null;
560 } 560 }
561 } 561 }
562 562
563 navigator.usb.test = new USBTest(); 563 navigator.usb.test = new USBTest();
564 564
565 })(); 565 })();
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/usb/resources/fake-devices.js ('k') | third_party/WebKit/Source/modules/webusb/USB.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698