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