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

Side by Side 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 unified diff | Download patch
OLDNEW
1 'use strict'; 1 'use strict';
2 2
3 function usb_test(func, name, properties) { 3 function usb_test(func, name, properties) {
4 promise_test(() => { 4 promise_test(() => {
5 return navigator.usb.test.initialize().then(() => { 5 return navigator.usb.test.initialize().then(() => {
6 let testResult = func(); 6 let testResult = func();
7 let cleanup = () => { 7 let cleanup = () => {
8 navigator.usb.test.reset(); 8 navigator.usb.test.reset();
9 }; 9 };
10 testResult.then(cleanup, cleanup); 10 testResult.then(cleanup, cleanup);
11 return testResult; 11 return testResult;
12 }); 12 });
13 }, name, properties); 13 }, name, properties);
14 } 14 }
15 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
16 function assertRejectsWithError(promise, name, message) { 48 function assertRejectsWithError(promise, name, message) {
17 return promise.then(() => { 49 return promise.then(() => {
18 assert_unreached('expected promise to reject with ' + name); 50 assert_unreached('expected promise to reject with ' + name);
19 }, error => { 51 }, error => {
20 assert_equals(error.name, name); 52 assert_equals(error.name, name);
21 if (message !== undefined) 53 if (message !== undefined)
22 assert_equals(error.message, message); 54 assert_equals(error.message, message);
23 }); 55 });
24 } 56 }
25 57
(...skipping 30 matching lines...) Expand all
56 }); 88 });
57 } 89 }
58 90
59 function runGarbageCollection() { 91 function runGarbageCollection() {
60 // Run gc() as a promise. 92 // Run gc() as a promise.
61 return new Promise((resolve, reject) => { 93 return new Promise((resolve, reject) => {
62 GCController.collect(); 94 GCController.collect();
63 setTimeout(resolve, 0); 95 setTimeout(resolve, 0);
64 }); 96 });
65 } 97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698