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

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

Issue 1946063002: Replace DeviceManager::GetDeviceChanges with a client interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proper_stubs
Patch Set: Rebase.d 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
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 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 status: device.TransferStatus.OK 318 status: device.TransferStatus.OK
319 }; 319 };
320 } 320 }
321 return Promise.resolve({ packets: packets }); 321 return Promise.resolve({ packets: packets });
322 } 322 }
323 }; 323 };
324 324
325 class MockDeviceManager { 325 class MockDeviceManager {
326 constructor() { 326 constructor() {
327 this.mockDevices_ = new Map(); 327 this.mockDevices_ = new Map();
328 this.addedDevices_ = [];
329 this.removedDevices_ = [];
330 this.deviceChangePromiseResolvers_ = [];
331 this.deviceCloseHandler_ = null; 328 this.deviceCloseHandler_ = null;
329 this.client_ = null;
332 } 330 }
333 331
334 bindToPipe(pipe) { 332 bindToPipe(pipe) {
335 this.stub_ = connection.bindHandleToStub( 333 this.stub_ = connection.bindHandleToStub(
336 pipe, deviceManager.DeviceManager); 334 pipe, deviceManager.DeviceManager);
337 bindings.StubBindings(this.stub_).delegate = this; 335 bindings.StubBindings(this.stub_).delegate = this;
338 } 336 }
339 337
340 reset() { 338 reset() {
341 this.mockDevices_.forEach(device => { 339 this.mockDevices_.forEach(device => {
342 for (var stub of device.stubs) 340 for (var stub of device.stubs)
343 bindings.StubBindings(stub).close(); 341 bindings.StubBindings(stub).close();
344 this.removedDevices_.push(device.info);
345 }); 342 });
346 this.mockDevices_.clear(); 343 this.mockDevices_.clear();
347 this.maybeResolveDeviceChangePromise();
348 } 344 }
349 345
350 addMockDevice(info) { 346 addMockDevice(info) {
351 let device = { 347 let device = {
352 info: info, 348 info: info,
353 stubs: [] 349 stubs: []
354 }; 350 };
355 this.mockDevices_.set(info.guid, device); 351 this.mockDevices_.set(info.guid, device);
356 this.addedDevices_.push(info); 352 if (this.client_)
357 this.maybeResolveDeviceChangePromise(); 353 this.client_.onDeviceAdded(info);
358 } 354 }
359 355
360 removeMockDevice(info) { 356 removeMockDevice(info) {
361 let device = this.mockDevices_.get(info.guid); 357 let device = this.mockDevices_.get(info.guid);
362 for (var stub of device.stubs) 358 for (var stub of device.stubs)
363 bindings.StubBindings(stub).close(); 359 bindings.StubBindings(stub).close();
364 this.mockDevices_.delete(info.guid); 360 this.mockDevices_.delete(info.guid);
365 this.removedDevices_.push(info); 361 if (this.client_)
366 this.maybeResolveDeviceChangePromise(); 362 this.client_.onDeviceRemoved(info);
367 } 363 }
368 364
369 setDeviceCloseHandler(handler) { 365 setDeviceCloseHandler(handler) {
370 this.deviceCloseHandler_ = handler; 366 this.deviceCloseHandler_ = handler;
371 } 367 }
372 368
373 getDevices(options) { 369 getDevices(options) {
374 let devices = []; 370 let devices = [];
375 this.mockDevices_.forEach(device => { 371 this.mockDevices_.forEach(device => {
376 devices.push(device.info); 372 devices.push(device.info);
377 }); 373 });
378 return Promise.resolve({ results: devices }); 374 return Promise.resolve({ results: devices });
379 } 375 }
380 376
381 getDeviceChanges() {
382 let promise = new Promise((resolve, reject) => {
383 this.deviceChangePromiseResolvers_.push(resolve);
384 });
385 this.maybeResolveDeviceChangePromise();
386 return promise;
387 }
388
389 maybeResolveDeviceChangePromise() {
390 if (this.addedDevices_.length == 0 &&
391 this.removedDevices_.length == 0) {
392 return;
393 }
394
395 let resolve = this.deviceChangePromiseResolvers_.shift();
396 if (resolve === undefined)
397 return;
398
399 resolve({
400 changes: {
401 devices_added: this.addedDevices_,
402 devices_removed: this.removedDevices_
403 }
404 });
405 this.addedDevices_ = [];
406 this.removedDevices_ = [];
407 }
408
409 getDevice(guid, stub) { 377 getDevice(guid, stub) {
410 let device = this.mockDevices_.get(guid); 378 let device = this.mockDevices_.get(guid);
411 if (device === undefined) { 379 if (device === undefined) {
412 bindings.StubBindings(stub).close(); 380 bindings.StubBindings(stub).close();
413 } else { 381 } else {
414 var mock = new MockDevice(device.info); 382 var mock = new MockDevice(device.info);
415 bindings.StubBindings(stub).delegate = mock; 383 bindings.StubBindings(stub).delegate = mock;
416 bindings.StubBindings(stub).connectionErrorHandler = () => { 384 bindings.StubBindings(stub).connectionErrorHandler = () => {
417 if (this.deviceCloseHandler_) 385 if (this.deviceCloseHandler_)
418 this.deviceCloseHandler_(device.info); 386 this.deviceCloseHandler_(device.info);
419 }; 387 };
420 device.stubs.push(stub); 388 device.stubs.push(stub);
421 } 389 }
422 } 390 }
391
392 setClient(client) {
393 this.client_ = client;
394 }
423 } 395 }
424 396
425 class MockChooserService { 397 class MockChooserService {
426 constructor() { 398 constructor() {
427 this.chosenDevice_ = null; 399 this.chosenDevice_ = null;
428 } 400 }
429 401
430 bindToPipe(pipe) { 402 bindToPipe(pipe) {
431 this.stub_ = connection.bindHandleToStub( 403 this.stub_ = connection.bindHandleToStub(
432 pipe, chooserService.ChooserService); 404 pipe, chooserService.ChooserService);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 } 442 }
471 443
472 function usb_test(func, name, properties) { 444 function usb_test(func, name, properties) {
473 mojo_test(mojo => usbMocks(mojo).then(usb => { 445 mojo_test(mojo => usbMocks(mojo).then(usb => {
474 let result = Promise.resolve(func(usb)); 446 let result = Promise.resolve(func(usb));
475 let cleanUp = () => usb.mockDeviceManager.reset(); 447 let cleanUp = () => usb.mockDeviceManager.reset();
476 result.then(cleanUp, cleanUp); 448 result.then(cleanUp, cleanUp);
477 return result; 449 return result;
478 }), name, properties); 450 }), name, properties);
479 } 451 }
OLDNEW
« no previous file with comments | « device/usb/public/interfaces/device_manager.mojom ('k') | third_party/WebKit/LayoutTests/usb/usbDevice-iframe.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698