[Pkg-mozext-commits] [requestpolicy] 215/280: mozmill: test redirects in new tabs

David Prévot taffit at moszumanska.debian.org
Sat May 2 20:30:30 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository requestpolicy.

commit e267f67520e0d088ca0ac70dbd2270a7ae06a5a7
Author: Martin Kimmerle <dev at 256k.de>
Date:   Tue Feb 10 16:04:09 2015 +0100

    mozmill: test redirects in new tabs
    
    This commit adds `testLinkClickRedirectInNewTab.js`.
---
 tests/mozmill/lib/rp-utils.js                      |  49 ++++++++-
 tests/mozmill/tests/testRedirect/manifest.ini      |   1 +
 .../testRedirect/testLinkClickRedirectInNewTab.js  | 117 +++++++++++++++++++++
 3 files changed, 164 insertions(+), 3 deletions(-)

diff --git a/tests/mozmill/lib/rp-utils.js b/tests/mozmill/lib/rp-utils.js
index aa0d9dd..e16c0f1 100644
--- a/tests/mozmill/lib/rp-utils.js
+++ b/tests/mozmill/lib/rp-utils.js
@@ -4,6 +4,8 @@
 
 "use strict";
 
+/* global findElement, assert */
+
 /**
  * @param {MozMillController} _controller
  * @param {ElemBase} tab
@@ -28,7 +30,7 @@ function selectText(_tabBrowser, text) {
 
 function getElementById(ancestor, id) {
   var element = ancestor.getElementById(id);
-  assert.ok(element, 'element #' + id + ' has been found.');
+  assert.ok(element, "element #" + id + " has been found.");
   return element;
 }
 
@@ -36,20 +38,61 @@ function getElementById(ancestor, id) {
  * @param {MozMillController} _controller
  * @return {MozMillElement} The link to click on.
  */
-function getLink(_controller, i) {
-  i = i || 0;
+function getLink(_controller, i = 0) {
   let links = _controller.window.content.document.getElementsByTagName("a");
   assert.ok(links.length >= i, "The page contains at least " + i + " links.");
   return findElement.Elem(links[i]);
 }
 
+/**
+ * @param {MozMillController} _controller
+ * @return {Array.<MozMillElement>} The links on the page
+ */
+function getLinks(_controller) {
+  let linksHtmlCollection = _controller.window.content.document
+      .getElementsByTagName("a");
+  let linksArray = Array.prototype.slice.call(linksHtmlCollection);
+  return linksArray.map(l => findElement.Elem(l));
+}
+
 function getNumLinks(_controller) {
   return _controller.window.content.document.getElementsByTagName("a").length;
 }
 
+/**
+ * Create all combinations of a two-dimensional array.
+ *
+ * Example:
+ * input: [[1,2],["a"],[5,6]]
+ * output: [[1,"a",5], [1,"a",6], [2,"a",5], [2,"a",6]]
+ */
+function combinations(list) {
+  var prefixes, tailCombinations;
+
+  if(list.length === 1) {
+    return list[0];
+  }
+
+  prefixes = list[0];
+  // recursively call `combinations()` on the tail of the array
+  tailCombinations = combinations(list.slice(1));
+
+  var combined = [];
+
+  prefixes.forEach(function(prefix) {
+    tailCombinations.forEach(function(tailCombination) {
+      combined.push([prefix].concat(tailCombination));
+    });
+  });
+
+  return combined;
+}
+
 // Export of functions
 exports.waitForTabLoad = waitForTabLoad;
 exports.selectText = selectText;
 exports.getElementById = getElementById;
 exports.getLink = getLink;
+exports.getLinks = getLinks;
 exports.getNumLinks = getNumLinks;
+exports.combinations = combinations;
diff --git a/tests/mozmill/tests/testRedirect/manifest.ini b/tests/mozmill/tests/testRedirect/manifest.ini
index f432235..edf37b8 100644
--- a/tests/mozmill/tests/testRedirect/manifest.ini
+++ b/tests/mozmill/tests/testRedirect/manifest.ini
@@ -2,4 +2,5 @@
 
 [testAutoRedirect.js]
 [testLinkClickRedirect.js]
+[testLinkClickRedirectInNewTab.js]
 #[testInlineRedirect.js]
diff --git a/tests/mozmill/tests/testRedirect/testLinkClickRedirectInNewTab.js b/tests/mozmill/tests/testRedirect/testLinkClickRedirectInNewTab.js
new file mode 100644
index 0000000..9271fca
--- /dev/null
+++ b/tests/mozmill/tests/testRedirect/testLinkClickRedirectInNewTab.js
@@ -0,0 +1,117 @@
+/* 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/. */
+
+"use strict";
+
+/* global require, mozmill */
+/* exported setupModule, teardownModule, testLinkClickRedirectInNewTab */
+
+var rpRootDir = "../../";
+var rpConst = require(rpRootDir + "lib/constants");
+var rootDir = rpRootDir + rpConst.mozmillTestsRootDir;
+
+var {assert, expect} = require(rootDir + "lib/assertions");
+var prefs = require(rootDir + "lib/prefs");
+var tabs = require(rootDir + "firefox/lib/tabs");
+
+var rpUtils = require(rpRootDir + "lib/rp-utils");
+
+var testURLs = [
+  // fixme: as soon as #599 is fixed, uncomment the following line:
+  //"http://www.maindomain.test/redirect-js-document-location-link.html",
+  "http://www.maindomain.test/redirect-links.html"
+];
+
+
+var setupModule = function(aModule) {
+  /* global controller, tabBrowser */
+  aModule.controller = mozmill.getBrowserController();
+  aModule.tabBrowser = new tabs.tabBrowser(aModule.controller);
+  aModule.tabBrowser.closeAllTabs();
+
+  prefs.setPref(rpConst.PREF_DEFAULT_ALLOW, false);
+};
+
+var teardownModule = function(aModule) {
+  prefs.clearUserPref(rpConst.PREF_DEFAULT_ALLOW);
+  aModule.tabBrowser.closeAllTabs();
+}
+
+
+function Link(mozmillElement) {
+  this.element = mozmillElement;
+  this.node = this.element.getNode();
+
+  let classNode = this.node.attributes.class;
+  this.redirectShouldBeAllowed = classNode ?
+      classNode.nodeValue.indexOf("redirectShouldBeAllowed") >= 0 : false;
+
+  this.href = this.node.href;
+}
+
+
+var testLinkClickRedirectInNewTab = function() {
+
+  testURLs.forEach(function(testURL) {
+    controller.open(testURL);
+    controller.waitForPageLoad();
+
+    // Create an array of Link() objects, one for each <a>
+    // anchor element on the page.
+    let links = rpUtils.getLinks(controller)
+                       .map((l) => new Link(l));
+
+    assert.ok(links.length > 0,
+              "There is at least one link on test page '" + testURL + "'");
+
+    // Test all links by opening them both via context menu and via
+    // middle click.
+    let testCombinations = rpUtils.combinations([
+      links,
+      ["contextMenu", "middleClick"]
+    ]);
+
+    testCombinations.forEach(function([link, tabOpenMethod], curIndex) {
+      let curTabIndex = curIndex + 1;
+
+      openLinkInTab(tabBrowser, link, tabOpenMethod);
+
+      rpUtils.waitForTabLoad(controller, tabBrowser.getTab(curTabIndex));
+
+      expect.ok(!panelExists(0), "The redirect notification bar is not " +
+                "displayed on the main tab after opening");
+
+      if (link.redirectShouldBeAllowed) {
+        // fixme: find a better waitFor-function that ensures that the part of
+        //        RP which is responsible for showing the panel *really* has
+        //        finished.
+        controller.waitFor(function() {
+            return controller.window.content.document.location.href !== link.href;
+        }, "The URL in the urlbar has changed.");
+
+        expect.ok(!panelExists(curTabIndex),
+                  "The redirect notification bar is hidden.");
+      } else {
+        controller.waitFor(
+            () => panelExists(curTabIndex),
+            "The redirect notification bar has been displayed " +
+            "for '" + link.href + "' on '" + testURL + "' with tab " +
+            "open method '" + tabOpenMethod + "'.");
+      }
+    });
+
+    tabBrowser.closeAllTabs();
+  });
+};
+
+function panelExists(index = 0) {
+  let panel = tabBrowser.getTabPanelElement(index,
+      '/{"value":"' + rpConst.REDIRECT_NOTIFICATION_VALUE + '"}');
+  return panel.exists();
+}
+
+
+function openLinkInTab(tabBrowser, link, tabOpenMethod="contextMenu") {
+  tabBrowser.openTab({method: tabOpenMethod, target: link.element});
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git



More information about the Pkg-mozext-commits mailing list