Bug 1246115 - Re-write and re-enable regression test. r?Gijs draft
authorMike Conley <mconley@mozilla.com>
Thu, 11 Feb 2016 11:10:04 -0800
changeset 330728 4192fbb002dc627153d64ee616dfe03eb25bfecf
parent 330727 a66a2dd0005285f195a6ec2ca27384de740bef7d
child 514226 03b64a101cfaaca9d4f38b5cca3299b5c6bcd44f
push id10814
push usermconley@mozilla.com
push dateFri, 12 Feb 2016 18:47:18 +0000
reviewersGijs
bugs1246115
milestone47.0a1
Bug 1246115 - Re-write and re-enable regression test. r?Gijs MozReview-Commit-ID: 2c7mZ6B3fdG
browser/components/safebrowsing/content/test/browser.ini
browser/components/safebrowsing/content/test/browser_bug415846.js
--- a/browser/components/safebrowsing/content/test/browser.ini
+++ b/browser/components/safebrowsing/content/test/browser.ini
@@ -1,12 +1,10 @@
 [DEFAULT]
 support-files = head.js
 
 [browser_forbidden.js]
 [browser_bug400731.js]
 skip-if = e10s
 [browser_bug415846.js]
-skip-if = true
-# Disabled because it seems to now touch network resources
-# skip-if = os == "mac"
+skip-if = os == "mac"
 # Disabled on Mac because of its bizarre special-and-unique
 # snowflake of a help menu.
--- a/browser/components/safebrowsing/content/test/browser_bug415846.js
+++ b/browser/components/safebrowsing/content/test/browser_bug415846.js
@@ -1,62 +1,86 @@
 /* Check for the correct behaviour of the report web forgery/not a web forgery
 menu items.
 
 Mac makes this astonishingly painful to test since their help menu is special magic,
 but we can at least test it on the other platforms.*/
-var menu;
 
-function test() {
-  waitForExplicitFinish();
-
-  gBrowser.selectedTab = gBrowser.addTab();
+const NORMAL_PAGE = "http://example.com";
+const PHISH_PAGE = "http://www.itisatrap.org/firefox/its-a-trap.html";
 
-  // Navigate to a normal site
-  gBrowser.addEventListener("DOMContentLoaded", testNormal, false);
-  content.location = "http://example.com/";
-}
+/**
+ * Opens a new tab and browses to some URL, tests for the existence
+ * of the phishing menu items, and then runs a test function to check
+ * the state of the menu once opened. This function will take care of
+ * opening and closing the menu.
+ *
+ * @param url (string)
+ *        The URL to browse the tab to.
+ * @param testFn (function)
+ *        The function to run once the menu has been opened. This
+ *        function will be passed the "reportMenu" and "errorMenu"
+ *        DOM nodes as arguments, in that order. This function
+ *        should not yield anything.
+ * @returns Promise
+ */
+function check_menu_at_page(url, testFn) {
+  return BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "about:blank",
+  }, function*(browser) {
+    // We don't get load events when the DocShell redirects to error
+    // pages, but we do get DOMContentLoaded, so we'll wait for that.
+    let dclPromise = ContentTask.spawn(browser, null, function*() {
+      yield ContentTaskUtils.waitForEvent(this, "DOMContentLoaded", false);
+    });
+    browser.loadURI(url);
+    yield dclPromise;
 
-function testNormal() {
-  gBrowser.removeEventListener("DOMContentLoaded", testNormal, false);
+    let menu = document.getElementById("menu_HelpPopup");
+    ok(menu, "Help menu should exist");
+
+    let reportMenu =
+      document.getElementById("menu_HelpPopup_reportPhishingtoolmenu");
+    ok(reportMenu, "Report phishing menu item should exist");
+
+    let errorMenu =
+      document.getElementById("menu_HelpPopup_reportPhishingErrortoolmenu");
+    ok(errorMenu, "Report phishing error menu item should exist");
 
-  // open the menu, to force it to update
-  menu = document.getElementById("menu_HelpPopup");
-  ok(menu, "Help menu should exist!");
+    let menuOpen = BrowserTestUtils.waitForEvent(menu, "popupshown");
+    menu.openPopup(null, "", 0, 0, false, null);
+    yield menuOpen;
 
-  menu.addEventListener("popupshown", testNormal_PopupListener, false);
-  menu.openPopup(null, "", 0, 0, false, null);
+    testFn(reportMenu, errorMenu);
+
+    let menuClose = BrowserTestUtils.waitForEvent(menu, "popuphidden");
+    menu.hidePopup();
+    yield menuClose;
+  });
 }
 
-function testNormal_PopupListener() {
-  menu.removeEventListener("popupshown", testNormal_PopupListener, false);
-
-  var reportMenu = document.getElementById("menu_HelpPopup_reportPhishingtoolmenu");
-  var errorMenu = document.getElementById("menu_HelpPopup_reportPhishingErrortoolmenu");
-  is(reportMenu.hidden, false, "Report phishing menu should be visible on normal sites");
-  is(errorMenu.hidden, true, "Report error menu item should be hidden on normal sites");
-  menu.hidePopup();
-
-  // Now launch the phishing test.  Can't use onload here because error pages don't
-  // fire normal load events.
-  window.addEventListener("DOMContentLoaded", testPhishing, true);
-  content.location = "http://www.itisatrap.org/firefox/its-a-trap.html";
-}
+/**
+ * Tests that we show the "Report this page" menu item at a normal
+ * page.
+ */
+add_task(function*() {
+  yield check_menu_at_page(NORMAL_PAGE, (reportMenu, errorMenu) => {
+    ok(!reportMenu.hidden,
+       "Report phishing menu should be visible on normal sites");
+    ok(errorMenu.hidden,
+       "Report error menu item should be hidden on normal sites");
+  });
+});
 
-function testPhishing() {
-  window.removeEventListener("DOMContentLoaded", testPhishing, true);
-
-  menu.addEventListener("popupshown", testPhishing_PopupListener, false);
-  menu.openPopup(null, "", 0, 0, false, null);
-}
-
-function testPhishing_PopupListener() {
-  menu.removeEventListener("popupshown", testPhishing_PopupListener, false);
+/**
+ * Tests that we show the "Report this page is okay" menu item at
+ * a reported attack site.
+ */
+add_task(function*() {
+  yield check_menu_at_page(PHISH_PAGE, (reportMenu, errorMenu) => {
+    ok(reportMenu.hidden,
+       "Report phishing menu should be hidden on phishing sites");
+    ok(!errorMenu.hidden,
+       "Report error menu item should be visible on phishing sites");
+  });
+});
 
-  var reportMenu = document.getElementById("menu_HelpPopup_reportPhishingtoolmenu");
-  var errorMenu = document.getElementById("menu_HelpPopup_reportPhishingErrortoolmenu");
-  is(reportMenu.hidden, true, "Report phishing menu should be hidden on phishing sites");
-  is(errorMenu.hidden, false, "Report error menu item should be visible on phishing sites");
-  menu.hidePopup();
-
-  gBrowser.removeCurrentTab();
-  finish();
-}