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

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

Issue 1951723002: Use the Mojo StubBindings interface correctly in WebUSB LayoutTests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 4 years, 7 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
« no previous file with comments | « mojo/public/js/connection.js ('k') | third_party/WebKit/Source/modules/webusb/USB.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 'use strict'; 1 'use strict';
2 2
3 function assertRejectsWithError(promise, name, message) { 3 function assertRejectsWithError(promise, name, message) {
4 return promise.then(() => { 4 return promise.then(() => {
5 assert_unreached('expected promise to reject with ' + name); 5 assert_unreached('expected promise to reject with ' + name);
6 }, error => { 6 }, error => {
7 assert_equals(error.name, name); 7 assert_equals(error.name, name);
8 if (message !== undefined) 8 if (message !== undefined)
9 assert_equals(error.message, message); 9 assert_equals(error.message, message);
10 }); 10 });
(...skipping 16 matching lines...) Expand all
27 function runGarbageCollection() { 27 function runGarbageCollection() {
28 // Run gc() as a promise. 28 // Run gc() as a promise.
29 return new Promise((resolve, reject) => { 29 return new Promise((resolve, reject) => {
30 GCController.collect(); 30 GCController.collect();
31 setTimeout(resolve, 0); 31 setTimeout(resolve, 0);
32 }); 32 });
33 } 33 }
34 34
35 function usbMocks(mojo) { 35 function usbMocks(mojo) {
36 return define('USB Mocks', [ 36 return define('USB Mocks', [
37 'mojo/public/js/bindings',
38 'mojo/public/js/connection',
37 'device/usb/public/interfaces/chooser_service.mojom', 39 'device/usb/public/interfaces/chooser_service.mojom',
38 'device/usb/public/interfaces/device_manager.mojom', 40 'device/usb/public/interfaces/device_manager.mojom',
39 'device/usb/public/interfaces/device.mojom', 41 'device/usb/public/interfaces/device.mojom',
40 ], (chooserService, deviceManager, device) => { 42 ], (bindings, connection, chooserService, deviceManager, device) => {
41 function assertDeviceInfoEquals(device, info) { 43 function assertDeviceInfoEquals(device, info) {
42 assert_equals(device.guid, info.guid); 44 assert_equals(device.guid, info.guid);
43 assert_equals(device.usbVersionMajor, info.usb_version_major); 45 assert_equals(device.usbVersionMajor, info.usb_version_major);
44 assert_equals(device.usbVersionMinor, info.usb_version_minor); 46 assert_equals(device.usbVersionMinor, info.usb_version_minor);
45 assert_equals(device.usbVersionSubminor, info.usb_version_subminor); 47 assert_equals(device.usbVersionSubminor, info.usb_version_subminor);
46 assert_equals(device.deviceClass, info.class_code); 48 assert_equals(device.deviceClass, info.class_code);
47 assert_equals(device.deviceSubclass, info.subclass_code); 49 assert_equals(device.deviceSubclass, info.subclass_code);
48 assert_equals(device.deviceProtocol, info.protocol_code); 50 assert_equals(device.deviceProtocol, info.protocol_code);
49 assert_equals(device.vendorId, info.vendor_id); 51 assert_equals(device.vendorId, info.vendor_id);
50 assert_equals(device.productId, info.product_id); 52 assert_equals(device.productId, info.product_id);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 type = 'isochronous'; 121 type = 'isochronous';
120 break; 122 break;
121 } 123 }
122 124
123 assert_equals(endpoint.endpointNumber, info.endpoint_number); 125 assert_equals(endpoint.endpointNumber, info.endpoint_number);
124 assert_equals(endpoint.direction, direction); 126 assert_equals(endpoint.direction, direction);
125 assert_equals(endpoint.type, type); 127 assert_equals(endpoint.type, type);
126 assert_equals(endpoint.packetSize, info.packet_size); 128 assert_equals(endpoint.packetSize, info.packet_size);
127 } 129 }
128 130
129 class MockDevice extends device.Device.stubClass { 131 class MockDevice {
130 constructor(info, pipe, closeHandler) { 132 constructor(info) {
131 super();
132 this.info_ = info; 133 this.info_ = info;
133 this.pipe_ = pipe;
134 this.router_ = new mojo.router.Router(pipe);
135 this.router_.setIncomingReceiver(this);
136 this.router_.setErrorHandler(() => {
137 if (this.opened_)
138 this.close();
139 });
140 this.opened_ = false; 134 this.opened_ = false;
141 this.currentConfiguration_ = undefined; 135 this.currentConfiguration_ = undefined;
142 this.claimedInterfaces_ = new Map(); 136 this.claimedInterfaces_ = new Map();
143 this.closeHandler_ = closeHandler;
144 } 137 }
145 138
146 getDeviceInfo() { 139 getDeviceInfo() {
147 return Promise.resolve({ info: this.info_ }); 140 return Promise.resolve({ info: this.info_ });
148 } 141 }
149 142
150 getConfiguration() { 143 getConfiguration() {
151 if (this.currentConfiguration_ === undefined) { 144 if (this.currentConfiguration_ === undefined) {
152 return Promise.resolve({ value: 0 }); 145 return Promise.resolve({ value: 0 });
153 } else { 146 } else {
154 return Promise.resolve({ 147 return Promise.resolve({
155 value: this.currentConfiguration_.configuration_value }); 148 value: this.currentConfiguration_.configuration_value });
156 } 149 }
157 } 150 }
158 151
159 open() { 152 open() {
160 assert_false(this.opened_); 153 assert_false(this.opened_);
161 this.opened_ = true; 154 this.opened_ = true;
162 return Promise.resolve({ error: device.OpenDeviceError.OK }); 155 return Promise.resolve({ error: device.OpenDeviceError.OK });
163 } 156 }
164 157
165 close() { 158 close() {
166 assert_true(this.opened_); 159 assert_true(this.opened_);
167 this.opened_ = false; 160 this.opened_ = false;
168 this.closeHandler_();
169 return Promise.resolve({ error: device.OpenDeviceError.OK }); 161 return Promise.resolve({ error: device.OpenDeviceError.OK });
170 } 162 }
171 163
172 setConfiguration(value) { 164 setConfiguration(value) {
173 assert_true(this.opened_); 165 assert_true(this.opened_);
174 166
175 let selected_configuration = this.info_.configurations.find( 167 let selected_configuration = this.info_.configurations.find(
176 configuration => configuration.configuration_value == value); 168 configuration => configuration.configuration_value == value);
177 if (selected_configuration !== undefined) { 169 if (selected_configuration !== undefined) {
178 this.currentConfiguration_ = selected_configuration; 170 this.currentConfiguration_ = selected_configuration;
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 packets[i] = { 315 packets[i] = {
324 length: packetLengths[i], 316 length: packetLengths[i],
325 transferred_length: packetLengths[i], 317 transferred_length: packetLengths[i],
326 status: device.TransferStatus.OK 318 status: device.TransferStatus.OK
327 }; 319 };
328 } 320 }
329 return Promise.resolve({ packets: packets }); 321 return Promise.resolve({ packets: packets });
330 } 322 }
331 }; 323 };
332 324
333 class MockDeviceManager extends deviceManager.DeviceManager.stubClass { 325 class MockDeviceManager {
334 constructor() { 326 constructor() {
335 super();
336 this.router_ = null;
337 this.mockDevices_ = new Map(); 327 this.mockDevices_ = new Map();
338 this.addedDevices_ = []; 328 this.addedDevices_ = [];
339 this.removedDevices_ = []; 329 this.removedDevices_ = [];
340 this.deviceChangePromiseResolvers_ = []; 330 this.deviceChangePromiseResolvers_ = [];
341 this.deviceCloseHandler_ = null; 331 this.deviceCloseHandler_ = null;
342 } 332 }
343 333
334 bindToPipe(pipe) {
335 this.stub_ = connection.bindHandleToStub(
336 pipe, deviceManager.DeviceManager);
337 bindings.StubBindings(this.stub_).delegate = this;
338 }
339
344 reset() { 340 reset() {
345 this.mockDevices_.forEach(device => { 341 this.mockDevices_.forEach(device => {
346 for (var handle of device.handles) 342 for (var stub of device.stubs)
347 mojo.core.close(handle.pipe_); 343 bindings.StubBindings(stub).close();
348 this.removedDevices_.push(device.info); 344 this.removedDevices_.push(device.info);
349 }); 345 });
350 this.mockDevices_.clear(); 346 this.mockDevices_.clear();
351 this.maybeResolveDeviceChangePromise(); 347 this.maybeResolveDeviceChangePromise();
352 } 348 }
353 349
354 addMockDevice(info) { 350 addMockDevice(info) {
355 let device = { 351 let device = {
356 info: info, 352 info: info,
357 handles: [] 353 stubs: []
358 }; 354 };
359 this.mockDevices_.set(info.guid, device); 355 this.mockDevices_.set(info.guid, device);
360 this.addedDevices_.push(info); 356 this.addedDevices_.push(info);
361 this.maybeResolveDeviceChangePromise(); 357 this.maybeResolveDeviceChangePromise();
362 } 358 }
363 359
364 removeMockDevice(info) { 360 removeMockDevice(info) {
365 let device = this.mockDevices_.get(info.guid); 361 let device = this.mockDevices_.get(info.guid);
366 for (var handle of device.handles) 362 for (var stub of device.stubs)
367 mojo.core.close(handle.pipe_); 363 bindings.StubBindings(stub).close();
368 this.mockDevices_.delete(info.guid); 364 this.mockDevices_.delete(info.guid);
369 this.removedDevices_.push(info); 365 this.removedDevices_.push(info);
370 this.maybeResolveDeviceChangePromise(); 366 this.maybeResolveDeviceChangePromise();
371 } 367 }
372 368
373 setDeviceCloseHandler(handler) { 369 setDeviceCloseHandler(handler) {
374 this.deviceCloseHandler_ = handler; 370 this.deviceCloseHandler_ = handler;
375 } 371 }
376 372
377 bindToPipe(pipe) {
378 assert_equals(this.router_, null);
379 this.router_ = new mojo.router.Router(pipe);
380 this.router_.setIncomingReceiver(this);
381 }
382
383 getDevices(options) { 373 getDevices(options) {
384 let devices = []; 374 let devices = [];
385 this.mockDevices_.forEach(device => { 375 this.mockDevices_.forEach(device => {
386 devices.push(device.info); 376 devices.push(device.info);
387 }); 377 });
388 return Promise.resolve({ results: devices }); 378 return Promise.resolve({ results: devices });
389 } 379 }
390 380
391 getDeviceChanges() { 381 getDeviceChanges() {
392 let promise = new Promise((resolve, reject) => { 382 let promise = new Promise((resolve, reject) => {
(...skipping 16 matching lines...) Expand all
409 resolve({ 399 resolve({
410 changes: { 400 changes: {
411 devices_added: this.addedDevices_, 401 devices_added: this.addedDevices_,
412 devices_removed: this.removedDevices_ 402 devices_removed: this.removedDevices_
413 } 403 }
414 }); 404 });
415 this.addedDevices_ = []; 405 this.addedDevices_ = [];
416 this.removedDevices_ = []; 406 this.removedDevices_ = [];
417 } 407 }
418 408
419 getDevice(guid, pipe) { 409 getDevice(guid, stub) {
420 let device = this.mockDevices_.get(guid); 410 let device = this.mockDevices_.get(guid);
421 if (device === undefined) { 411 if (device === undefined) {
422 mojo.core.close(pipe); 412 bindings.StubBindings(stub).close();
423 } else { 413 } else {
424 var mock = new MockDevice(device.info, pipe, () => { 414 var mock = new MockDevice(device.info);
415 bindings.StubBindings(stub).delegate = mock;
416 bindings.StubBindings(stub).connectionErrorHandler = () => {
425 if (this.deviceCloseHandler_) 417 if (this.deviceCloseHandler_)
426 this.deviceCloseHandler_(device.info); 418 this.deviceCloseHandler_(device.info);
427 }); 419 };
428 device.handles.push(mock); 420 device.stubs.push(stub);
429 } 421 }
430 } 422 }
431 } 423 }
432 424
433 class MockChooserService 425 class MockChooserService {
434 extends chooserService.ChooserService.stubClass {
435 constructor() { 426 constructor() {
436 super();
437 this.router_ = null;
438 this.chosenDevice_ = null; 427 this.chosenDevice_ = null;
439 } 428 }
440 429
441 bindToPipe(pipe) { 430 bindToPipe(pipe) {
442 assert_equals(this.router_, null); 431 this.stub_ = connection.bindHandleToStub(
443 this.router_ = new mojo.router.Router(pipe); 432 pipe, chooserService.ChooserService);
444 this.router_.setIncomingReceiver(this); 433 bindings.StubBindings(this.stub_).delegate = this;
445 } 434 }
446 435
447 setChosenDevice(deviceInfo) { 436 setChosenDevice(deviceInfo) {
448 this.chosenDeviceInfo_ = deviceInfo; 437 this.chosenDeviceInfo_ = deviceInfo;
449 } 438 }
450 439
451 getPermission(deviceFilters) { 440 getPermission(deviceFilters) {
452 return Promise.resolve({ result: this.chosenDeviceInfo_ }); 441 return Promise.resolve({ result: this.chosenDeviceInfo_ });
453 } 442 }
454 } 443 }
(...skipping 26 matching lines...) Expand all
481 } 470 }
482 471
483 function usb_test(func, name, properties) { 472 function usb_test(func, name, properties) {
484 mojo_test(mojo => usbMocks(mojo).then(usb => { 473 mojo_test(mojo => usbMocks(mojo).then(usb => {
485 let result = Promise.resolve(func(usb)); 474 let result = Promise.resolve(func(usb));
486 let cleanUp = () => usb.mockDeviceManager.reset(); 475 let cleanUp = () => usb.mockDeviceManager.reset();
487 result.then(cleanUp, cleanUp); 476 result.then(cleanUp, cleanUp);
488 return result; 477 return result;
489 }), name, properties); 478 }), name, properties);
490 } 479 }
OLDNEW
« no previous file with comments | « mojo/public/js/connection.js ('k') | third_party/WebKit/Source/modules/webusb/USB.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698