1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/customizableui/test/browser_942581_unregisterArea_keeps_placements.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const kToolbarName = "test-unregisterArea-placements-toolbar"; 1.11 +const kTestWidgetPfx = "test-widget-for-unregisterArea-placements-"; 1.12 +const kTestWidgetCount = 3; 1.13 +registerCleanupFunction(removeCustomToolbars); 1.14 + 1.15 +// unregisterArea should keep placements by default and restore them when re-adding the area 1.16 +add_task(function() { 1.17 + let widgetIds = []; 1.18 + for (let i = 0; i < kTestWidgetCount; i++) { 1.19 + let id = kTestWidgetPfx + i; 1.20 + widgetIds.push(id); 1.21 + let spec = {id: id, type: 'button', removable: true, label: "unregisterArea test", tooltiptext: "" + i}; 1.22 + CustomizableUI.createWidget(spec); 1.23 + } 1.24 + for (let i = kTestWidgetCount; i < kTestWidgetCount * 2; i++) { 1.25 + let id = kTestWidgetPfx + i; 1.26 + widgetIds.push(id); 1.27 + createDummyXULButton(id, "unregisterArea XUL test " + i); 1.28 + } 1.29 + let toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds); 1.30 + checkAbstractAndRealPlacements(toolbarNode, widgetIds); 1.31 + 1.32 + // Now move one of them: 1.33 + CustomizableUI.moveWidgetWithinArea(kTestWidgetPfx + kTestWidgetCount, 0); 1.34 + // Clone the array so we know this is the modified one: 1.35 + let modifiedWidgetIds = [...widgetIds]; 1.36 + let movedWidget = modifiedWidgetIds.splice(kTestWidgetCount, 1)[0]; 1.37 + modifiedWidgetIds.unshift(movedWidget); 1.38 + 1.39 + // Check it: 1.40 + checkAbstractAndRealPlacements(toolbarNode, modifiedWidgetIds); 1.41 + 1.42 + // Then unregister 1.43 + CustomizableUI.unregisterArea(kToolbarName); 1.44 + 1.45 + // Check we tell the outside world no dangerous things: 1.46 + checkWidgetFates(widgetIds); 1.47 + // Only then remove the real node 1.48 + toolbarNode.remove(); 1.49 + 1.50 + // Now move one of the items to the palette, and another to the navbar: 1.51 + let lastWidget = modifiedWidgetIds.pop(); 1.52 + CustomizableUI.removeWidgetFromArea(lastWidget); 1.53 + lastWidget = modifiedWidgetIds.pop(); 1.54 + CustomizableUI.addWidgetToArea(lastWidget, CustomizableUI.AREA_NAVBAR); 1.55 + 1.56 + // Recreate ourselves with the default placements being the same: 1.57 + toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds); 1.58 + // Then check that after doing this, our actual placements match 1.59 + // the modified list, not the default one. 1.60 + checkAbstractAndRealPlacements(toolbarNode, modifiedWidgetIds); 1.61 + 1.62 + // Now remove completely: 1.63 + CustomizableUI.unregisterArea(kToolbarName, true); 1.64 + checkWidgetFates(modifiedWidgetIds); 1.65 + toolbarNode.remove(); 1.66 + 1.67 + // One more time: 1.68 + // Recreate ourselves with the default placements being the same: 1.69 + toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds); 1.70 + // Should now be back to default: 1.71 + checkAbstractAndRealPlacements(toolbarNode, widgetIds); 1.72 + CustomizableUI.unregisterArea(kToolbarName, true); 1.73 + checkWidgetFates(widgetIds); 1.74 + toolbarNode.remove(); 1.75 + 1.76 + //XXXgijs: ensure cleanup function doesn't barf: 1.77 + gAddedToolbars.delete(kToolbarName); 1.78 + 1.79 + // Remove all the XUL widgets, destroy the others: 1.80 + for (let widget of widgetIds) { 1.81 + let widgetWrapper = CustomizableUI.getWidget(widget); 1.82 + if (widgetWrapper.provider == CustomizableUI.PROVIDER_XUL) { 1.83 + gNavToolbox.palette.querySelector("#" + widget).remove(); 1.84 + } else { 1.85 + CustomizableUI.destroyWidget(widget); 1.86 + } 1.87 + } 1.88 +}); 1.89 + 1.90 +function checkAbstractAndRealPlacements(aNode, aExpectedPlacements) { 1.91 + assertAreaPlacements(kToolbarName, aExpectedPlacements); 1.92 + let physicalWidgetIds = [node.id for (node of aNode.childNodes)]; 1.93 + placementArraysEqual(aNode.id, physicalWidgetIds, aExpectedPlacements); 1.94 +} 1.95 + 1.96 +function checkWidgetFates(aWidgetIds) { 1.97 + for (let widget of aWidgetIds) { 1.98 + ok(!CustomizableUI.getPlacementOfWidget(widget), "Widget should be in palette"); 1.99 + ok(!document.getElementById(widget), "Widget should not be in the DOM"); 1.100 + let widgetInPalette = !!gNavToolbox.palette.querySelector("#" + widget); 1.101 + let widgetProvider = CustomizableUI.getWidget(widget).provider; 1.102 + let widgetIsXULWidget = widgetProvider == CustomizableUI.PROVIDER_XUL; 1.103 + is(widgetInPalette, widgetIsXULWidget, "Just XUL Widgets should be in the palette"); 1.104 + } 1.105 +} 1.106 + 1.107 +add_task(function asyncCleanup() { 1.108 + yield resetCustomization(); 1.109 +});