Bug 962251 - Test case, r=smaug
authorKershaw Chang <kechang@mozilla.com>
Wed, 30 Mar 2016 00:54:00 +0200
changeset 291141 31397f0f3154c2d609155d99339a7625a50eafa4
parent 291140 60a67e654ae703d0f2584a76778e8571b9598770
child 291142 d5a9ff0ae110c5b5fda716b5aa70a21ed1bc1cf3
push id74486
push usercbook@mozilla.com
push dateFri, 01 Apr 2016 07:48:23 +0000
treeherdermozilla-inbound@90312ff603de [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs962251
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 962251 - Test case, r=smaug
dom/base/test/iframe_bug962251.html
dom/base/test/mochitest.ini
dom/base/test/test_bug962251.html
new file mode 100644
--- /dev/null
+++ b/dom/base/test/iframe_bug962251.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+<head>
+</head>
+<body>
+<input id="textinput" type="text"></input>
+<input id="textinput1" type="text"></input>
+</body>
+<script>
+  var SimpleTest = parent.SimpleTest;
+
+  var ok = SimpleTest.ok;
+  var info = SimpleTest.info;
+  var is = SimpleTest.is;
+  var finish = SimpleTest.finish.bind(SimpleTest);
+
+  var input = document.getElementById("textinput");
+  input.addEventListener("focus", function onFocus(aEvent) {
+    input.removeEventListener("focus", onFocus);
+    is(aEvent.target.id, "textinput", "Input should be focused.");
+    ok(aEvent.relatedTarget === null, "The relatedTarget should be null.");
+    parent.postMessage("runNextTest", "*");
+  });
+  input.focus();
+</script>
+</html>
\ No newline at end of file
--- a/dom/base/test/mochitest.ini
+++ b/dom/base/test/mochitest.ini
@@ -1,11 +1,12 @@
 [DEFAULT]
 support-files =
   audio.ogg
+  iframe_bug962251.html
   iframe_bug976673.html
   iframe_main_bug1022229.html
   iframe_sandbox_bug1022229.html
   file_empty.html
   iframe_postMessage_solidus.html
   file_setname.html
   345339_iframe.html
   Ahem.ttf
@@ -891,8 +892,9 @@ skip-if = buildapp == 'b2g' #no ssl supp
 [test_bug1198095.html]
 [test_bug1187157.html]
 [test_bug769117.html]
 [test_bug1250148.html]
 [test_bug1240471.html]
 [test_mozbrowser_apis_allowed.html]
 [test_mozbrowser_apis_blocked.html]
 [test_document_register.html]
+[test_bug962251.html]
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_bug962251.html
@@ -0,0 +1,258 @@
+
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=962251
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 962251</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript">
+
+  SimpleTest.waitForExplicitFinish();
+
+  function Test_ElementsInTheSameDocument() {
+    var button1 = document.getElementById("button1");
+    var button2 = document.getElementById("button2");
+
+    button1.focus();
+    is(button1.id, document.activeElement.id, "How did we call focus on button1 and it did" +
+                                              " not become the active element?");
+
+    var getBlurEvent = false;
+    button1.addEventListener("blur", function onBlur(aEvent) {
+      button1.removeEventListener("blur", onBlur);
+      is(aEvent.target.id, "button1", "Button1 should lose focus.");
+      ok(aEvent.relatedTarget, "The relatedTarget should not be null.");
+      is(aEvent.relatedTarget.id, "button2", "The relatedTarget should be button2.");
+      getBlurEvent = true;
+    });
+
+    button2.addEventListener("focus", function onFocus(aEvent) {
+      button2.removeEventListener("focus", onFocus);
+      ok(getBlurEvent, "Must get blur event first.");
+      is(aEvent.target.id, "button2", "Button2 should be focused.");
+      ok(aEvent.relatedTarget, "The relatedTarget should not be null.");
+      is(aEvent.relatedTarget.id, "button1", "The relatedTarget should be button1.");
+      button2.blur();
+    });
+
+    button2.addEventListener("blur", function onBlur(aEvent) {
+      button2.removeEventListener("blur", onBlur);
+      is(aEvent.target.id, "button2", "Button2 should lose focus.");
+      ok(aEvent.relatedTarget === null, "The relatedTarget should be null.");
+      runTests();
+    });
+
+    button2.focus();
+  }
+
+  function Test_ElementsInDifferentDocument() {
+    var button2 = document.getElementById("button2");
+    button2.focus();
+    button2.addEventListener("blur", function onBlur(aEvent) {
+      button2.removeEventListener("blur", onBlur);
+      is(aEvent.target.id, "button2", "Button2 should lose focus.");
+      ok(aEvent.relatedTarget === null, "The relatedTarget should be null, since it's in another document.");
+    });
+
+    var iframe = document.createElement("iframe");
+    iframe.id = "iframe";
+    iframe.src = "iframe_bug962251.html";
+    window.addEventListener("message", function onMessage(aEvent) {
+      window.removeEventListener("message", onMessage);
+      if (aEvent.data == "runNextTest") {
+        runTests();
+      }
+    });
+    document.getElementById("content").appendChild(iframe);
+  }
+
+  function Test_FocusEventOnWindow() {
+    var iframe1 = document.createElement("iframe");
+    iframe1.id = "iframe1";
+    iframe1.src = "about:blank";
+
+    document.getElementById("content").appendChild(iframe1);
+    document.getElementById("button2").focus();
+    var iframe = document.getElementById("iframe");
+    var expectedEventTarget = [iframe.contentDocument, iframe.contentWindow];
+    var expectedEventTarget1 = [iframe1.contentDocument, iframe1.contentWindow];
+    iframe.contentWindow.addEventListener("focus", function onFocus(aEvent) {
+      var eventTarget = expectedEventTarget.shift();
+      ok(aEvent.target === eventTarget, "Get expected focus event target.");
+      ok(aEvent.relatedTarget === null, "relatedTarget should be null.");
+      if (!expectedEventTarget.length) {
+        iframe.contentWindow.removeEventListener("focus", onFocus, true);
+        runTests();
+      }
+    }, true);
+    iframe1.contentWindow.addEventListener("focus", function onFocus(aEvent) {
+      var eventTarget = expectedEventTarget1.shift();
+      ok(aEvent.target === eventTarget, "Get expected focus event target.");
+      ok(aEvent.relatedTarget === null, "relatedTarget should be null.");
+      if (!expectedEventTarget1.length) {
+        iframe1.contentWindow.removeEventListener("focus", onFocus, true);
+        // Append items for blur event listener
+        expectedEventTarget1.push(iframe1.contentDocument);
+        expectedEventTarget1.push(iframe1.contentWindow);
+        iframe.contentWindow.focus();
+      }
+    }, true);
+    iframe1.contentWindow.addEventListener("blur", function onBlur(aEvent) {
+      var eventTarget = expectedEventTarget1.shift();
+      ok(aEvent.target === eventTarget, "Get expected blur event target.");
+      ok(aEvent.relatedTarget === null, "relatedTarget should be null.");
+      if (!expectedEventTarget1.length) {
+        iframe1.contentWindow.removeEventListener("blur", onBlur, true);
+      }
+    }, true);
+    iframe1.contentWindow.focus();
+  }
+
+  function Test_SetFocusInBlurEvent() {
+    var button1 = document.getElementById("button1");
+    var button2 = document.getElementById("button2");
+    var button3 = document.getElementById("button3");
+
+    button1.focus();
+    is(button1.id, document.activeElement.id, "document.activeElement.id is button1");
+
+    button1.addEventListener("blur", function onBlur(aEvent) {
+      button1.removeEventListener("blur", onBlur);
+      info("button1 blur");
+      is(aEvent.relatedTarget.id, button2.id, "relatedTarget.id should be button2.");
+      button3.focus();
+    });
+    button1.addEventListener("focus", function onFocus(aEvent) {
+      button1.removeEventListener("focus", onFocus);
+      info("button1 focus");
+    });
+
+    button2.addEventListener("blur", function onBlur(aEvent) {
+      button2.removeEventListener("blur", onBlur);
+      info("button2 blur");
+    });
+    button2.addEventListener("focus", function onFocus(aEvent) {
+      button2.removeEventListener("focus", onFocus);
+      info("button2 focus");
+    });
+
+    button3.addEventListener("blur", function onBlur(aEvent) {
+      button3.removeEventListener("blur", onBlur);
+      info("button3 blur");
+    });
+    button3.addEventListener("focus", function onFocus(aEvent) {
+      button3.removeEventListener("focus", onFocus);
+      info("button3 focus");
+      ok(aEvent.relatedTarget === null, "aEvent.relatedTarget is null.");
+      runTests();
+    });
+
+    button2.focus();
+  }
+
+  function Test_ListenFocusBlurEventOnWindow1() {
+    var button2 = document.getElementById("button2");
+    button2.focus();
+
+    var iframe = document.getElementById("iframe");
+    var input = iframe.contentDocument.getElementById("textinput");
+    var expectedEventTarget = [button2, document, window];
+    var expectedEventTarget1 = [iframe.contentDocument, iframe.contentWindow, input];
+    window.addEventListener("blur", function onBlur(aEvent) {
+      var item = expectedEventTarget.shift();
+      ok(aEvent.target === item, "Get an expected blur event.");
+      ok(aEvent.relatedTarget === null, "relatedTarget should be null.");
+      if (!expectedEventTarget.length) {
+        iframe.contentWindow.removeEventListener("blur", onBlur, true);
+      }
+    }, true);
+    iframe.contentWindow.addEventListener("focus", function onFocus(aEvent) {
+      var item = expectedEventTarget1.shift();
+      ok(aEvent.target === item, "Get an expected focus event.");
+      ok(aEvent.relatedTarget === null, "relatedTarget should be null.");
+      if (!expectedEventTarget1.length) {
+        iframe.contentWindow.removeEventListener("focus", onFocus, true);
+        runTests();
+      }
+    }, true);
+
+    input.focus();
+  }
+
+  function Test_ListenFocusBlurEventOnWindow2() {
+    var iframe = document.getElementById("iframe");
+    var input = iframe.contentDocument.getElementById("textinput");
+    var input1 = iframe.contentDocument.getElementById("textinput1");
+
+    ok(iframe.contentDocument.activeElement === input, "Current focused element should be input.");
+    iframe.contentWindow.addEventListener("focus", function onFocus(aEvent) {
+      iframe.contentWindow.removeEventListener("focus", onFocus, true);
+      ok(aEvent.target === input1, "Input1 is focused.");
+      ok(aEvent.relatedTarget === input, "relatedTarget should be input.");
+      runTests();
+    }, true);
+    iframe.contentWindow.addEventListener("blur", function onBlur(aEvent) {
+      iframe.contentWindow.removeEventListener("blur", onBlur, true);
+      ok(aEvent.target === input, "Input is not focused.");
+      ok(aEvent.relatedTarget === input1, "relatedTarget should be input1.");
+    }, true);
+
+    input1.focus();
+  }
+
+  function Test_ListenFocusBlurEventOnWindow3() {
+    var iframe = document.getElementById("iframe");
+    var input1 = iframe.contentDocument.getElementById("textinput1");
+
+    ok(iframe.contentDocument.activeElement === input1, "Current focused element should be input1.");
+    iframe.contentWindow.addEventListener("blur", function onBlur(aEvent) {
+      iframe.contentWindow.removeEventListener("blur", onBlur, true);
+      ok(aEvent.target === input1, "Input1 is not focused.");
+      ok(aEvent.relatedTarget === null, "relatedTarget should be null.");
+      runTests();
+    }, true);
+
+    input1.blur();
+  }
+
+  var tests = [
+    Test_ElementsInTheSameDocument,
+    Test_ElementsInDifferentDocument,
+    Test_FocusEventOnWindow,
+    Test_SetFocusInBlurEvent,
+    Test_ListenFocusBlurEventOnWindow1,
+    Test_ListenFocusBlurEventOnWindow2,
+    Test_ListenFocusBlurEventOnWindow3
+  ];
+
+  function runTests()
+  {
+    if (!tests.length) {
+      SimpleTest.finish();
+      return;
+    }
+
+    var test = tests.shift();
+    window.setTimeout(function () {
+      test();
+    });
+  }
+
+  </script>
+</head>
+<body onload="runTests();">
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=962251">Mozilla Bug 962251</a>
+<p id="display"></p>
+<div id="content">
+  <button id="button1">1</button>
+  <button id="button2">2</button>
+  <button id="button3">3</button>
+</div>
+<pre id="test">
+</pre>
+</body>
+</html>
\ No newline at end of file