head.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var gActiveListeners = {};
  6. function registerPopupEventHandler(eventName, callback) {
  7. gActiveListeners[eventName] = function (event) {
  8. if (event.target != PopupNotifications.panel)
  9. return;
  10. PopupNotifications.panel.removeEventListener(eventName,
  11. gActiveListeners[eventName],
  12. false);
  13. delete gActiveListeners[eventName];
  14. callback.call(PopupNotifications.panel);
  15. }
  16. PopupNotifications.panel.addEventListener(eventName,
  17. gActiveListeners[eventName],
  18. false);
  19. }
  20. function unregisterPopupEventHandler(eventName)
  21. {
  22. PopupNotifications.panel.removeEventListener(eventName,
  23. gActiveListeners[eventName],
  24. false);
  25. delete gActiveListeners[eventName];
  26. }
  27. function unregisterAllPopupEventHandlers()
  28. {
  29. for (let eventName in gActiveListeners) {
  30. PopupNotifications.panel.removeEventListener(eventName,
  31. gActiveListeners[eventName],
  32. false);
  33. }
  34. gActiveListeners = {};
  35. }
  36. function triggerMainCommand(popup)
  37. {
  38. info("triggering main command");
  39. let notifications = popup.childNodes;
  40. ok(notifications.length > 0, "at least one notification displayed");
  41. let notification = notifications[0];
  42. info("triggering command: " + notification.getAttribute("buttonlabel"));
  43. // 20, 10 so that the inner button is hit
  44. EventUtils.synthesizeMouse(notification.button, 20, 10, {});
  45. }
  46. function triggerSecondaryCommand(popup, index)
  47. {
  48. info("triggering secondary command, " + index);
  49. let notifications = popup.childNodes;
  50. ok(notifications.length > 0, "at least one notification displayed");
  51. let notification = notifications[0];
  52. // Cancel the arrow panel slide-in transition (bug 767133) such that
  53. // it won't interfere with us interacting with the dropdown.
  54. SpecialPowers.wrap(document).getAnonymousNodes(popup)[0].style.transition = "none";
  55. notification.button.focus();
  56. popup.addEventListener("popupshown", function () {
  57. popup.removeEventListener("popupshown", arguments.callee, false);
  58. // Press down until the desired command is selected
  59. for (let i = 0; i <= index; i++)
  60. EventUtils.synthesizeKey("VK_DOWN", {});
  61. // Activate
  62. EventUtils.synthesizeKey("VK_RETURN", {});
  63. }, false);
  64. // One down event to open the popup
  65. EventUtils.synthesizeKey("VK_DOWN", { altKey: (navigator.platform.indexOf("Mac") == -1) });
  66. }
  67. function dismissNotification(popup)
  68. {
  69. info("dismissing notification");
  70. executeSoon(function () {
  71. EventUtils.synthesizeKey("VK_ESCAPE", {});
  72. });
  73. }
  74. function setFinishedCallback(callback, win)
  75. {
  76. if (!win) {
  77. win = window;
  78. }
  79. ContentTask.spawn(win.gBrowser.selectedBrowser, null, function*() {
  80. return yield new Promise(resolve => {
  81. content.wrappedJSObject.testFinishedCallback = (result, exception) => {
  82. info("got finished callback");
  83. resolve({result, exception});
  84. };
  85. });
  86. }).then(({result, exception}) => {
  87. callback(result, exception);
  88. });
  89. }
  90. function dispatchEvent(eventName)
  91. {
  92. info("dispatching event: " + eventName);
  93. let event = document.createEvent("Events");
  94. event.initEvent(eventName, false, false);
  95. gBrowser.selectedBrowser.contentWindow.dispatchEvent(event);
  96. }
  97. function setPermission(url, permission)
  98. {
  99. const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
  100. let uri = Components.classes["@mozilla.org/network/io-service;1"]
  101. .getService(Components.interfaces.nsIIOService)
  102. .newURI(url, null, null);
  103. let ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
  104. .getService(Ci.nsIScriptSecurityManager);
  105. let principal = ssm.createCodebasePrincipal(uri, {});
  106. Components.classes["@mozilla.org/permissionmanager;1"]
  107. .getService(nsIPermissionManager)
  108. .addFromPrincipal(principal, permission,
  109. nsIPermissionManager.ALLOW_ACTION);
  110. }
  111. function removePermission(url, permission)
  112. {
  113. let uri = Components.classes["@mozilla.org/network/io-service;1"]
  114. .getService(Components.interfaces.nsIIOService)
  115. .newURI(url, null, null);
  116. let ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
  117. .getService(Ci.nsIScriptSecurityManager);
  118. let principal = ssm.createCodebasePrincipal(uri, {});
  119. Components.classes["@mozilla.org/permissionmanager;1"]
  120. .getService(Components.interfaces.nsIPermissionManager)
  121. .removeFromPrincipal(principal, permission);
  122. }
  123. function getPermission(url, permission)
  124. {
  125. let uri = Components.classes["@mozilla.org/network/io-service;1"]
  126. .getService(Components.interfaces.nsIIOService)
  127. .newURI(url, null, null);
  128. let ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
  129. .getService(Ci.nsIScriptSecurityManager);
  130. let principal = ssm.createCodebasePrincipal(uri, {});
  131. return Components.classes["@mozilla.org/permissionmanager;1"]
  132. .getService(Components.interfaces.nsIPermissionManager)
  133. .testPermissionFromPrincipal(principal, permission);
  134. }