helpers.js (5337B)
1 // Appends a list item with `innerHTML` to the document's 'list' element. 2 function log(innerHTML) { 3 const li = document.createElement('li'); 4 li.innerHTML = innerHTML; 5 document.getElementById('list').appendChild(li); 6 } 7 8 // Returns a string with the label and bounds of screen `s` for logging. 9 function screenLog(s) { 10 return `'${s.label}': (${s.left},${s.top} ${s.width}x${s.height})`; 11 } 12 13 // Returns a string with the bounds of window `w` for logging. 14 function windowLog(w) { 15 return `(${w.screenLeft},${w.screenTop} ${w.outerWidth}x${w.outerHeight})`; 16 } 17 18 // Appends a button with `innerHTML` to the document's `list` element. 19 // Waits for a test driver or manual click, and disables the button afterwards. 20 async function buttonClick(test, innerHTML) { 21 const button = document.createElement('button'); 22 button.innerHTML = innerHTML; 23 const li = document.createElement('li'); 24 li.appendChild(button) 25 document.getElementById('list').appendChild(li); 26 const click = new EventWatcher(test, button, ['click']).wait_for('click'); 27 try { // Support manual testing where test_driver is not running. 28 await test_driver.click(button); 29 } catch { 30 } 31 await click; 32 button.disabled = true; 33 } 34 35 // Grants `window-management` permission and caches `window.screenDetails`. 36 async function setUpWindowManagement(test) { 37 assert_implements( 38 'getScreenDetails' in self && 'isExtended' in screen, 39 `API not supported; use Chrome or Chromium (not content_shell)`); 40 if (!screen.isExtended) 41 log(`WARNING: Use multiple screens for full test coverage`); 42 if (window.location.href.startsWith('file')) 43 log(`WARNING: Run via 'wpt serve'; file URLs lack permission support`); 44 45 try { // Support manual testing where test_driver is not running. 46 await test_driver.set_permission({ name: 'window-management' }, 'granted'); 47 } catch { 48 } 49 await buttonClick(test, 'Request screen details'); 50 window.screenDetails = await window.getScreenDetails(); 51 assert_true(!!window.screenDetails, 'Error getting screen details'); 52 } 53 54 // Polls until `condition` is true, with the given `interval` and `duration`. 55 // Returns a promise that will be resolved on success or timeout. 56 async function poll(condition, interval = 100, duration = 3000) { 57 const timeout = Date.now() + duration; 58 const loop = (resolve) => { 59 if (condition() || Date.now() > timeout) 60 resolve(); 61 else 62 step_timeout(loop, interval, resolve); 63 } 64 return new Promise(loop); 65 } 66 67 // Open and return a popup on `screen`, optionally asserting placement. 68 async function openPopupOnScreen(screen, assertPlacement = true) { 69 const left = screen.availLeft + Math.floor(screen.availWidth / 2) - 150; 70 const top = screen.availTop + Math.floor(screen.availHeight / 2) - 50; 71 let features = `left=${left},top=${top},width=300,height=100`; 72 log(`Opening a popup with features '${features}' on ${screenLog(screen)}`); 73 // Window.open() synchronously returns a Window with estimated screenLeft|Top, 74 // which may be clamped to the opener's screen or incompletely initialized. 75 let popup = window.open('/resources/blank.html', '', features); 76 77 if (assertPlacement) { 78 // Assert the popup is eventually placed at the expected location. 79 // This may occur after window load, document ready and visible, etc. 80 const initialBounds = windowLog(popup); 81 log(`<div style='margin-left: 40px'>Initial: ${initialBounds}</div>`); 82 await poll(() => { return popup.screenLeft == left && 83 popup.screenTop == top }); 84 popup.document.write(`Requested: (${left},${top} 300x100) <br> \ 85 Initial: ${initialBounds} <br> \ 86 Resolved: ${windowLog(popup)}`); 87 log(`<div style='margin-left: 40px'>Resolved: ${windowLog(popup)}</div>`); 88 const context = `popup: ${windowLog(popup)}, ${screenLog(screen)}`; 89 assert_equals(popup.screenLeft, left, context); 90 assert_equals(popup.screenTop, top, context); 91 } 92 93 return popup; 94 } 95 96 // Returns true if window `w` bounds are on screen `s` with threshold `t`. 97 function isWindowOnScreen(w, s, t = 100) { 98 return (w.screenLeft >= s.left - t) && (w.screenTop >= s.top - t) && 99 (w.screenLeft + w.outerWidth <= s.left + s.width + t) && 100 (w.screenTop + w.outerHeight <= s.top + s.height + t); 101 } 102 103 // Asserts window `w` currentScreen matches screen `s`. Awaits pending changes, 104 // e.g. fullscreen promises may resolve before screen change: crbug.com/1330724. 105 async function assertWindowHasCurrentScreen(w, s) { 106 log(`assertWindowHasCurrentScreen w: ${windowLog(w)} s: ${screenLog(s)}`); 107 await poll(() => { return s === w.screenDetails.currentScreen; }); 108 assert_equals(screenLog(s), screenLog(w.screenDetails.currentScreen)); 109 } 110 111 // Asserts window `w` bounds roughly match screen `s`. Awaits pending changes, 112 // e.g. fullscreen promises may resolve before bounds change: crbug.com/1330724. 113 async function assertWindowBoundsOnScreen(w, s) { 114 log(`assertWindowBoundsOnScreen w: ${windowLog(w)} s: ${screenLog(s)}`); 115 await poll(() => { return isWindowOnScreen(w, s); }); 116 assert_true(isWindowOnScreen(w, s), `${windowLog(w)} on ${screenLog(s)}`); 117 } 118 119 // Asserts window `w` bounds and currentScreen match screen `s`. 120 async function assertWindowOnScreen(w, s) { 121 await assertWindowHasCurrentScreen(w, s); 122 await assertWindowBoundsOnScreen(w, s); 123 }