Bug 959531 - More about:home testing. r=felipe, a=bajaj
authorBill McCloskey <wmccloskey@mozilla.com>
Mon, 20 Jan 2014 15:13:35 -0800
changeset 167994 1b3060857876bd9cb6f4eb895d26d5302549cd98
parent 167993 664bc0ef6421ce6cd56c5e3a4b5264ba027eb434
child 167995 4b60f871d7f2557aca1b39210356b47557f06266
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfelipe, bajaj
bugs959531
milestone27.0
Bug 959531 - More about:home testing. r=felipe, a=bajaj
browser/base/content/content.js
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_aboutHome.js
browser/base/content/test/general/test_bug959531.html
--- a/browser/base/content/content.js
+++ b/browser/base/content/content.js
@@ -105,24 +105,24 @@ let AboutHomeListener = {
   onPageLoad: function() {
     let doc = content.document;
     if (doc.documentURI.toLowerCase() != "about:home" ||
         doc.documentElement.hasAttribute("hasBrowserHandlers")) {
       return;
     }
 
     doc.documentElement.setAttribute("hasBrowserHandlers", "true");
-    let updateListener = this;
-    addMessageListener("AboutHome:Update", updateListener);
+    let self = this;
+    addMessageListener("AboutHome:Update", self);
     addEventListener("click", this.onClick, true);
     addEventListener("pagehide", function onPageHide(event) {
       if (event.target.defaultView.frameElement)
         return;
-      removeMessageListener("AboutHome:Update", updateListener);
-      removeEventListener("click", this.onClick, true);
+      removeMessageListener("AboutHome:Update", self);
+      removeEventListener("click", self.onClick, true);
       removeEventListener("pagehide", onPageHide, true);
       if (event.target.documentElement)
         event.target.documentElement.removeAttribute("hasBrowserHandlers");
     }, true);
 
     // XXX bug 738646 - when Marketplace is launched, remove this statement and
     // the hidden attribute set on the apps button in aboutHome.xhtml
     if (Services.prefs.getPrefType("browser.aboutHome.apps") == Services.prefs.PREF_BOOL &&
@@ -139,16 +139,21 @@ let AboutHomeListener = {
   onClick: function(aEvent) {
     if (!aEvent.isTrusted || // Don't trust synthetic events
         aEvent.button == 2 || aEvent.target.localName != "button") {
       return;
     }
 
     let originalTarget = aEvent.originalTarget;
     let ownerDoc = originalTarget.ownerDocument;
+    if (ownerDoc.documentURI != "about:home") {
+      // This shouldn't happen, but we're being defensive.
+      return;
+    }
+
     let elmId = originalTarget.getAttribute("id");
 
     switch (elmId) {
       case "restorePreviousSession":
         sendAsyncMessage("AboutHome:RestorePreviousSession");
         ownerDoc.getElementById("launcher").removeAttribute("session");
         break;
 
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -69,16 +69,17 @@ support-files =
   plugin_unknown.html
   print_postdata.sjs
   redirect_bug623155.sjs
   test-mixedcontent-securityerrors.html
   test_bug435035.html
   test_bug462673.html
   test_bug628179.html
   test_bug839103.html
+  test_bug959531.html
   test_wyciwyg_copying.html
   title_test.svg
   video.ogg
   zoom_test.html
   test_no_mcb_on_http_site_img.html
   test_no_mcb_on_http_site_img.css
   test_no_mcb_on_http_site_font.html
   test_no_mcb_on_http_site_font.css
--- a/browser/base/content/test/general/browser_aboutHome.js
+++ b/browser/base/content/test/general/browser_aboutHome.js
@@ -320,17 +320,54 @@ let gTests = [
     Services.obs.addObserver(searchObserver, "browser-search-engine-modified", false);
     registerCleanupFunction(function () {
       Services.obs.removeObserver(searchObserver, "browser-search-engine-modified");
     });
     Services.search.addEngine("http://test:80/browser/browser/base/content/test/general/POSTSearchEngine.xml",
                               Ci.nsISearchEngine.DATA_XML, null, false);
     return deferred.promise;
   }
-}
+},
+
+{
+  desc: "Make sure that a page can't imitate about:home",
+  setup: function () { },
+  run: function (aSnippetsMap)
+  {
+    let deferred = Promise.defer();
+
+    let browser = gBrowser.selectedTab.linkedBrowser;
+    waitForLoad(() => {
+      let button = browser.contentDocument.getElementById("settings");
+      ok(button, "Found settings button in test page");
+      button.click();
+
+      // It may take a few turns of the event loop before the window
+      // is displayed, so we wait.
+      function check(n) {
+        let win = Services.wm.getMostRecentWindow("Browser:Preferences");
+        ok(!win, "Preferences window not showing");
+        if (win) {
+          win.close();
+        }
+
+        if (n > 0) {
+          executeSoon(() => check(n-1));
+        } else {
+          deferred.resolve();
+        }
+      }
+
+      check(5);
+    });
+
+    browser.loadURI("https://example.com/browser/browser/base/content/test/general/test_bug959531.html");
+    return deferred.promise;
+  }
+},
 
 ];
 
 function test()
 {
   waitForExplicitFinish();
   requestLongerTimeout(2);
   ignoreAllUncaughtExceptions();
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/test_bug959531.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>Test for content page with settings button</title>
+  </head>
+  <body>
+    <button name="settings" id="settings">Settings</button>
+  </body>
+</html>