Bug 1358475 - Opening Preferences with subcategory on an existing Preferences page would result blank page draft
authorFischer.json <fischer.json@gmail.com>
Sat, 22 Apr 2017 16:42:31 +0800
changeset 566715 bb641c14e01cb3d4bcdfb26a0d31c812f903110d
parent 566156 8b854986038cf3f3f240697e27ef48ea65914c13
child 625398 c693745529563ebd16d4586f8e863187fe372eb4
push id55306
push userbmo:fliu@mozilla.com
push dateSat, 22 Apr 2017 09:46:42 +0000
bugs1358475
milestone55.0a1
Bug 1358475 - Opening Preferences with subcategory on an existing Preferences page would result blank page MozReview-Commit-ID: 5rYkiLkDgfT
browser/components/preferences/in-content/preferences.js
browser/components/preferences/in-content/tests/browser_bug1020245_openPreferences_to_paneContent.js
--- a/browser/components/preferences/in-content/preferences.js
+++ b/browser/components/preferences/in-content/preferences.js
@@ -143,24 +143,25 @@ function telemetryBucketForCategory(cate
 function onHashChange() {
   gotoPref();
 }
 
 function gotoPref(aCategory) {
   let categories = document.getElementById("categories");
   const kDefaultCategoryInternalName = "paneGeneral";
   let hash = document.location.hash;
+
+  let category = aCategory || hash.substr(1) || kDefaultCategoryInternalName;
+  let breakIndex = category.indexOf("-");
   // Subcategories allow for selecting smaller sections of the preferences
   // until proper search support is enabled (bug 1353954).
-  let breakIndex = hash.indexOf("-");
-  let subcategory = breakIndex != -1 && hash.substring(breakIndex + 1);
+  let subcategory = breakIndex != -1 && category.substring(breakIndex + 1);
   if (subcategory) {
-    hash = hash.substring(0, breakIndex);
+    category = category.substring(0, breakIndex);
   }
-  let category = aCategory || hash.substr(1) || kDefaultCategoryInternalName;
   category = friendlyPrefCategoryNameToInternalName(category);
 
   // Updating the hash (below) or changing the selected category
   // will re-enter gotoPref.
   if (gLastHash == category && !subcategory)
     return;
   let item = categories.querySelector(".category[value=" + category + "]");
   if (!item) {
--- a/browser/components/preferences/in-content/tests/browser_bug1020245_openPreferences_to_paneContent.js
+++ b/browser/components/preferences/in-content/tests/browser_bug1020245_openPreferences_to_paneContent.js
@@ -2,16 +2,17 @@
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Services.prefs.setBoolPref("browser.preferences.instantApply", true);
 
 registerCleanupFunction(function() {
   Services.prefs.clearUserPref("browser.preferences.instantApply");
 });
 
+// Test opening to the differerent panes and subcategories in Preferences
 add_task(function*() {
   let prefs = yield openPreferencesViaOpenPreferencesAPI("panePrivacy");
   is(prefs.selectedPane, "panePrivacy", "Privacy pane was selected");
   prefs = yield openPreferencesViaOpenPreferencesAPI("advanced");
   is(prefs.selectedPane, "paneAdvanced", "Advanced pane was selected");
   prefs = yield openPreferencesViaHash("privacy");
   is(prefs.selectedPane, "panePrivacy", "Privacy pane is selected when hash is 'privacy'");
   prefs = yield openPreferencesViaOpenPreferencesAPI("nonexistant-category");
@@ -31,16 +32,36 @@ add_task(function*() {
   is(prefs.selectedPane, "paneGeneral", "General pane is selected by default");
   doc = gBrowser.contentDocument;
   is(doc.location.hash, "#general", "The subcategory should be removed from the URI");
   ok(doc.querySelector("#startupGroup").hidden, "Startup should be hidden when only Search is requested");
   ok(!doc.querySelector("#engineList").hidden, "The search engine list should be visible when Search is requested");
   yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
 });
 
+// Test opening Preferences with subcategory on an existing Preferences tab. See bug 1358475.
+add_task(function*() {
+  let prefs = yield openPreferencesViaOpenPreferencesAPI("privacy-reports", {leaveOpen: true});
+  is(prefs.selectedPane, "panePrivacy", "Privacy pane is selected by default");
+  let doc = gBrowser.contentDocument;
+  is(doc.location.hash, "#privacy", "The subcategory should be removed from the URI");
+  ok(doc.querySelector("#locationBarGroup").hidden, "Location Bar prefs should be hidden when only Reports are requested");
+  ok(!doc.querySelector("#header-privacy").hidden, "The header should be visible when a subcategory is requested");
+  // Since already opened one about:preferences tab up there and the goal is to test on the existing one,
+  // here just call the `openPreferences` API without the helping function.
+  openPreferences("privacy-reports");
+  let selectedPane = gBrowser.contentWindow.history.state;
+  is(selectedPane, "panePrivacy", "Privacy pane is selected still");
+  is(doc.location.hash, "#privacy", "The subcategory should be removed from the URI");
+  ok(doc.querySelector("#locationBarGroup").hidden, "Location Bar prefs should be hidden when only Reports are requested");
+  ok(!doc.querySelector("#header-privacy").hidden, "The header should be visible when a subcategory is requested");
+  yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
+});
+
+
 function openPreferencesViaHash(aPane) {
   let deferred = Promise.defer();
   gBrowser.selectedTab = gBrowser.addTab("about:preferences" + (aPane ? "#" + aPane : ""));
   let newTabBrowser = gBrowser.selectedBrowser;
 
   newTabBrowser.addEventListener("Initialized", function() {
     newTabBrowser.contentWindow.addEventListener("load", function() {
       let win = gBrowser.contentWindow;