dom/html/test/file_fullscreen-api-keys.html
author Xidorn Quan <quanxunzhen@gmail.com>
Thu, 21 May 2015 09:52:26 +1200
changeset 244793 8f57a2e8384b29139e14685e9329f69edf1c6e91
parent 212347 8ccc36e1d05cb871a3e6a0632fb2e0c652a9d58d
child 279299 1f5974ec0cf83c730dc4ba3592c2c12b865ddd67
permissions -rw-r--r--
Bug 1053413 part 0 - Fix of some fullscreen tests. r=smaug

 <!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=545812

Test that restricted key pressed drop documents out of DOM full-screen mode.

-->
<head>
  <title>Test for Bug 545812</title>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.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 **/

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

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

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

var gKeyTestIndex = 0;
var gKeyName;
var gKeyCode;
var gKeySuppressed;
var gKeyReceived = false;

function keyHandler(event) {
  gKeyReceived = true;
}

function checkKeyEffect() {
  is(gKeySuppressed, !gKeyReceived, "Should not receive key events for " + gKeyName);
  is(document.mozFullScreen, false, "Should exit full-screen mode for " + gKeyName + " press");
  if (gKeyTestIndex < keyList.length) {
    setTimeout(testNextKey, 0);
  } else {
    opener.nextTest();
  }
}

function testTrustedKeyEvents() {
  document.body.focus();
  gKeyReceived = false;
  addFullscreenChangeContinuation("exit", checkKeyEffect);
  synthesizeKey(gKeyName, {});
}

function testScriptInitiatedKeyEvents() {
  // Script initiated untrusted key events should not cause full-screen exit.
  document.body.focus();
  gKeyReceived = false;
  var evt = document.createEvent("KeyEvents");
  evt.initKeyEvent("keydown", true, true, window,
                   false, false, false, false,
                   gKeyCode, 0);
  document.body.dispatchEvent(evt);

  evt = document.createEvent("KeyEvents");
  evt.initKeyEvent("keypress", true, true, window,
                   false, false, false, false,
                   gKeyCode, 0);
  document.body.dispatchEvent(evt);

  evt = document.createEvent("KeyEvents");
  evt.initKeyEvent("keyup", true, true, window,
                   false, false, false, false,
                   gKeyCode, 0);
  document.body.dispatchEvent(evt);
  
  ok(gKeyReceived, "dispatchEvent should dispatch events synchronously");
  ok(document.mozFullScreen,
     "Should remain in full-screen mode for script initiated key events for " + gKeyName);
}

function testNextKey() {
  if (!document.mozFullScreen) {
    // Async request fullscreen to avoid the request be accidentally
    // cancelled due to unfinished clean-up of exiting fullscreen.
    setTimeout(() => {
      addFullscreenChangeContinuation("enter", reallyTestNextKey);
      document.body.mozRequestFullScreen();
    }, 0);
  }
  else {
    reallyTestNextKey();
  }
}

function reallyTestNextKey() {
  ok(document.mozFullScreen, "Must be in full-screen mode");

  gKeyName = keyList[gKeyTestIndex].code;
  gKeyCode = KeyEvent["DOM_" + gKeyName];
  gKeySuppressed = keyList[gKeyTestIndex].suppressed;
  gKeyTestIndex++;

  testScriptInitiatedKeyEvents();
  testTrustedKeyEvents();
}

window.addEventListener("keydown", keyHandler, true);
window.addEventListener("keyup", keyHandler, true);
window.addEventListener("keypress", keyHandler, true);

function begin() {
  testNextKey();
}
</script>
</pre>
</body>
</html>