Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js |
| diff --git a/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js b/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js |
| index 0ec82916bf90d4e1709cd26e63e628d8271c8895..5177414b2f6bc75e1553f9d185000cf1fd287556 100644 |
| --- a/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js |
| +++ b/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js |
| @@ -13,6 +13,44 @@ function usb_test(func, name, properties) { |
| }, name, properties); |
| } |
| +// Returns a promise that is resolved when the next USBConnectionEvent of the |
| +// given type is received. |
| +function connectionEventPromise(eventType) { |
| + return new Promise(resolve => { |
| + let eventHandler = e => { |
| + assert_true(e instanceof USBConnectionEvent); |
| + navigator.usb.removeEventListener(eventType, eventHandler); |
| + resolve(e.device); |
| + }; |
| + navigator.usb.addEventListener(eventType, eventHandler); |
| + }); |
| +} |
| + |
| +// Returns a promise that is resolved with an object containing a fake |
| +// USBDevice and its guid (for calling navigator.usb.test.removeFakeDevice(). |
|
ortuno
2017/04/18 00:44:22
optional:
// Creates a fake device and returns a
Reilly Grant (use Gerrit)
2017/04/18 01:27:52
Done.
|
| +function getFakeDeviceAndGuid() { |
| + let promise = connectionEventPromise('connect'); |
| + let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| + return promise.then(device => { |
| + return { device: device, guid: guid }; |
|
ortuno
2017/04/18 00:44:22
optional: Would it make sense/be worth it to retur
Reilly Grant (use Gerrit)
2017/04/18 01:27:52
Done.
|
| + }); |
| +} |
| + |
| +// Returns a promise that is resolved with just a fake USBDevice. |
| +function getFakeDevice() { |
| + return getFakeDeviceAndGuid().then(({ device, guid }) => { |
| + return device; |
| + }); |
| +} |
| + |
| +// Disconnects the given device and returns a promise that is resolved when it |
| +// is done. |
| +function waitForDisconnect(guid) { |
| + let promise = connectionEventPromise('disconnect'); |
| + navigator.usb.test.removeFakeDevice(guid); |
| + return promise; |
| +} |
| + |
| function assertRejectsWithError(promise, name, message) { |
| return promise.then(() => { |
| assert_unreached('expected promise to reject with ' + name); |