browser_permission_dismiss.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. const ORIGIN_URI = Services.io.newURI("http://mochi.test:8888", null, null);
  3. const PERMISSION_NAME = "desktop-notification";
  4. const PROMPT_ALLOW_BUTTON = -1;
  5. const PROMPT_BLOCK_BUTTON = 0;
  6. const TEST_URL = "http://mochi.test:8888/browser/dom/notification/test/browser/notification.html";
  7. /**
  8. * Clicks the specified web-notifications prompt button.
  9. *
  10. * @param {Number} aButtonIndex Number indicating which button to click.
  11. * See the constants in this file.
  12. * @note modified from toolkit/components/passwordmgr/test/browser/head.js
  13. */
  14. function clickDoorhangerButton(aButtonIndex) {
  15. ok(true, "Looking for action at index " + aButtonIndex);
  16. let popup = PopupNotifications.getNotification("web-notifications");
  17. let notifications = popup.owner.panel.childNodes;
  18. ok(notifications.length > 0, "at least one notification displayed");
  19. ok(true, notifications.length + " notification(s)");
  20. let notification = notifications[0];
  21. if (aButtonIndex == -1) {
  22. ok(true, "Triggering main action");
  23. notification.button.doCommand();
  24. } else if (aButtonIndex <= popup.secondaryActions.length) {
  25. ok(true, "Triggering secondary action " + aButtonIndex);
  26. notification.childNodes[aButtonIndex].doCommand();
  27. }
  28. }
  29. /**
  30. * Opens a tab which calls `Notification.requestPermission()` with a callback
  31. * argument, calls the `task` function while the permission prompt is open,
  32. * and verifies that the expected permission is set.
  33. *
  34. * @param {Function} task Task function to run to interact with the prompt.
  35. * @param {String} permission Expected permission value.
  36. * @return {Promise} resolving when the task function is done and the tab
  37. * closes.
  38. */
  39. function tabWithRequest(task, permission) {
  40. Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
  41. return BrowserTestUtils.withNewTab({
  42. gBrowser,
  43. url: TEST_URL,
  44. }, function*(browser) {
  45. let requestPromise = ContentTask.spawn(browser, {
  46. permission
  47. }, function*({permission}) {
  48. function requestCallback(perm) {
  49. is(perm, permission,
  50. "Should call the legacy callback with the permission state");
  51. }
  52. let perm = yield content.window.Notification
  53. .requestPermission(requestCallback);
  54. is(perm, permission,
  55. "Should resolve the promise with the permission state");
  56. });
  57. yield BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
  58. yield task();
  59. yield requestPromise;
  60. });
  61. }
  62. add_task(function* setup() {
  63. SimpleTest.registerCleanupFunction(() => {
  64. Services.perms.remove(ORIGIN_URI, PERMISSION_NAME);
  65. });
  66. });
  67. add_task(function* test_requestPermission_granted() {
  68. yield tabWithRequest(function() {
  69. clickDoorhangerButton(PROMPT_ALLOW_BUTTON);
  70. }, "granted");
  71. ok(!PopupNotifications.getNotification("web-notifications"),
  72. "Should remove the doorhanger notification icon if granted");
  73. is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
  74. Services.perms.ALLOW_ACTION,
  75. "Check permission in perm. manager");
  76. });
  77. add_task(function* test_requestPermission_denied() {
  78. yield tabWithRequest(function() {
  79. clickDoorhangerButton(PROMPT_BLOCK_BUTTON);
  80. }, "denied");
  81. ok(!PopupNotifications.getNotification("web-notifications"),
  82. "Should remove the doorhanger notification icon if denied");
  83. is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
  84. Services.perms.DENY_ACTION,
  85. "Check permission in perm. manager");
  86. });
  87. add_task(function* test_requestPermission_dismissed() {
  88. yield tabWithRequest(function() {
  89. PopupNotifications.panel.hidePopup();
  90. }, "default");
  91. ok(!PopupNotifications.getNotification("web-notifications"),
  92. "Should remove the doorhanger notification icon if dismissed");
  93. is(Services.perms.testPermission(ORIGIN_URI, PERMISSION_NAME),
  94. Services.perms.UNKNOWN_ACTION,
  95. "Check permission in perm. manager");
  96. });