Bug 887438 - Add a currentSet setter shim for all toolbars to improve add-on compatibility, r=jaws

--HG--
extra : rebase_source : 428b730b03df255c48e9e6493d8ed803e512578a
This commit is contained in:
Gijs Kruitbosch 2013-07-05 15:36:00 +02:00
parent a67fb0a7c0
commit 7415bac026
6 changed files with 136 additions and 5 deletions

View File

@ -528,6 +528,7 @@
defaultset="unified-back-forward-button,urlbar-container,reload-button,stop-button,search-container,webrtc-status-button,bookmarks-menu-button,downloads-button,home-button,social-share-button"
customizationtarget="nav-bar-customizationtarget"
overflowbutton="nav-bar-overflow-button"
overflowtarget="widget-overflow-list"
context="toolbar-context-menu">
<hbox id="nav-bar-customizationtarget" class="customization-target" flex="1">

View File

@ -133,13 +133,59 @@
]]></getter>
</property>
<property name="currentSet" readonly="true">
<property name="currentSet">
<getter><![CDATA[
if (!this._customizationTarget)
return "";
return [node.id for (node of this._customizationTarget.children)].join(',');
let rv = [];
for (let node of this._customizationTarget.children) {
if (node.getAttribute("skipintoolbarset") != "true") {
rv.push(node.id);
}
}
if (this.getAttribute("overflowing") == "true") {
let overflowTarget = this.getAttribute("overflowtarget");
let overflowList = this.ownerDocument.getElementById(overflowTarget);
for (let node of overflowList.children) {
if (node.getAttribute("skipintoolbarset") != "true") {
rv.push(node.id);
}
}
}
return rv.join(',');
]]></getter>
<setter><![CDATA[
// Get list of new and old ids:
let newVal = (val || '').split(',').filter(x => x);
let oldIds = CustomizableUI.getWidgetIdsInArea(this.id);
// Get a list of items only in the new list
let newIds = [id for (id of newVal) if (oldIds.indexOf(id) == -1)];
CustomizableUI.beginBatchUpdate();
for (let newId of newIds) {
let nextId = newId;
let pos;
do {
// Get the next item
nextId = newVal[newVal.indexOf(nextId) + 1];
// Figure out where it is in the old list
pos = oldIds.indexOf(nextId);
// If it's not in the old list, repeat:
} while (pos == -1 && nextId);
if (pos == -1) {
pos = null; // We didn't find anything, insert at the end
}
CustomizableUI.addWidgetToArea(newId, this.id, pos);
}
let currentIds = this.currentSet.split(',');
let removedIds = [id for (id of currentIds) if (newIds.indexOf(id) == -1 && newVal.indexOf(id) == -1)];
for (let removedId of removedIds) {
CustomizableUI.removeWidgetFromArea(removedId);
}
CustomizableUI.endBatchUpdate();
]]></setter>
</property>

View File

@ -2031,7 +2031,7 @@ OverflowableToolbar.prototype = {
init: function() {
this._target = this._toolbar.customizationTarget;
let doc = this._toolbar.ownerDocument;
this._list = doc.getElementById("widget-overflow-list");
this._list = doc.getElementById(this._toolbar.getAttribute("overflowtarget"));
this._toolbar.customizationTarget.addEventListener("overflow", this);
let window = doc.defaultView;

View File

@ -16,6 +16,7 @@ MOCHITEST_BROWSER_FILES = \
browser_877447_skip_missing_ids.js \
browser_878452_drag_to_panel.js \
browser_880382_drag_wide_widgets_in_panel.js \
browser_887438_currentset_shim.js \
head.js \
$(NULL)

View File

@ -0,0 +1,84 @@
/* 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/. */
let navbar = document.getElementById("nav-bar")
let navbarCT = navbar.customizationTarget;
let overflowPanelList = document.getElementById("widget-overflow-list");
let gTests = [
{
desc: "Reading currentset",
run: function() {
let nodeIds = [];
for (let node of navbarCT.childNodes) {
if (node.getAttribute("skipintoolbarset") != "true") {
nodeIds.push(node.id);
}
}
for (let node of overflowPanelList.childNodes) {
if (node.getAttribute("skipintoolbarset") != "true") {
nodeIds.push(node.id);
}
}
let currentSet = navbar.currentSet;
is(currentSet.split(',').length, nodeIds.length, "Should be just as many nodes as there are.");
is(currentSet, nodeIds.join(','), "Current set and node IDs should match.");
}
},
{
desc: "Insert, then remove items",
run: function() {
let currentSet = navbar.currentSet;
let newCurrentSet = currentSet.replace('home-button', 'feed-button,sync-button,home-button');
navbar.currentSet = newCurrentSet;
is(newCurrentSet, navbar.currentSet, "Current set should match expected current set.");
let feedBtn = document.getElementById("feed-button");
let syncBtn = document.getElementById("sync-button");
ok(feedBtn, "Feed button should have been added.");
ok(syncBtn, "Sync button should have been added.");
if (feedBtn && syncBtn) {
let feedParent = feedBtn.parentNode;
let syncParent = syncBtn.parentNode;
ok(feedParent == navbarCT || feedParent == overflowPanelList,
"Feed button should be in navbar or overflow");
ok(syncParent == navbarCT || syncParent == overflowPanelList,
"Feed button should be in navbar or overflow");
is(feedBtn.nextElementSibling, syncBtn, "Feed button should be next to sync button.");
let homeBtn = document.getElementById("home-button");
is(syncBtn.nextElementSibling, homeBtn, "Sync button should be next to home button.");
}
navbar.currentSet = currentSet;
is(currentSet, navbar.currentSet, "Should be able to remove the added items.");
}
},
{
desc: "Simultaneous insert/remove:",
run: function() {
let currentSet = navbar.currentSet;
let newCurrentSet = currentSet.replace('home-button', 'feed-button');
navbar.currentSet = newCurrentSet;
is(newCurrentSet, navbar.currentSet, "Current set should match expected current set.");
let feedBtn = document.getElementById("feed-button");
ok(feedBtn, "Feed button should have been added.");
let homeBtn = document.getElementById("home-button");
ok(!homeBtn, "Home button should have been removed.");
if (feedBtn) {
let feedParent = feedBtn.parentNode;
ok(feedParent == navbarCT || feedParent == overflowPanelList,
"Feed button should be in navbar or overflow");
}
navbar.currentSet = currentSet;
is(currentSet, navbar.currentSet, "Should be able to return to original state.");
}
}
];
function asyncCleanup() {
yield resetCustomization();
}
function test() {
waitForExplicitFinish();
runTests(gTests, asyncCleanup);
}

View File

@ -89,8 +89,7 @@ function todoAssertAreaPlacements(areaId, expectedPlacements) {
}
function getAreaWidgetIds(areaId) {
let widgetAry = CustomizableUI.getWidgetsInArea(areaId);
return widgetAry.map(x => x.id);
return CustomizableUI.getWidgetIdsInArea(areaId);
}
function simulateItemDrag(toDrag, target) {