summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/notification/test_notification_storage.html
blob: ce5530cb8790bba5bb71f527882ebef0ec6fa61d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!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>