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

Side by Side Diff: third_party/WebKit/LayoutTests/external/wpt/webusb/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, 5 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 // These tests rely on the User Agent providing an implementation of the
4 // WebUSB Testing API (https://wicg.github.io/webusb/test/).
5 //
6 // In Chromium-based browsers this implementation is provided by a polyfill
7 // in order to reduce the amount of test-only code shipped to users. To enable
8 // these tests the browser must be run with these options:
9 //
10 // --enable-blink-features=MojoJS,MojoJSTest
11 let loadChromiumResources = Promise.resolve().then(() => {
12 if (!MojoInterfaceInterceptor) {
13 // Do nothing on non-Chromium-based browsers or when the Mojo bindings are
14 // not present in the global namespace.
15 return;
16 }
17
18 let chain = Promise.resolve();
19 [
20 '/resources/chromium/mojo_bindings.js',
21 '/resources/chromium/device.mojom.js',
22 '/resources/chromium/device_manager.mojom.js',
23 '/resources/chromium/chooser_service.mojom.js',
24 '/resources/chromium/webusb-test.js',
25 ].forEach(path => {
26 let script = document.createElement('script');
27 script.src = path;
28 script.async = false;
29 chain = chain.then(() => new Promise(resolve => {
30 script.onload = () => resolve();
31 }));
32 document.head.appendChild(script);
33 });
34
35 return chain;
36 });
37
3 function usb_test(func, name, properties) { 38 function usb_test(func, name, properties) {
4 promise_test(async () => { 39 promise_test(async () => {
5 await navigator.usb.test.initialize() 40 if (navigator.usb.test === undefined) {
41 // Try loading a polyfill for the WebUSB Testing API.
42 await loadChromiumResources;
43 }
44
45 await navigator.usb.test.initialize();
6 try { 46 try {
7 await func(); 47 await func();
8 } finally { 48 } finally {
9 await navigator.usb.test.reset(); 49 await navigator.usb.test.reset();
10 } 50 }
11 }, name, properties); 51 }, name, properties);
12 } 52 }
13 53
14 // Returns a promise that is resolved when the next USBConnectionEvent of the 54 // Returns a promise that is resolved when the next USBConnectionEvent of the
15 // given type is received. 55 // given type is received.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 } else if (Array.isArray(deviceInit[property])) { 105 } else if (Array.isArray(deviceInit[property])) {
66 assert_equals(usbDevice[property].length, deviceInit[property].length); 106 assert_equals(usbDevice[property].length, deviceInit[property].length);
67 for (var i = 0; i < usbDevice[property].length; ++i) 107 for (var i = 0; i < usbDevice[property].length; ++i)
68 assertDeviceInfoEquals(usbDevice[property][i], deviceInit[property][i]); 108 assertDeviceInfoEquals(usbDevice[property][i], deviceInit[property][i]);
69 } else { 109 } else {
70 assert_equals(usbDevice[property], deviceInit[property], property); 110 assert_equals(usbDevice[property], deviceInit[property], property);
71 } 111 }
72 } 112 }
73 } 113 }
74 114
75 // TODO(reillyg): Remove when jyasskin upstreams this to testharness.js: 115 function callWithTrustedClick(callback) {
76 // https://crbug.com/509058.
77 function callWithKeyDown(functionCalledOnKeyPress) {
78 return new Promise(resolve => { 116 return new Promise(resolve => {
79 function onKeyPress() { 117 let button = document.createElement('button');
80 document.removeEventListener('keypress', onKeyPress, false); 118 button.textContent = 'click to continue test';
81 resolve(functionCalledOnKeyPress()); 119 button.style.display = 'block';
82 } 120 button.style.fontSize = '20px';
83 document.addEventListener('keypress', onKeyPress, false); 121 button.style.padding = '10px';
84 122 button.onclick = () => {
85 eventSender.keyDown(' ', []); 123 resolve(callback());
124 document.body.removeChild(button);
125 };
126 document.body.appendChild(button);
86 }); 127 });
87 } 128 }
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