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

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

Issue 2816663002: Ensure tests don't depend on fake devices being added synchronously (Closed)
Patch Set: Remove GUIDs from the test interface 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 side-by-side diff with in-line comments
Download patch
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..22e5a65d657b25440d6e1a14645d1cf05a06097f 100644
--- a/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js
+++ b/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js
@@ -13,6 +13,38 @@ 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);
+ });
+}
+
+// Creates a fake device and returns a promise that resolves once the
+// 'connect' event is fired for the fake device. The promise is resolved with
+// an object containing the fake USB device and the corresponding USBDevice.
+function getFakeDevice() {
+ let promise = connectionEventPromise('connect');
+ let fakeDevice = navigator.usb.test.addFakeDevice(fakeDeviceInit);
+ return promise.then(device => {
+ return { device: device, fakeDevice: fakeDevice };
+ });
+}
+
+// Disconnects the given device and returns a promise that is resolved when it
+// is done.
+function waitForDisconnect(fakeDevice) {
+ let promise = connectionEventPromise('disconnect');
+ fakeDevice.disconnect();
+ return promise;
+}
+
function assertRejectsWithError(promise, name, message) {
return promise.then(() => {
assert_unreached('expected promise to reject with ' + name);

Powered by Google App Engine
This is Rietveld 408576698