Bug 1276738 - Test that named windows work properly. r?gabor draft
authorMike Conley <mconley@mozilla.com>
Mon, 30 May 2016 17:26:52 -0400
changeset 374029 cf0dc1575e77b4e792e99cc7ec10e089900950c9
parent 374028 677e38e8f04be88fcb377409777e113dac44bd8b
child 374030 0a29121f97d410e8743040a309f9b7ddfb2f6c29
push id19904
push usermconley@mozilla.com
push dateWed, 01 Jun 2016 17:55:36 +0000
reviewersgabor
bugs1276738
milestone49.0a1
Bug 1276738 - Test that named windows work properly. r?gabor MozReview-Commit-ID: 80uzqBvPmOA
embedding/components/windowwatcher/test/mochitest.ini
embedding/components/windowwatcher/test/moz.build
embedding/components/windowwatcher/test/test_named_window.html
new file mode 100644
--- /dev/null
+++ b/embedding/components/windowwatcher/test/mochitest.ini
@@ -0,0 +1,4 @@
+[DEFAULT]
+tags = openwindow
+
+[test_named_window.html]
--- a/embedding/components/windowwatcher/test/moz.build
+++ b/embedding/components/windowwatcher/test/moz.build
@@ -3,8 +3,11 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 BROWSER_CHROME_MANIFESTS += [
     'browser.ini',
 ]
 
+MOCHITEST_MANIFESTS += [
+    'mochitest.ini',
+]
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/embedding/components/windowwatcher/test/test_named_window.html
@@ -0,0 +1,92 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+Test that when content opens a new window with a name, that the
+newly opened window actually gets that name, and that subsequent
+attempts to open a window with that name will target the same
+window.
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test named windows</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <script src="head.js" type="application/javascript;version=1.8"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+  <a href="#" id="link">Click me</a>
+
+  <script type="application/javascript">
+    "use strict";
+
+    const NAME = "my_window";
+    const TARGET_URL = "data:text/html,<html><body>test_named_window.html new window</body></html>";
+    const TARGET_URL_2 = TARGET_URL + "#2";
+    const TARGET_URL_3 = TARGET_URL + "#3";
+
+    /**
+     * Returns a Promise that resolves once some target has had
+     * some event dispatched on it.
+     *
+     * @param target
+     *        The thing to wait for the event to be dispatched
+     *        through.
+     * @param eventName
+     *        The name of the event to wait for.
+     * @returns Promise
+     */
+    function promiseEvent(target, eventName) {
+      return new Promise(resolve => {
+        target.addEventListener(eventName, function onEvent(e) {
+          target.removeEventListener(eventName, onEvent, true);
+          resolve(e);
+        }, true);
+      });
+    }
+
+    add_task(function*() {
+      // This magic value of 2 means that by default, when content tries
+      // to open a new window, it'll actually open in a new window instead
+      // of a new tab.
+      yield SpecialPowers.pushPrefEnv({"set": [
+        ["browser.link.open_newwindow", 2],
+      ]});
+
+      let win1 = window.open(TARGET_URL, "my_window");
+      yield promiseEvent(win1, "load");
+
+      let name = SpecialPowers.wrap(win1)
+                 .QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
+                 .getInterface(SpecialPowers.Ci.nsIWebNavigation)
+                 .QueryInterface(SpecialPowers.Ci.nsIDocShellTreeItem)
+                 .name;
+
+      is(name, NAME, "Should have the expected name");
+      is(win1.location.href, new URL(TARGET_URL).href,
+         "Should have loaded target TARGET_URL in the original window");
+
+      let hashChange = promiseEvent(win1, "hashchange");
+      let win2 = window.open(TARGET_URL_2, "my_window");
+      yield hashChange;
+
+      is(win1, win2, "Should have gotten back the same window");
+      is(win1.location.href, new URL(TARGET_URL_2).href,
+         "Should have re-targeted pre-existing window");
+
+      hashChange = promiseEvent(win1, "hashchange");
+      let link = document.getElementById("link");
+      link.setAttribute("target", NAME);
+      link.setAttribute("href", TARGET_URL_3);
+      link.click();
+
+      yield hashChange;
+
+      is(win1.location.href, new URL(TARGET_URL_3).href,
+         "Should have re-targeted pre-existing window");
+
+      win1.close();
+    });
+  </script>
+</body>
+</html>
\ No newline at end of file