utils.js (1461B)
1 'use strict'; 2 3 function waitForEvent(target, type, options) { 4 return new Promise((resolve, reject) => { 5 target.addEventListener(type, resolve, options); 6 }); 7 } 8 9 function waitForAnimationFrame(w) { 10 let targetWindow = w || window; 11 return new Promise((resolve, reject) => { 12 targetWindow.requestAnimationFrame(resolve); 13 }); 14 } 15 16 function waitForEvent(target, type, options) { 17 return new Promise((resolve, reject) => { 18 target.addEventListener(type, resolve, options); 19 }); 20 } 21 22 function waitForLoad(target) { 23 return waitForEvent(target, 'load'); 24 } 25 26 function timeOut(test, ms) { 27 return new Promise((resolve, reject) => { 28 test.step_timeout(resolve, ms); 29 }); 30 } 31 32 // If an element with autofocus is connected to a document and this function 33 // is called, the autofocus result is deterministic after returning from the 34 // function. 35 // Exception: If the document has script-blocking style sheets, this function 36 // doesn't work well. 37 async function waitUntilStableAutofocusState(w) { 38 let targetWindow = w || window; 39 // Awaiting one animation frame is an easy way to determine autofocus state. 40 await waitForAnimationFrame(targetWindow); 41 } 42 43 async function waitForIframeLoad(src, w = window) { 44 const iframe = w.document.createElement("iframe"); 45 let loadPromise = new Promise(resolve => { 46 iframe.addEventListener("load", () => resolve(iframe)); 47 }); 48 iframe.src = src; 49 w.document.body.appendChild(iframe); 50 return loadPromise; 51 }