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