Bug 1358475 - The category must be retrieved from either the argument, the hash, or the default category name before computing the subcategory. r=jaws
Computing the subcategory based off of just the hash doesn't work when switchToTabHavingURI is used to open the preferences.
MozReview-Commit-ID: 5rYkiLkDgfT
--- 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,38 @@ 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("general-search", {leaveOpen: true});
+ is(prefs.selectedPane, "paneGeneral", "General pane is selected by default");
+ let 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");
+ // The reasons that here just call the `openPreferences` API without the helping function are
+ // - already opened one about:preferences tab up there and
+ // - the goal is to test on the existing tab and
+ // - using `openPreferencesViaOpenPreferencesAPI` would introduce more handling of additional about:blank and unneccessary event
+ openPreferences("privacy-reports");
+ let selectedPane = gBrowser.contentWindow.history.state;
+ is(selectedPane, "panePrivacy", "Privacy pane should be selected");
+ 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;