Bug 1111027 - Add tests for the showHeartbeat UITour function. r=MattN
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Tue, 27 Jan 2015 23:58:35 -0800
changeset 239447 b80b6de33ff295dd8bb61b663a4837d072ce16a0
parent 239446 c93764d04a2401b64fa1b5044b462db7917cc63c
child 239448 10bc2f59111b4cfbab4d37f9815d2bff4f14668e
push id497
push usermleibovic@mozilla.com
push dateWed, 28 Jan 2015 16:43:37 +0000
reviewersMattN
bugs1111027
milestone38.0a1
Bug 1111027 - Add tests for the showHeartbeat UITour function. r=MattN
browser/components/uitour/test/browser.ini
browser/components/uitour/test/browser_UITour_heartbeat.js
--- a/browser/components/uitour/test/browser.ini
+++ b/browser/components/uitour/test/browser.ini
@@ -12,16 +12,18 @@ skip-if = e10s # Bug 941428 - UITour.jsm
 # [browser_UITour3.js] Bug 1113038
 # skip-if = os == "linux" || e10s # Linux: Bug 986760, Bug 989101; e10s: Bug 941428 - UITour.jsm not e10s friendly
 [browser_UITour_availableTargets.js]
 skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly
 [browser_UITour_detach_tab.js]
 skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly
 [browser_UITour_annotation_size_attributes.js]
 skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly.
+[browser_UITour_heartbeat.js]
+skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly.
 [browser_UITour_loop.js]
 skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly.
 [browser_UITour_modalDialog.js]
 run-if = os == "mac" # modal dialog disabling only working on OS X
 skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly
 [browser_UITour_observe.js]
 skip-if = e10s # Bug 941428 - UITour.jsm not e10s friendly.
 [browser_UITour_panel_close_annotation.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/uitour/test/browser_UITour_heartbeat.js
@@ -0,0 +1,231 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+let gTestTab;
+let gContentAPI;
+let gContentWindow;
+let notificationBox = document.getElementById("high-priority-global-notificationbox");
+
+Components.utils.import("resource:///modules/UITour.jsm");
+
+function test() {
+  UITourTest();
+}
+
+/**
+ * Simulate a click on a rating element in the Heartbeat notification.
+ *
+ * @param aId
+ *        The id of the notification box.
+ * @param aScore
+ *        The score related to the rating element we want to click on.
+ */
+function simulateVote(aId, aScore) {
+  // UITour.jsm prefixes the notification box ID with "heartbeat-" to prevent collisions.
+  let notification = notificationBox.getNotificationWithValue("heartbeat-" + aId);
+
+  let ratingContainer = notification.childNodes[0];
+  ok(ratingContainer, "The notification has a valid rating container.");
+
+  let ratingElement = ratingContainer.getElementsByAttribute("data-score", aScore);
+  ok(ratingElement[0], "The rating container contains the requested rating element.");
+
+  ratingElement[0].click();
+}
+
+/**
+ * Remove the notification box.
+ *
+ * @param aId
+ *        The id of the notification box to remove.
+ */
+function cleanUpNotification(aId) {
+  let notification = notificationBox.getNotificationWithValue("heartbeat-" + aId);
+  notificationBox.removeNotification(notification);
+}
+
+let tests = [
+  /**
+   * Check that the "stars" heartbeat UI correctly shows and closes.
+   */
+  function test_heartbeat_stars_show(done) {
+    let flowId = "ui-ratefirefox-" + Math.random();
+    let engagementURL = "http://example.com";
+
+    gContentAPI.observe(function (aEventName, aData) {
+      switch (aEventName) {
+        case "Heartbeat:NotificationOffered": {
+          info("'Heartbeat:Offered' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          cleanUpNotification(flowId);
+          break;
+        }
+        case "Heartbeat:NotificationClosed": {
+          info("'Heartbeat:NotificationClosed' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          done();
+          break;
+        }
+        default:
+          // We are not expecting other states for this test.
+          ok(false, "Unexpected notification received: " + aEventName);
+      }
+    });
+
+    gContentAPI.showHeartbeat("How would you rate Firefox?", "Thank you!", flowId, engagementURL);
+  },
+
+  /**
+   * Test that the heartbeat UI correctly works with null engagement URL.
+   */
+  function test_heartbeat_null_engagementURL(done) {
+    let flowId = "ui-ratefirefox-" + Math.random();
+    let originalTabCount = gBrowser.tabs.length;
+
+    gContentAPI.observe(function (aEventName, aData) {
+      switch (aEventName) {
+        case "Heartbeat:NotificationOffered": {
+          info("'Heartbeat:Offered' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          // The UI was just shown. We can simulate a click on a rating element (i.e., "star").
+          simulateVote(flowId, 2);
+          break;
+        }
+        case "Heartbeat:Voted": {
+          info("'Heartbeat:Voted' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          break;
+        }
+        case "Heartbeat:NotificationClosed": {
+          info("'Heartbeat:NotificationClosed' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          is(gBrowser.tabs.length, originalTabCount, "No engagement tab should be opened.");
+          done();
+          break;
+        }
+        default:
+          // We are not expecting other states for this test.
+          ok(false, "Unexpected notification received: " + aEventName);
+      }
+    });
+
+    gContentAPI.showHeartbeat("How would you rate Firefox?", "Thank you!", flowId, null);
+  },
+
+   /**
+   * Test that the heartbeat UI correctly works with an invalid, but non null, engagement URL.
+   */
+  function test_heartbeat_invalid_engagement_URL(done) {
+    let flowId = "ui-ratefirefox-" + Math.random();
+    let originalTabCount = gBrowser.tabs.length;
+    let invalidEngagementURL = "invalidEngagement";
+
+    gContentAPI.observe(function (aEventName, aData) {
+      switch (aEventName) {
+        case "Heartbeat:NotificationOffered": {
+          info("'Heartbeat:Offered' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          // The UI was just shown. We can simulate a click on a rating element (i.e., "star").
+          simulateVote(flowId, 2);
+          break;
+        }
+        case "Heartbeat:Voted": {
+          info("'Heartbeat:Voted' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          break;
+        }
+        case "Heartbeat:NotificationClosed": {
+          info("'Heartbeat:NotificationClosed' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          is(gBrowser.tabs.length, originalTabCount, "No engagement tab should be opened.");
+          done();
+          break;
+        }
+        default:
+          // We are not expecting other states for this test.
+          ok(false, "Unexpected notification received: " + aEventName);
+      }
+    });
+
+    gContentAPI.showHeartbeat("How would you rate Firefox?", "Thank you!", flowId, invalidEngagementURL);
+  },
+
+  /**
+   * Test that the score is correctly reported.
+   */
+  function test_heartbeat_stars_vote(done) {
+    const expectedScore = 4;
+    let flowId = "ui-ratefirefox-" + Math.random();
+
+    gContentAPI.observe(function (aEventName, aData) {
+      switch (aEventName) {
+        case "Heartbeat:NotificationOffered": {
+          info("'Heartbeat:Offered' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          // The UI was just shown. We can simulate a click on a rating element (i.e., "star").
+          simulateVote(flowId, expectedScore);
+          break;
+        }
+        case "Heartbeat:Voted": {
+          info("'Heartbeat:Voted' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          is(aData.score, expectedScore, "Should report a score of " + expectedScore);
+          done();
+          break;
+        }
+        case "Heartbeat:NotificationClosed": {
+          info("'Heartbeat:NotificationClosed' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          break;
+        }
+        default:
+          // We are not expecting other states for this test.
+          ok(false, "Unexpected notification received: " + aEventName);
+      }
+    });
+
+    gContentAPI.showHeartbeat("How would you rate Firefox?", "Thank you!", flowId, null);
+  },
+
+  /**
+   * Test that the engagement page is correctly opened when voting.
+   */
+  function test_heartbeat_engagement_tab(done) {
+    let engagementURL = "http://example.com";
+    let flowId = "ui-ratefirefox-" + Math.random();
+    let originalTabCount = gBrowser.tabs.length;
+    const expectedTabCount = originalTabCount + 1;
+
+    gContentAPI.observe(function (aEventName, aData) {
+      switch (aEventName) {
+        case "Heartbeat:NotificationOffered": {
+          info("'Heartbeat:Offered' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          // The UI was just shown. We can simulate a click on a rating element (i.e., "star").
+          simulateVote(flowId, 1);
+          break;
+        }
+        case "Heartbeat:Voted": {
+          info("'Heartbeat:Voted' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          break;
+        }
+        case "Heartbeat:NotificationClosed": {
+          info("'Heartbeat:NotificationClosed' notification received (timestamp " + aData.timestamp.toString() + ").");
+          ok(Number.isFinite(aData.timestamp), "Timestamp must be a number.");
+          is(gBrowser.tabs.length, expectedTabCount, "Engagement URL should open in a new tab.");
+          gBrowser.removeCurrentTab();
+          done();
+          break;
+        }
+        default:
+          // We are not expecting other states for this test.
+          ok(false, "Unexpected notification received: " + aEventName);
+      }
+    });
+
+    gContentAPI.showHeartbeat("How would you rate Firefox?", "Thank you!", flowId, engagementURL);
+  }
+];