This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1563319 - Enable the What's New UI when pref is enabled (#5153)
- Loading branch information
Showing
6 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
"use strict"; | ||
|
||
ChromeUtils.defineModuleGetter(this, "Services", | ||
"resource://gre/modules/Services.jsm"); | ||
ChromeUtils.defineModuleGetter(this, "EveryWindow", | ||
"resource:///modules/EveryWindow.jsm"); | ||
|
||
const WHATSNEW_ENABLED_PREF = "browser.messaging-system.whatsNewPanel.enabled"; | ||
|
||
const TOOLBAR_BUTTON_ID = "whats-new-menu-button"; | ||
const APPMENU_BUTTON_ID = "appMenu-whatsnew-button"; | ||
const PANEL_HEADER_SELECTOR = "#PanelUI-whatsNew-title > label"; | ||
|
||
const BUTTON_STRING_ID = "cfr-whatsnew-button"; | ||
|
||
class _ToolbarPanelHub { | ||
constructor() { | ||
this._showAppmenuButton = this._showAppmenuButton.bind(this); | ||
this._hideAppmenuButton = this._hideAppmenuButton.bind(this); | ||
this._showToolbarButton = this._showToolbarButton.bind(this); | ||
this._hideToolbarButton = this._hideToolbarButton.bind(this); | ||
} | ||
|
||
init() { | ||
if (this.whatsNewPanelEnabled) { | ||
this.enableAppmenuButton(); | ||
// TODO: this will eventually be called when the badge message gets triggered | ||
// instead of here in init. | ||
this.enableToolbarButton(); | ||
} | ||
} | ||
|
||
uninit() { | ||
EveryWindow.unregisterCallback(TOOLBAR_BUTTON_ID); | ||
EveryWindow.unregisterCallback(APPMENU_BUTTON_ID); | ||
} | ||
|
||
get whatsNewPanelEnabled() { | ||
return Services.prefs.getBoolPref(WHATSNEW_ENABLED_PREF, false); | ||
} | ||
|
||
maybeInsertFTL(win) { | ||
win.MozXULElement.insertFTLIfNeeded("browser/newtab/asrouter.ftl"); | ||
} | ||
|
||
// Turns on the Appmenu (hamburger menu) button for all open windows and future windows. | ||
enableAppmenuButton() { | ||
EveryWindow.registerCallback( | ||
APPMENU_BUTTON_ID, | ||
this._showAppmenuButton, | ||
this._hideAppmenuButton | ||
); | ||
} | ||
|
||
// Turns on the Toolbar button for all open windows and future windows. | ||
enableToolbarButton() { | ||
EveryWindow.registerCallback( | ||
TOOLBAR_BUTTON_ID, | ||
this._showToolbarButton, | ||
this._hideToolbarButton | ||
); | ||
} | ||
|
||
_showAppmenuButton(win) { | ||
this.maybeInsertFTL(win); | ||
this._showElement(win.browser.ownerDocument, APPMENU_BUTTON_ID, BUTTON_STRING_ID); | ||
} | ||
|
||
_hideAppmenuButton(win) { | ||
this._hideElement(win.browser.ownerDocument, APPMENU_BUTTON_ID); | ||
} | ||
|
||
_showToolbarButton(win) { | ||
const document = win.browser.ownerDocument; | ||
this.maybeInsertFTL(win); | ||
this._showElement(document, TOOLBAR_BUTTON_ID, BUTTON_STRING_ID); | ||
// The toolbar dropdown panel uses this extra header element that is hidden | ||
// in the appmenu subview version of the panel. We only need to set it | ||
// when showing the toolbar button. | ||
document.l10n.setAttributes(document.querySelector(PANEL_HEADER_SELECTOR), "cfr-whatsnew-panel-header"); | ||
} | ||
|
||
_hideToolbarButton(win) { | ||
this._hideElement(win.browser.ownerDocument, TOOLBAR_BUTTON_ID); | ||
} | ||
|
||
_showElement(document, id, string_id) { | ||
const el = document.getElementById(id); | ||
document.l10n.setAttributes(el, string_id); | ||
el.removeAttribute("hidden"); | ||
} | ||
|
||
_hideElement(document, id) { | ||
document.getElementById(id).setAttribute("hidden", true); | ||
} | ||
} | ||
|
||
this._ToolbarPanelHub = _ToolbarPanelHub; | ||
|
||
/** | ||
* ToolbarPanelHub - singleton instance of _ToolbarPanelHub that can initiate | ||
* message requests and render messages. | ||
*/ | ||
this.ToolbarPanelHub = new _ToolbarPanelHub(); | ||
|
||
const EXPORTED_SYMBOLS = ["ToolbarPanelHub", "_ToolbarPanelHub"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {_ToolbarPanelHub} from "lib/ToolbarPanelHub.jsm"; | ||
import {GlobalOverrider} from "test/unit/utils"; | ||
|
||
describe("ToolbarPanelHub", () => { | ||
let globals; | ||
let sandbox; | ||
let instance; | ||
let everyWindowStub; | ||
let fakeWindow; | ||
let fakeElementById; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
globals = new GlobalOverrider(); | ||
instance = new _ToolbarPanelHub(); | ||
fakeElementById = { | ||
setAttribute: sandbox.stub(), | ||
removeAttribute: sandbox.stub(), | ||
}; | ||
fakeWindow = { | ||
browser: { | ||
ownerDocument: { | ||
l10n: { | ||
setAttributes: sandbox.stub() | ||
}, | ||
getElementById: sandbox.stub().returns(fakeElementById), | ||
querySelector: sandbox.stub().returns({}), | ||
} | ||
}, | ||
MozXULElement: {insertFTLIfNeeded: sandbox.stub()}, | ||
}; | ||
everyWindowStub = { | ||
registerCallback: sandbox.stub(), | ||
unregisterCallback: sandbox.stub(), | ||
}; | ||
globals.set("EveryWindow", everyWindowStub); | ||
}); | ||
afterEach(() => { | ||
instance.uninit(); | ||
sandbox.restore(); | ||
}); | ||
it("should create an instance", () => { | ||
assert.ok(instance); | ||
}); | ||
it("should not enableAppmenuButton() on init if pref is not enabled", () => { | ||
sandbox.stub(global.Services.prefs, "getBoolPref").returns(false); | ||
instance.enableAppmenuButton = sandbox.stub(); | ||
instance.init(); | ||
assert.notCalled(instance.enableAppmenuButton); | ||
}); | ||
it("should enableAppmenuButton() on init if pref is enabled", () => { | ||
sandbox.stub(global.Services.prefs, "getBoolPref").returns(true); | ||
instance.enableAppmenuButton = sandbox.stub(); | ||
instance.init(); | ||
assert.calledOnce(instance.enableAppmenuButton); | ||
}); | ||
it("should unregisterCallback on uninit", () => { | ||
instance.uninit(); | ||
assert.calledTwice(everyWindowStub.unregisterCallback); | ||
}); | ||
it("should registerCallback on enableAppmenuButton", () => { | ||
instance.enableAppmenuButton(); | ||
assert.calledOnce(everyWindowStub.registerCallback); | ||
}); | ||
it("should registerCallback on enableToolbarButton", () => { | ||
instance.enableToolbarButton(); | ||
assert.calledOnce(everyWindowStub.registerCallback); | ||
}); | ||
it("should unhide appmenu button on _showAppmenuButton", () => { | ||
instance._showAppmenuButton(fakeWindow); | ||
assert.calledWith(fakeElementById.removeAttribute, "hidden"); | ||
}); | ||
it("should hide appmenu button on _hideAppmenuButton", () => { | ||
instance._hideAppmenuButton(fakeWindow); | ||
assert.calledWith(fakeElementById.setAttribute, "hidden", true); | ||
}); | ||
it("should unhide toolbar button on _showToolbarButton", () => { | ||
instance._showToolbarButton(fakeWindow); | ||
assert.calledWith(fakeElementById.removeAttribute, "hidden"); | ||
}); | ||
it("should hide toolbar button on _hideToolbarButton", () => { | ||
instance._hideToolbarButton(fakeWindow); | ||
assert.calledWith(fakeElementById.setAttribute, "hidden", true); | ||
}); | ||
}); |