|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Notification Basics</title> |
|
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <script type="text/javascript" src="MockServices.js"></script> |
|
7 <script type="text/javascript" src="NotificationTest.js"></script> |
|
8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
9 </head> |
|
10 <body> |
|
11 <p id="display"></p> |
|
12 <div id="content" style="display: none"></div> |
|
13 <pre id="test"></pre> |
|
14 <script type="text/javascript"> |
|
15 |
|
16 function deleteAllNotifications() { |
|
17 var promise = Notification.get(); |
|
18 promise.then(function (notifications) { |
|
19 notifications.forEach(function(notification) { |
|
20 notification.close(); |
|
21 }); |
|
22 }); |
|
23 } |
|
24 |
|
25 var info = NotificationTest.info; |
|
26 |
|
27 var steps = [ |
|
28 function (done) { |
|
29 info("Test that Notifcation.get fulfills the promise"); |
|
30 var promise = Notification.get(); |
|
31 ok(promise.then, "should return a promise"); |
|
32 |
|
33 // Create a new notification to make sure |
|
34 // Notification.get() works while creating |
|
35 var notification = new Notification("this is a test"); |
|
36 |
|
37 promise.then(function () { |
|
38 ok(true, "promise should be fulfilled"); |
|
39 done(); |
|
40 }); |
|
41 }, |
|
42 |
|
43 deleteAllNotifications, |
|
44 |
|
45 function (done) { |
|
46 info("Test adding a notification, and making sure get returns it"); |
|
47 NotificationTest.allowNotifications(); |
|
48 var options = { |
|
49 dir: "auto", |
|
50 lang: "", |
|
51 body: "This is a notification body", |
|
52 tag: "sometag", |
|
53 icon: "icon.png" |
|
54 }; |
|
55 var notification = new Notification("This is a title", options); |
|
56 var promise = Notification.get(); |
|
57 promise.then(function (notifications) { |
|
58 ok(notifications.length, "should return notifications"); |
|
59 for (var i = 0; i < notifications.length; i++) { |
|
60 var notification = notifications[i]; |
|
61 if (notification.tag === options.tag) { |
|
62 ok(true, "should contain newly created notification"); |
|
63 for (var key in options) { |
|
64 is(notification[key], options[key], key + " property should match"); |
|
65 } |
|
66 notification.close(); |
|
67 return; |
|
68 } |
|
69 } |
|
70 ok(false, "should contain newly created notification"); |
|
71 notification.close(); |
|
72 }); |
|
73 notification.onclose = done; |
|
74 }, |
|
75 |
|
76 function (done) { |
|
77 info("Testing fetching notification by tag filter"); |
|
78 var n1 = new Notification("title1", {tag: "tag1"}); |
|
79 var n2 = new Notification("title2", {tag: "tag2"}); |
|
80 var n3 = new Notification("title3", {tag: "tag3"}); |
|
81 var promise = Notification.get({tag: "tag3"}); |
|
82 promise.then(function (notifications) { |
|
83 var notification = notifications[0]; |
|
84 is(notifications.length, 1, "should return 1 notification"); |
|
85 is(notifications[0].title, "title3", "titles should match"); |
|
86 is(notifications[0].tag, "tag3", "tags should match"); |
|
87 var closeCount = 0; |
|
88 var waitForAll = function () { |
|
89 if (++closeCount >= 3) { |
|
90 done(); |
|
91 } |
|
92 }; |
|
93 n1.onclose = waitForAll; |
|
94 n2.onclose = waitForAll; |
|
95 n3.onclose = waitForAll; |
|
96 n1.close(); |
|
97 n2.close(); |
|
98 n3.close(); |
|
99 }); |
|
100 }, |
|
101 |
|
102 deleteAllNotifications, |
|
103 |
|
104 function (done) { |
|
105 info("Testing fetching no notifications"); |
|
106 var promise = Notification.get(); |
|
107 promise.then(function (notifications) { |
|
108 is(notifications.length, 0, "should return 0 notifications"); |
|
109 done(); |
|
110 }); |
|
111 }, |
|
112 |
|
113 function (done) { |
|
114 info("Testing fetching multiple notifications"); |
|
115 var n1 = new Notification("title1"); |
|
116 var n2 = new Notification("title2"); |
|
117 var n3 = new Notification("title3"); |
|
118 var promise = Notification.get(); |
|
119 promise.then(function (notifications) { |
|
120 is(notifications.length, 3, "should return 2 notifications"); |
|
121 done(); |
|
122 }); |
|
123 } |
|
124 ]; |
|
125 |
|
126 MockServices.register(); |
|
127 NotificationTest.run(steps, function () { |
|
128 MockServices.unregister(); |
|
129 }); |
|
130 </script> |
|
131 </body> |
|
132 </html> |