dom/tests/mochitest/notification/test_notification_resend.html
author Alexandre Lissy <lissyx@lissyx.dyndns.org>
Thu, 05 Jun 2014 03:00:00 -0400
changeset 186861 d0737ba3f4c9c612f67b9e26ce95e53818d029ed
parent 182229 21d743a319c659b9290b714846fd0752ec3f3e71
child 200728 812981a7ba3f897ca8a3067c9baa48fc274ea5b4
permissions -rw-r--r--
Bug 1020581 - Notification resend test feedback improvement and less strict on timestamp. r=mhenretty

<!DOCTYPE HTML>
<html>
<head>
  <title>Testing mozResendAllNotifications() resend behavior</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="MockServices.js"></script>
  <script type="text/javascript" src="NotificationTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=967475">Bug 967475</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script type="text/javascript">
  var info = NotificationTest.info;
  var now = new Date().getTime();
  var notifications = [];
  var manifestURL;

  var steps = [
    function (done) {
      if (window.Notification) {
        SpecialPowers.pushPrefEnv({"set": [
          ["dom.ignore_webidl_scope_checks", true],
          ]}, done);
      } else {
        ok(true, "Notifications are not enabled on the platform.");
        done();
      }
    },

    function (done) {
      info("Set manifestURL");
      var request = window.navigator.mozApps.getSelf();
      request.onsuccess = function() {
        if (request.result) {
          manifestURL = request.result.manifestURL;
        } else {
          manifestURL = window.location.origin;
        }
        info("Value of manifestURL is: " + manifestURL);
        done();
      };
    },

    function (done) {
      info("Test that we have mozChromeNotifications API");
      ok(('mozChromeNotifications' in navigator), "should have mozChromeNotifications API");
      ok(('mozResendAllNotifications' in navigator.mozChromeNotifications), "should have mozResendAllNotifications()");
      done();
    },

    function (done) {
      info("Making sure we have no previous notification pending");
      var promise = Notification.get();
      promise.then(function (notifications) {
        is(notifications.length, 0, "notifications are all cleaned");
        done();
      });
    },

    function (done) {
      info("Sending one notification");
      var notif = new Notification("title");
      ok(notif, "Notification object is valid");
      notifications.push(notif);

      var promise = Notification.get();
      promise.then(function (notifications) {
        is(notifications.length, 1, "one notification has been sent");
        done();
      });
    },

    function (done) {
      info("Trying to resend the notification");
      var notif = notifications.pop();
      notif.onclose = function() {
        done();
      };

      navigator.mozChromeNotifications.mozResendAllNotifications(function(number) {
        is(number, 1, "One notification resent");
        notif.close();
      });
    },

    function (done) {
      info("Sending two notifications, closing one");
      var notif1 = new Notification("title1");
      ok(notif1, "Notification object is valid");
      notif1.onclose = function() {
        done();
      };

      var payload = {
        body: "Body",
        tag: "fakeTag",
        icon: "icon.jpg",
        lang: "en-US",
        dir: "ltr"
      };
      var notif2 = new Notification("Title2", payload);
      ok(notif2, "Notification object is valid");
      notifications.push(notif2);

      var promise = Notification.get();
      promise.then(function (notifications) {
        is(notifications.length, 2, "two notifications have been sent");
        notif1.close();
      });
    },

    function (done) {
      info("Checking if only notif2 is resent");
      navigator.mozChromeNotifications.mozResendAllNotifications(function(number) {
        is(number, 1, "One notification resent");
        var promise = Notification.get();
        promise.then(function (notifications) {
          is(notifications.length, 1, "one notification still available");
          is(notifications[0].title, "Title2", "notification title is 'Title2'");
          done();
        });
      });
    },

    function (done) {
      info("Checking ShowAppNotification behavior");

      var notif2 = notifications.pop();
      notif2.onclose = function() {
        done();
      };

      navigator.mozChromeNotifications.mozResendAllNotifications(function(number) {
        is(number, 1, "One notification resent");
        var appNotifs = MockServices.activeAppNotifications;
        var alertNotifs = MockServices.activeAlertNotifications;
        var nbAppNotifs = Object.keys(appNotifs).length;
        var nbAlertNotifs = Object.keys(alertNotifs).length;
        is(nbAppNotifs, 1, "AlertsServices has one app notification");
        is(nbAlertNotifs, 1, "AlertsServices has one alert notification");

        var uuidRegEx = /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/;
        var notif = appNotifs[Object.keys(appNotifs)[0]];
        ok(notif, "Notification object is valid");
        ok((typeof notif.observer === "object"), "Notification observer is valid");
        is(notif.title, "Title2", "Notification title is valid: " + notif.title);
        is(notif.text, "Body", "Notification body is valid: " + notif.text);
        is(notif.manifestURL, manifestURL, "Notification manifest URL is valid: " + notif.manifestURL);
        is(notif.imageURL, "icon.jpg", "Notification icon URL is valid: " + notif.imageURL);
        is(notif.lang, "en-US", "Notification lang is valid: " + notif.lang);
        is(notif.id, notif.manifestURL + "#tag:" + notif.tag, "Notification id is valid: " + notif.id);
        ok(notif.dbId.match(uuidRegEx), "Notification dbId is valid: " + notif.dbId);
        is(notif.dir, "ltr", "Notification dir is valid: " + notif.dir);
        is(notif.tag, "fakeTag", "Notification tag is valid: " + notif.tag);
        ok((notif.timestamp >= now), "Notification timestamp is valid: (" + notif.timestamp + " >= " + now + ")");
        notif2.close();
      });
    }
  ];

  MockServices.register();
  NotificationTest.run(steps, function () {
    MockServices.unregister();
  });
</script>
</body>
</html>