toolkit/content/tests/chrome/test_notificationbox.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial