dom/tests/mochitest/notification/test_notification_storage.html
author Alexandre Lissy <lissyx@lissyx.dyndns.org>
Mon, 18 May 2015 09:49:00 +0200
changeset 260535 fe2c942655ec
parent 246964 fa8bd3194aca
child 450272 52fbf40d3e7d
permissions -rw-r--r--
Bug 960762 - Fix intermittence of Notification mochitests. r=mhenretty, a=test-only

<!DOCTYPE HTML>
<html>
<head>
  <title>Notification Basics</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>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script type="text/javascript">

  SimpleTest.requestFlakyTimeout("untriaged");

  function deleteAllNotifications(done) {
    var promise = Notification.get();
    promise.then(function (notifications) {
      notifications.forEach(function(notification) {
        notification.close();
      });
      done();
    });
  }

  var info = NotificationTest.info;

  var steps = [
    function (done) {
      info("Test that Notifcation.get fulfills the promise");
      var promise = Notification.get();
      ok(promise.then, "should return a promise");

      // Create a new notification to make sure
      // Notification.get() works while creating
      var notification = new Notification("this is a test");

      promise.then(function () {
        ok(true, "promise should be fulfilled");
        done();
      });
    },

    deleteAllNotifications,

    function (done) {
      info("Test adding a notification, and making sure get returns it");
      NotificationTest.allowNotifications();
      var options = NotificationTest.payload;

      var notification = new Notification("This is a title", options);
      var promise = Notification.get();
      promise.then(function (notifications) {
        ok(notifications.length, "should return notifications");
        for (var i = 0; i < notifications.length; i++) {
          var notification = notifications[i];
          if (notification.tag === options.tag) {
            ok(true, "should contain newly created notification");
            for (var key in options) {
              if (key === "data") {
                ok(NotificationTest.customDataMatches(notification.data),
                   "data property should match");
                continue;
              }
              is(notification[key], options[key], key + " property should match");
            }
            notification.close();
            return;
          }
        }
        ok(false, "should contain newly created notification");
        notification.close();
      });
      notification.onclose = done;
    },

    function (done) {
      info("Testing fetching notification by tag filter");
      var n1 = new Notification("title1", {tag: "tag1"});
      var n2 = new Notification("title2", {tag: "tag2"});
      var n3 = new Notification("title3", {tag: "tag3"});
      var promise = Notification.get({tag: "tag3"});
      promise.then(function (notifications) {
        var notification = notifications[0];
        is(notifications.length, 1, "should return 1 notification");
        is(notifications[0].title, "title3", "titles should match");
        is(notifications[0].tag, "tag3", "tags should match");
        var closeCount = 0;
        var waitForAll = function () {
          if (++closeCount >= 3) {
            done();
          }
        };
        n1.onclose = waitForAll;
        n2.onclose = waitForAll;
        n3.onclose = waitForAll;
        n1.close();
        n2.close();
        n3.close();
      });
    },

    deleteAllNotifications,

    function (done) {
      info("Testing fetching no notifications");
      var promise = Notification.get();
      promise.then(function (notifications) {
        is(notifications.length, 0, "should return 0 notifications");
        done();
      });
    },

    function (done) {
      info("Testing fetching multiple notifications");
      var n1 = new Notification("title1");
      var n2 = new Notification("title2");
      var n3 = new Notification("title3");
      var promise = Notification.get();
      promise.then(function (notifications) {
        is(notifications.length, 3, "should return 3 notifications");
        n1.close();
        n2.close();
        n3.close();
        done();
      });
    },

    deleteAllNotifications,

    function (done) {
      info("Testing 'alertfinished' removes the notification from DB");
      var n = new Notification("test-title" + Math.random());
      n.onclose = function() {
        Notification.get().then(function(notifications) {
          is(notifications.length, 0, "should return 0 notifications");
          done();
        });
      };
      info("Installing 'onshow' for " + n.title);
      n.onshow = function() {
        info("Triggered 'onshow' for " + n.title);
        window.setTimeout(function() {
          NotificationTest.fireCloseEvent(n.title);
        }, 100);
      };
    }
  ];

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