|
1 <?xml version="1.0"?> |
|
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> |
|
4 <!-- |
|
5 XUL Widget Test for notificationbox |
|
6 --> |
|
7 <window title="Notification Box" width="500" height="600" |
|
8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> |
|
9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
10 |
|
11 <notificationbox id="nb"/> |
|
12 <menupopup id="menupopup" onpopupshown="this.hidePopup()" onpopuphidden="checkPopupClosed()"> |
|
13 <menuitem label="One"/> |
|
14 </menupopup> |
|
15 |
|
16 <!-- test results are displayed in the html:body --> |
|
17 <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/> |
|
18 |
|
19 <!-- test code goes here --> |
|
20 <script type="application/javascript"><![CDATA[ |
|
21 SimpleTest.waitForExplicitFinish(); |
|
22 |
|
23 var testtag_notificationbox_buttons = [ |
|
24 { |
|
25 label: "Button 1", |
|
26 accesskey: "u", |
|
27 callback: testtag_notificationbox_buttonpressed, |
|
28 popup: "menupopup" |
|
29 } |
|
30 ]; |
|
31 |
|
32 function testtag_notificationbox_buttonpressed(event) |
|
33 { |
|
34 } |
|
35 |
|
36 function testtag_notificationbox(nb) |
|
37 { |
|
38 testtag_notificationbox_State(nb, "initial", null, 0); |
|
39 |
|
40 SimpleTest.ise(nb.notificationsHidden, false, "initial notificationsHidden"); |
|
41 SimpleTest.ise(nb.removeAllNotifications(false), undefined, "initial removeAllNotifications"); |
|
42 testtag_notificationbox_State(nb, "initial removeAllNotifications", null, 0); |
|
43 SimpleTest.ise(nb.removeAllNotifications(true), undefined, "initial removeAllNotifications immediate"); |
|
44 testtag_notificationbox_State(nb, "initial removeAllNotifications immediate", null, 0); |
|
45 |
|
46 runTimedTests(tests, -1, nb, null); |
|
47 } |
|
48 |
|
49 var notification_last_event, notification_last_event_item; |
|
50 function notification_eventCallback(event) |
|
51 { |
|
52 notification_last_event = event; |
|
53 notification_last_event_item = this; |
|
54 } |
|
55 |
|
56 function testtag_notification_eventCallback(expectedEvent, ntf, testName) |
|
57 { |
|
58 SimpleTest.is(notification_last_event, expectedEvent, |
|
59 testName + ": event name"); |
|
60 SimpleTest.is(notification_last_event_item, ntf, |
|
61 testName + ": event item"); |
|
62 |
|
63 notification_last_event = null; |
|
64 notification_last_event_item = null; |
|
65 } |
|
66 |
|
67 var tests = |
|
68 [ |
|
69 { |
|
70 test: function(nb, ntf) { |
|
71 // append a new notification |
|
72 var ntf = nb.appendNotification("Notification", "note", "happy.png", |
|
73 nb.PRIORITY_INFO_LOW, testtag_notificationbox_buttons); |
|
74 SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification"); |
|
75 return ntf; |
|
76 }, |
|
77 result: function(nb, ntf) { |
|
78 testtag_notificationbox_State(nb, "append", ntf, 1); |
|
79 testtag_notification_State(nb, ntf, "append", "Notification", "note", |
|
80 "happy.png", nb.PRIORITY_INFO_LOW); |
|
81 |
|
82 // check the getNotificationWithValue method |
|
83 var ntf_found = nb.getNotificationWithValue("note"); |
|
84 SimpleTest.ise(ntf, ntf_found, "getNotificationWithValue note"); |
|
85 |
|
86 var none_found = nb.getNotificationWithValue("notenone"); |
|
87 SimpleTest.ise(none_found, null, "getNotificationWithValue null"); |
|
88 return ntf; |
|
89 } |
|
90 }, |
|
91 { |
|
92 test: function(nb, ntf) { |
|
93 // check that notifications can be removed properly |
|
94 nb.removeNotification(ntf); |
|
95 return ntf; |
|
96 }, |
|
97 result: function(nb, ntf) { |
|
98 testtag_notificationbox_State(nb, "removeNotification", null, 0); |
|
99 |
|
100 // try removing the notification again to make sure an exception occurs |
|
101 var exh = false; |
|
102 try { |
|
103 nb.removeNotification(ntf); |
|
104 } catch (ex) { exh = true; } |
|
105 SimpleTest.ise(exh, true, "removeNotification again"); |
|
106 testtag_notificationbox_State(nb, "removeNotification again", null, 0); |
|
107 |
|
108 } |
|
109 }, |
|
110 { |
|
111 test: function(nb, ntf) { |
|
112 // append a new notification, but now with an event callback |
|
113 var ntf = nb.appendNotification("Notification", "note", "happy.png", |
|
114 nb.PRIORITY_INFO_LOW, |
|
115 testtag_notificationbox_buttons, |
|
116 notification_eventCallback); |
|
117 SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification with callback"); |
|
118 return ntf; |
|
119 }, |
|
120 result: function(nb, ntf) { |
|
121 testtag_notificationbox_State(nb, "append with callback", ntf, 1); |
|
122 return ntf; |
|
123 } |
|
124 }, |
|
125 { |
|
126 test: function(nb, ntf) { |
|
127 nb.removeNotification(ntf); |
|
128 return ntf; |
|
129 }, |
|
130 result: function(nb, ntf) { |
|
131 testtag_notificationbox_State(nb, "removeNotification with callback", |
|
132 null, 0); |
|
133 |
|
134 testtag_notification_eventCallback("removed", ntf, "removeNotification()"); |
|
135 return [1, null]; |
|
136 } |
|
137 }, |
|
138 { |
|
139 repeat: true, |
|
140 test: function(nb, arr) { |
|
141 var idx = arr[0]; |
|
142 var ntf = arr[1]; |
|
143 switch (idx) { |
|
144 case 1: |
|
145 // append a new notification |
|
146 ntf = nb.appendNotification("Notification", "note", "happy.png", |
|
147 nb.PRIORITY_INFO_LOW, testtag_notificationbox_buttons); |
|
148 SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification"); |
|
149 |
|
150 // Test persistence |
|
151 ntf.persistence++; |
|
152 |
|
153 return [idx, ntf]; |
|
154 case 2: |
|
155 case 3: |
|
156 nb.removeTransientNotifications(); |
|
157 |
|
158 return [idx, ntf]; |
|
159 } |
|
160 }, |
|
161 result: function(nb, arr) { |
|
162 var idx = arr[0]; |
|
163 var ntf = arr[1]; |
|
164 switch (idx) { |
|
165 case 1: |
|
166 testtag_notificationbox_State(nb, "notification added", ntf, 1); |
|
167 testtag_notification_State(nb, ntf, "append", "Notification", "note", |
|
168 "happy.png", nb.PRIORITY_INFO_LOW); |
|
169 SimpleTest.ise(ntf.persistence, 1, "persistence is 1"); |
|
170 |
|
171 return [++idx, ntf]; |
|
172 case 2: |
|
173 testtag_notificationbox_State(nb, "first removeTransientNotifications", ntf, 1); |
|
174 testtag_notification_State(nb, ntf, "append", "Notification", "note", |
|
175 "happy.png", nb.PRIORITY_INFO_LOW); |
|
176 SimpleTest.ise(ntf.persistence, 0, "persistence is now 0"); |
|
177 |
|
178 return [++idx, ntf]; |
|
179 case 3: |
|
180 testtag_notificationbox_State(nb, "second removeTransientNotifications", null, 0); |
|
181 |
|
182 this.repeat = false; |
|
183 } |
|
184 } |
|
185 }, |
|
186 { |
|
187 test: function(nb, ntf) { |
|
188 // append another notification |
|
189 var ntf = nb.appendNotification("Notification", "note", "happy.png", |
|
190 nb.PRIORITY_INFO_MEDIUM, testtag_notificationbox_buttons); |
|
191 SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification again"); |
|
192 return ntf; |
|
193 }, |
|
194 result: function(nb, ntf) { |
|
195 // check that appending a second notification after removing the first one works |
|
196 testtag_notificationbox_State(nb, "append again", ntf, 1); |
|
197 testtag_notification_State(nb, ntf, "append again", "Notification", "note", |
|
198 "happy.png", nb.PRIORITY_INFO_MEDIUM); |
|
199 return ntf; |
|
200 } |
|
201 }, |
|
202 { |
|
203 test: function(nb, ntf) { |
|
204 // check the removeCurrentNotification method |
|
205 nb.removeCurrentNotification(); |
|
206 return ntf; |
|
207 }, |
|
208 result: function(nb, ntf) { |
|
209 testtag_notificationbox_State(nb, "removeCurrentNotification", null, 0); |
|
210 } |
|
211 }, |
|
212 { |
|
213 test: function(nb, ntf) { |
|
214 var ntf = nb.appendNotification("Notification", "note", "happy.png", |
|
215 nb.PRIORITY_INFO_HIGH, testtag_notificationbox_buttons); |
|
216 return ntf; |
|
217 }, |
|
218 result: function(nb, ntf) { |
|
219 // test the removeAllNotifications method |
|
220 testtag_notificationbox_State(nb, "append info_high", ntf, 1); |
|
221 SimpleTest.ise(ntf.priority, nb.PRIORITY_INFO_HIGH, |
|
222 "notification.priority " + nb.PRIORITY_INFO_HIGH); |
|
223 SimpleTest.ise(nb.removeAllNotifications(false), undefined, "removeAllNotifications"); |
|
224 } |
|
225 }, |
|
226 { |
|
227 test: function(nb, unused) { |
|
228 // add a number of notifications and check that they are added in order |
|
229 nb.appendNotification("Four", "4", null, nb.PRIORITY_INFO_HIGH, testtag_notificationbox_buttons); |
|
230 nb.appendNotification("Seven", "7", null, nb.PRIORITY_WARNING_HIGH, testtag_notificationbox_buttons); |
|
231 nb.appendNotification("Two", "2", null, nb.PRIORITY_INFO_LOW, null); |
|
232 nb.appendNotification("Eight", "8", null, nb.PRIORITY_CRITICAL_LOW, null); |
|
233 nb.appendNotification("Five", "5", null, nb.PRIORITY_WARNING_LOW, null); |
|
234 nb.appendNotification("Six", "6", null, nb.PRIORITY_WARNING_HIGH, null); |
|
235 nb.appendNotification("One", "1", null, nb.PRIORITY_INFO_LOW, null); |
|
236 nb.appendNotification("Nine", "9", null, nb.PRIORITY_CRITICAL_MEDIUM, null); |
|
237 var ntf = nb.appendNotification("Ten", "10", null, nb.PRIORITY_CRITICAL_HIGH, null); |
|
238 nb.appendNotification("Three", "3", null, nb.PRIORITY_INFO_MEDIUM, null); |
|
239 return ntf; |
|
240 }, |
|
241 result: function(nb, ntf) { |
|
242 SimpleTest.ise(nb.currentNotification == ntf ? |
|
243 nb.currentNotification.value : null, "10", "appendNotification order"); |
|
244 return 1; |
|
245 } |
|
246 }, |
|
247 { |
|
248 // test closing notifications to make sure that the current notification is still set properly |
|
249 repeat: true, |
|
250 test: function(nb, testidx) { |
|
251 switch (testidx) { |
|
252 case 1: |
|
253 nb.getNotificationWithValue("10").close(); |
|
254 return [1, 9]; |
|
255 case 2: |
|
256 nb.removeNotification(nb.getNotificationWithValue("9")); |
|
257 return [2, 8]; |
|
258 case 3: |
|
259 nb.removeCurrentNotification(); |
|
260 return [3, 7]; |
|
261 case 4: |
|
262 nb.getNotificationWithValue("6").close(); |
|
263 return [4, 7]; |
|
264 case 5: |
|
265 nb.removeNotification(nb.getNotificationWithValue("5")); |
|
266 return [5, 7]; |
|
267 case 6: |
|
268 nb.removeCurrentNotification(); |
|
269 return [6, 4]; |
|
270 } |
|
271 }, |
|
272 result: function(nb, arr) { |
|
273 // arr is [testindex, expectedvalue] |
|
274 SimpleTest.ise(nb.currentNotification.value, "" + arr[1], "close order " + arr[0]); |
|
275 SimpleTest.ise(nb.allNotifications.length, 10 - arr[0], "close order " + arr[0] + " count"); |
|
276 if (arr[0] == 6) |
|
277 this.repeat = false; |
|
278 return ++arr[0]; |
|
279 } |
|
280 }, |
|
281 { |
|
282 test: function(nb, ntf) { |
|
283 var exh = false; |
|
284 try { |
|
285 nb.appendNotification("no", "no", "no", 0, null); |
|
286 } catch (ex) { exh = true; } |
|
287 SimpleTest.ise(exh, true, "appendNotification priority too low"); |
|
288 |
|
289 exh = false; |
|
290 try { |
|
291 nb.appendNotification("no", "no", "no", 11, null); |
|
292 } catch (ex) { exh = true; } |
|
293 SimpleTest.ise(exh, true, "appendNotification priority too high"); |
|
294 |
|
295 // check that the other priority types work properly |
|
296 runTimedTests(appendPriorityTests, -1, nb, nb.PRIORITY_WARNING_LOW); |
|
297 } |
|
298 } |
|
299 ]; |
|
300 |
|
301 var appendPriorityTests = [ |
|
302 { |
|
303 test: function(nb, priority) { |
|
304 var ntf = nb.appendNotification("Notification", "note", "happy.png", |
|
305 priority, testtag_notificationbox_buttons); |
|
306 SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification " + priority); |
|
307 return [ntf, priority]; |
|
308 }, |
|
309 result: function(nb, obj) { |
|
310 SimpleTest.ise(obj[0].priority, obj[1], "notification.priority " + obj[1]); |
|
311 return obj[1]; |
|
312 } |
|
313 }, |
|
314 { |
|
315 test: function(nb, priority) { |
|
316 nb.removeCurrentNotification(); |
|
317 return priority; |
|
318 }, |
|
319 result: function(nb, priority) { |
|
320 if (priority == nb.PRIORITY_CRITICAL_BLOCK) { |
|
321 let ntf = nb.appendNotification("Notification", "note", "happy.png", |
|
322 nb.PRIORITY_INFO_LOW, testtag_notificationbox_buttons); |
|
323 setTimeout(checkPopupTest, 50, nb, ntf); |
|
324 } |
|
325 else { |
|
326 runTimedTests(appendPriorityTests, -1, nb, ++priority); |
|
327 } |
|
328 } |
|
329 } |
|
330 ]; |
|
331 |
|
332 function testtag_notificationbox_State(nb, testid, expecteditem, expectedcount) |
|
333 { |
|
334 SimpleTest.ise(nb.currentNotification, expecteditem, testid + " currentNotification"); |
|
335 SimpleTest.ise(nb.allNotifications ? nb.allNotifications.length : "no value", |
|
336 expectedcount, testid + " allNotifications"); |
|
337 } |
|
338 |
|
339 function testtag_notification_State(nb, ntf, testid, label, value, image, priority) |
|
340 { |
|
341 SimpleTest.ise(ntf.control, nb, testid + " notification.control"); |
|
342 SimpleTest.ise(ntf.label, label, testid + " notification.label"); |
|
343 SimpleTest.ise(ntf.value, value, testid + " notification.value"); |
|
344 SimpleTest.ise(ntf.image, image, testid + " notification.image"); |
|
345 SimpleTest.ise(ntf.priority, priority, testid + " notification.priority"); |
|
346 |
|
347 var type; |
|
348 switch (priority) { |
|
349 case nb.PRIORITY_INFO_LOW: |
|
350 case nb.PRIORITY_INFO_MEDIUM: |
|
351 case nb.PRIORITY_INFO_HIGH: |
|
352 type = "info"; |
|
353 break; |
|
354 case nb.PRIORITY_WARNING_LOW: |
|
355 case nb.PRIORITY_WARNING_MEDIUM: |
|
356 case nb.PRIORITY_WARNING_HIGH: |
|
357 type = "warning"; |
|
358 break; |
|
359 case nb.PRIORITY_CRITICAL_LOW: |
|
360 case nb.PRIORITY_CRITICAL_MEDIUM: |
|
361 case nb.PRIORITY_CRITICAL_HIGH: |
|
362 case nb.PRIORITY_CRITICAL_BLOCK: |
|
363 type = "critical"; |
|
364 break; |
|
365 } |
|
366 |
|
367 SimpleTest.ise(ntf.type, type, testid + " notification.type"); |
|
368 } |
|
369 |
|
370 function checkPopupTest(nb, ntf) |
|
371 { |
|
372 if (nb._animating) |
|
373 setTimeout(checkPopupTest, ntf); |
|
374 else { |
|
375 var evt = new Event(""); |
|
376 ntf.dispatchEvent(evt); |
|
377 evt.target.buttonInfo = testtag_notificationbox_buttons[0]; |
|
378 ntf._doButtonCommand(evt); |
|
379 } |
|
380 } |
|
381 |
|
382 function checkPopupClosed() |
|
383 { |
|
384 is(document.popupNode, null, "popupNode null after popup is closed"); |
|
385 SimpleTest.finish(); |
|
386 } |
|
387 |
|
388 /** |
|
389 * run one or more tests which perform a test operation, wait for a delay, |
|
390 * then perform a result operation. |
|
391 * |
|
392 * tests - array of objects where each object is : |
|
393 * { |
|
394 * test: test function, |
|
395 * result: result function |
|
396 * repeat: true to repeat the test |
|
397 * } |
|
398 * idx - starting index in tests |
|
399 * element - element to run tests on |
|
400 * arg - argument to pass between test functions |
|
401 * |
|
402 * If, after executing the result part, the repeat property of the test is |
|
403 * true, then the test is repeated. If the repeat property is not true, |
|
404 * continue on to the next test. |
|
405 * |
|
406 * The test and result functions take two arguments, the element and the arg. |
|
407 * The test function may return a value which will passed to the result |
|
408 * function as its arg. The result function may also return a value which |
|
409 * will be passed to the next repetition or the next test in the array. |
|
410 */ |
|
411 function runTimedTests(tests, idx, element, arg) |
|
412 { |
|
413 if (idx >= 0 && "result" in tests[idx]) |
|
414 arg = tests[idx].result(element, arg); |
|
415 |
|
416 // if not repeating, move on to the next test |
|
417 if (idx == -1 || !tests[idx].repeat) |
|
418 idx++; |
|
419 |
|
420 if (idx < tests.length) { |
|
421 var result = tests[idx].test(element, arg); |
|
422 setTimeout(runTimedTestsWait, 50, tests, idx, element, result); |
|
423 } |
|
424 } |
|
425 |
|
426 function runTimedTestsWait(tests, idx, element, arg) |
|
427 { |
|
428 // use this secret property to check if the animation is still running. If it |
|
429 // is, then the notification hasn't fully opened or closed yet |
|
430 if (element._animating) |
|
431 setTimeout(runTimedTestsWait, 50, tests, idx, element, arg); |
|
432 else |
|
433 runTimedTests(tests, idx, element, arg); |
|
434 } |
|
435 |
|
436 setTimeout(testtag_notificationbox, 0, document.getElementById('nb')); |
|
437 ]]> |
|
438 </script> |
|
439 |
|
440 </window> |
|
441 |