summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/screen-orientation/resources/orientation-utils.js
blob: 95383750f16e1a47d39e514627e03f2ba8c3b8ca (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
52
/**
 *
 * @param {object} options
 * @param {string} options.src - The iframe src
 * @param {Window} options.context - The browsing context in which the iframe will be created
 * @param {string} options.sandbox - The sandbox attribute for the iframe
 * @returns
 */
export async function attachIframe(options = {}) {
  const { src, context, sandbox, allowFullscreen } = {
    ...{
      src: "about:blank",
      context: self,
      allowFullscreen: true,
      sandbox: null,
    },
    ...options,
  };
  const iframe = context.document.createElement("iframe");
  if (sandbox !== null) iframe.sandbox = sandbox;
  iframe.allowFullscreen = allowFullscreen;
  await new Promise((resolve) => {
    iframe.onload = resolve;
    iframe.src = src;
    context.document.body.appendChild(iframe);
  });
  return iframe;
}

export function getOppositeOrientation() {
  return screen.orientation.type.startsWith("portrait")
    ? "landscape"
    : "portrait";
}

export function makeCleanup(
  initialOrientation = screen.orientation?.type.split(/-/)[0]
) {
  return async () => {
    if (initialOrientation) {
      try {
        await screen.orientation.lock(initialOrientation);
      } catch {}
    }
    screen.orientation.unlock();
    requestAnimationFrame(async () => {
      try {
        await document.exitFullscreen();
      } catch {}
    });
  };
}