summaryrefslogtreecommitdiffstats
path: root/dom/html/test/file_fullscreen-api.html
blob: 89162058ec8016fdf072ccc7f6e7e59929fba935 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=545812

Test DOM full-screen API.

-->
<head>
  <title>Test for Bug 545812</title>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="file_fullscreen-utils.js"></script>
  <style>
  body {
    background-color: black;
  }
  </style>
</head>
<body>
<script type="application/javascript">

/** Test for Bug 545812 **/

function ok(condition, msg) {
  opener.ok(condition, "[fullscreen] " + msg);
}

function is(a, b, msg) {
  opener.is(a, b, "[fullscreen] " + msg);
}

/*
<html>
  <body onload='document.body.requestFullscreen();'>
  <iframe id='inner-frame'></iframe>
  </body>
</html>
*/
var iframeContents = "data:text/html;charset=utf-8,<html><body onload%3D'parent.SimpleTest.waitForFocus(function(){document.body.requestFullscreen();});'><iframe id%3D'inner-frame'><%2Fiframe><%2Fbody><%2Fhtml>";

var iframe = null;
var outOfDocElement = null;
var inDocElement = null;
var container = null;
var button = null;


function sendMouseClick(element) {
  synthesizeMouseAtCenter(element, {});
}

function fullScreenElement() {
  return document.getElementById('full-screen-element');
}

function enter1(event) {
  is(event.target, document, "Event target should be full-screen document #1");
  ok(document.fullscreen, "Document should be in fullscreen");
  is(document.fullscreenElement, fullScreenElement(),
     "Full-screen element should be div element.");
  ok(document.fullscreenElement.matches(":fullscreen"),
     "FSE should match :fullscreen");
  var fse = fullScreenElement();
  addFullscreenChangeContinuation("exit", exit1);
  fse.parentNode.removeChild(fse);
  is(document.fullscreenElement, null,
     "Full-screen element should be null after removing.");
  document.body.appendChild(fse);
  is(document.fullscreenElement, null,
     "Full-screen element should still be null after re-adding former FSE.");
}

function exit1(event) {
  is(event.target, document, "Event target should be full-screen document #2");
  ok(!document.fullscreen, "Document should not be in fullscreen");
  is(document.fullscreenElement, null, "Full-screen element should be null.");
  iframe = document.createElement("iframe");
  iframe.allowFullscreen = true;
  addFullscreenChangeContinuation("enter", enter2);
  document.body.appendChild(iframe);
  iframe.src = iframeContents;
}

function enter2(event) {
  is(event.target, document, "Event target should be full-screen document #3");
  is(document.fullscreenElement, iframe,
     "Full-screen element should be iframe element.");
  is(iframe.contentDocument.fullscreenElement, iframe.contentDocument.body,
     "Full-screen element in subframe should be body");
  
  // The iframe's body is full-screen. Cancel full-screen in the subdocument to return
  // the full-screen element to the previous full-screen element. This causes
  // a fullscreenchange event.
  addFullscreenChangeContinuation("exit", exit2);
  document.exitFullscreen();
}

function exit2(event) {
  is(document.fullscreenElement, null,
     "Full-screen element should have rolled back.");
  is(iframe.contentDocument.fullscreenElement, null,
     "Full-screen element in subframe should be null");
  
  addFullscreenChangeContinuation("enter", enter3);
  fullScreenElement().requestFullscreen();
}

function enter3(event) {
  is(event.target, document, "Event target should be full-screen document #3");
  is(document.fullscreenElement, fullScreenElement(),
     "Full-screen element should be div.");
  
  // Transplant the FSE into subdoc. Should exit full-screen.
  addFullscreenChangeContinuation("exit", exit3);
  var _innerFrame = iframe.contentDocument.getElementById("inner-frame");
  var fse = fullScreenElement();
  _innerFrame.contentDocument.body.appendChild(fse);
  is(document.fullscreenElement, null,
     "Full-screen element transplanted, should be null.");
  is(iframe.contentDocument.fullscreenElement, null,
     "Full-screen element in outer frame should be null.");
  is(_innerFrame.contentDocument.fullscreenElement, null,
     "Full-screen element in inner frame should be null.");
  
  document.body.appendChild(fse);
}

function exit3(event) {
  is(event.target, document, "Event target should be full-screen document #4");
  is(document.fullscreenElement, null, "Full-screen element should be null.");
  document.body.removeChild(iframe);
  iframe = null;

  // Do a request out of document. It should be denied.
  // Continue test in the following fullscreenerror handler.
  outOfDocElement = document.createElement("div");
  addFullscreenErrorContinuation(error1);    
  outOfDocElement.requestFullscreen();
}

function error1(event) {
  ok(!document.fullscreenElement,
     "Requests for full-screen from not-in-doc elements should fail.");
  container = document.createElement("div");
  inDocElement = document.createElement("div");
  container.appendChild(inDocElement);
  fullScreenElement().appendChild(container);

  addFullscreenChangeContinuation("enter", enter4);
  inDocElement.requestFullscreen();
}

function enter4(event) {
  is(event.target, document, "Event target should be full-screen document #5");
  is(document.fullscreenElement, inDocElement, "FSE should be inDocElement.");

  // Remove full-screen ancestor element from document, verify it stops being reported as current FSE.
  addFullscreenChangeContinuation("exit", exit_to_arg_test_1);
  container.parentNode.removeChild(container);
  is(document.fullscreenElement, null,
     "Should not have a full-screen element again.");
}

function exit_to_arg_test_1(event) {
  ok(!document.fullscreenElement,
     "Should have left full-screen mode (third time).");
  addFullscreenChangeContinuation("enter", enter_from_arg_test_1);
  var threw = false;
  try {
    fullScreenElement().requestFullscreen(123);
  } catch (e) {
    threw = true;
    // trigger normal fullscreen so that we continue
    fullScreenElement().requestFullscreen();
  }
  ok(!threw, "requestFullscreen with bogus arg (123) shouldn't throw exception");
}

function enter_from_arg_test_1(event) {
  ok(document.fullscreenElement,
     "Should have entered full-screen after calling with bogus (ignored) argument (fourth time)");
  addFullscreenChangeContinuation("exit", exit_to_arg_test_2);
  document.exitFullscreen();
}

function exit_to_arg_test_2(event) {
  ok(!document.fullscreenElement,
     "Should have left full-screen mode (fourth time).");
  addFullscreenChangeContinuation("enter", enter_from_arg_test_2);
  var threw = false;
  try {
    fullScreenElement().requestFullscreen({ vrDisplay: null });
  } catch (e) {
    threw = true;
    // trigger normal fullscreen so that we continue
    fullScreenElement().requestFullscreen();
  }
  ok(!threw, "requestFullscreen with { vrDisplay: null } shouldn't throw exception");
}

function enter_from_arg_test_2(event) {
  ok(document.fullscreenElement,
     "Should have entered full-screen after calling with vrDisplay null argument (fifth time)");
  addFullscreenChangeContinuation("exit", exit4);
  document.exitFullscreen();
}

function exit4(event) {
  ok(!document.fullscreenElement,
     "Should be back in non-full-screen mode (fifth time)");
  SpecialPowers.pushPrefEnv({"set":[["full-screen-api.allow-trusted-requests-only", true]]}, function() {
    addFullscreenErrorContinuation(error2);
    fullScreenElement().requestFullscreen();
  });
}

function error2(event) {
  ok(!document.fullscreenElement,
     "Should still be in normal mode, because calling context isn't trusted.");
  button = document.createElement("button");
  button.onclick = function(){fullScreenElement().requestFullscreen();}
  fullScreenElement().appendChild(button);
  addFullscreenChangeContinuation("enter", enter5);
  sendMouseClick(button);
}

function enter5(event) {
  ok(document.fullscreenElement, "Moved to full-screen after mouse click");
  addFullscreenChangeContinuation("exit", exit5);
  document.exitFullscreen();
}

function exit5(event) {
  ok(!document.fullscreenElement,
     "Should have left full-screen mode (last time).");
  SpecialPowers.pushPrefEnv({
    "set":[["full-screen-api.allow-trusted-requests-only", false],
           ["full-screen-api.enabled", false]]}, function() {
              is(document.fullscreenEnabled, false, "document.fullscreenEnabled should be false if full-screen-api.enabled is false");
              addFullscreenErrorContinuation(error3);
              fullScreenElement().requestFullscreen();
  });
}

function error3(event) {
  ok(!document.fullscreenElement,
     "Should still be in normal mode, because pref is not enabled.");

  SpecialPowers.pushPrefEnv({"set":[["full-screen-api.enabled", true]]}, function() {
    is(document.fullscreenEnabled, true, "document.fullscreenEnabled should be true if full-screen-api.enabled is true");
    opener.nextTest();
  });
}

function begin() {
  testNamespaces(() => {
    addFullscreenChangeContinuation("enter", enter1);
    fullScreenElement().requestFullscreen();
  });
}

function testNamespaces(followupTestFn) {
  let tests = [
    {allowed: false, name: "element", ns: "http://www.w3.org/XML/1998/namespace"},
    {allowed: false, name: "element", ns: "http://www.w3.org/1999/xlink"},
    {allowed: false, name: "element", ns: "http://www.w3.org/2000/svg"},
    {allowed: false, name: "element", ns: "http://www.w3.org/1998/Math/MathML"},
    {allowed: false, name: "mathml",  ns: "unknown"},
    {allowed: false, name: "svg",     ns: "unknown"},
    {allowed: true,  name: "element", ns: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"},
    {allowed: true,  name: "element", ns: "http://www.w3.org/1999/xhtml"},
    {allowed: true,  name: "svg",     ns: "http://www.w3.org/1999/xhtml"},
    {allowed: true,  name: "math",    ns: "http://www.w3.org/1999/xhtml"},
    {allowed: true,  name: "svg",     ns: "http://www.w3.org/2000/svg"},
    {allowed: true,  name: "math",    ns: "http://www.w3.org/1998/Math/MathML"},
    {allowed: true,  name: "element"},
  ];

  function runNextNamespaceTest() {
    let test = tests.shift();
    if (!test) {
      followupTestFn();
      return;
    }

    let elem = test.ns ? document.createElementNS(test.ns, test.name) :
                         document.createElement(test.name);
    document.body.appendChild(elem);

    if (test.allowed) {
      addFullscreenChangeContinuation("enter", () => {
        ok(document.fullscreen, "Document should be in fullscreen");
        is(document.fullscreenElement, elem,
           `Element named '${test.name}' in this namespace should be allowed: ${test.ns}`);
        addFullscreenChangeContinuation("exit", runNextNamespaceTest);
        document.body.removeChild(elem);
      });
    } else {
      addFullscreenErrorContinuation(() => {
        ok(!document.fullscreenElement,
           `Element named '${test.name}' in this namespace should not be allowed: ${test.ns}`);
        document.body.removeChild(elem);
        runNextNamespaceTest();
      });
    }

    elem.requestFullscreen();
  }

  runNextNamespaceTest();
}
</script>
</pre>
<div id="full-screen-element"></div>
</body>
</html>