Bug 1071238 - UI Tour: add ability to put a widget in the toolbar. r=mattn a=dolske
authorJustin Dolske <dolske@mozilla.com>
Mon, 06 Oct 2014 15:49:30 -0700
changeset 225698 1c96180e6a5b
parent 225697 d37b92959827
child 225699 9d4b08eecd9a
push id3985
push userbmcbride@mozilla.com
push date2014-10-16 01:27 +0000
treeherdermozilla-beta@9d4b08eecd9a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattn, dolske
bugs1071238
milestone34.0
Bug 1071238 - UI Tour: add ability to put a widget in the toolbar. r=mattn a=dolske
browser/modules/UITour.jsm
browser/modules/test/browser_UITour.js
browser/modules/test/uitour.js
--- a/browser/modules/UITour.jsm
+++ b/browser/modules/UITour.jsm
@@ -94,17 +94,20 @@ this.UITour = {
                                                         "class",
                                                         "toolbarbutton-icon");
       },
       widgetName: "PanelUI-customize",
     }],
     ["help",        {query: "#PanelUI-help"}],
     ["home",        {query: "#home-button"}],
     ["loop",        {query: "#loop-call-button"}],
-    ["forget",      {query: "#panic-button"}],
+    ["forget", {
+      query: "#panic-button",
+      widgetName: "panic-button",
+      allowAdd: true }],
     ["privateWindow",  {query: "#privatebrowsing-button"}],
     ["quit",        {query: "#PanelUI-quit"}],
     ["search",      {
       query: "#searchbar",
       widgetName: "search-container",
     }],
     ["searchProvider", {
       query: (aDocument) => {
@@ -424,16 +427,25 @@ this.UITour = {
 
       case "showFirefoxAccounts": {
         // 'signup' is the only action that makes sense currently, so we don't
         // accept arbitrary actions just to be safe...
         // We want to replace the current tab.
         contentDocument.location.href = "about:accounts?action=signup";
         break;
       }
+
+      case "addNavBarWidget": {
+        // Add a widget to the toolbar
+        let targetPromise = this.getTarget(window, data.name);
+        targetPromise.then(target => {
+          this.addNavBarWidget(target, contentDocument, data.callbackID);
+        }).then(null, Cu.reportError);
+        break;
+      }
     }
 
     if (!this.originTabs.has(window))
       this.originTabs.set(window, new Set());
 
     this.originTabs.get(window).add(tab);
     tab.addEventListener("TabClose", this);
     tab.addEventListener("TabBecomingWindow", this);
@@ -697,16 +709,17 @@ this.UITour = {
       }
 
       deferred.resolve({
         addTargetListener: targetObject.addTargetListener,
         node: node,
         removeTargetListener: targetObject.removeTargetListener,
         targetName: aTargetName,
         widgetName: targetObject.widgetName,
+        allowAdd: targetObject.allowAdd,
       });
     }).then(null, Cu.reportError);
     return deferred.promise;
   },
 
   targetIsInAppMenu: function(aTarget) {
     let placement = CustomizableUI.getPlacementOfWidget(aTarget.widgetName || aTarget.node.id);
     if (placement && placement.area == CustomizableUI.AREA_PANEL) {
@@ -1170,16 +1183,34 @@ this.UITour = {
     }, (err) => {
       Cu.reportError(err);
       this.sendPageCallback(aContentDocument, aCallbackID, {
         targets: [],
       });
     });
   },
 
+  addNavBarWidget: function (aTarget, aContentDocument, aCallbackID) {
+    if (aTarget.node) {
+      Cu.reportError("UITour: can't add a widget already present: " + data.target);
+      return;
+    }
+    if (!aTarget.allowAdd) {
+      Cu.reportError("UITour: not allowed to add this widget: " + data.target);
+      return;
+    }
+    if (!aTarget.widgetName) {
+      Cu.reportError("UITour: can't add a widget without a widgetName property: " + data.target);
+      return;
+    }
+
+    CustomizableUI.addWidgetToArea(aTarget.widgetName, CustomizableUI.AREA_NAVBAR);
+    this.sendPageCallback(aContentDocument, aCallbackID);
+  },
+
   _addAnnotationPanelMutationObserver: function(aPanelEl) {
 #ifdef XP_LINUX
     let observer = this._annotationPanelMutationObservers.get(aPanelEl);
     if (observer) {
       return;
     }
     let win = aPanelEl.ownerDocument.defaultView;
     observer = new win.MutationObserver(this._annotationMutationCallback);
--- a/browser/modules/test/browser_UITour.js
+++ b/browser/modules/test/browser_UITour.js
@@ -260,15 +260,40 @@ let tests = [
         is(desc.textContent, "search text", "Popup should have correct description text");
 
         done();
       });
     });
 
     gContentAPI.showInfo("urlbar", "urlbar title", "urlbar text");
   },
+  function test_addToolbarButton(done) {
+    let placement = CustomizableUI.getPlacementOfWidget("panic-button");
+    is(placement, null, "default UI has panic button in the palette");
+
+    gContentAPI.getConfiguration("availableTargets", (data) => {
+      let available = (data.targets.indexOf("forget") != -1);
+      ok(!available, "Forget button should not be available by default");
+
+      gContentAPI.addNavBarWidget("forget", () => {
+        info("addNavBarWidget callback successfully called");
+
+        let placement = CustomizableUI.getPlacementOfWidget("panic-button");
+        is(placement.area, CustomizableUI.AREA_NAVBAR);
+
+        gContentAPI.getConfiguration("availableTargets", (data) => {
+          let available = (data.targets.indexOf("forget") != -1);
+          ok(available, "Forget button should now be available");
+
+          // Cleanup
+          CustomizableUI.removeWidgetFromArea("panic-button");
+          done();
+        });
+      });
+    });
+  },
 
   // Make sure this test is last in the file so the appMenu gets left open and done will confirm it got tore down.
   function cleanupMenus(done) {
     gContentAPI.showMenu("appMenu");
     done();
   },
 ];
--- a/browser/modules/test/uitour.js
+++ b/browser/modules/test/uitour.js
@@ -174,9 +174,16 @@ if (typeof Mozilla == 'undefined') {
 			configuration: configName,
 		});
 	};
 
 	Mozilla.UITour.showFirefoxAccounts = function() {
 		_sendEvent('showFirefoxAccounts');
 	};
 
+	Mozilla.UITour.addNavBarWidget= function(name, callback) {
+		_sendEvent('addNavBarWidget', {
+			name: name,
+			callbackID: _waitForCallback(callback),
+		});
+	};
+
 })();