browser_1042100_default_placements_update.js (7608B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 function getSavedStatePlacements(area) { 7 return CustomizableUI.getTestOnlyInternalProp("gSavedState").placements[area]; 8 } 9 10 // NB: This uses some ugly hacks to get into the CUI module from elsewhere... 11 // don't try this at home, kids. 12 function test() { 13 // Customize something to make sure stuff changed: 14 CustomizableUI.addWidgetToArea( 15 "save-page-button", 16 CustomizableUI.AREA_NAVBAR 17 ); 18 19 let oldState = CustomizableUI.getTestOnlyInternalProp("gSavedState"); 20 registerCleanupFunction(() => 21 CustomizableUI.setTestOnlyInternalProp("gSavedState", oldState) 22 ); 23 24 let gFuturePlacements = 25 CustomizableUI.getTestOnlyInternalProp("gFuturePlacements"); 26 is( 27 gFuturePlacements.size, 28 0, 29 "All future placements should be dealt with by now." 30 ); 31 32 let gPalette = CustomizableUI.getTestOnlyInternalProp("gPalette"); 33 let CustomizableUIInternal = CustomizableUI.getTestOnlyInternalProp( 34 "CustomizableUIInternal" 35 ); 36 CustomizableUIInternal.updateForNewVersion(); 37 is(gFuturePlacements.size, 0, "No change to future placements initially."); 38 39 let currentVersion = CustomizableUI.getTestOnlyInternalProp("kVersion"); 40 41 // Add our widget to the defaults: 42 let testWidgetNew = { 43 id: "test-messing-with-default-placements-new", 44 label: "Test messing with default placements - should be inserted", 45 defaultArea: CustomizableUI.AREA_NAVBAR, 46 introducedInVersion: currentVersion + 1, 47 }; 48 49 let normalizedWidget = CustomizableUIInternal.normalizeWidget( 50 testWidgetNew, 51 CustomizableUI.SOURCE_BUILTIN 52 ); 53 ok(normalizedWidget, "Widget should be normalizable"); 54 if (!normalizedWidget) { 55 return; 56 } 57 gPalette.set(testWidgetNew.id, normalizedWidget); 58 59 let testWidgetOld = { 60 id: "test-messing-with-default-placements-old", 61 label: "Test messing with default placements - should NOT be inserted", 62 defaultArea: CustomizableUI.AREA_NAVBAR, 63 introducedInVersion: currentVersion, 64 }; 65 66 normalizedWidget = CustomizableUIInternal.normalizeWidget( 67 testWidgetOld, 68 CustomizableUI.SOURCE_BUILTIN 69 ); 70 ok(normalizedWidget, "Widget should be normalizable"); 71 if (!normalizedWidget) { 72 return; 73 } 74 gPalette.set(testWidgetOld.id, normalizedWidget); 75 76 // Now increase the version in the module: 77 CustomizableUI.setTestOnlyInternalProp( 78 "kVersion", 79 CustomizableUI.getTestOnlyInternalProp("kVersion") + 1 80 ); 81 82 let hadSavedState = !!CustomizableUI.getTestOnlyInternalProp("gSavedState"); 83 if (!hadSavedState) { 84 CustomizableUI.setTestOnlyInternalProp("gSavedState", { 85 currentVersion: CustomizableUI.getTestOnlyInternalProp("kVersion") - 1, 86 }); 87 } 88 89 // Then call the re-init routine so we re-add the builtin widgets 90 CustomizableUIInternal.updateForNewVersion(); 91 is(gFuturePlacements.size, 1, "Should have 1 more future placement"); 92 let haveNavbarPlacements = gFuturePlacements.has(CustomizableUI.AREA_NAVBAR); 93 ok(haveNavbarPlacements, "Should have placements for nav-bar"); 94 if (haveNavbarPlacements) { 95 let placements = [...gFuturePlacements.get(CustomizableUI.AREA_NAVBAR)]; 96 97 // Ignore widgets that are placed using the pref facility and not the 98 // versioned facility. They're independent of kVersion and the saved 99 // state's current version, so they may be present in the placements. 100 for (let i = 0; i < placements.length; ) { 101 if (placements[i] == testWidgetNew.id) { 102 i++; 103 continue; 104 } 105 let pref = "browser.toolbarbuttons.introduced." + placements[i]; 106 let introduced = Services.prefs.getBoolPref(pref, false); 107 if (!introduced) { 108 i++; 109 continue; 110 } 111 placements.splice(i, 1); 112 } 113 114 is(placements.length, 1, "Should have 1 newly placed widget in nav-bar"); 115 is( 116 placements[0], 117 testWidgetNew.id, 118 "Should have our test widget to be placed in nav-bar" 119 ); 120 } 121 122 // Reset kVersion 123 CustomizableUI.setTestOnlyInternalProp( 124 "kVersion", 125 CustomizableUI.getTestOnlyInternalProp("kVersion") - 1 126 ); 127 128 // Now test that the builtin photon migrations work: 129 130 CustomizableUI.setTestOnlyInternalProp("gSavedState", { 131 currentVersion: 6, 132 placements: { 133 "nav-bar": ["urlbar-container", "bookmarks-menu-button"], 134 "PanelUI-contents": ["panic-button", "edit-controls"], 135 }, 136 }); 137 Services.prefs.setIntPref("browser.proton.toolbar.version", 0); 138 CustomizableUIInternal.updateForNewVersion(); 139 CustomizableUIInternal.updateForNewProtonVersion(); 140 { 141 let navbarPlacements = getSavedStatePlacements("nav-bar"); 142 let springs = navbarPlacements.filter(id => id.includes("spring")); 143 is(springs.length, 2, "Should have 2 toolbarsprings in placements now"); 144 navbarPlacements = navbarPlacements.filter(id => !id.includes("spring")); 145 Assert.deepEqual( 146 navbarPlacements, 147 [ 148 "back-button", 149 "forward-button", 150 "stop-reload-button", 151 "vertical-spacer", 152 "urlbar-container", 153 "downloads-button", 154 "fxa-toolbar-menu-button", 155 "reset-pbm-toolbar-button", 156 ], 157 "Nav-bar placements should be correct." 158 ); 159 160 Assert.deepEqual(getSavedStatePlacements("widget-overflow-fixed-list"), [ 161 "panic-button", 162 ]); 163 } 164 165 // Finally, test the downloads and fxa avatar button migrations work. 166 let oldNavbarPlacements = [ 167 "vertical-spacer", 168 "urlbar-container", 169 "customizableui-special-spring3", 170 "search-container", 171 ]; 172 CustomizableUI.setTestOnlyInternalProp("gSavedState", { 173 currentVersion: 10, 174 placements: { 175 "nav-bar": Array.from(oldNavbarPlacements), 176 "widget-overflow-fixed-list": ["downloads-button"], 177 }, 178 }); 179 CustomizableUIInternal.updateForNewVersion(); 180 Assert.deepEqual( 181 getSavedStatePlacements("nav-bar"), 182 oldNavbarPlacements.concat([ 183 "downloads-button", 184 "fxa-toolbar-menu-button", 185 "reset-pbm-toolbar-button", 186 ]), 187 "Downloads button inserted in navbar" 188 ); 189 Assert.deepEqual( 190 getSavedStatePlacements("widget-overflow-fixed-list"), 191 [], 192 "Overflow panel is empty" 193 ); 194 195 CustomizableUI.setTestOnlyInternalProp("gSavedState", { 196 currentVersion: 10, 197 placements: { 198 "nav-bar": ["downloads-button"].concat(oldNavbarPlacements), 199 }, 200 }); 201 CustomizableUIInternal.updateForNewVersion(); 202 Assert.deepEqual( 203 getSavedStatePlacements("nav-bar"), 204 oldNavbarPlacements.concat([ 205 "downloads-button", 206 "fxa-toolbar-menu-button", 207 "reset-pbm-toolbar-button", 208 ]), 209 "Downloads button reinserted in navbar" 210 ); 211 212 oldNavbarPlacements = [ 213 "vertical-spacer", 214 "urlbar-container", 215 "customizableui-special-spring3", 216 "search-container", 217 "other-widget", 218 ]; 219 CustomizableUI.setTestOnlyInternalProp("gSavedState", { 220 currentVersion: 10, 221 placements: { 222 "nav-bar": Array.from(oldNavbarPlacements), 223 }, 224 }); 225 CustomizableUIInternal.updateForNewVersion(); 226 let expectedNavbarPlacements = [ 227 "vertical-spacer", 228 "urlbar-container", 229 "customizableui-special-spring3", 230 "search-container", 231 "downloads-button", 232 "other-widget", 233 "fxa-toolbar-menu-button", 234 "reset-pbm-toolbar-button", 235 ]; 236 Assert.deepEqual( 237 getSavedStatePlacements("nav-bar"), 238 expectedNavbarPlacements, 239 "Downloads button inserted in navbar before other widgets" 240 ); 241 242 gFuturePlacements.delete(CustomizableUI.AREA_NAVBAR); 243 gPalette.delete(testWidgetNew.id); 244 gPalette.delete(testWidgetOld.id); 245 }