content/html/content/test/file_fullscreen-api.html
author David Anderson <danderson@mozilla.com>
Fri, 04 Nov 2011 14:02:27 -0700
changeset 111599 89fff0ee52d5e237060a44b133a68e723dab8ae4
parent 111527 8cfeba5239a9e4f20c462d6fb20421b4e4e7c735
parent 80902 5a6ea5db053eb41af0ca869ab35489ee4b04b766
child 111600 e784f2911b5bd7956f52be6a93f010a032cf364e
permissions -rw-r--r--
Merge from mozilla-central.

<!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>
  <style>
  body {
    background-color: black;
  }
  </style>
</head>
<body onload="fullScreenElement().mozRequestFullScreen();">

<script type="application/javascript">

/** Test for Bug 545812 **/

function ok(condition, msg) {
  opener.ok(condition, msg);
}

function is(a, b, msg) {
  opener.is(a, b, msg);
}

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

var iframe = null;
var outOfDocElement = null;
var inDocElement = null;
var container = null;
var button = null;
var fullScreenChangeCount = 0;
var fullscreendenied = false;

document.addEventListener("mozfullscreenerror", function(){fullscreendenied=true;}, false);

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

function setRequireTrustedContext(value) {
  opener.SpecialPowers.setBoolPref("full-screen-api.allow-trusted-requests-only", value);
}

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

function fullScreenChange(event) {
  switch (fullScreenChangeCount) {
    case 0: {
      ok(document.mozFullScreen, "Should be in full-screen mode (first time)");
      is(event.target, fullScreenElement(), "Event target should be full-screen element");
      is(document.mozFullScreenElement, fullScreenElement(),
         "Full-screen element should be div element.");
      ok(document.mozFullScreenElement.mozMatchesSelector(":-moz-full-screen"),
         "FSE should match :-moz-full-screen");
      var fse = fullScreenElement();
      fse.parentNode.removeChild(fse);
      is(document.mozFullScreenElement, null,
         "Full-screen element should be null after removing.");
      ok(!document.mozFullScreen, "Should have left full-screen mode when we remove full-screen element");
      document.body.appendChild(fse);
      ok(!document.mozFullScreen, "Should not return to full-screen mode when re-adding former FSE");
      is(document.mozFullScreenElement, null,
         "Full-screen element should still be null after re-adding former FSE.");
      break;
    }
    case 1: {
      ok(!document.mozFullScreen, "Should have left full-screen mode (first time)");
      is(event.target, document, "Event target should be document when we exit via removing from doc");
      is(document.mozFullScreenElement, null, "Full-screen element should be null.");
      iframe = document.createElement("iframe");
      iframe.mozAllowFullScreen = true;
      document.body.appendChild(iframe);
      iframe.src = iframeContents;
      break;
    }
    case 2: {
      ok(document.mozFullScreen, "Should be back in full-screen mode (second time)");
      is(event.target, iframe,
         "Event target should be full-screen element container");
      is(document.mozFullScreenElement, iframe,
        "Full-screen element should be iframe element.");
      var fse = fullScreenElement();
      fse.mozRequestFullScreen();
      ok(document.mozFullScreen, "Should still be full-screen mode after re-requesting.");
      is(document.mozFullScreenElement, fse, "Full-screen element should have switched to requestee.");
      var _innerFrame = iframe.contentDocument.getElementById("inner-frame");
      _innerFrame.contentDocument.body.appendChild(fse);
      ok(!document.mozFullScreen, "Should exit full-screen after transplanting FSE");
      is(document.mozFullScreenElement, null, "Full-screen element transplanted, should be null.");
      is(iframe.contentDocument.mozFullScreenElement, null, "Full-screen element in outer frame should be null.");
      is(_innerFrame.contentDocument.mozFullScreenElement, null, "Full-screen element in inner frame should be null.");
      ok(!iframe.contentDocument.mozFullScreen, "Outer frame should not acquire full-screen status.");
      ok(!_innerFrame.contentDocument.mozFullScreen, "Inner frame should not acquire full-screen status.");
      
      document.body.appendChild(fse);
      break;
    }
    case 3: {
      ok(!document.mozFullScreen, "Should be back in non-full-screen mode (second time)");
      is(event.target, document,
         "Event target should be full-screen element container");
      is(document.mozFullScreenElement, null, "Full-screen element should be null.");
      document.body.removeChild(iframe);
      iframe = null;
      outOfDocElement = document.createElement("div");
      outOfDocElement.mozRequestFullScreen();
      ok(!document.mozFullScreen, "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);
      
      inDocElement.mozRequestFullScreen();
      ok(document.mozFullScreen, "Should grant request to return to full-screen mode (third time)");
      break;
    }
    case 4: {
      ok(document.mozFullScreen, "Should still be in full-screen mode (third time)");
      is(event.target, inDocElement, "Event target should be inDocElement");
      is(document.mozFullScreenElement, inDocElement,
        "FSE should be inDocElement.");       
      
      var n = container;
      do {
        ok(n.mozMatchesSelector(":-moz-full-screen-ancestor"), "Ancestor " + n + " should match :-moz-full-screen-ancestor")
        n = n.parentNode;
      } while (n && n.mozMatchesSelector);
        
      // Remove full-screen ancestor element from document, verify it stops being reported as current FSE.
      container.parentNode.removeChild(container);
      ok(!document.mozFullScreen,
         "Should exit full-screen mode after removing full-screen element ancestor from document");
      is(document.mozFullScreenElement, null,
        "Should not have a full-screen element again.");
      break;
    }
    case 5: {
      ok(!document.mozFullScreen, "Should be back in non-full-screen mode (third time)");
      setRequireTrustedContext(true);
      fullscreendenied = false;
      fullScreenElement().mozRequestFullScreen();
      ok(!document.mozFullScreen, "Should still be in normal mode, because calling context isn't trusted.");

      setTimeout(
        function() {
          ok(fullscreendenied, "Request for fullscreen should have been denied because calling context isn't trusted");
          button = document.createElement("button");
          button.onclick = function(){fullScreenElement().mozRequestFullScreen();}
          fullScreenElement().appendChild(button);
          sendMouseClick(button);
        }, 0);
      break;
    }
    case 6: {
      ok(document.mozFullScreen, "Moved to full-screen after mouse click");
      document.mozCancelFullScreen();
      ok(document.mozFullScreen, "Should still be in full-screen mode, because calling context isn't trusted.");
      setRequireTrustedContext(false);
      document.mozCancelFullScreen();
      ok(!document.mozFullScreen, "Should have left full-screen mode.");
      break;
    }
    case 7: {
      ok(!document.mozFullScreen, "Should have left full-screen mode (last time).");

      SpecialPowers.setBoolPref("full-screen-api.enabled", false);
      is(document.mozFullScreenEnabled, false, "document.mozFullScreenEnabled should be false if full-screen-api.enabled is false");
      fullscreendenied = false;
      fullScreenElement().mozRequestFullScreen();
      setTimeout(
        function() {
          ok(!document.mozFullScreen, "Should still be in normal mode, because pref is not enabled.");

          SpecialPowers.setBoolPref("full-screen-api.enabled", true);
          is(document.mozFullScreenEnabled, true, "document.mozFullScreenEnabled should be true if full-screen-api.enabled is true");

          iframe = document.createElement("iframe");
          fullScreenElement().appendChild(iframe);
          iframe.src = iframeContents;
          ok(!document.mozFullScreen, "Should still be in normal mode, because iframe did not have mozallowfullscreen attribute.");
          fullScreenElement().removeChild(iframe);
          iframe = null;

          // Set timeout for calling finish(), so that any pending "mozfullscreenchange" events
          // would have a chance to fire.
          setTimeout(function(){opener.nextTest();}, 0);
        }, 0);
      break;
    }
    default: {
      ok(false, "Should not receive any more fullscreenchange events!");
    }
  }
  fullScreenChangeCount++;
}

document.addEventListener("mozfullscreenchange", fullScreenChange, false);

</script>
</pre>
<div id="full-screen-element"></div>
</body>
</html>