summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/fullscreen/FullscreenFrame.sys.mjs
blob: 08c804a60ddab60f32ca52f1db3475388191d65c (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * test helper JSWindowActors used by the browser_fullscreen_api_fission.js test.
 */

export class FullscreenFrameChild extends JSWindowActorChild {
  actorCreated() {
    this.fullscreen_events = [];
  }

  changed() {
    return new Promise(resolve => {
      this.contentWindow.document.addEventListener(
        "fullscreenchange",
        () => resolve(),
        {
          once: true,
        }
      );
    });
  }

  requestFullscreen() {
    let doc = this.contentWindow.document;
    let button = doc.createElement("button");
    doc.body.appendChild(button);

    return new Promise(resolve => {
      button.onclick = () => {
        doc.body.requestFullscreen().then(resolve);
        doc.body.removeChild(button);
      };
      button.click();
    });
  }

  receiveMessage(msg) {
    switch (msg.name) {
      case "WaitForChange":
        return this.changed();
      case "ExitFullscreen":
        return this.contentWindow.document.exitFullscreen();
      case "RequestFullscreen":
        return Promise.all([this.changed(), this.requestFullscreen()]);
      case "CreateChild":
        let child = msg.data;
        let iframe = this.contentWindow.document.createElement("iframe");
        iframe.allow = child.allow_fullscreen ? "fullscreen" : "";
        iframe.name = child.name;

        let loaded = new Promise(resolve => {
          iframe.addEventListener(
            "load",
            () => resolve(iframe.browsingContext),
            { once: true }
          );
        });
        iframe.src = child.url;
        this.contentWindow.document.body.appendChild(iframe);
        return loaded;
      case "GetEvents":
        return Promise.resolve(this.fullscreen_events);
      case "ClearEvents":
        this.fullscreen_events = [];
        return Promise.resolve();
      case "GetFullscreenElement":
        let document = this.contentWindow.document;
        let child_iframe = this.contentWindow.document.getElementsByTagName(
          "iframe"
        )
          ? this.contentWindow.document.getElementsByTagName("iframe")[0]
          : null;
        switch (document.fullscreenElement) {
          case null:
            return Promise.resolve("null");
          case document:
            return Promise.resolve("document");
          case document.body:
            return Promise.resolve("body");
          case child_iframe:
            return Promise.resolve("child_iframe");
          default:
            return Promise.resolve("other");
        }
    }

    return Promise.reject("Unexpected Message");
  }

  async handleEvent(event) {
    switch (event.type) {
      case "fullscreenchange":
        this.fullscreen_events.push(true);
        break;
      case "fullscreenerror":
        this.fullscreen_events.push(false);
        break;
    }
  }
}