1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/content/tests/chrome/test_notificationbox.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,441 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> 1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> 1.7 +<!-- 1.8 + XUL Widget Test for notificationbox 1.9 + --> 1.10 +<window title="Notification Box" width="500" height="600" 1.11 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.12 + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 1.13 + 1.14 + <notificationbox id="nb"/> 1.15 + <menupopup id="menupopup" onpopupshown="this.hidePopup()" onpopuphidden="checkPopupClosed()"> 1.16 + <menuitem label="One"/> 1.17 + </menupopup> 1.18 + 1.19 + <!-- test results are displayed in the html:body --> 1.20 + <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/> 1.21 + 1.22 + <!-- test code goes here --> 1.23 + <script type="application/javascript"><![CDATA[ 1.24 +SimpleTest.waitForExplicitFinish(); 1.25 + 1.26 +var testtag_notificationbox_buttons = [ 1.27 + { 1.28 + label: "Button 1", 1.29 + accesskey: "u", 1.30 + callback: testtag_notificationbox_buttonpressed, 1.31 + popup: "menupopup" 1.32 + } 1.33 +]; 1.34 + 1.35 +function testtag_notificationbox_buttonpressed(event) 1.36 +{ 1.37 +} 1.38 + 1.39 +function testtag_notificationbox(nb) 1.40 +{ 1.41 + testtag_notificationbox_State(nb, "initial", null, 0); 1.42 + 1.43 + SimpleTest.ise(nb.notificationsHidden, false, "initial notificationsHidden"); 1.44 + SimpleTest.ise(nb.removeAllNotifications(false), undefined, "initial removeAllNotifications"); 1.45 + testtag_notificationbox_State(nb, "initial removeAllNotifications", null, 0); 1.46 + SimpleTest.ise(nb.removeAllNotifications(true), undefined, "initial removeAllNotifications immediate"); 1.47 + testtag_notificationbox_State(nb, "initial removeAllNotifications immediate", null, 0); 1.48 + 1.49 + runTimedTests(tests, -1, nb, null); 1.50 +} 1.51 + 1.52 +var notification_last_event, notification_last_event_item; 1.53 +function notification_eventCallback(event) 1.54 +{ 1.55 + notification_last_event = event; 1.56 + notification_last_event_item = this; 1.57 +} 1.58 + 1.59 +function testtag_notification_eventCallback(expectedEvent, ntf, testName) 1.60 +{ 1.61 + SimpleTest.is(notification_last_event, expectedEvent, 1.62 + testName + ": event name"); 1.63 + SimpleTest.is(notification_last_event_item, ntf, 1.64 + testName + ": event item"); 1.65 + 1.66 + notification_last_event = null; 1.67 + notification_last_event_item = null; 1.68 +} 1.69 + 1.70 +var tests = 1.71 +[ 1.72 + { 1.73 + test: function(nb, ntf) { 1.74 + // append a new notification 1.75 + var ntf = nb.appendNotification("Notification", "note", "happy.png", 1.76 + nb.PRIORITY_INFO_LOW, testtag_notificationbox_buttons); 1.77 + SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification"); 1.78 + return ntf; 1.79 + }, 1.80 + result: function(nb, ntf) { 1.81 + testtag_notificationbox_State(nb, "append", ntf, 1); 1.82 + testtag_notification_State(nb, ntf, "append", "Notification", "note", 1.83 + "happy.png", nb.PRIORITY_INFO_LOW); 1.84 + 1.85 + // check the getNotificationWithValue method 1.86 + var ntf_found = nb.getNotificationWithValue("note"); 1.87 + SimpleTest.ise(ntf, ntf_found, "getNotificationWithValue note"); 1.88 + 1.89 + var none_found = nb.getNotificationWithValue("notenone"); 1.90 + SimpleTest.ise(none_found, null, "getNotificationWithValue null"); 1.91 + return ntf; 1.92 + } 1.93 + }, 1.94 + { 1.95 + test: function(nb, ntf) { 1.96 + // check that notifications can be removed properly 1.97 + nb.removeNotification(ntf); 1.98 + return ntf; 1.99 + }, 1.100 + result: function(nb, ntf) { 1.101 + testtag_notificationbox_State(nb, "removeNotification", null, 0); 1.102 + 1.103 + // try removing the notification again to make sure an exception occurs 1.104 + var exh = false; 1.105 + try { 1.106 + nb.removeNotification(ntf); 1.107 + } catch (ex) { exh = true; } 1.108 + SimpleTest.ise(exh, true, "removeNotification again"); 1.109 + testtag_notificationbox_State(nb, "removeNotification again", null, 0); 1.110 + 1.111 + } 1.112 + }, 1.113 + { 1.114 + test: function(nb, ntf) { 1.115 + // append a new notification, but now with an event callback 1.116 + var ntf = nb.appendNotification("Notification", "note", "happy.png", 1.117 + nb.PRIORITY_INFO_LOW, 1.118 + testtag_notificationbox_buttons, 1.119 + notification_eventCallback); 1.120 + SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification with callback"); 1.121 + return ntf; 1.122 + }, 1.123 + result: function(nb, ntf) { 1.124 + testtag_notificationbox_State(nb, "append with callback", ntf, 1); 1.125 + return ntf; 1.126 + } 1.127 + }, 1.128 + { 1.129 + test: function(nb, ntf) { 1.130 + nb.removeNotification(ntf); 1.131 + return ntf; 1.132 + }, 1.133 + result: function(nb, ntf) { 1.134 + testtag_notificationbox_State(nb, "removeNotification with callback", 1.135 + null, 0); 1.136 + 1.137 + testtag_notification_eventCallback("removed", ntf, "removeNotification()"); 1.138 + return [1, null]; 1.139 + } 1.140 + }, 1.141 + { 1.142 + repeat: true, 1.143 + test: function(nb, arr) { 1.144 + var idx = arr[0]; 1.145 + var ntf = arr[1]; 1.146 + switch (idx) { 1.147 + case 1: 1.148 + // append a new notification 1.149 + ntf = nb.appendNotification("Notification", "note", "happy.png", 1.150 + nb.PRIORITY_INFO_LOW, testtag_notificationbox_buttons); 1.151 + SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification"); 1.152 + 1.153 + // Test persistence 1.154 + ntf.persistence++; 1.155 + 1.156 + return [idx, ntf]; 1.157 + case 2: 1.158 + case 3: 1.159 + nb.removeTransientNotifications(); 1.160 + 1.161 + return [idx, ntf]; 1.162 + } 1.163 + }, 1.164 + result: function(nb, arr) { 1.165 + var idx = arr[0]; 1.166 + var ntf = arr[1]; 1.167 + switch (idx) { 1.168 + case 1: 1.169 + testtag_notificationbox_State(nb, "notification added", ntf, 1); 1.170 + testtag_notification_State(nb, ntf, "append", "Notification", "note", 1.171 + "happy.png", nb.PRIORITY_INFO_LOW); 1.172 + SimpleTest.ise(ntf.persistence, 1, "persistence is 1"); 1.173 + 1.174 + return [++idx, ntf]; 1.175 + case 2: 1.176 + testtag_notificationbox_State(nb, "first removeTransientNotifications", ntf, 1); 1.177 + testtag_notification_State(nb, ntf, "append", "Notification", "note", 1.178 + "happy.png", nb.PRIORITY_INFO_LOW); 1.179 + SimpleTest.ise(ntf.persistence, 0, "persistence is now 0"); 1.180 + 1.181 + return [++idx, ntf]; 1.182 + case 3: 1.183 + testtag_notificationbox_State(nb, "second removeTransientNotifications", null, 0); 1.184 + 1.185 + this.repeat = false; 1.186 + } 1.187 + } 1.188 + }, 1.189 + { 1.190 + test: function(nb, ntf) { 1.191 + // append another notification 1.192 + var ntf = nb.appendNotification("Notification", "note", "happy.png", 1.193 + nb.PRIORITY_INFO_MEDIUM, testtag_notificationbox_buttons); 1.194 + SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification again"); 1.195 + return ntf; 1.196 + }, 1.197 + result: function(nb, ntf) { 1.198 + // check that appending a second notification after removing the first one works 1.199 + testtag_notificationbox_State(nb, "append again", ntf, 1); 1.200 + testtag_notification_State(nb, ntf, "append again", "Notification", "note", 1.201 + "happy.png", nb.PRIORITY_INFO_MEDIUM); 1.202 + return ntf; 1.203 + } 1.204 + }, 1.205 + { 1.206 + test: function(nb, ntf) { 1.207 + // check the removeCurrentNotification method 1.208 + nb.removeCurrentNotification(); 1.209 + return ntf; 1.210 + }, 1.211 + result: function(nb, ntf) { 1.212 + testtag_notificationbox_State(nb, "removeCurrentNotification", null, 0); 1.213 + } 1.214 + }, 1.215 + { 1.216 + test: function(nb, ntf) { 1.217 + var ntf = nb.appendNotification("Notification", "note", "happy.png", 1.218 + nb.PRIORITY_INFO_HIGH, testtag_notificationbox_buttons); 1.219 + return ntf; 1.220 + }, 1.221 + result: function(nb, ntf) { 1.222 + // test the removeAllNotifications method 1.223 + testtag_notificationbox_State(nb, "append info_high", ntf, 1); 1.224 + SimpleTest.ise(ntf.priority, nb.PRIORITY_INFO_HIGH, 1.225 + "notification.priority " + nb.PRIORITY_INFO_HIGH); 1.226 + SimpleTest.ise(nb.removeAllNotifications(false), undefined, "removeAllNotifications"); 1.227 + } 1.228 + }, 1.229 + { 1.230 + test: function(nb, unused) { 1.231 + // add a number of notifications and check that they are added in order 1.232 + nb.appendNotification("Four", "4", null, nb.PRIORITY_INFO_HIGH, testtag_notificationbox_buttons); 1.233 + nb.appendNotification("Seven", "7", null, nb.PRIORITY_WARNING_HIGH, testtag_notificationbox_buttons); 1.234 + nb.appendNotification("Two", "2", null, nb.PRIORITY_INFO_LOW, null); 1.235 + nb.appendNotification("Eight", "8", null, nb.PRIORITY_CRITICAL_LOW, null); 1.236 + nb.appendNotification("Five", "5", null, nb.PRIORITY_WARNING_LOW, null); 1.237 + nb.appendNotification("Six", "6", null, nb.PRIORITY_WARNING_HIGH, null); 1.238 + nb.appendNotification("One", "1", null, nb.PRIORITY_INFO_LOW, null); 1.239 + nb.appendNotification("Nine", "9", null, nb.PRIORITY_CRITICAL_MEDIUM, null); 1.240 + var ntf = nb.appendNotification("Ten", "10", null, nb.PRIORITY_CRITICAL_HIGH, null); 1.241 + nb.appendNotification("Three", "3", null, nb.PRIORITY_INFO_MEDIUM, null); 1.242 + return ntf; 1.243 + }, 1.244 + result: function(nb, ntf) { 1.245 + SimpleTest.ise(nb.currentNotification == ntf ? 1.246 + nb.currentNotification.value : null, "10", "appendNotification order"); 1.247 + return 1; 1.248 + } 1.249 + }, 1.250 + { 1.251 + // test closing notifications to make sure that the current notification is still set properly 1.252 + repeat: true, 1.253 + test: function(nb, testidx) { 1.254 + switch (testidx) { 1.255 + case 1: 1.256 + nb.getNotificationWithValue("10").close(); 1.257 + return [1, 9]; 1.258 + case 2: 1.259 + nb.removeNotification(nb.getNotificationWithValue("9")); 1.260 + return [2, 8]; 1.261 + case 3: 1.262 + nb.removeCurrentNotification(); 1.263 + return [3, 7]; 1.264 + case 4: 1.265 + nb.getNotificationWithValue("6").close(); 1.266 + return [4, 7]; 1.267 + case 5: 1.268 + nb.removeNotification(nb.getNotificationWithValue("5")); 1.269 + return [5, 7]; 1.270 + case 6: 1.271 + nb.removeCurrentNotification(); 1.272 + return [6, 4]; 1.273 + } 1.274 + }, 1.275 + result: function(nb, arr) { 1.276 + // arr is [testindex, expectedvalue] 1.277 + SimpleTest.ise(nb.currentNotification.value, "" + arr[1], "close order " + arr[0]); 1.278 + SimpleTest.ise(nb.allNotifications.length, 10 - arr[0], "close order " + arr[0] + " count"); 1.279 + if (arr[0] == 6) 1.280 + this.repeat = false; 1.281 + return ++arr[0]; 1.282 + } 1.283 + }, 1.284 + { 1.285 + test: function(nb, ntf) { 1.286 + var exh = false; 1.287 + try { 1.288 + nb.appendNotification("no", "no", "no", 0, null); 1.289 + } catch (ex) { exh = true; } 1.290 + SimpleTest.ise(exh, true, "appendNotification priority too low"); 1.291 + 1.292 + exh = false; 1.293 + try { 1.294 + nb.appendNotification("no", "no", "no", 11, null); 1.295 + } catch (ex) { exh = true; } 1.296 + SimpleTest.ise(exh, true, "appendNotification priority too high"); 1.297 + 1.298 + // check that the other priority types work properly 1.299 + runTimedTests(appendPriorityTests, -1, nb, nb.PRIORITY_WARNING_LOW); 1.300 + } 1.301 + } 1.302 +]; 1.303 + 1.304 +var appendPriorityTests = [ 1.305 + { 1.306 + test: function(nb, priority) { 1.307 + var ntf = nb.appendNotification("Notification", "note", "happy.png", 1.308 + priority, testtag_notificationbox_buttons); 1.309 + SimpleTest.ise(ntf && ntf.localName == "notification", true, "append notification " + priority); 1.310 + return [ntf, priority]; 1.311 + }, 1.312 + result: function(nb, obj) { 1.313 + SimpleTest.ise(obj[0].priority, obj[1], "notification.priority " + obj[1]); 1.314 + return obj[1]; 1.315 + } 1.316 + }, 1.317 + { 1.318 + test: function(nb, priority) { 1.319 + nb.removeCurrentNotification(); 1.320 + return priority; 1.321 + }, 1.322 + result: function(nb, priority) { 1.323 + if (priority == nb.PRIORITY_CRITICAL_BLOCK) { 1.324 + let ntf = nb.appendNotification("Notification", "note", "happy.png", 1.325 + nb.PRIORITY_INFO_LOW, testtag_notificationbox_buttons); 1.326 + setTimeout(checkPopupTest, 50, nb, ntf); 1.327 + } 1.328 + else { 1.329 + runTimedTests(appendPriorityTests, -1, nb, ++priority); 1.330 + } 1.331 + } 1.332 + } 1.333 +]; 1.334 + 1.335 +function testtag_notificationbox_State(nb, testid, expecteditem, expectedcount) 1.336 +{ 1.337 + SimpleTest.ise(nb.currentNotification, expecteditem, testid + " currentNotification"); 1.338 + SimpleTest.ise(nb.allNotifications ? nb.allNotifications.length : "no value", 1.339 + expectedcount, testid + " allNotifications"); 1.340 +} 1.341 + 1.342 +function testtag_notification_State(nb, ntf, testid, label, value, image, priority) 1.343 +{ 1.344 + SimpleTest.ise(ntf.control, nb, testid + " notification.control"); 1.345 + SimpleTest.ise(ntf.label, label, testid + " notification.label"); 1.346 + SimpleTest.ise(ntf.value, value, testid + " notification.value"); 1.347 + SimpleTest.ise(ntf.image, image, testid + " notification.image"); 1.348 + SimpleTest.ise(ntf.priority, priority, testid + " notification.priority"); 1.349 + 1.350 + var type; 1.351 + switch (priority) { 1.352 + case nb.PRIORITY_INFO_LOW: 1.353 + case nb.PRIORITY_INFO_MEDIUM: 1.354 + case nb.PRIORITY_INFO_HIGH: 1.355 + type = "info"; 1.356 + break; 1.357 + case nb.PRIORITY_WARNING_LOW: 1.358 + case nb.PRIORITY_WARNING_MEDIUM: 1.359 + case nb.PRIORITY_WARNING_HIGH: 1.360 + type = "warning"; 1.361 + break; 1.362 + case nb.PRIORITY_CRITICAL_LOW: 1.363 + case nb.PRIORITY_CRITICAL_MEDIUM: 1.364 + case nb.PRIORITY_CRITICAL_HIGH: 1.365 + case nb.PRIORITY_CRITICAL_BLOCK: 1.366 + type = "critical"; 1.367 + break; 1.368 + } 1.369 + 1.370 + SimpleTest.ise(ntf.type, type, testid + " notification.type"); 1.371 +} 1.372 + 1.373 +function checkPopupTest(nb, ntf) 1.374 +{ 1.375 + if (nb._animating) 1.376 + setTimeout(checkPopupTest, ntf); 1.377 + else { 1.378 + var evt = new Event(""); 1.379 + ntf.dispatchEvent(evt); 1.380 + evt.target.buttonInfo = testtag_notificationbox_buttons[0]; 1.381 + ntf._doButtonCommand(evt); 1.382 + } 1.383 +} 1.384 + 1.385 +function checkPopupClosed() 1.386 +{ 1.387 + is(document.popupNode, null, "popupNode null after popup is closed"); 1.388 + SimpleTest.finish(); 1.389 +} 1.390 + 1.391 +/** 1.392 + * run one or more tests which perform a test operation, wait for a delay, 1.393 + * then perform a result operation. 1.394 + * 1.395 + * tests - array of objects where each object is : 1.396 + * { 1.397 + * test: test function, 1.398 + * result: result function 1.399 + * repeat: true to repeat the test 1.400 + * } 1.401 + * idx - starting index in tests 1.402 + * element - element to run tests on 1.403 + * arg - argument to pass between test functions 1.404 + * 1.405 + * If, after executing the result part, the repeat property of the test is 1.406 + * true, then the test is repeated. If the repeat property is not true, 1.407 + * continue on to the next test. 1.408 + * 1.409 + * The test and result functions take two arguments, the element and the arg. 1.410 + * The test function may return a value which will passed to the result 1.411 + * function as its arg. The result function may also return a value which 1.412 + * will be passed to the next repetition or the next test in the array. 1.413 + */ 1.414 +function runTimedTests(tests, idx, element, arg) 1.415 +{ 1.416 + if (idx >= 0 && "result" in tests[idx]) 1.417 + arg = tests[idx].result(element, arg); 1.418 + 1.419 + // if not repeating, move on to the next test 1.420 + if (idx == -1 || !tests[idx].repeat) 1.421 + idx++; 1.422 + 1.423 + if (idx < tests.length) { 1.424 + var result = tests[idx].test(element, arg); 1.425 + setTimeout(runTimedTestsWait, 50, tests, idx, element, result); 1.426 + } 1.427 +} 1.428 + 1.429 +function runTimedTestsWait(tests, idx, element, arg) 1.430 +{ 1.431 + // use this secret property to check if the animation is still running. If it 1.432 + // is, then the notification hasn't fully opened or closed yet 1.433 + if (element._animating) 1.434 + setTimeout(runTimedTestsWait, 50, tests, idx, element, arg); 1.435 + else 1.436 + runTimedTests(tests, idx, element, arg); 1.437 +} 1.438 + 1.439 +setTimeout(testtag_notificationbox, 0, document.getElementById('nb')); 1.440 +]]> 1.441 +</script> 1.442 + 1.443 +</window> 1.444 +