summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_975719_customtoolbars_behaviour.js
blob: 73fc7c1ff9bcd05ccb3fdcfc79a9ecddbbda689b (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* 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";

requestLongerTimeout(2);

const kXULWidgetId = "a-test-button"; // we'll create a button with this ID.

add_task(function setup() {
  // create a XUL button and add it to the palette.
  createDummyXULButton(kXULWidgetId, "test-button");
});

add_task(function* customizeToolbarAndKeepIt() {
  ok(gNavToolbox.toolbarset, "There should be a toolbarset");
  let toolbarID = "testAustralisCustomToolbar";
  gNavToolbox.appendCustomToolbar(toolbarID, "");
  let toolbarDOMID = getToolboxCustomToolbarId(toolbarID);
  let toolbarElement = document.getElementById(toolbarDOMID);
  ok(toolbarElement, "There should be a toolbar");
  if (!toolbarElement) {
    ok(false, "No toolbar created, bailing out of the test.");
    return;
  }
  is(toolbarElement.nextSibling, gNavToolbox.toolbarset,
     "Toolbar should have been inserted in toolbox, before toolbarset element");
  let cuiAreaType = CustomizableUI.getAreaType(toolbarDOMID);
  is(cuiAreaType, CustomizableUI.TYPE_TOOLBAR,
     "CustomizableUI should know the area and think it's a toolbar");
  if (cuiAreaType != CustomizableUI.TYPE_TOOLBAR) {
    ok(false, "Toolbar not registered successfully, bailing out of the test.");
    toolbarElement.remove();
    return;
  }
  ok(!CustomizableUI.getWidgetIdsInArea(toolbarDOMID).length, "There should be no widgets in the area yet.");
  CustomizableUI.addWidgetToArea("open-file-button", toolbarDOMID, 0);
  ok(toolbarElement.hasChildNodes(), "Toolbar should now have a button.");
  assertAreaPlacements(toolbarDOMID, ["open-file-button"]);

  gNavToolbox.toolbarset.setAttribute("toolbar1", toolbarID + ":open-file-button");
  document.persist(gNavToolbox.toolbarset.id, "toolbar1");

  yield startCustomizing();
  // First, exit customize mode without doing anything, and verify the toolbar doesn't get removed.
  yield endCustomizing();
  ok(!CustomizableUI.inDefaultState, "Shouldn't be in default state, the toolbar should still be there.");
  cuiAreaType = CustomizableUI.getAreaType(toolbarDOMID);
  is(cuiAreaType, CustomizableUI.TYPE_TOOLBAR,
     "CustomizableUI should still know the area and think it's a toolbar");
  ok(toolbarElement.parentNode, "Toolbar should still be in the DOM.");
  ok(toolbarElement.hasChildNodes(), "Toolbar should still have items in it.");
  assertAreaPlacements(toolbarDOMID, ["open-file-button"]);

  let newWindow = yield openAndLoadWindow({}, true);
  is(newWindow.gNavToolbox.toolbarset.getAttribute("toolbar1"),
     gNavToolbox.toolbarset.getAttribute("toolbar1"),
     "Attribute should be the same in new window");
  yield promiseWindowClosed(newWindow);

  // Then customize again, and this time empty out the toolbar and verify it *does* get removed.
  yield startCustomizing();
  let openFileButton = document.getElementById("open-file-button");
  let palette = document.getElementById("customization-palette");
  simulateItemDrag(openFileButton, palette);
  ok(!CustomizableUI.inDefaultState, "Shouldn't be in default state because there's still a non-collapsed toolbar.");
  ok(!toolbarElement.hasChildNodes(), "Toolbar should have no more child nodes.");

  toolbarElement.collapsed = true;
  ok(CustomizableUI.inDefaultState, "Should be in default state because there's now just a collapsed toolbar.");
  toolbarElement.collapsed = false;
  ok(!CustomizableUI.inDefaultState, "Shouldn't be in default state because there's a non-collapsed toolbar again.");
  yield endCustomizing();
  ok(CustomizableUI.inDefaultState, "Should be in default state because the toolbar should have been removed.");

  newWindow = yield openAndLoadWindow({}, true);
  ok(!newWindow.gNavToolbox.toolbarset.hasAttribute("toolbar1"),
     "Attribute should be gone in new window");
  yield promiseWindowClosed(newWindow);

  ok(!toolbarElement.parentNode, "Toolbar should no longer be in the DOM.");
  cuiAreaType = CustomizableUI.getAreaType(toolbarDOMID);
  is(cuiAreaType, null, "CustomizableUI should have forgotten all about the area");
});

add_task(function* resetShouldDealWithCustomToolbars() {
  ok(gNavToolbox.toolbarset, "There should be a toolbarset");
  let toolbarID = "testAustralisCustomToolbar";
  gNavToolbox.appendCustomToolbar(toolbarID, "");
  let toolbarDOMID = getToolboxCustomToolbarId(toolbarID);
  let toolbarElement = document.getElementById(toolbarDOMID);
  ok(toolbarElement, "There should be a toolbar");
  if (!toolbarElement) {
    ok(false, "No toolbar created, bailing out of the test.");
    return;
  }
  is(toolbarElement.nextSibling, gNavToolbox.toolbarset,
     "Toolbar should have been inserted in toolbox, before toolbarset element");
  let cuiAreaType = CustomizableUI.getAreaType(toolbarDOMID);
  is(cuiAreaType, CustomizableUI.TYPE_TOOLBAR,
     "CustomizableUI should know the area and think it's a toolbar");
  if (cuiAreaType != CustomizableUI.TYPE_TOOLBAR) {
    ok(false, "Toolbar not registered successfully, bailing out of the test.");
    toolbarElement.remove();
    return;
  }
  ok(!CustomizableUI.getWidgetIdsInArea(toolbarDOMID).length, "There should be no widgets in the area yet.");
  CustomizableUI.addWidgetToArea(kXULWidgetId, toolbarDOMID, 0);
  ok(toolbarElement.hasChildNodes(), "Toolbar should now have a button.");
  assertAreaPlacements(toolbarDOMID, [kXULWidgetId]);

  gNavToolbox.toolbarset.setAttribute("toolbar2", `${toolbarID}:${kXULWidgetId}`);
  document.persist(gNavToolbox.toolbarset.id, "toolbar2");

  let newWindow = yield openAndLoadWindow({}, true);
  is(newWindow.gNavToolbox.toolbarset.getAttribute("toolbar2"),
     gNavToolbox.toolbarset.getAttribute("toolbar2"),
     "Attribute should be the same in new window");
  yield promiseWindowClosed(newWindow);

  CustomizableUI.reset();

  newWindow = yield openAndLoadWindow({}, true);
  ok(!newWindow.gNavToolbox.toolbarset.hasAttribute("toolbar2"),
     "Attribute should be gone in new window");
  yield promiseWindowClosed(newWindow);

  ok(CustomizableUI.inDefaultState, "Should be in default state after reset.");
  let xulButton = document.getElementById(kXULWidgetId);
  ok(!xulButton, "XUL button shouldn't be in the document anymore.");
  ok(gNavToolbox.palette.querySelector(`#${kXULWidgetId}`), "XUL button should be in the palette");
  ok(!toolbarElement.hasChildNodes(), "Toolbar should have no more child nodes.");
  ok(!toolbarElement.parentNode, "Toolbar should no longer be in the DOM.");
  cuiAreaType = CustomizableUI.getAreaType(toolbarDOMID);
  is(cuiAreaType, null, "CustomizableUI should have forgotten all about the area");
});


add_task(function*() {
  let newWin = yield openAndLoadWindow({}, true);
  ok(!newWin.gNavToolbox.toolbarset.hasAttribute("toolbar1"), "New window shouldn't have attribute toolbar1");
  ok(!newWin.gNavToolbox.toolbarset.hasAttribute("toolbar2"), "New window shouldn't have attribute toolbar2");
  yield promiseWindowClosed(newWin);
});