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

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

Issue 2789723003: Migrate WebUSB LayoutTests into external/wpt (Closed)
Patch Set: Add README.md and more comments explaining the polyfill Created 3 years, 6 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
(Empty)
1 'use strict';
2
3 function usb_test(func, name, properties) {
4 promise_test(async () => {
5 await navigator.usb.test.initialize()
6 try {
7 await func();
8 } finally {
9 await navigator.usb.test.reset();
10 }
11 }, name, properties);
12 }
13
14 // Returns a promise that is resolved when the next USBConnectionEvent of the
15 // given type is received.
16 function connectionEventPromise(eventType) {
17 return new Promise(resolve => {
18 let eventHandler = e => {
19 assert_true(e instanceof USBConnectionEvent);
20 navigator.usb.removeEventListener(eventType, eventHandler);
21 resolve(e.device);
22 };
23 navigator.usb.addEventListener(eventType, eventHandler);
24 });
25 }
26
27 // Creates a fake device and returns a promise that resolves once the
28 // 'connect' event is fired for the fake device. The promise is resolved with
29 // an object containing the fake USB device and the corresponding USBDevice.
30 function getFakeDevice() {
31 let promise = connectionEventPromise('connect');
32 let fakeDevice = navigator.usb.test.addFakeDevice(fakeDeviceInit);
33 return promise.then(device => {
34 return { device: device, fakeDevice: fakeDevice };
35 });
36 }
37
38 // Disconnects the given device and returns a promise that is resolved when it
39 // is done.
40 function waitForDisconnect(fakeDevice) {
41 let promise = connectionEventPromise('disconnect');
42 fakeDevice.disconnect();
43 return promise;
44 }
45
46 function assertRejectsWithError(promise, name, message) {
47 return promise.then(() => {
48 assert_unreached('expected promise to reject with ' + name);
49 }, error => {
50 assert_equals(error.name, name);
51 if (message !== undefined)
52 assert_equals(error.message, message);
53 });
54 }
55
56 function assertDeviceInfoEquals(usbDevice, deviceInit) {
57 for (var property in deviceInit) {
58 if (property == 'activeConfigurationValue') {
59 if (deviceInit.activeConfigurationValue == 0) {
60 assert_equals(usbDevice.configuration, null);
61 } else {
62 assert_equals(usbDevice.configuration.configurationValue,
63 deviceInit.activeConfigurationValue);
64 }
65 } else if (Array.isArray(deviceInit[property])) {
66 assert_equals(usbDevice[property].length, deviceInit[property].length);
67 for (var i = 0; i < usbDevice[property].length; ++i)
68 assertDeviceInfoEquals(usbDevice[property][i], deviceInit[property][i]);
69 } else {
70 assert_equals(usbDevice[property], deviceInit[property], property);
71 }
72 }
73 }
74
75 // TODO(reillyg): Remove when jyasskin upstreams this to testharness.js:
76 // https://crbug.com/509058.
77 function callWithKeyDown(functionCalledOnKeyPress) {
78 return new Promise(resolve => {
79 function onKeyPress() {
80 document.removeEventListener('keypress', onKeyPress, false);
81 resolve(functionCalledOnKeyPress());
82 }
83 document.addEventListener('keypress', onKeyPress, false);
84
85 eventSender.keyDown(' ', []);
86 });
87 }
88
89 function runGarbageCollection() {
90 // Run gc() as a promise.
91 return new Promise((resolve, reject) => {
92 GCController.collect();
93 setTimeout(resolve, 0);
94 });
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698