|
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 TOOLBARID = "test-toolbar-added-during-customize-mode"; |
|
8 |
|
9 add_task(function*() { |
|
10 yield startCustomizing(); |
|
11 let toolbar = createToolbarWithPlacements(TOOLBARID, []); |
|
12 CustomizableUI.addWidgetToArea("sync-button", TOOLBARID); |
|
13 let syncButton = document.getElementById("sync-button"); |
|
14 ok(syncButton, "Sync button should exist."); |
|
15 is(syncButton.parentNode.localName, "toolbarpaletteitem", "Sync button's parent node should be a wrapper."); |
|
16 |
|
17 simulateItemDrag(syncButton, gNavToolbox.palette); |
|
18 ok(!CustomizableUI.getPlacementOfWidget("sync-button"), "Button moved to the palette"); |
|
19 ok(gNavToolbox.palette.querySelector("#sync-button"), "Sync button really is in palette."); |
|
20 |
|
21 simulateItemDrag(syncButton, toolbar); |
|
22 ok(CustomizableUI.getPlacementOfWidget("sync-button"), "Button moved out of palette"); |
|
23 is(CustomizableUI.getPlacementOfWidget("sync-button").area, TOOLBARID, "Button's back on toolbar"); |
|
24 ok(toolbar.querySelector("#sync-button"), "Sync button really is on toolbar."); |
|
25 |
|
26 yield endCustomizing(); |
|
27 isnot(syncButton.parentNode.localName, "toolbarpaletteitem", "Sync button's parent node should not be a wrapper outside customize mode."); |
|
28 yield startCustomizing(); |
|
29 |
|
30 is(syncButton.parentNode.localName, "toolbarpaletteitem", "Sync button's parent node should be a wrapper back in customize mode."); |
|
31 |
|
32 simulateItemDrag(syncButton, gNavToolbox.palette); |
|
33 ok(!CustomizableUI.getPlacementOfWidget("sync-button"), "Button moved to the palette"); |
|
34 ok(gNavToolbox.palette.querySelector("#sync-button"), "Sync button really is in palette."); |
|
35 |
|
36 ok(!CustomizableUI.inDefaultState, "Not in default state while toolbar is not collapsed yet."); |
|
37 setToolbarVisibility(toolbar, false); |
|
38 ok(CustomizableUI.inDefaultState, "In default state while toolbar is collapsed."); |
|
39 |
|
40 setToolbarVisibility(toolbar, true); |
|
41 |
|
42 info("Check that removing the area registration from within customize mode works"); |
|
43 CustomizableUI.unregisterArea(TOOLBARID); |
|
44 ok(CustomizableUI.inDefaultState, "Now that the toolbar is no longer registered, should be in default state."); |
|
45 ok(!(new Set(gCustomizeMode.areas)).has(toolbar), "Toolbar shouldn't be known to customize mode."); |
|
46 |
|
47 CustomizableUI.registerArea(TOOLBARID, {legacy: true, defaultPlacements: []}); |
|
48 CustomizableUI.registerToolbarNode(toolbar, []); |
|
49 ok(!CustomizableUI.inDefaultState, "Now that the toolbar is registered again, should no longer be in default state."); |
|
50 ok((new Set(gCustomizeMode.areas)).has(toolbar), "Toolbar should be known to customize mode again."); |
|
51 |
|
52 simulateItemDrag(syncButton, toolbar); |
|
53 ok(CustomizableUI.getPlacementOfWidget("sync-button"), "Button moved out of palette"); |
|
54 is(CustomizableUI.getPlacementOfWidget("sync-button").area, TOOLBARID, "Button's back on toolbar"); |
|
55 ok(toolbar.querySelector("#sync-button"), "Sync button really is on toolbar."); |
|
56 |
|
57 let otherWin = yield openAndLoadWindow({}, true); |
|
58 let otherTB = otherWin.document.createElementNS(kNSXUL, "toolbar"); |
|
59 otherTB.id = TOOLBARID; |
|
60 otherTB.setAttribute("customizable", "true"); |
|
61 let wasInformedCorrectlyOfAreaAppearing = false; |
|
62 let listener = { |
|
63 onAreaNodeRegistered: function(aArea, aNode) { |
|
64 if (aNode == otherTB) { |
|
65 wasInformedCorrectlyOfAreaAppearing = true; |
|
66 } |
|
67 } |
|
68 }; |
|
69 CustomizableUI.addListener(listener); |
|
70 otherWin.gNavToolbox.appendChild(otherTB); |
|
71 ok(wasInformedCorrectlyOfAreaAppearing, "Should have been told area was registered."); |
|
72 CustomizableUI.removeListener(listener); |
|
73 |
|
74 ok(otherTB.querySelector("#sync-button"), "Sync button is on other toolbar, too."); |
|
75 |
|
76 simulateItemDrag(syncButton, gNavToolbox.palette); |
|
77 ok(!CustomizableUI.getPlacementOfWidget("sync-button"), "Button moved to the palette"); |
|
78 ok(gNavToolbox.palette.querySelector("#sync-button"), "Sync button really is in palette."); |
|
79 ok(!otherTB.querySelector("#sync-button"), "Sync button is in palette in other window, too."); |
|
80 |
|
81 simulateItemDrag(syncButton, toolbar); |
|
82 ok(CustomizableUI.getPlacementOfWidget("sync-button"), "Button moved out of palette"); |
|
83 is(CustomizableUI.getPlacementOfWidget("sync-button").area, TOOLBARID, "Button's back on toolbar"); |
|
84 ok(toolbar.querySelector("#sync-button"), "Sync button really is on toolbar."); |
|
85 ok(otherTB.querySelector("#sync-button"), "Sync button is on other toolbar, too."); |
|
86 |
|
87 let wasInformedCorrectlyOfAreaDisappearing = false; |
|
88 //XXXgijs So we could be using promiseWindowClosed here. However, after |
|
89 // repeated random oranges, I'm instead relying on onWindowClosed below to |
|
90 // fire appropriately - it is linked to an unload event as well, and so |
|
91 // reusing it prevents a potential race between unload handlers where the |
|
92 // one from promiseWindowClosed could fire before the onWindowClosed |
|
93 // (and therefore onAreaNodeRegistered) one, causing the test to fail. |
|
94 let windowCloseDeferred = Promise.defer(); |
|
95 listener = { |
|
96 onAreaNodeUnregistered: function(aArea, aNode, aReason) { |
|
97 if (aArea == TOOLBARID) { |
|
98 is(aNode, otherTB, "Should be informed about other toolbar"); |
|
99 is(aReason, CustomizableUI.REASON_WINDOW_CLOSED, "Reason should be correct."); |
|
100 wasInformedCorrectlyOfAreaDisappearing = (aReason === CustomizableUI.REASON_WINDOW_CLOSED); |
|
101 } |
|
102 }, |
|
103 onWindowClosed: function(aWindow) { |
|
104 if (aWindow == otherWin) { |
|
105 windowCloseDeferred.resolve(aWindow); |
|
106 } else { |
|
107 info("Other window was closed!"); |
|
108 info("Other window title: " + (aWindow.document && aWindow.document.title)); |
|
109 info("Our window title: " + (otherWin.document && otherWin.document.title)); |
|
110 } |
|
111 }, |
|
112 }; |
|
113 CustomizableUI.addListener(listener); |
|
114 otherWin.close(); |
|
115 let windowClosed = yield windowCloseDeferred.promise; |
|
116 |
|
117 is(windowClosed, otherWin, "Window should have sent onWindowClosed notification."); |
|
118 ok(wasInformedCorrectlyOfAreaDisappearing, "Should be told about window closing."); |
|
119 // Closing the other window should not be counted against this window's customize mode: |
|
120 is(syncButton.parentNode.localName, "toolbarpaletteitem", "Sync button's parent node should still be a wrapper."); |
|
121 isnot(gCustomizeMode.areas.indexOf(toolbar), -1, "Toolbar should still be a customizable area for this customize mode instance."); |
|
122 |
|
123 yield gCustomizeMode.reset(); |
|
124 |
|
125 yield endCustomizing(); |
|
126 |
|
127 CustomizableUI.removeListener(listener); |
|
128 wasInformedCorrectlyOfAreaDisappearing = false; |
|
129 listener = { |
|
130 onAreaNodeUnregistered: function(aArea, aNode, aReason) { |
|
131 if (aArea == TOOLBARID) { |
|
132 is(aNode, toolbar, "Should be informed about this window's toolbar"); |
|
133 is(aReason, CustomizableUI.REASON_AREA_UNREGISTERED, "Reason for final removal should be correct."); |
|
134 wasInformedCorrectlyOfAreaDisappearing = (aReason === CustomizableUI.REASON_AREA_UNREGISTERED); |
|
135 } |
|
136 }, |
|
137 } |
|
138 CustomizableUI.addListener(listener); |
|
139 removeCustomToolbars(); |
|
140 ok(wasInformedCorrectlyOfAreaDisappearing, "Should be told about area being unregistered."); |
|
141 CustomizableUI.removeListener(listener); |
|
142 ok(CustomizableUI.inDefaultState, "Should be fine after exiting customize mode."); |
|
143 }); |