OLD | NEW |
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 Loading... |
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 } | |
OLD | NEW |