dom/events/test/test_dom_before_after_keyboard_event.html
author Andrea Marchesini <amarchesini@mozilla.com>
Thu, 17 Nov 2016 14:52:16 +0100
changeset 352791 e1a57d1dcfdb2ea11ce0829d2a2a2f11482ad3ff
parent 352649 55b599ca3530c8286f6ad15405d99d2bcaecfbd1
permissions -rw-r--r--
Bug 1315905 - Cleanup Necko http security check. r=valentin, a=gchang

<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Bug 989198</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/NativeKeyCodes.js"></script>
  <script type="text/javascript" src="bug989198_helper.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="runTests();">
<a target="_blank"
   href="https://bugzilla.mozilla.org/show_bug.cgi?id=989198">Mozilla Bug 989198</a>
<p id="display"></p>
<pre id="test">
<script type="application/javascript">

SimpleTest.waitForExplicitFinish();

function cleanupTest()
{
  teardownHandlers(window, embedderHandler);
  runTests();
}

function testEventOrderAndAttr()
{
  const kTests = [
    {
      description: "Testing the order of the events",
      expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
                          kChild | kKeyDownEvent,
                          kParent | kAfterEvent | kKeyDownEvent ],
                        [ kParent | kBeforeEvent | kKeyUpEvent,
                          kChild | kKeyUpEvent,
                          kParent | kAfterEvent | kKeyUpEvent ] ],
      resultEvents: [],
      classifiedEvents: [ [], [] ],
      doPreventDefaultAt: kUnknownEvent
    },
    {
      description: "Testing the order of the events, calling preventDefault() at \"mozbrowserbeforekeydown\" event",
      expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
                          kParent | kAfterEvent | kKeyDownEvent ],
                        [ kParent | kBeforeEvent | kKeyUpEvent,
                          kChild | kKeyUpEvent,
                          kParent | kAfterEvent | kKeyUpEvent ] ],
      resultEvents: [],
      classifiedEvents: [ [], [] ],
      doPreventDefaultAt: kParent | kBeforeEvent | kKeyDownEvent
    },
    {
      description: "Testing the order of the events, calling preventDefault() at \"mozbrowserbeforekeyup\" event",
      expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
                          kChild | kKeyDownEvent,
                          kParent | kAfterEvent | kKeyDownEvent ],
                        [ kParent | kBeforeEvent | kKeyUpEvent,
                          kParent | kAfterEvent | kKeyUpEvent ] ],
      resultEvents: [],
      classifiedEvents: [ [], [] ],
      doPreventDefaultAt: kParent | kBeforeEvent | kKeyUpEvent
    },
    {
      description: "Testing the order of the events, calling preventDefault() at \"keydown\" event",
      expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
                          kChild | kKeyDownEvent,
                          kParent | kAfterEvent | kKeyDownEvent ],
                        [ kParent | kBeforeEvent | kKeyUpEvent,
                          kChild | kKeyUpEvent,
                          kParent | kAfterEvent | kKeyUpEvent ] ],
      resultEvents: [],
      classifiedEvents: [ [], [] ],
      doPreventDefaultAt: kChild | kKeyDownEvent
    },
    {
      description: "Testing the order of the events, calling preventDefault() at \"keyup\" event",
      expectedEvents: [ [ kParent | kBeforeEvent | kKeyDownEvent,
                          kChild | kKeyDownEvent,
                          kParent | kAfterEvent | kKeyDownEvent ],
                        [ kParent | kBeforeEvent | kKeyUpEvent,
                          kChild | kKeyUpEvent,
                          kParent | kAfterEvent | kKeyUpEvent ] ],
      resultEvents: [],
      classifiedEvents: [ [], [] ],
      doPreventDefaultAt: kChild | kKeyUpEvent
    }
  ];

  for (var k = 0; k < kTests.length; k++ ) {
    gCurrentTest = kTests[k];
    synthesizeKey('a', {}, document.getElementById("embedded").contentWindow);
    classifyEvents(kTests[k]);
    verifyResults(kTests[k]);
  }

  runTests();
}

var tests = [
  function addPermissions() {
    SpecialPowers.pushPermissions(
      [{ type: "before-after-keyboard-event", allow: true, context: document },
       { type: "browser", allow: true, context: document }],
      runTests);
  },
  function addPreferences() {
    SpecialPowers.pushPrefEnv(
      { "set": [["dom.beforeAfterKeyboardEvent.enabled", true],
                ["dom.mozBrowserFramesEnabled", true],
                ["network.disable.ipc.security", true],
                ["dom.ipc.tabs.disabled", false]] },
      runTests);
  },

  // Tests for in-process iframe, i.e. <iframe mozbrowser>.
  function () {
    prepareTest(false);
  },
  testEventOrderAndAttr,
  cleanupTest,
];

function runTests()
{
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

</script>
</body>
</html>