summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/interaction/focus/the-autofocus-attribute/resources/utils.js
blob: e928e275407cbeee9fefffa9a3772b9d55ccc62e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';

function waitForEvent(target, type, options) {
  return new Promise((resolve, reject) => {
    target.addEventListener(type, resolve, options);
  });
}

function waitForAnimationFrame(w) {
  let targetWindow = w || window;
  return new Promise((resolve, reject) => {
    targetWindow.requestAnimationFrame(resolve);
  });
}

function waitForEvent(target, type, options) {
  return new Promise((resolve, reject) => {
    target.addEventListener(type, resolve, options);
  });
}

function waitForLoad(target) {
  return waitForEvent(target, 'load');
}

function timeOut(test, ms) {
  return new Promise((resolve, reject) => {
    test.step_timeout(resolve, ms);
  });
}

// If an element with autofocus is connected to a document and this function
// is called, the autofocus result is deterministic after returning from the
// function.
// Exception: If the document has script-blocking style sheets, this function
// doesn't work well.
async function waitUntilStableAutofocusState(w) {
  let targetWindow = w || window;
  // Awaiting one animation frame is an easy way to determine autofocus state.
  await waitForAnimationFrame(targetWindow);
}

async function waitForIframeLoad(src, w = window) {
  const iframe = w.document.createElement("iframe");
  let loadPromise = new Promise(resolve => {
    iframe.addEventListener("load", () => resolve(iframe));
  });
  iframe.src = src;
  w.document.body.appendChild(iframe);
  return loadPromise;
}