Bug 1135045: support notification bar buttons of type menu-button with a custom menupopup. r=Standard8
authorMike de Boer <mdeboer@mozilla.com>
Thu, 12 Mar 2015 15:13:35 +0100
changeset 250097 0761779e008bb8d4821c6a7107798f8929e6bde4
parent 250096 c154c877d4e473084f4d436f564e14594584739f
child 250098 77e37031bb11ee879383b6a8a347f8a7f3aa4fee
push id1010
push usermleibovic@mozilla.com
push dateThu, 12 Mar 2015 18:26:20 +0000
reviewersStandard8
bugs1135045
milestone39.0a1
Bug 1135045: support notification bar buttons of type menu-button with a custom menupopup. r=Standard8
toolkit/content/tests/chrome/test_notificationbox.xul
toolkit/content/widgets/notification.xml
--- a/toolkit/content/tests/chrome/test_notificationbox.xul
+++ b/toolkit/content/tests/chrome/test_notificationbox.xul
@@ -24,16 +24,18 @@ var testtag_notificationbox_buttons = [
   {
     label: "Button 1",
     accesskey: "u",
     callback: testtag_notificationbox_buttonpressed,
     popup: "menupopup"
   }
 ];
 
+var NSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+
 function testtag_notificationbox_buttonpressed(event)
 {
 }
 
 function testtag_notificationbox(nb)
 {
   testtag_notificationbox_State(nb, "initial", null, 0);
 
@@ -127,16 +129,53 @@ var tests =
       nb.removeNotification(ntf);
       return ntf;
     },
     result: function(nb, ntf) {
       testtag_notificationbox_State(nb, "removeNotification with callback",
                                     null, 0);
 
       testtag_notification_eventCallback("removed", ntf, "removeNotification()");
+      return ntf;
+    }
+  },
+  {
+    test: function(nb, ntf) {
+      // Create a popup to be used by a menu-button.
+      var doc = nb.ownerDocument;
+      var menuPopup = doc.createElementNS(NSXUL, "menupopup");
+      var menuItem = menuPopup.appendChild(doc.createElementNS(NSXUL, "menuitem"));
+      menuItem.setAttribute("label", "Menu Item");
+      // Append a notification with a button of type 'menu-button'.
+      ntf = nb.appendNotification(
+        "Notification", "note", "happy.png",
+        nb.PRIORITY_WARNING_LOW,
+        [{
+          label: "Button",
+          type: "menu-button",
+          popup: menuPopup
+        }]
+      );
+
+      return ntf;
+    },
+    result: function(nb, ntf) {
+      testtag_notificationbox_State(nb, "append", ntf, 1);
+      testtag_notification_State(nb, ntf, "append", "Notification", "note",
+                                 "happy.png", nb.PRIORITY_WARNING_LOW);
+      var button = ntf.querySelector(".notification-button");
+      SimpleTest.ise(button.type, "menu-button", "Button type should be set");
+      var menuPopup = button.getElementsByTagNameNS(NSXUL, "menupopup");
+      SimpleTest.ise(menuPopup.length, 1, "There should be a menu attached");
+      var menuItem = menuPopup[0].firstChild;
+      SimpleTest.ise(menuItem.localName, "menuitem", "There should be a menu item");
+      SimpleTest.ise(menuItem.getAttribute("label"), "Menu Item", "Label should match");
+      // Clean up.
+      nb.removeNotification(ntf);
+
       return [1, null];
     }
   },
   {
     repeat: true,
     test: function(nb, arr) {
       var idx = arr[0];
       var ntf = arr[1];
--- a/toolkit/content/widgets/notification.xml
+++ b/toolkit/content/widgets/notification.xml
@@ -118,16 +118,26 @@
               // set to false). There cannot be multiple default buttons.
               var defaultElem;
 
               for (var b = 0; b < aButtons.length; b++) {
                 var button = aButtons[b];
                 var buttonElem = document.createElementNS(XULNS, "button");
                 buttonElem.setAttribute("label", button.label);
                 buttonElem.setAttribute("accesskey", button.accessKey);
+                if (typeof button.type == "string") {
+                  buttonElem.setAttribute("type", button.type);
+                  if ((button.type == "menu-button" || button.type == "menu") &&
+                      "popup" in button) {
+                    buttonElem.appendChild(button.popup);
+                    delete button.popup;
+                  }
+                  if (typeof button.anchor == "string")
+                    buttonElem.setAttribute("anchor", button.anchor);
+                }
                 buttonElem.classList.add("notification-button");
 
                 if (button.isDefault ||
                     b == 0 && !("isDefault" in button))
                   defaultElem = buttonElem;
 
                 newitem.appendChild(buttonElem);
                 buttonElem.buttonInfo = button;