[Pkg-mozext-commits] [requestpolicy] 35/100: add first Mozmill unit tests

David Prévot taffit at moszumanska.debian.org
Fri Dec 12 22:56:53 UTC 2014


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

taffit pushed a commit to branch master
in repository requestpolicy.

commit 7d4cac431160bca9ca6592b2e1044617a3167146
Author: myrdd <myrdd at users.noreply.github.com>
Date:   Fri Sep 26 23:53:40 2014 +0200

    add first Mozmill unit tests
---
 tests/link_1.html                                  |   2 +-
 tests/mozmill/manifest.ini                         |   4 +
 tests/mozmill/tests/manifest.ini                   |   3 +
 tests/mozmill/tests/testLinkClicks/manifest.ini    |   4 +
 .../tests/testLinkClicks/testOpenInNewTab.js       | 110 ++++++++++++++++
 .../tests/testLinkClicks/testOpenInNewWindow.js    | 146 +++++++++++++++++++++
 6 files changed, 268 insertions(+), 1 deletion(-)

diff --git a/tests/link_1.html b/tests/link_1.html
index 458d2e4..4614ed4 100644
--- a/tests/link_1.html
+++ b/tests/link_1.html
@@ -4,7 +4,7 @@
 <body>
 
 <div style="margin : 100px 0 0 100px">
-<a href="http://www.otherdomain.test/cross-site-link">Cross-site link</a>
+<a href="http://www.otherdomain.test/destination.html">Cross-site link</a>
 </div>
 
 <br /><br />
diff --git a/tests/mozmill/manifest.ini b/tests/mozmill/manifest.ini
new file mode 100644
index 0000000..6975b74
--- /dev/null
+++ b/tests/mozmill/manifest.ini
@@ -0,0 +1,4 @@
+[DEFAULT]
+server-root = ../../../../data
+
+[include:tests/manifest.ini]
diff --git a/tests/mozmill/tests/manifest.ini b/tests/mozmill/tests/manifest.ini
new file mode 100644
index 0000000..f857451
--- /dev/null
+++ b/tests/mozmill/tests/manifest.ini
@@ -0,0 +1,3 @@
+[parent:../manifest.ini]
+
+[include:testLinkClicks/manifest.ini]
diff --git a/tests/mozmill/tests/testLinkClicks/manifest.ini b/tests/mozmill/tests/testLinkClicks/manifest.ini
new file mode 100644
index 0000000..3d0e073
--- /dev/null
+++ b/tests/mozmill/tests/testLinkClicks/manifest.ini
@@ -0,0 +1,4 @@
+[parent:../manifest.ini]
+
+[testOpenInNewTab.js]
+[testOpenInNewWindow.js]
diff --git a/tests/mozmill/tests/testLinkClicks/testOpenInNewTab.js b/tests/mozmill/tests/testLinkClicks/testOpenInNewTab.js
new file mode 100644
index 0000000..94baddf
--- /dev/null
+++ b/tests/mozmill/tests/testLinkClicks/testOpenInNewTab.js
@@ -0,0 +1,110 @@
+/* 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";
+
+var {assert, expect} = require("../../../../../../lib/assertions");
+var prefs = require("../../../../../lib/prefs");
+var tabs = require("../../../../../lib/tabs");
+var utils = require("../../../../../../lib/utils");
+
+const PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow";
+
+const REDIRECT_NOTIFICATION_VALUE = "request-policy-meta-redirect";
+const TEST_URL = "http://www.maindomain.test/link_1.html";
+
+
+var setupModule = function(aModule) {
+  aModule.controller = mozmill.getBrowserController();
+
+  aModule.tabBrowser = new tabs.tabBrowser(aModule.controller);
+  aModule.tabBrowser.closeAllTabs();
+
+  prefs.preferences.setPref(PREF_DEFAULT_ALLOW, false);
+}
+
+var teardownModule = function(aModule) {
+  prefs.preferences.clearUserPref(PREF_DEFAULT_ALLOW);
+  utils.closeContentAreaContextMenu(aModule.controller);
+  aModule.tabBrowser.closeAllTabs();
+}
+
+
+var testOpenInBackgroundTab = function() {
+  controller.open(TEST_URL);
+  controller.waitForPageLoad();
+
+  let link = getLink();
+  let linkURL = link.getNode().href;
+
+  let i = 1;
+  while (true === openNextTab(i, link)) {
+    // Check that i+1 tabs are open
+    assert.waitFor(function () {
+      return tabBrowser.length === (i + 1);
+    }, "Tab " + (i + 1) + " opened.");
+    ++i;
+  }
+
+  assertCorrectLocations(linkURL);
+  assertNoRedirects();
+}
+
+
+/**
+ * @return {MozMillElement} The link to click on.
+ */
+var getLink = function() {
+  let links = controller.window.content.document.getElementsByTagName("a");
+  assert.notEqual(links.length, 0, "A link has been found on the test page.");
+
+  return findElement.Elem(links[0]);
+}
+
+/**
+ * Opens the next tab.
+ * @return {boolean}
+ *         true if a new tab has been opened.
+ *         false if no tab needs to open anymore.
+ */
+var openNextTab = function(i, link) {
+  switch (i) {
+    case 1:
+      // Open another tab by middle-clicking on the link
+      tabBrowser.openTab({method: "middleClick", target: link});
+      return true;
+      break;
+
+    case 2:
+      // Open link via context menu in a new tab:
+      tabBrowser.openTab({method: "contextMenu", target: link});
+      return true;
+      break;
+
+    default:
+      return false;
+      break;
+  }
+}
+
+var assertCorrectLocations = function(linkURL) {
+  for (let index = 1; index < tabBrowser.length; ++index) {
+    tabBrowser.selectedIndex = index;
+    assert.equal(controller.tabs.activeTab.location.href, linkURL,
+        "The location in the new tab is correct.");
+  }
+}
+
+
+/**
+ * Assert that the link clicks have not been detected as redirects.
+ */
+var assertNoRedirects = function() {
+  for (let index = 0; index < tabBrowser.length; ++index) {
+    var panel = tabBrowser.getTabPanelElement(index,
+        '/{"value":"' + REDIRECT_NOTIFICATION_VALUE + '"}');
+    assert.ok(false === panel.exists(),
+        "Following the link didn't cause a redirect");
+  }
+}
diff --git a/tests/mozmill/tests/testLinkClicks/testOpenInNewWindow.js b/tests/mozmill/tests/testLinkClicks/testOpenInNewWindow.js
new file mode 100644
index 0000000..8f8e4aa
--- /dev/null
+++ b/tests/mozmill/tests/testLinkClicks/testOpenInNewWindow.js
@@ -0,0 +1,146 @@
+/* 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";
+
+var {assert, expect} = require("../../../../../../lib/assertions");
+var prefs = require("../../../../../lib/prefs");
+var tabs = require("../../../../../lib/tabs");
+var utils = require("../../../../../../lib/utils");
+
+const PREF_DEFAULT_ALLOW = "extensions.requestpolicy.defaultPolicy.allow";
+
+const REDIRECT_NOTIFICATION_VALUE = "request-policy-meta-redirect";
+const TEST_URL = "http://www.maindomain.test/link_1.html";
+
+
+var setupModule = function(aModule) {
+  aModule.controller = mozmill.getBrowserController();
+  aModule.otherController = null;
+
+  aModule.tabBrowser = new tabs.tabBrowser(aModule.controller);
+  aModule.tabBrowser.closeAllTabs();
+  aModule.otherTabBrowser = null;
+
+  prefs.preferences.setPref(PREF_DEFAULT_ALLOW, false);
+};
+
+var teardownModule = function(aModule) {
+  prefs.preferences.clearUserPref(PREF_DEFAULT_ALLOW);
+  utils.closeContentAreaContextMenu(aModule.controller);
+  aModule.tabBrowser.closeAllTabs();
+
+  closeWindow(aModule.otherController);
+};
+
+var testOpenInNewWindow = function () {
+  controller.open(TEST_URL);
+  controller.waitForPageLoad();
+
+  let link = getLink();
+  let linkURL = link.getNode().href;
+
+  let i = 1;
+  while (true === openNextWindow(i, link)) {
+    assert.waitFor(function () {
+      // Make sure that we work on the correct window
+      var windows = mozmill.utils.getWindows("navigator:browser");
+      for (var j = 0; j < windows.length; ++j) {
+        if (windows[j] !== controller.window) {
+          otherController = new mozmill.controller.MozMillController(windows[j]);
+          otherTabBrowser = new tabs.tabBrowser(otherController);
+          break;
+        }
+      }
+
+      return !!otherController;
+    }, "Newly opened browser window has been found.");
+
+    // sleep as a workaround because the "busy" attribute is not present
+    // immediately.
+    controller.sleep(100);
+
+    assert.waitFor(function() {
+      return !otherTabBrowser.getTab(0).getNode().hasAttribute("busy");
+    }, "The tab has loaded");
+
+    assert.equal(otherController.tabs.activeTab.location.href, linkURL,
+        "The location in the new window is correct.");
+
+    assertNoRedirects();
+    closeWindow(otherController);
+    otherController = null;
+    otherTabBrowser = null;
+    ++i;
+  }
+};
+
+var closeWindow = function(_controller) {
+  if (_controller && _controller.window) {
+    _controller.window.close();
+  }
+};
+
+
+/**
+ * @return {MozMillElement} The link to click on.
+ */
+var getLink = function() {
+  let links = controller.window.content.document.getElementsByTagName("a");
+  assert.notEqual(links.length, 0, "A link has been found on the test page.");
+
+  return findElement.Elem(links[0]);
+};
+
+/**
+ * Opens the next tab.
+ * @return {boolean}
+ *         true if a new tab has been opened.
+ *         false if no tab needs to open anymore.
+ */
+var openNextWindow = function(i, link) {
+  var contextMenuItemID;
+
+  switch (i) {
+    case 1:
+      // Context Menu: Open Link in New Window
+      contextMenuItemID = "#context-openlink";
+      break;
+
+    case 2:
+      // Context Menu: Open Link in New Private Window
+      contextMenuItemID = "#context-openlinkprivate";
+      break;
+
+    default:
+      return false;
+      break;
+  }
+
+  // There seems to be a bug in Mozmill: the right-click is not performed the
+  // second time. The workaround seems to be to wait for some miliseconds before
+  // continuing. I set the timeout to 100 ms. If this time is still not enough
+  // for anybody, simply increase it.
+  // It seemed that this bug has to do with the focus. However, the
+  // HTMLElement.blur() function doesn't work for <a> Elements.
+  controller.sleep(100);
+
+  var contextMenu = controller.getMenu("#contentAreaContextMenu");
+  contextMenu.select(contextMenuItemID, link);
+  return true;
+};
+
+/**
+ * Assert that the link clicks have not been detected as redirects.
+ */
+var assertNoRedirects = function() {
+  var tabBrowsers = [tabBrowser, otherTabBrowser];
+
+  for (let i = 0; i < tabBrowsers.length; ++i) {
+    var panel = tabBrowsers[i].getTabPanelElement(0,
+        '/{"value":"' + REDIRECT_NOTIFICATION_VALUE + '"}');
+    assert.ok(false === panel.exists(),
+        "Following the link didn't cause a redirect");
+  }
+};

-- 
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