Bug 1042100 - add test for _introduceNewBuiltinWidgets, r=Unfocused
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 25 Jul 2014 12:07:20 +0100
changeset 195936 443c1f32324e4557b9cd7b90d53a3470dae1960a
parent 195935 e8f997d315e0ee726f1413f5e709c1449fa3f960
child 195937 7c7c65151eaf273a21a315742db7e09742af389c
push id27203
push userkwierso@gmail.com
push dateFri, 25 Jul 2014 22:06:33 +0000
treeherdermozilla-central@a77e22d72a04 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersUnfocused
bugs1042100
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1042100 - add test for _introduceNewBuiltinWidgets, r=Unfocused
browser/components/customizableui/src/CustomizableUI.jsm
browser/components/customizableui/test/browser.ini
browser/components/customizableui/test/browser_1042100_default_placements_update.js
--- a/browser/components/customizableui/src/CustomizableUI.jsm
+++ b/browser/components/customizableui/src/CustomizableUI.jsm
@@ -45,18 +45,19 @@ const kPrefDrawInTitlebar            = "
  */
 const kSubviewEvents = [
   "ViewShowing",
   "ViewHiding"
 ];
 
 /**
  * The current version. We can use this to auto-add new default widgets as necessary.
+ * (would be const but isn't because of testing purposes)
  */
-const kVersion = 0;
+let kVersion = 0;
 
 /**
  * gPalette is a map of every widget that CustomizableUI.jsm knows about, keyed
  * on their IDs.
  */
 let gPalette = new Map();
 
 /**
@@ -267,18 +268,16 @@ let CustomizableUIInternal = {
       CustomizableUI.AREA_ADDONBAR,
 #ifndef XP_MACOSX
       CustomizableUI.AREA_MENUBAR,
 #endif
     ]);
   },
 
   _defineBuiltInWidgets: function() {
-    //XXXunf Need to figure out how to auto-add new builtin widgets in new
-    //       app versions to already customized areas.
     for (let widgetDefinition of CustomizableWidgets) {
       this.createBuiltinWidget(widgetDefinition);
     }
   },
 
   _introduceNewBuiltinWidgets: function() {
     if (!gSavedState || gSavedState.currentVersion >= kVersion) {
       return;
--- a/browser/components/customizableui/test/browser.ini
+++ b/browser/components/customizableui/test/browser.ini
@@ -111,10 +111,11 @@ skip-if = os == "linux"
 [browser_987640_charEncoding.js]
 [browser_992747_toggle_noncustomizable_toolbar.js]
 [browser_993322_widget_notoolbar.js]
 [browser_995164_registerArea_during_customize_mode.js]
 [browser_996364_registerArea_different_properties.js]
 [browser_996635_remove_non_widgets.js]
 [browser_1003588_no_specials_in_panel.js]
 [browser_1008559_anchor_undo_restore.js]
+[browser_1042100_default_placements_update.js]
 [browser_bootstrapped_custom_toolbar.js]
 [browser_panel_toggle.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/customizableui/test/browser_1042100_default_placements_update.js
@@ -0,0 +1,86 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+// 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("feed-button", CustomizableUI.AREA_NAVBAR);
+
+  // Check what version we're on:
+  let CustomizableUIBSPass = Cu.import("resource:///modules/CustomizableUI.jsm", {});
+
+  is(CustomizableUIBSPass.gFuturePlacements.size, 0,
+     "All future placements should be dealt with by now.");
+
+  let {CustomizableUIInternal, gFuturePlacements, gPalette} = CustomizableUIBSPass;
+  CustomizableUIInternal._introduceNewBuiltinWidgets();
+  is(gFuturePlacements.size, 0,
+     "No change to future placements initially.");
+
+  let currentVersion = CustomizableUIBSPass.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;
+  }
+  CustomizableUIBSPass.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;
+  }
+  CustomizableUIBSPass.gPalette.set(testWidgetOld.id, normalizedWidget);
+
+
+  // Now increase the version in the module:
+  CustomizableUIBSPass.kVersion++;
+
+  let hadSavedState = !!CustomizableUIBSPass.gSavedState
+  if (!hadSavedState) {
+    CustomizableUIBSPass.gSavedState = {currentVersion: CustomizableUIBSPass.kVersion - 1};
+  }
+
+  // Then call the re-init routine so we re-add the builtin widgets
+  CustomizableUIInternal._introduceNewBuiltinWidgets();
+  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)];
+    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");
+  }
+
+  gFuturePlacements.delete(CustomizableUI.AREA_NAVBAR);
+  CustomizableUIBSPass.kVersion--;
+  gPalette.delete(testWidgetNew.id);
+  gPalette.delete(testWidgetOld.id);
+  if (!hadSavedState) {
+    CustomizableUIBSPass.gSavedState = null;
+  }
+}
+