summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_947987_removable_default.js
blob: 1c657eeed85a97b3b4082aaba8530aa50cd5ea6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* 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";

var kWidgetId = "test-removable-widget-default";
const kNavBar = CustomizableUI.AREA_NAVBAR;
var widgetCounter = 0;

registerCleanupFunction(removeCustomToolbars);

// Sanity checks
add_task(function() {
  let brokenSpec = { id: kWidgetId + widgetCounter++, removable: false };
  SimpleTest.doesThrow(
    () => CustomizableUI.createWidget(brokenSpec),
    "Creating non-removable widget without defaultArea should throw."
  );

  // Widget without removable set should be removable:
  let wrapper = CustomizableUI.createWidget({
    id: kWidgetId + widgetCounter++,
  });
  ok(
    CustomizableUI.isWidgetRemovable(wrapper.id),
    "Should be removable by default."
  );
  CustomizableUI.destroyWidget(wrapper.id);
});

// Test non-removable widget with defaultArea
add_task(async function() {
  // Non-removable widget with defaultArea should work:
  let spec = {
    id: kWidgetId + widgetCounter++,
    removable: false,
    defaultArea: kNavBar,
  };
  let widgetWrapper;
  try {
    widgetWrapper = CustomizableUI.createWidget(spec);
  } catch (ex) {
    ok(
      false,
      "Creating a non-removable widget with a default area should not throw."
    );
    return;
  }

  let placement = CustomizableUI.getPlacementOfWidget(spec.id);
  ok(placement, "Widget should be placed.");
  is(placement.area, kNavBar, "Widget should be in navbar");
  let singleWrapper = widgetWrapper.forWindow(window);
  ok(singleWrapper, "Widget should exist in window.");
  ok(singleWrapper.node, "Widget node should exist in window.");
  let expectedParent = CustomizableUI.getCustomizeTargetForArea(
    kNavBar,
    window
  );
  is(
    singleWrapper.node.parentNode,
    expectedParent,
    "Widget should be in navbar."
  );

  let otherWin = await openAndLoadWindow(true);
  placement = CustomizableUI.getPlacementOfWidget(spec.id);
  ok(placement, "Widget should be placed.");
  is(placement && placement.area, kNavBar, "Widget should be in navbar");

  singleWrapper = widgetWrapper.forWindow(otherWin);
  ok(singleWrapper, "Widget should exist in other window.");
  if (singleWrapper) {
    ok(singleWrapper.node, "Widget node should exist in other window.");
    if (singleWrapper.node) {
      let expectedParentInOtherWin = CustomizableUI.getCustomizeTargetForArea(
        kNavBar,
        otherWin
      );
      is(
        singleWrapper.node.parentNode,
        expectedParentInOtherWin,
        "Widget should be in navbar in other window."
      );
    }
  }
  CustomizableUI.destroyWidget(spec.id);
  await promiseWindowClosed(otherWin);
});

add_task(async function asyncCleanup() {
  await resetCustomization();
});