Bug 1229927 - Allow `showHeartbeat` to change the icon.r?MattN draft
authorFischer.json <fischer.json@gmail.com>
Tue, 07 Jun 2016 15:57:24 +0800
changeset 376498 854ca4931508707936bfcf5e28435393bea59ed8
parent 370474 5511d54a3f172c1d68f98cc55dce4de1d0ba1b51
child 523173 ce2f1afc3d6c646269ce8973266ec0a981a98235
push id20599
push userbmo:fliu@mozilla.com
push dateWed, 08 Jun 2016 06:29:00 +0000
reviewersMattN
bugs1229927
milestone49.0a1
Bug 1229927 - Allow `showHeartbeat` to change the icon.r?MattN MozReview-Commit-ID: 9by6Z0rN5IT
browser/components/uitour/UITour.jsm
browser/components/uitour/test/browser_UITour_heartbeat.js
--- a/browser/components/uitour/UITour.jsm
+++ b/browser/components/uitour/UITour.jsm
@@ -1251,19 +1251,36 @@ this.UITour = {
           ]));
 
           // Return true so that the notification bar doesn't close itself since
           // we have a thank you message to show.
           return true;
         },
       }];
     }
+
+    let defaultIcon = "chrome://browser/skin/heartbeat-icon.svg";
+    let iconURL = defaultIcon;
+    try {
+      // Take the optional icon URL if specified
+      if (aOptions.iconURL) {
+        iconURL = new URL(aOptions.iconURL);
+        // For now, only allow chrome URIs.
+        if (iconURL.protocol != "chrome:") {
+          iconURL = defaultIcon;
+          throw new Error("Invalid protocol");
+        }
+      }
+    } catch (error) {
+      log.error("showHeartbeat: Invalid icon URL specified.");
+    }
+
     // Create the notification. Prefix its ID to decrease the chances of collisions.
     let notice = nb.appendNotification(aOptions.message, "heartbeat-" + aOptions.flowId,
-                                       "chrome://browser/skin/heartbeat-icon.svg",
+                                       iconURL,
                                        nb.PRIORITY_INFO_HIGH, buttons,
                                        (aEventType) => {
                                          if (aEventType != "removed") {
                                            return;
                                          }
                                          // Let the consumer know the notification bar was closed.
                                          // This also happens after voting.
                                          maybeNotifyHeartbeat("Heartbeat:NotificationClosed");
--- a/browser/components/uitour/test/browser_UITour_heartbeat.js
+++ b/browser/components/uitour/test/browser_UITour_heartbeat.js
@@ -201,16 +201,61 @@ add_UITour_task(function* test_heartbeat
   info("'Heartbeat:TelemetrySent' notification received");
   checkTelemetry(data, flowId, ["offeredTS", "closedTS"]);
 
   // This rejects whenever an unexpected notification is received.
   yield receivedExpectedPromise;
 })
 
 /**
+ * Check that the heartbeat UI correctly takes optional icon URL.
+ */
+add_UITour_task(function* test_heartbeat_take_optional_icon_URL() {
+  let flowId = "ui-ratefirefox-" + Math.random();
+  let engagementURL = "http://example.com";
+  let iconURL = "chrome://branding/content/icon48.png";
+
+  // We need to call |gContentAPI.observe| at least once to set a valid |notificationListener|
+  // in UITour-lib.js, otherwise no message will get propagated.
+  gContentAPI.observe(() => {});
+
+  let receivedExpectedPromise = promiseWaitExpectedNotifications(
+    ["Heartbeat:NotificationOffered", "Heartbeat:NotificationClosed", "Heartbeat:TelemetrySent"]);
+
+  // Show the Heartbeat notification and wait for it to be displayed.
+  let shownPromise = promiseWaitHeartbeatNotification("Heartbeat:NotificationOffered");
+  gContentAPI.showHeartbeat("How would you rate Firefox?", "Thank you!", flowId, engagementURL, null, null, {
+    iconURL: iconURL
+  });
+
+  // Validate the returned timestamp.
+  let data = yield shownPromise;
+  validateTimestamp('Heartbeat:Offered', data.timestamp);
+
+  // Check the icon URL
+  let notification = getHeartbeatNotification(flowId);
+  is(notification.image, iconURL, "The optional icon URL is not taken correctly");
+
+  // Close the heartbeat notification.
+  let closedPromise = promiseWaitHeartbeatNotification("Heartbeat:NotificationClosed");
+  let pingSentPromise = promiseWaitHeartbeatNotification("Heartbeat:TelemetrySent");
+  cleanUpNotification(flowId);
+
+  data = yield closedPromise;
+  validateTimestamp('Heartbeat:NotificationClosed', data.timestamp);
+
+  data = yield pingSentPromise;
+  info("'Heartbeat:TelemetrySent' notification received");
+  checkTelemetry(data, flowId, ["offeredTS", "closedTS"]);
+
+  // This rejects whenever an unexpected notification is received.
+  yield receivedExpectedPromise;
+})
+
+/**
  * Test that the heartbeat UI correctly works with null engagement URL.
  */
 add_UITour_task(function* test_heartbeat_null_engagementURL() {
   let flowId = "ui-ratefirefox-" + Math.random();
   let originalTabCount = gBrowser.tabs.length;
 
   // We need to call |gContentAPI.observe| at least once to set a valid |notificationListener|
   // in UITour-lib.js, otherwise no message will get propagated.