Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 7 | |
michael@0 | 8 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 9 | |
michael@0 | 10 | const gIsWindows = ("@mozilla.org/windows-registry-key;1" in Cc); |
michael@0 | 11 | const gIsOSX = ("nsILocalFileMac" in Ci); |
michael@0 | 12 | const gIsLinux = ("@mozilla.org/gnome-gconf-service;1" in Cc) || |
michael@0 | 13 | ("@mozilla.org/gio-service;1" in Cc); |
michael@0 | 14 | const gDirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); |
michael@0 | 15 | |
michael@0 | 16 | // Finds the test plugin library |
michael@0 | 17 | function get_test_plugin(secondplugin=false) { |
michael@0 | 18 | var pluginEnum = gDirSvc.get("APluginsDL", Ci.nsISimpleEnumerator); |
michael@0 | 19 | while (pluginEnum.hasMoreElements()) { |
michael@0 | 20 | let dir = pluginEnum.getNext().QueryInterface(Ci.nsILocalFile); |
michael@0 | 21 | let name = get_platform_specific_plugin_name(secondplugin); |
michael@0 | 22 | let plugin = dir.clone(); |
michael@0 | 23 | plugin.append(name); |
michael@0 | 24 | if (plugin.exists()) { |
michael@0 | 25 | plugin.normalize(); |
michael@0 | 26 | return plugin; |
michael@0 | 27 | } |
michael@0 | 28 | } |
michael@0 | 29 | return null; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | // Finds the test nsIPluginTag |
michael@0 | 33 | function get_test_plugintag(aName="Test Plug-in") { |
michael@0 | 34 | const Cc = Components.classes; |
michael@0 | 35 | const Ci = Components.interfaces; |
michael@0 | 36 | |
michael@0 | 37 | var name = aName || "Test Plug-in"; |
michael@0 | 38 | var host = Cc["@mozilla.org/plugin/host;1"]. |
michael@0 | 39 | getService(Ci.nsIPluginHost); |
michael@0 | 40 | var tags = host.getPluginTags(); |
michael@0 | 41 | |
michael@0 | 42 | for (var i = 0; i < tags.length; i++) { |
michael@0 | 43 | if (tags[i].name == name) |
michael@0 | 44 | return tags[i]; |
michael@0 | 45 | } |
michael@0 | 46 | return null; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // Creates a fake ProfDS directory key, copied from do_get_profile |
michael@0 | 50 | function do_get_profile_startup() { |
michael@0 | 51 | let env = Components.classes["@mozilla.org/process/environment;1"] |
michael@0 | 52 | .getService(Components.interfaces.nsIEnvironment); |
michael@0 | 53 | // the python harness sets this in the environment for us |
michael@0 | 54 | let profd = env.get("XPCSHELL_TEST_PROFILE_DIR"); |
michael@0 | 55 | let file = Components.classes["@mozilla.org/file/local;1"] |
michael@0 | 56 | .createInstance(Components.interfaces.nsILocalFile); |
michael@0 | 57 | file.initWithPath(profd); |
michael@0 | 58 | |
michael@0 | 59 | let dirSvc = Components.classes["@mozilla.org/file/directory_service;1"] |
michael@0 | 60 | .getService(Components.interfaces.nsIProperties); |
michael@0 | 61 | let provider = { |
michael@0 | 62 | getFile: function(prop, persistent) { |
michael@0 | 63 | persistent.value = true; |
michael@0 | 64 | if (prop == "ProfDS") { |
michael@0 | 65 | return file.clone(); |
michael@0 | 66 | } |
michael@0 | 67 | throw Components.results.NS_ERROR_FAILURE; |
michael@0 | 68 | }, |
michael@0 | 69 | QueryInterface: function(iid) { |
michael@0 | 70 | if (iid.equals(Components.interfaces.nsIDirectoryServiceProvider) || |
michael@0 | 71 | iid.equals(Components.interfaces.nsISupports)) { |
michael@0 | 72 | return this; |
michael@0 | 73 | } |
michael@0 | 74 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 75 | } |
michael@0 | 76 | }; |
michael@0 | 77 | dirSvc.QueryInterface(Components.interfaces.nsIDirectoryService) |
michael@0 | 78 | .registerProvider(provider); |
michael@0 | 79 | return file.clone(); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | function get_platform_specific_plugin_name(secondplugin=false) { |
michael@0 | 83 | if (secondplugin) { |
michael@0 | 84 | if (gIsWindows) return "npsecondtest.dll"; |
michael@0 | 85 | if (gIsOSX) return "SecondTest.plugin"; |
michael@0 | 86 | if (gIsLinux) return "libnpsecondtest.so"; |
michael@0 | 87 | } else { |
michael@0 | 88 | if (gIsWindows) return "nptest.dll"; |
michael@0 | 89 | if (gIsOSX) return "Test.plugin"; |
michael@0 | 90 | if (gIsLinux) return "libnptest.so"; |
michael@0 | 91 | } |
michael@0 | 92 | return null; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function get_platform_specific_plugin_suffix() { |
michael@0 | 96 | if (gIsWindows) return ".dll"; |
michael@0 | 97 | else if (gIsOSX) return ".plugin"; |
michael@0 | 98 | else if (gIsLinux) return ".so"; |
michael@0 | 99 | else return null; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | function get_test_plugin_no_symlink() { |
michael@0 | 103 | let dirSvc = Cc["@mozilla.org/file/directory_service;1"] |
michael@0 | 104 | .getService(Ci.nsIProperties); |
michael@0 | 105 | let pluginEnum = dirSvc.get("APluginsDL", Ci.nsISimpleEnumerator); |
michael@0 | 106 | while (pluginEnum.hasMoreElements()) { |
michael@0 | 107 | let dir = pluginEnum.getNext().QueryInterface(Ci.nsILocalFile); |
michael@0 | 108 | let plugin = dir.clone(); |
michael@0 | 109 | plugin.append(get_platform_specific_plugin_name()); |
michael@0 | 110 | if (plugin.exists()) { |
michael@0 | 111 | return plugin; |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | return null; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | let gGlobalScope = this; |
michael@0 | 118 | function loadAddonManager() { |
michael@0 | 119 | let ns = {}; |
michael@0 | 120 | Cu.import("resource://gre/modules/Services.jsm", ns); |
michael@0 | 121 | let head = "../../../../toolkit/mozapps/extensions/test/xpcshell/head_addons.js"; |
michael@0 | 122 | let file = do_get_file(head); |
michael@0 | 123 | let uri = ns.Services.io.newFileURI(file); |
michael@0 | 124 | ns.Services.scriptloader.loadSubScript(uri.spec, gGlobalScope); |
michael@0 | 125 | createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); |
michael@0 | 126 | startupManager(); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | // Install addon and return a Promise<boolean> that is |
michael@0 | 130 | // resolve with true on success, false otherwise. |
michael@0 | 131 | function installAddon(relativePath) { |
michael@0 | 132 | let deferred = Promise.defer(); |
michael@0 | 133 | let success = () => deferred.resolve(true); |
michael@0 | 134 | let fail = () => deferred.resolve(false); |
michael@0 | 135 | let listener = { |
michael@0 | 136 | onDownloadCancelled: fail, |
michael@0 | 137 | onDownloadFailed: fail, |
michael@0 | 138 | onInstallCancelled: fail, |
michael@0 | 139 | onInstallFailed: fail, |
michael@0 | 140 | onInstallEnded: success, |
michael@0 | 141 | }; |
michael@0 | 142 | |
michael@0 | 143 | let installCallback = install => { |
michael@0 | 144 | install.addListener(listener); |
michael@0 | 145 | install.install(); |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | let file = do_get_file(relativePath, false); |
michael@0 | 149 | AddonManager.getInstallForFile(file, installCallback, |
michael@0 | 150 | "application/x-xpinstall"); |
michael@0 | 151 | |
michael@0 | 152 | return deferred.promise; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | // Uninstall addon and return a Promise<boolean> that is |
michael@0 | 156 | // resolve with true on success, false otherwise. |
michael@0 | 157 | function uninstallAddon(id) { |
michael@0 | 158 | let deferred = Promise.defer(); |
michael@0 | 159 | |
michael@0 | 160 | AddonManager.getAddonByID(id, addon => { |
michael@0 | 161 | if (!addon) { |
michael@0 | 162 | deferred.resolve(false); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | let listener = {}; |
michael@0 | 166 | let handler = addon => { |
michael@0 | 167 | if (addon.id !== id) { |
michael@0 | 168 | return; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | AddonManager.removeAddonListener(listener); |
michael@0 | 172 | deferred.resolve(true); |
michael@0 | 173 | }; |
michael@0 | 174 | |
michael@0 | 175 | listener.onUninstalled = handler; |
michael@0 | 176 | listener.onDisabled = handler; |
michael@0 | 177 | |
michael@0 | 178 | AddonManager.addAddonListener(listener); |
michael@0 | 179 | addon.uninstall(); |
michael@0 | 180 | }); |
michael@0 | 181 | |
michael@0 | 182 | return deferred.promise; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | // Returns a Promise<Addon> that is resolved with |
michael@0 | 186 | // the corresponding addon or rejected. |
michael@0 | 187 | function getAddonByID(id) { |
michael@0 | 188 | let deferred = Promise.defer(); |
michael@0 | 189 | AddonManager.getAddonByID(id, addon => deferred.resolve(addon)); |
michael@0 | 190 | return deferred.promise; |
michael@0 | 191 | } |