Bug 1328756 - Add a pref for and options to the Long Press of the plus button, r=gijs

This commit is contained in:
Andrea Marchesini 2017-01-12 15:29:13 +01:00
parent 45074cb823
commit cfbaa1dd1d
3 changed files with 86 additions and 19 deletions

View File

@ -1425,10 +1425,16 @@ pref("privacy.trackingprotection.introURL", "https://www.mozilla.org/%LOCALE%/fi
pref("privacy.userContext.enabled", true);
pref("privacy.userContext.ui.enabled", true);
pref("privacy.usercontext.about_newtab_segregation.enabled", true);
// 0 disables long press, 1 when clicked, the menu is shown, 2 the menu is shown after X milliseconds.
pref("privacy.usercontext.longPressBehavior", 2);
#else
pref("privacy.userContext.enabled", false);
pref("privacy.userContext.ui.enabled", false);
pref("privacy.usercontext.about_newtab_segregation.enabled", false);
// 0 disables long press, 1 when clicked, the menu is shown, 2 the menu is shown after X milliseconds.
pref("privacy.usercontext.longPressBehavior", 0);
#endif
#ifndef RELEASE_OR_BETA

View File

@ -5469,14 +5469,16 @@
this._tabAnimationLoggingEnabled = false;
}
this._browserNewtabpageEnabled = Services.prefs.getBoolPref("browser.newtabpage.enabled");
Services.prefs.addObserver("privacy.usercontext", this, false);
this.observe(null, "nsPref:changed", "privacy.userContext.enabled");
Services.prefs.addObserver("privacy.userContext.enabled", this, false);
this.observe(null, "nsPref:changed", "privacy.usercontext.longPressBehavior");
]]>
</constructor>
<destructor>
<![CDATA[
Services.prefs.removeObserver("privacy.userContext.enabled", this);
Services.prefs.removeObserver("privacy.usercontext.longPressBehavior", this);
]]>
</destructor>
@ -5512,17 +5514,35 @@
<body><![CDATA[
switch (aTopic) {
case "nsPref:changed":
// This is the only pref observed.
// This is has to deal with changes in
// privacy.userContext.enabled and
// privacy.usercontext.longPressBehavior.
let containersEnabled = Services.prefs.getBoolPref("privacy.userContext.enabled")
&& !PrivateBrowsingUtils.isWindowPrivate(window);
// This pref won't change so often, so just recreate the menu.
let longPressBehavior = Services.prefs.getIntPref("privacy.usercontext.longPressBehavior");
// If longPressBehavior pref is set to 0 (or any invalid value)
// long press menu is disabled.
if (containersEnabled && (longPressBehavior <= 0 || longPressBehavior > 2)) {
containersEnabled = false;
}
const newTab = document.getElementById("new-tab-button");
const newTab2 = document.getAnonymousElementByAttribute(this, "anonid", "tabs-newtab-button")
if (containersEnabled) {
for (let parent of [newTab, newTab2]) {
if (!parent)
continue;
for (let parent of [newTab, newTab2]) {
if (!parent)
continue;
gClickAndHoldListenersOnElement.remove(parent);
parent.removeAttribute("type");
if (parent.firstChild) {
parent.firstChild.remove();
}
if (containersEnabled) {
let popup = document.createElementNS(
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
"menupopup");
@ -5535,19 +5555,14 @@
popup.setAttribute("position", "after_end");
parent.appendChild(popup);
gClickAndHoldListenersOnElement.add(parent);
// longPressBehavior == 2 means that the menu is shown after X
// millisecs. Otherwise, with 1, the menu is open immediatelly.
if (longPressBehavior == 2) {
gClickAndHoldListenersOnElement.add(parent);
}
parent.setAttribute("type", "menu");
}
} else {
for (let parent of [newTab, newTab2]) {
if (!parent)
continue;
gClickAndHoldListenersOnElement.remove(parent);
parent.removeAttribute("type");
if (!parent.firstChild)
continue;
parent.firstChild.remove();
}
}
break;

View File

@ -3,9 +3,10 @@
// Testing that when the user opens the add tab menu and clicks menu items
// the correct context id is opened
add_task(function* test() {
add_task(function* test_menu_with_timeout() {
yield SpecialPowers.pushPrefEnv({"set": [
["privacy.userContext.enabled", true]
["privacy.userContext.enabled", true],
["privacy.usercontext.longPressBehavior", 2]
]});
let newTab = document.getElementById('tabbrowser-tabs');
@ -34,6 +35,51 @@ add_task(function* test() {
}
});
add_task(function* test_menu_without_timeout() {
yield SpecialPowers.pushPrefEnv({"set": [
["privacy.userContext.enabled", true],
["privacy.usercontext.longPressBehavior", 1]
]});
let newTab = document.getElementById('tabbrowser-tabs');
let newTabButton = document.getAnonymousElementByAttribute(newTab, "anonid", "tabs-newtab-button");
ok(newTabButton, "New tab button exists");
ok(!newTabButton.hidden, "New tab button is visible");
yield BrowserTestUtils.waitForCondition(() => !!document.getAnonymousElementByAttribute(newTab, "anonid", "newtab-popup"), "Wait for popup to exist");
let popup = document.getAnonymousElementByAttribute(newTab, "anonid", "newtab-popup");
for (let i = 1; i <= 4; i++) {
let popupShownPromise = BrowserTestUtils.waitForEvent(popup, "popupshown");
EventUtils.synthesizeMouseAtCenter(newTabButton, {type: "mousedown"});
yield popupShownPromise;
let contextIdItem = popup.querySelector(`menuitem[data-usercontextid="${i}"]`);
ok(contextIdItem, `User context id ${i} exists`);
let waitForTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
EventUtils.synthesizeMouseAtCenter(contextIdItem, {});
let tab = yield waitForTabPromise;
is(tab.getAttribute('usercontextid'), i, `New tab has UCI equal ${i}`);
yield BrowserTestUtils.removeTab(tab);
}
});
add_task(function* test_no_menu() {
yield SpecialPowers.pushPrefEnv({"set": [
["privacy.userContext.enabled", true],
["privacy.usercontext.longPressBehavior", 0]
]});
let newTab = document.getElementById('tabbrowser-tabs');
let newTabButton = document.getAnonymousElementByAttribute(newTab, "anonid", "tabs-newtab-button");
ok(newTabButton, "New tab button exists");
ok(!newTabButton.hidden, "New tab button is visible");
let popup = document.getAnonymousElementByAttribute(newTab, "anonid", "newtab-popup");
ok(!popup, "new tab should not have a popup");
});
add_task(function* test_private_mode() {
let privateWindow = yield BrowserTestUtils.openNewBrowserWindow({private: true});