summaryrefslogtreecommitdiffstats
path: root/dom/base/test/fullscreen/fullscreen_helpers.js
blob: 41548cf8aa061a1520eb94308ab9c900ac5b76ea (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_URLS = [
  // all frames are in different process.
  `data:text/html,
    <div name="div" id="div" style="width: 100px; height: 100px; background: red;">
    <iframe id="iframe" allowfullscreen="yes"
     src="https://example.com/browser/dom/base/test/fullscreen/file_fullscreen-iframe-middle.html"></iframe>
    </div>`,
  // toplevel and inner most iframe are in same process, and middle iframe is
  // in a different process.
  `https://example.org/browser/dom/base/test/fullscreen/file_fullscreen-iframe-top.html`,
  // toplevel and middle iframe are in same process, and inner most iframe is
  // in a different process.
  `https://example.com/browser/dom/base/test/fullscreen/file_fullscreen-iframe-top.html`,
];

function waitRemoteFullscreenExitEvents(aBrowsingContexts) {
  let promises = [];
  aBrowsingContexts.forEach(([aBrowsingContext, aName]) => {
    promises.push(
      SpecialPowers.spawn(aBrowsingContext, [aName], async name => {
        return new Promise(resolve => {
          let document = content.document;
          document.addEventListener(
            "fullscreenchange",
            function changeHandler() {
              if (document.fullscreenElement) {
                return;
              }

              ok(true, `check remote DOM fullscreen event (${name})`);
              document.removeEventListener("fullscreenchange", changeHandler);
              resolve();
            }
          );
        });
      })
    );
  });
  return Promise.all(promises);
}

function waitDOMFullscreenEvent(
  aDocument,
  aIsInFullscreen,
  aWaitUntil = false
) {
  return new Promise(resolve => {
    function errorHandler() {
      ok(false, "should not get fullscreenerror event");
      aDocument.removeEventListener("fullscreenchange", changeHandler);
      aDocument.removeEventListener("fullscreenerror", errorHandler);
      resolve();
    }

    function changeHandler() {
      if (aWaitUntil && aIsInFullscreen != !!aDocument.fullscreenElement) {
        return;
      }

      is(
        aIsInFullscreen,
        !!aDocument.fullscreenElement,
        "check DOM fullscreen (event)"
      );
      aDocument.removeEventListener("fullscreenchange", changeHandler);
      aDocument.removeEventListener("fullscreenerror", errorHandler);
      resolve();
    }

    aDocument.addEventListener("fullscreenchange", changeHandler);
    aDocument.addEventListener("fullscreenerror", errorHandler);
  });
}

function waitWidgetFullscreenEvent(
  aWindow,
  aIsInFullscreen,
  aWaitUntil = false
) {
  return BrowserTestUtils.waitForEvent(aWindow, "fullscreen", false, () => {
    if (
      aWaitUntil &&
      aIsInFullscreen !=
        aWindow.document.documentElement.hasAttribute("inFullscreen")
    ) {
      return false;
    }

    is(
      aIsInFullscreen,
      aWindow.document.documentElement.hasAttribute("inFullscreen"),
      "check widget fullscreen (event)"
    );
    return true;
  });
}

function waitForFullScreenObserver(
  aDocument,
  aIsInFullscreen,
  aWaitUntil = false
) {
  return TestUtils.topicObserved("fullscreen-painted", () => {
    if (
      aWaitUntil &&
      aIsInFullscreen !=
        aDocument.documentElement.hasAttribute("inDOMFullscreen")
    ) {
      return false;
    }

    is(
      aIsInFullscreen,
      aDocument.documentElement.hasAttribute("inDOMFullscreen"),
      "check fullscreen (observer)"
    );
    return true;
  });
}

function waitForFullscreenState(
  aDocument,
  aIsInFullscreen,
  aWaitUntil = false
) {
  return Promise.all([
    waitWidgetFullscreenEvent(
      aDocument.defaultView,
      aIsInFullscreen,
      aWaitUntil
    ),
    waitDOMFullscreenEvent(aDocument, aIsInFullscreen, aWaitUntil),
    waitForFullScreenObserver(aDocument, aIsInFullscreen, aWaitUntil),
  ]);
}

// Wait for fullscreenchange event for fullscreen exit. And wait for
// fullscreen-painted observed conditionally.
async function waitForFullscreenExit(aDocument) {
  info(`waitForFullscreenExit`);
  let promiseFsObserver = null;
  let observer = function () {
    if (aDocument.documentElement.hasAttribute("inDOMFullscreen")) {
      info(`waitForFullscreenExit, fullscreen-painted, inDOMFullscreen`);
      Services.obs.removeObserver(observer, "fullscreen-painted");
      promiseFsObserver = waitForFullScreenObserver(aDocument, false);
    }
  };
  Services.obs.addObserver(observer, "fullscreen-painted");

  await waitDOMFullscreenEvent(aDocument, false, true);
  // If there is a fullscreen-painted observer notified for inDOMFullscreen set,
  // we expect to have a subsequent fullscreen-painted observer notified with
  // inDOMFullscreen unset.
  if (promiseFsObserver) {
    info(`waitForFullscreenExit, promiseFsObserver`);
    await promiseFsObserver;
    return;
  }

  Services.obs.removeObserver(observer, "fullscreen-painted");
  // If inDOMFullscreen is set we expect to have a subsequent fullscreen-painted
  // observer notified with inDOMFullscreen unset.
  if (aDocument.documentElement.hasAttribute("inDOMFullscreen")) {
    info(`waitForFullscreenExit, inDOMFullscreen`);
    await waitForFullScreenObserver(aDocument, false, true);
  }
}