head_urlformatter.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
16  * The Initial Developer of the Original Code is
17  * Dave Townsend <dtownsend@oxymoronical.com>.
18  *
19  * Portions created by the Initial Developer are Copyright (C) 2007
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK *****
37  */
38 
39 /*
40  * These are intended to be a set of base functions enabling unit testing
41  * for the Extension Manager component.
42  */
43 
44 const PREFIX_NS_EM_RDF = "http://www.mozilla.org/2004/em-rdf#";
45 const PREFIX_NS_CHROME = "http://www.mozilla.org/rdf/chrome#";
46 const PREFIX_ITEM_URI = "urn:mozilla:item:";
47 
49 const NS_APP_PROFILE_DIR_STARTUP = "ProfDS";
50 const NS_OS_TEMP_DIR = "TmpD";
51 
52 const NS_INSTALL_LOCATION_APPPROFILE = "app-profile";
53 
54 const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
55 const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}");
56 
57 const XULRUNTIME_CONTRACTID = "@mozilla.org/xre/runtime;1";
58 const XULRUNTIME_CID = Components.ID("{b96e56dc-1dd1-11b2-91a1-96a45c3e4adf}");
59 
60 var gEM = null;
61 var gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
62  .getService(Components.interfaces.nsIRDFService);
63 
67 function EM_NS(property) {
68  return PREFIX_NS_EM_RDF + property;
69 }
70 
71 function CHROME_NS(property) {
72  return PREFIX_NS_CHROME + property;
73 }
74 
75 function EM_R(property) {
76  return gRDF.GetResource(EM_NS(property));
77 }
78 
79 function EM_L(literal) {
80  return gRDF.GetLiteral(literal);
81 }
82 
83 function EM_I(integer) {
84  return gRDF.GetIntLiteral(integer);
85 }
86 
87 function EM_D(integer) {
88  return gRDF.GetDateLiteral(integer);
89 }
90 
98 function stringData(literalOrResource) {
99  if (literalOrResource instanceof Components.interfaces.nsIRDFLiteral)
100  return literalOrResource.Value;
101  if (literalOrResource instanceof Components.interfaces.nsIRDFResource)
102  return literalOrResource.Value;
103  return undefined;
104 }
105 
112 function intData(literal) {
113  if (literal instanceof Components.interfaces.nsIRDFInt)
114  return literal.Value;
115  return undefined;
116 }
117 
125 function getResourceForID(id) {
126  return gRDF.GetResource(PREFIX_ITEM_URI + id);
127 }
128 
138 function getManifestProperty(id, property) {
139  return stringData(gEM.datasource.GetTarget(getResourceForID(id), EM_R(property), true));
140 }
141 
149 {
150  var lf = gTestRoot.clone();
151  lf.append("unit");
152  lf.append("addons");
153  lf.append(name + ".xpi");
154  if (!lf.exists())
155  do_throw("Addon "+name+" does not exist.");
156 
157  return lf;
158 }
159 
171 function createAppInfo(id, name, version, platformVersion)
172 {
173  var XULAppInfo = {
174  vendor: "Mozilla",
175  name: name,
176  ID: id,
177  version: version,
178  appBuildID: "2007010101",
179  platformVersion: platformVersion,
180  platformBuildID: "2007010101",
181  inSafeMode: false,
182  logConsoleErrors: true,
183  OS: "XPCShell",
184  XPCOMABI: "noarch-spidermonkey",
185 
186  QueryInterface: function QueryInterface(iid) {
187  if (iid.equals(Components.interfaces.nsIXULAppInfo)
188  || iid.equals(Components.interfaces.nsIXULRuntime)
189  || iid.equals(Components.interfaces.nsISupports))
190  return this;
191 
192  throw Components.results.NS_ERROR_NO_INTERFACE;
193  }
194  };
195 
196  var XULAppInfoFactory = {
197  createInstance: function (outer, iid) {
198  if (outer != null)
199  throw Components.results.NS_ERROR_NO_AGGREGATION;
200  return XULAppInfo.QueryInterface(iid);
201  }
202  };
203  var registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
204  registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo",
205  XULAPPINFO_CONTRACTID, XULAppInfoFactory);
206  registrar.registerFactory(XULRUNTIME_CID, "XULAppInfo",
207  XULRUNTIME_CONTRACTID, XULAppInfoFactory);
208 }
209 
214 function startupEM()
215 {
216  // Make sure the update service is initialised.
217  var updateSvc = Components.classes["@mozilla.org/updates/update-service;1"]
218  .getService(Components.interfaces.nsISupports);
219 
220  gEM = Components.classes["@mozilla.org/extensions/manager;1"]
221  .getService(Components.interfaces.nsIExtensionManager);
222 
223  gEM.QueryInterface(Components.interfaces.nsIObserver);
224  gEM.observe(null, "app-startup", null);
225  gEM.observe(null, "profile-after-change", "startup");
226 
227  var needsRestart = false;
228  try {
229  needsRestart = gEM.checkForMismatches();
230  }
231  catch (e) {
232  needsRestart = gEM.start(null);
233  }
234 
235  if (needsRestart)
236  gEM.start(null);
237 
238  // Make sure extension manager datasource is initialized by requesting it
239  var dummy = gEM.datasource;
240 }
241 
246 function shutdownEM()
247 {
248  // xpcshell calls xpcom-shutdown so we don't actually do anything here.
249  gDirSvc.unregisterProvider(dirProvider);
250  gEM = null;
251 }
252 
257 function restartEM()
258 {
259  // We must unregister any datasources that may have been read from addons.
260  var extensions = gProfD.clone();
261  extensions.append("extensions");
262  if (extensions.exists()) {
263  var ioServ = Components.classes["@mozilla.org/network/io-service;1"]
264  .getService(Components.interfaces.nsIIOService);
265  var addons = extensions.directoryEntries;
266  while (addons.hasMoreElements()) {
267  var addon = addons.getNext().QueryInterface(Components.interfaces.nsIFile);
268  if (addon.isDirectory() && addon.leafName != "staged-xpis") {
269  addon.append("install.rdf");
270  if (addon.exists()) {
271  var ds = gRDF.GetDataSource(ioServ.newFileURI(addon).spec);
272  gRDF.UnregisterDataSource(ds);
273  }
274  }
275  }
276  }
277 
278  var needsRestart = gEM.start(null);
279  if (needsRestart)
280  gEM.start(null);
281 }
282 
283 var gDirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
284  .getService(Components.interfaces.nsIProperties);
285 var gTestRoot = gDirSvc.get("CurProcD", Components.interfaces.nsILocalFile);
286 gTestRoot = gTestRoot.parent.parent;
287 gTestRoot.append("_tests");
288 gTestRoot.append("xpcshell-simple");
289 gTestRoot.append("test_extensionmanager");
290 
291 // Need to create and register a profile folder.
292 var gProfD = gTestRoot.clone();
293 gProfD.append("profile");
294 if (gProfD.exists())
295  gProfD.remove(true);
296 gProfD.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
297 
298 var dirProvider = {
299  getFile: function(prop, persistent) {
300  persistent.value = true;
301  if (prop == NS_APP_USER_PROFILE_50_DIR ||
303  return gProfD.clone();
304  return null;
305  },
306  QueryInterface: function(iid) {
307  if (iid.equals(Components.interfaces.nsIDirectoryServiceProvider) ||
308  iid.equals(Components.interfaces.nsISupports)) {
309  return this;
310  }
311  throw Components.results.NS_ERROR_NO_INTERFACE;
312  }
313 };
314 gDirSvc.QueryInterface(Components.interfaces.nsIDirectoryService)
315  .registerProvider(dirProvider);
316 
317 var gPrefs = Components.classes["@mozilla.org/preferences;1"]
318  .getService(Components.interfaces.nsIPrefBranch);
319 // Enable more extensive EM logging
320 gPrefs.setBoolPref("extensions.logging.enabled", true);
var gRDF
const NS_APP_USER_PROFILE_50_DIR
function EM_D(integer)
var registrar
const NS_APP_PROFILE_DIR_STARTUP
menuItem id
Definition: FeedWriter.js:971
gDirSvc QueryInterface(Components.interfaces.nsIDirectoryService).registerProvider(dirProvider)
const PREFIX_ITEM_URI
function EM_L(literal)
var XULAppInfo
var dirProvider
function EM_NS(property)
sidebarFactory createInstance
Definition: nsSidebar.js:351
function do_get_addon(name)
function stringData(literalOrResource)
function startupEM()
var gPrefs
const NS_OS_TEMP_DIR
var gTestRoot
var gEM
version(170)
function restartEM()
function EM_I(integer)
var gDirSvc
const XULRUNTIME_CID
function getFile(fileName)
function EM_R(property)
const XULAPPINFO_CONTRACTID
function getResourceForID(id)
const PREFIX_NS_CHROME
return null
Definition: FeedWriter.js:1143
const XULRUNTIME_CONTRACTID
function intData(literal)
function CHROME_NS(property)
const XULAPPINFO_CID
const PREFIX_NS_EM_RDF
function createAppInfo(id, name, version, platformVersion)
function shutdownEM()
var XULAppInfoFactory
const NS_INSTALL_LOCATION_APPPROFILE
function getManifestProperty(id, property)
var gProfD