summaryrefslogtreecommitdiff
path: root/dom/html/test/browser_fullscreen-api-keys.js
blob: 92358efeee7f8b238ce0a32c49b9d729b6592bcd (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
"use strict";

/** Test for Bug 545812 **/

// List of key codes which should exit full-screen mode.
const kKeyList = [
  { code: "VK_ESCAPE", suppressed: true},
  { code: "VK_F11",    suppressed: false},
];

function frameScript() {
  let doc = content.document;
  addMessageListener("Test:RequestFullscreen", () => {
    doc.body.requestFullscreen();
  });
  addMessageListener("Test:DispatchUntrustedKeyEvents", msg => {
    var evt = new content.CustomEvent("Test:DispatchKeyEvents", {
      detail: { code: msg.data }
    });
    content.dispatchEvent(evt);
  });

  doc.addEventListener("fullscreenchange", () => {
    sendAsyncMessage("Test:FullscreenChanged", !!doc.fullscreenElement);
  });

  function keyHandler(evt) {
    sendAsyncMessage("Test:KeyReceived", {
      type: evt.type,
      keyCode: evt.keyCode
    });
  }
  doc.addEventListener("keydown", keyHandler, true);
  doc.addEventListener("keyup", keyHandler, true);
  doc.addEventListener("keypress", keyHandler, true);

  function waitUntilActive() {
    if (doc.docShell.isActive && doc.hasFocus()) {
      sendAsyncMessage("Test:Activated");
    } else {
      setTimeout(waitUntilActive, 10);
    }
  }
  waitUntilActive();
}

var gMessageManager;

function listenOneMessage(aMsg, aListener) {
  function listener({ data }) {
    gMessageManager.removeMessageListener(aMsg, listener);
    aListener(data);
  }
  gMessageManager.addMessageListener(aMsg, listener);
}

function promiseOneMessage(aMsg) {
  return new Promise(resolve => listenOneMessage(aMsg, resolve));
}

function captureUnexpectedFullscreenChange() {
  ok(false, "Caught an unexpected fullscreen change");
}

function* temporaryRemoveUnexpectedFullscreenChangeCapture(callback) {
  gMessageManager.removeMessageListener(
    "Test:FullscreenChanged", captureUnexpectedFullscreenChange);
  yield* callback();
  gMessageManager.addMessageListener(
    "Test:FullscreenChanged", captureUnexpectedFullscreenChange);
}

function captureUnexpectedKeyEvent(type) {
  ok(false, `Caught an unexpected ${type} event`);
}

function* temporaryRemoveUnexpectedKeyEventCapture(callback) {
  gMessageManager.removeMessageListener(
    "Test:KeyReceived", captureUnexpectedKeyEvent);
  yield* callback();
  gMessageManager.addMessageListener(
    "Test:KeyReceived", captureUnexpectedKeyEvent);
}

function receiveExpectedKeyEvents(keyCode) {
  return new Promise(resolve => {
    let events = ["keydown", "keypress", "keyup"];
    function listener({ data }) {
      let expected = events.shift();
      is(data.type, expected, `Should receive a ${expected} event`);
      is(data.keyCode, keyCode,
         `Should receive the event with key code ${keyCode}`);
      if (!events.length) {
        gMessageManager.removeMessageListener("Test:KeyReceived", listener);
        resolve();
      }
    }
    gMessageManager.addMessageListener("Test:KeyReceived", listener);
  });
}

const kPage = "http://example.org/browser/" +
              "dom/html/test/file_fullscreen-api-keys.html";

add_task(function* () {
  yield pushPrefs(
    ["full-screen-api.transition-duration.enter", "0 0"],
    ["full-screen-api.transition-duration.leave", "0 0"]);

  let tab = gBrowser.addTab(kPage);
  let browser = tab.linkedBrowser;
  gBrowser.selectedTab = tab;
  registerCleanupFunction(() => gBrowser.removeTab(tab));
  yield waitForDocLoadComplete();

  gMessageManager = browser.messageManager;
  gMessageManager.loadFrameScript(
    "data:,(" + frameScript.toString() + ")();", false);

  // Wait for the document being actived, so that
  // fullscreen request won't be denied.
  yield promiseOneMessage("Test:Activated");

  // Register listener to capture unexpected events
  gMessageManager.addMessageListener(
    "Test:FullscreenChanged", captureUnexpectedFullscreenChange);
  gMessageManager.addMessageListener(
    "Test:KeyReceived", captureUnexpectedKeyEvent);
  registerCleanupFunction(() => {
    gMessageManager.removeMessageListener(
      "Test:FullscreenChanged", captureUnexpectedFullscreenChange);
    gMessageManager.removeMessageListener(
      "Test:KeyReceived", captureUnexpectedKeyEvent);
  });

  for (let {code, suppressed} of kKeyList) {
    var keyCode = KeyEvent["DOM_" + code];
    info(`Test keycode ${code} (${keyCode})`);

    info("Enter fullscreen");
    yield* temporaryRemoveUnexpectedFullscreenChangeCapture(function* () {
      gMessageManager.sendAsyncMessage("Test:RequestFullscreen");
      let state = yield promiseOneMessage("Test:FullscreenChanged");
      ok(state, "The content should have entered fullscreen");
      ok(document.fullscreenElement,
         "The chrome should also be in fullscreen");
    });

    info("Dispatch untrusted key events from content");
    yield* temporaryRemoveUnexpectedKeyEventCapture(function* () {
      let promiseExpectedKeyEvents = receiveExpectedKeyEvents(keyCode);
      gMessageManager.sendAsyncMessage("Test:DispatchUntrustedKeyEvents", code);
      yield promiseExpectedKeyEvents;
    });

    info("Send trusted key events");
    yield* temporaryRemoveUnexpectedFullscreenChangeCapture(function* () {
      yield* temporaryRemoveUnexpectedKeyEventCapture(function* () {
        let promiseExpectedKeyEvents = suppressed ?
          Promise.resolve() : receiveExpectedKeyEvents(keyCode);
        EventUtils.synthesizeKey(code, {});
        yield promiseExpectedKeyEvents;
        let state = yield promiseOneMessage("Test:FullscreenChanged");
        ok(!state, "The content should have exited fullscreen");
        ok(!document.fullscreenElement,
           "The chrome should also have exited fullscreen");
      });
    });
  }
});