notification_worker.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. function ok(test, message) {
  2. postMessage({ type: 'ok', test: test, message: message });
  3. }
  4. function is(a, b, message) {
  5. postMessage({ type: 'is', test1: a, test2: b, message: message });
  6. }
  7. if (self.Notification) {
  8. var steps = [
  9. function () {
  10. ok(typeof Notification === "function", "Notification constructor exists");
  11. ok(Notification.permission, "Notification.permission exists");
  12. ok(typeof Notification.requestPermission === "undefined", "Notification.requestPermission should not exist");
  13. },
  14. function (done) {
  15. var options = {
  16. dir: "auto",
  17. lang: "",
  18. body: "This is a notification body",
  19. tag: "sometag",
  20. icon: "icon.png",
  21. data: ["a complex object that should be", { "structured": "cloned" }],
  22. mozbehavior: { vibrationPattern: [30, 200, 30] },
  23. };
  24. var notification = new Notification("This is a title", options);
  25. ok(notification !== undefined, "Notification exists");
  26. is(notification.onclick, null, "onclick() should be null");
  27. is(notification.onshow, null, "onshow() should be null");
  28. is(notification.onerror, null, "onerror() should be null");
  29. is(notification.onclose, null, "onclose() should be null");
  30. is(typeof notification.close, "function", "close() should exist");
  31. is(notification.dir, options.dir, "auto should get set");
  32. is(notification.lang, options.lang, "lang should get set");
  33. is(notification.body, options.body, "body should get set");
  34. is(notification.tag, options.tag, "tag should get set");
  35. is(notification.icon, options.icon, "icon should get set");
  36. is(notification.data[0], "a complex object that should be", "data item 0 should be a matching string");
  37. is(notification.data[1]["structured"], "cloned", "data item 1 should be a matching object literal");
  38. // store notification in test context
  39. this.notification = notification;
  40. notification.onshow = function () {
  41. ok(true, "onshow handler should be called");
  42. done();
  43. };
  44. },
  45. function (done) {
  46. var notification = this.notification;
  47. notification.onclose = function () {
  48. ok(true, "onclose handler should be called");
  49. done();
  50. };
  51. notification.close();
  52. },
  53. ];
  54. onmessage = function(e) {
  55. var context = {};
  56. (function executeRemainingTests(remainingTests) {
  57. if (!remainingTests.length) {
  58. postMessage({type: 'finish'});
  59. return;
  60. }
  61. var nextTest = remainingTests.shift();
  62. var finishTest = executeRemainingTests.bind(null, remainingTests);
  63. var startTest = nextTest.call.bind(nextTest, context, finishTest);
  64. try {
  65. startTest();
  66. // if no callback was defined for test function,
  67. // we must manually invoke finish to continue
  68. if (nextTest.length === 0) {
  69. finishTest();
  70. }
  71. } catch (e) {
  72. ok(false, "Test threw exception! " + nextTest + " " + e);
  73. finishTest();
  74. }
  75. })(steps);
  76. }
  77. } else {
  78. ok(true, "Notifications are not enabled in workers on the platform.");
  79. postMessage({ type: 'finish' });
  80. }