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

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

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

Powered by Google App Engine
This is Rietveld 408576698