tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_942581_unregisterArea_keeps_placements.js (4294B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const kToolbarName = "test-unregisterArea-placements-toolbar";
      8 const kTestWidgetPfx = "test-widget-for-unregisterArea-placements-";
      9 const kTestWidgetCount = 3;
     10 registerCleanupFunction(removeCustomToolbars);
     11 
     12 // unregisterArea should keep placements by default and restore them when re-adding the area
     13 add_task(async function () {
     14  let widgetIds = [];
     15  for (let i = 0; i < kTestWidgetCount; i++) {
     16    let id = kTestWidgetPfx + i;
     17    widgetIds.push(id);
     18    let spec = {
     19      id,
     20      type: "button",
     21      removable: true,
     22      label: "unregisterArea test",
     23      tooltiptext: "" + i,
     24    };
     25    CustomizableUI.createWidget(spec);
     26  }
     27  for (let i = kTestWidgetCount; i < kTestWidgetCount * 2; i++) {
     28    let id = kTestWidgetPfx + i;
     29    widgetIds.push(id);
     30    createDummyXULButton(id, "unregisterArea XUL test " + i);
     31  }
     32  let toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds);
     33  checkAbstractAndRealPlacements(toolbarNode, widgetIds);
     34 
     35  // Now move one of them:
     36  CustomizableUI.moveWidgetWithinArea(kTestWidgetPfx + kTestWidgetCount, 0);
     37  // Clone the array so we know this is the modified one:
     38  let modifiedWidgetIds = [...widgetIds];
     39  let movedWidget = modifiedWidgetIds.splice(kTestWidgetCount, 1)[0];
     40  modifiedWidgetIds.unshift(movedWidget);
     41 
     42  // Check it:
     43  checkAbstractAndRealPlacements(toolbarNode, modifiedWidgetIds);
     44 
     45  // Then unregister
     46  CustomizableUI.unregisterArea(kToolbarName);
     47 
     48  // Check we tell the outside world no dangerous things:
     49  checkWidgetFates(widgetIds);
     50  // Only then remove the real node
     51  toolbarNode.remove();
     52 
     53  // Now move one of the items to the palette, and another to the navbar:
     54  let lastWidget = modifiedWidgetIds.pop();
     55  CustomizableUI.removeWidgetFromArea(lastWidget);
     56  lastWidget = modifiedWidgetIds.pop();
     57  CustomizableUI.addWidgetToArea(lastWidget, CustomizableUI.AREA_NAVBAR);
     58 
     59  // Recreate ourselves with the default placements being the same:
     60  toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds);
     61  // Then check that after doing this, our actual placements match
     62  // the modified list, not the default one.
     63  checkAbstractAndRealPlacements(toolbarNode, modifiedWidgetIds);
     64 
     65  // Now remove completely:
     66  CustomizableUI.unregisterArea(kToolbarName, true);
     67  checkWidgetFates(modifiedWidgetIds);
     68  toolbarNode.remove();
     69 
     70  // One more time:
     71  // Recreate ourselves with the default placements being the same:
     72  toolbarNode = createToolbarWithPlacements(kToolbarName, widgetIds);
     73  // Should now be back to default:
     74  checkAbstractAndRealPlacements(toolbarNode, widgetIds);
     75  CustomizableUI.unregisterArea(kToolbarName, true);
     76  checkWidgetFates(widgetIds);
     77  toolbarNode.remove();
     78 
     79  // XXXgijs: ensure cleanup function doesn't barf:
     80  gAddedToolbars.delete(kToolbarName);
     81 
     82  // Remove all the XUL widgets, destroy the others:
     83  for (let widget of widgetIds) {
     84    let widgetWrapper = CustomizableUI.getWidget(widget);
     85    if (widgetWrapper.provider == CustomizableUI.PROVIDER_XUL) {
     86      gNavToolbox.palette.querySelector("#" + widget).remove();
     87    } else {
     88      CustomizableUI.destroyWidget(widget);
     89    }
     90  }
     91 });
     92 
     93 function checkAbstractAndRealPlacements(aNode, aExpectedPlacements) {
     94  assertAreaPlacements(kToolbarName, aExpectedPlacements);
     95  let physicalWidgetIds = Array.from(aNode.children, node => node.id);
     96  placementArraysEqual(aNode.id, physicalWidgetIds, aExpectedPlacements);
     97 }
     98 
     99 function checkWidgetFates(aWidgetIds) {
    100  for (let widget of aWidgetIds) {
    101    ok(
    102      !CustomizableUI.getPlacementOfWidget(widget),
    103      "Widget should be in palette"
    104    );
    105    ok(!document.getElementById(widget), "Widget should not be in the DOM");
    106    let widgetInPalette = !!gNavToolbox.palette.querySelector("#" + widget);
    107    let widgetProvider = CustomizableUI.getWidget(widget).provider;
    108    let widgetIsXULWidget = widgetProvider == CustomizableUI.PROVIDER_XUL;
    109    is(
    110      widgetInPalette,
    111      widgetIsXULWidget,
    112      "Just XUL Widgets should be in the palette"
    113    );
    114  }
    115 }
    116 
    117 add_task(async function asyncCleanup() {
    118  await resetCustomization();
    119 });