// Appends a list item with `innerHTML` to the document's 'list' element. function log(innerHTML) { const li = document.createElement('li'); li.innerHTML = innerHTML; document.getElementById('list').appendChild(li); } // Returns a string with the label and bounds of screen `s` for logging. function screenLog(s) { return `'${s.label}': (${s.left},${s.top} ${s.width}x${s.height})`; } // Returns a string with the bounds of window `w` for logging. function windowLog(w) { return `(${w.screenLeft},${w.screenTop} ${w.outerWidth}x${w.outerHeight})`; } // Appends a button with `innerHTML` to the document's `list` element. // Waits for a test driver or manual click, and disables the button afterwards. async function buttonClick(test, innerHTML) { const button = document.createElement('button'); button.innerHTML = innerHTML; const li = document.createElement('li'); li.appendChild(button) document.getElementById('list').appendChild(li); const click = new EventWatcher(test, button, ['click']).wait_for('click'); try { // Support manual testing where test_driver is not running. await test_driver.click(button); } catch { } await click; button.disabled = true; } // Grants `window-management` permission and caches `window.screenDetails`. async function setUpWindowManagement(test) { assert_implements( 'getScreenDetails' in self && 'isExtended' in screen, `API not supported; use Chrome or Chromium (not content_shell)`); if (!screen.isExtended) log(`WARNING: Use multiple screens for full test coverage`); if (window.location.href.startsWith('file')) log(`WARNING: Run via 'wpt serve'; file URLs lack permission support`); try { // Support manual testing where test_driver is not running. await test_driver.set_permission({ name: 'window-management' }, 'granted'); } catch { } await buttonClick(test, 'Request screen details'); window.screenDetails = await window.getScreenDetails(); assert_true(!!window.screenDetails, 'Error getting screen details'); } // Polls until `condition` is true, with the given `interval` and `duration`. // Returns a promise that will be resolved on success or timeout. async function poll(condition, interval = 100, duration = 3000) { const timeout = Date.now() + duration; const loop = (resolve) => { if (condition() || Date.now() > timeout) resolve(); else step_timeout(loop, interval, resolve); } return new Promise(loop); } // Open and return a popup on `screen`, optionally asserting placement. async function openPopupOnScreen(screen, assertPlacement = true, fullscreen = false) { const left = screen.availLeft + Math.floor(screen.availWidth / 2) - 150; const top = screen.availTop + Math.floor(screen.availHeight / 2) - 50; let features = `left=${left},top=${top},width=300,height=100`; if (fullscreen) { features += ",fullscreen"; } log(`Opening a popup with features '${features}' on ${screenLog(screen)}`); // Window.open() synchronously returns a Window with estimated screenLeft|Top, // which may be clamped to the opener's screen or incompletely initialized. let popup = window.open('/resources/blank.html', '', features); if (assertPlacement) { // Assert the popup is eventually placed at the expected location. // This may occur after window load, document ready and visible, etc. const initialBounds = windowLog(popup); log(`