michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const kToolbarName = "test-unregisterArea-placements-toolbar"; michael@0: const kTestWidgetPfx = "test-widget-for-unregisterArea-placements-"; michael@0: const kTestWidgetCount = 3; michael@0: registerCleanupFunction(removeCustomToolbars); michael@0: michael@0: // unregisterArea should keep placements by default and restore them when re-adding the area michael@0: add_task(function() { michael@0: let widgetIds = []; michael@0: for (let i = 0; i < kTestWidgetCount; i++) { michael@0: let id = kTestWidgetPfx + i; michael@0: widgetIds.push(id); michael@0: let spec = {id: id, type: 'button', removable: true, label: "unregisterArea test", tooltiptext: "" + i}; michael@0: CustomizableUI.createWidget(spec); michael@0: } michael@0: for (let i = kTestWidgetCount; i < kTestWidgetCount * 2; i++) { michael@0: let id = kTestWidgetPfx + i; michael@0: widgetIds.push(id); michael@0: createDummyXULButton(id, "unregisterArea XUL test " + i); michael@0: } michael@0: let toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds); michael@0: checkAbstractAndRealPlacements(toolbarNode, widgetIds); michael@0: michael@0: // Now move one of them: michael@0: CustomizableUI.moveWidgetWithinArea(kTestWidgetPfx + kTestWidgetCount, 0); michael@0: // Clone the array so we know this is the modified one: michael@0: let modifiedWidgetIds = [...widgetIds]; michael@0: let movedWidget = modifiedWidgetIds.splice(kTestWidgetCount, 1)[0]; michael@0: modifiedWidgetIds.unshift(movedWidget); michael@0: michael@0: // Check it: michael@0: checkAbstractAndRealPlacements(toolbarNode, modifiedWidgetIds); michael@0: michael@0: // Then unregister michael@0: CustomizableUI.unregisterArea(kToolbarName); michael@0: michael@0: // Check we tell the outside world no dangerous things: michael@0: checkWidgetFates(widgetIds); michael@0: // Only then remove the real node michael@0: toolbarNode.remove(); michael@0: michael@0: // Now move one of the items to the palette, and another to the navbar: michael@0: let lastWidget = modifiedWidgetIds.pop(); michael@0: CustomizableUI.removeWidgetFromArea(lastWidget); michael@0: lastWidget = modifiedWidgetIds.pop(); michael@0: CustomizableUI.addWidgetToArea(lastWidget, CustomizableUI.AREA_NAVBAR); michael@0: michael@0: // Recreate ourselves with the default placements being the same: michael@0: toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds); michael@0: // Then check that after doing this, our actual placements match michael@0: // the modified list, not the default one. michael@0: checkAbstractAndRealPlacements(toolbarNode, modifiedWidgetIds); michael@0: michael@0: // Now remove completely: michael@0: CustomizableUI.unregisterArea(kToolbarName, true); michael@0: checkWidgetFates(modifiedWidgetIds); michael@0: toolbarNode.remove(); michael@0: michael@0: // One more time: michael@0: // Recreate ourselves with the default placements being the same: michael@0: toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds); michael@0: // Should now be back to default: michael@0: checkAbstractAndRealPlacements(toolbarNode, widgetIds); michael@0: CustomizableUI.unregisterArea(kToolbarName, true); michael@0: checkWidgetFates(widgetIds); michael@0: toolbarNode.remove(); michael@0: michael@0: //XXXgijs: ensure cleanup function doesn't barf: michael@0: gAddedToolbars.delete(kToolbarName); michael@0: michael@0: // Remove all the XUL widgets, destroy the others: michael@0: for (let widget of widgetIds) { michael@0: let widgetWrapper = CustomizableUI.getWidget(widget); michael@0: if (widgetWrapper.provider == CustomizableUI.PROVIDER_XUL) { michael@0: gNavToolbox.palette.querySelector("#" + widget).remove(); michael@0: } else { michael@0: CustomizableUI.destroyWidget(widget); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: function checkAbstractAndRealPlacements(aNode, aExpectedPlacements) { michael@0: assertAreaPlacements(kToolbarName, aExpectedPlacements); michael@0: let physicalWidgetIds = [node.id for (node of aNode.childNodes)]; michael@0: placementArraysEqual(aNode.id, physicalWidgetIds, aExpectedPlacements); michael@0: } michael@0: michael@0: function checkWidgetFates(aWidgetIds) { michael@0: for (let widget of aWidgetIds) { michael@0: ok(!CustomizableUI.getPlacementOfWidget(widget), "Widget should be in palette"); michael@0: ok(!document.getElementById(widget), "Widget should not be in the DOM"); michael@0: let widgetInPalette = !!gNavToolbox.palette.querySelector("#" + widget); michael@0: let widgetProvider = CustomizableUI.getWidget(widget).provider; michael@0: let widgetIsXULWidget = widgetProvider == CustomizableUI.PROVIDER_XUL; michael@0: is(widgetInPalette, widgetIsXULWidget, "Just XUL Widgets should be in the palette"); michael@0: } michael@0: } michael@0: michael@0: add_task(function asyncCleanup() { michael@0: yield resetCustomization(); michael@0: });