summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_1042100_default_placements_update.js
blob: c20546c300e412cebc0289cfcf82af37f0dd36fe (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function getSavedStatePlacements(area) {
  return CustomizableUI.getTestOnlyInternalProp("gSavedState").placements[area];
}

// NB: This uses some ugly hacks to get into the CUI module from elsewhere...
// don't try this at home, kids.
function test() {
  // Customize something to make sure stuff changed:
  CustomizableUI.addWidgetToArea(
    "save-page-button",
    CustomizableUI.AREA_NAVBAR
  );

  let oldState = CustomizableUI.getTestOnlyInternalProp("gSavedState");
  registerCleanupFunction(() =>
    CustomizableUI.setTestOnlyInternalProp("gSavedState", oldState)
  );

  let gFuturePlacements =
    CustomizableUI.getTestOnlyInternalProp("gFuturePlacements");
  is(
    gFuturePlacements.size,
    0,
    "All future placements should be dealt with by now."
  );

  let gPalette = CustomizableUI.getTestOnlyInternalProp("gPalette");
  let CustomizableUIInternal = CustomizableUI.getTestOnlyInternalProp(
    "CustomizableUIInternal"
  );
  CustomizableUIInternal._updateForNewVersion();
  is(gFuturePlacements.size, 0, "No change to future placements initially.");

  let currentVersion = CustomizableUI.getTestOnlyInternalProp("kVersion");

  // Add our widget to the defaults:
  let testWidgetNew = {
    id: "test-messing-with-default-placements-new",
    label: "Test messing with default placements - should be inserted",
    defaultArea: CustomizableUI.AREA_NAVBAR,
    introducedInVersion: currentVersion + 1,
  };

  let normalizedWidget = CustomizableUIInternal.normalizeWidget(
    testWidgetNew,
    CustomizableUI.SOURCE_BUILTIN
  );
  ok(normalizedWidget, "Widget should be normalizable");
  if (!normalizedWidget) {
    return;
  }
  gPalette.set(testWidgetNew.id, normalizedWidget);

  let testWidgetOld = {
    id: "test-messing-with-default-placements-old",
    label: "Test messing with default placements - should NOT be inserted",
    defaultArea: CustomizableUI.AREA_NAVBAR,
    introducedInVersion: currentVersion,
  };

  normalizedWidget = CustomizableUIInternal.normalizeWidget(
    testWidgetOld,
    CustomizableUI.SOURCE_BUILTIN
  );
  ok(normalizedWidget, "Widget should be normalizable");
  if (!normalizedWidget) {
    return;
  }
  gPalette.set(testWidgetOld.id, normalizedWidget);

  // Now increase the version in the module:
  CustomizableUI.setTestOnlyInternalProp(
    "kVersion",
    CustomizableUI.getTestOnlyInternalProp("kVersion") + 1
  );

  let hadSavedState = !!CustomizableUI.getTestOnlyInternalProp("gSavedState");
  if (!hadSavedState) {
    CustomizableUI.setTestOnlyInternalProp("gSavedState", {
      currentVersion: CustomizableUI.getTestOnlyInternalProp("kVersion") - 1,
    });
  }

  // Then call the re-init routine so we re-add the builtin widgets
  CustomizableUIInternal._updateForNewVersion();
  is(gFuturePlacements.size, 1, "Should have 1 more future placement");
  let haveNavbarPlacements = gFuturePlacements.has(CustomizableUI.AREA_NAVBAR);
  ok(haveNavbarPlacements, "Should have placements for nav-bar");
  if (haveNavbarPlacements) {
    let placements = [...gFuturePlacements.get(CustomizableUI.AREA_NAVBAR)];

    // Ignore widgets that are placed using the pref facility and not the
    // versioned facility.  They're independent of kVersion and the saved
    // state's current version, so they may be present in the placements.
    for (let i = 0; i < placements.length; ) {
      if (placements[i] == testWidgetNew.id) {
        i++;
        continue;
      }
      let pref = "browser.toolbarbuttons.introduced." + placements[i];
      let introduced = Services.prefs.getBoolPref(pref, false);
      if (!introduced) {
        i++;
        continue;
      }
      placements.splice(i, 1);
    }

    is(placements.length, 1, "Should have 1 newly placed widget in nav-bar");
    is(
      placements[0],
      testWidgetNew.id,
      "Should have our test widget to be placed in nav-bar"
    );
  }

  // Reset kVersion
  CustomizableUI.setTestOnlyInternalProp(
    "kVersion",
    CustomizableUI.getTestOnlyInternalProp("kVersion") - 1
  );

  // Now test that the builtin photon migrations work:

  CustomizableUI.setTestOnlyInternalProp("gSavedState", {
    currentVersion: 6,
    placements: {
      "nav-bar": ["urlbar-container", "bookmarks-menu-button"],
      "PanelUI-contents": ["panic-button", "edit-controls"],
    },
  });
  Services.prefs.setIntPref("browser.proton.toolbar.version", 0);
  CustomizableUIInternal._updateForNewVersion();
  CustomizableUIInternal._updateForNewProtonVersion();
  {
    let navbarPlacements = getSavedStatePlacements("nav-bar");
    let springs = navbarPlacements.filter(id => id.includes("spring"));
    is(springs.length, 2, "Should have 2 toolbarsprings in placements now");
    navbarPlacements = navbarPlacements.filter(id => !id.includes("spring"));
    Assert.deepEqual(
      navbarPlacements,
      [
        "back-button",
        "forward-button",
        "stop-reload-button",
        "urlbar-container",
        "downloads-button",
        "fxa-toolbar-menu-button",
      ],
      "Nav-bar placements should be correct."
    );

    Assert.deepEqual(getSavedStatePlacements("widget-overflow-fixed-list"), [
      "panic-button",
    ]);
  }

  // Finally, test the downloads and fxa avatar button migrations work.
  let oldNavbarPlacements = [
    "urlbar-container",
    "customizableui-special-spring3",
    "search-container",
  ];
  CustomizableUI.setTestOnlyInternalProp("gSavedState", {
    currentVersion: 10,
    placements: {
      "nav-bar": Array.from(oldNavbarPlacements),
      "widget-overflow-fixed-list": ["downloads-button"],
    },
  });
  CustomizableUIInternal._updateForNewVersion();
  Assert.deepEqual(
    getSavedStatePlacements("nav-bar"),
    oldNavbarPlacements.concat(["downloads-button", "fxa-toolbar-menu-button"]),
    "Downloads button inserted in navbar"
  );
  Assert.deepEqual(
    getSavedStatePlacements("widget-overflow-fixed-list"),
    [],
    "Overflow panel is empty"
  );

  CustomizableUI.setTestOnlyInternalProp("gSavedState", {
    currentVersion: 10,
    placements: {
      "nav-bar": ["downloads-button"].concat(oldNavbarPlacements),
    },
  });
  CustomizableUIInternal._updateForNewVersion();
  Assert.deepEqual(
    getSavedStatePlacements("nav-bar"),
    oldNavbarPlacements.concat(["downloads-button", "fxa-toolbar-menu-button"]),
    "Downloads button reinserted in navbar"
  );

  oldNavbarPlacements = [
    "urlbar-container",
    "customizableui-special-spring3",
    "search-container",
    "other-widget",
  ];
  CustomizableUI.setTestOnlyInternalProp("gSavedState", {
    currentVersion: 10,
    placements: {
      "nav-bar": Array.from(oldNavbarPlacements),
    },
  });
  CustomizableUIInternal._updateForNewVersion();
  let expectedNavbarPlacements = [
    "urlbar-container",
    "customizableui-special-spring3",
    "search-container",
    "downloads-button",
    "other-widget",
    "fxa-toolbar-menu-button",
  ];
  Assert.deepEqual(
    getSavedStatePlacements("nav-bar"),
    expectedNavbarPlacements,
    "Downloads button inserted in navbar before other widgets"
  );

  gFuturePlacements.delete(CustomizableUI.AREA_NAVBAR);
  gPalette.delete(testWidgetNew.id);
  gPalette.delete(testWidgetOld.id);
}