XPCOMUtilify nsURLFormatter and add tests. r=dietrich, a=schrep

This commit is contained in:
rflint@ryanflint.com 2008-01-07 02:44:54 -08:00
parent 2b370d65c9
commit df7ea151fe
5 changed files with 200 additions and 126 deletions

View File

@ -44,4 +44,8 @@ include $(DEPTH)/config/autoconf.mk
DIRS = public src
ifdef ENABLE_TESTS
DIRS += tests
endif
include $(topsrcdir)/config/rules.mk

View File

@ -42,7 +42,7 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
EXTRA_COMPONENTS = \
EXTRA_PP_COMPONENTS = \
nsURLFormatter.js \
$(NULL)

View File

@ -1,40 +1,39 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the nsURLFormatterService.
*
* The Initial Developer of the Original Code is
* Axel Hecht <axel@mozilla.com>
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Dietrich Ayala <dietrich@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is the nsURLFormatterService.
#
# The Initial Developer of the Original Code is
# Axel Hecht <axel@mozilla.com>
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Dietrich Ayala <dietrich@mozilla.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
/**
* @class nsURLFormatterService
*
@ -46,71 +45,49 @@
* http[s]://%LOCALE%.%SERVICE%.mozilla.[com|org]/%LOCALE%/
*/
/**
* constants
*/
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
const CID = Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}");
const CONTRACT_ID = "@mozilla.org/toolkit/URLFormatterService;1";
const CLASS_NAME = "Application URL Formatter Service";
/**
* class definition
*/
function nsURLFormatterService() {
}
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
function nsURLFormatterService() {}
nsURLFormatterService.prototype = {
classDescription: "Application URL Formatter Service",
contractID: "@mozilla.org/toolkit/URLFormatterService;1",
classID: Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIURLFormatter]),
/**
* Built-in values
*/
_defaults: {
get appInfo() {
if (!this._appInfo)
this._appInfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
return this._appInfo;
},
LOCALE: function() {
var chromereg = Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIXULChromeRegistry);
return chromereg.getSelectedLocale('global');
},
VENDOR: function() { return this.appInfo.vendor; },
NAME: function() { return this.appInfo.name; },
ID: function() { return this.appInfo.ID; },
VERSION: function() { return this.appInfo.version; },
APPBUILDID: function() { return this.appInfo.appBuildID; },
PLATFORMVERSION: function() { return this.appInfo.platformVersion; },
PLATFORMBUILDID: function() { return this.appInfo.platformBuildID; },
APP: function() { return this.appInfo.name.toLowerCase().replace(/ /, ""); }
LOCALE: function() Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIXULChromeRegistry).getSelectedLocale('global'),
VENDOR: function() this.appInfo.vendor,
NAME: function() this.appInfo.name,
ID: function() this.appInfo.ID,
VERSION: function() this.appInfo.version,
APPBUILDID: function() this.appInfo.appBuildID,
PLATFORMVERSION: function() this.appInfo.platformVersion,
PLATFORMBUILDID: function() this.appInfo.platformBuildID,
APP: function() this.appInfo.name.toLowerCase().replace(/ /, "")
},
/**
* nsIURLFormatter.formatURL
*/
formatURL: function uf_formatURL(aFormat) {
var _this = this;
var replacementCallback = function(aMatch, aKey) {
if (aKey in _this._defaults) // supported defaults
return _this._defaults[aKey]();
Components.utils.reportError("formatURL: Couldn't find value for key: " + aKey);
Cu.reportError("formatURL: Couldn't find value for key: " + aKey);
return '';
}
return aFormat.replace(/%([A-Z]+)%/gi, replacementCallback);
return aFormat.replace(/%([A-Z]+)%/g, replacementCallback);
},
/**
* nsIURLFormatter.formatURLPref
*/
formatURLPref: function uf_formatURLPref(aPref) {
var format = null;
var PS = Cc['@mozilla.org/preferences-service;1'].
@ -124,56 +101,13 @@ nsURLFormatterService.prototype = {
try {
format = PS.getComplexValue(aPref, Ci.nsISupportsString).data;
} catch(ex) {
Components.utils.reportError("formatURLPref: Couldn't get pref: " + aPref);
Cu.reportError("formatURLPref: Couldn't get pref: " + aPref);
return "about:blank";
}
}
return this.formatURL(format);
},
QueryInterface: function uf_QueryInterface(aIID) {
if (!aIID.equals(Ci.nsIURLFormatter) &&
!aIID.equals(Ci.nsISupports))
throw Cr.NS_ERROR_NO_INTERFACE;
return this;
}
};
/**
* XPCOM Registration, etc
*/
var nsURLFormatterFactory = {
createInstance: function (aOuter, aIID) {
if (aOuter != null)
throw Cr.NS_ERROR_NO_AGGREGATION;
return (new nsURLFormatterService()).QueryInterface(aIID);
}
};
var nsURLFormatterModule = {
registerSelf: function(aCompMgr, aFileSpec, aLocation, aType) {
aCompMgr = aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
aCompMgr.registerFactoryLocation(CID, CLASS_NAME,
CONTRACT_ID, aFileSpec, aLocation, aType);
},
unregisterSelf: function(aCompMgr, aLocation, aType) {
aCompMgr = aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
aCompMgr.unregisterFactoryLocation(CID, aLocation);
},
getClassObject: function(aCompMgr, aCID, aIID) {
if (!aIID.equals(Ci.nsIFactory))
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
if (aCID.equals(CID))
return nsURLFormatterFactory;
throw Cr.NS_ERROR_NO_INTERFACE;
},
canUnload: function(aCompMgr) { return true; }
};
function NSGetModule(aCompMgr, aFileSpec) { return nsURLFormatterModule; }
function NSGetModule(aCompMgr, aFileSpec)
XPCOMUtils.generateModule([nsURLFormatterService]);

View File

@ -0,0 +1,73 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Ryan Flint <rflint@dslr.net> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
var XULAppInfo = {
vendor: "Mozilla",
name: "Url Formatter Test",
ID: "urlformattertest@test.mozilla.org",
version: "1",
appBuildID: "2007122405",
platformVersion: "2.0",
platformBuildID: "2007122406",
inSafeMode: false,
logConsoleErrors: true,
OS: "XPCShell",
XPCOMABI: "noarch-spidermonkey",
QueryInterface: function QueryInterface(iid) {
if (iid.equals(Ci.nsIXULAppInfo) ||
iid.equals(Ci.nsIXULRuntime) ||
iid.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
var XULAppInfoFactory = {
createInstance: function (outer, iid) {
if (outer != null)
throw Cr.NS_ERROR_NO_AGGREGATION;
return XULAppInfo.QueryInterface(iid);
}
};
var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(Components.ID("{ecff8849-cee8-40a7-bd4a-3f4fdfeddb5c}"),
"XULAppInfo", "@mozilla.org/xre/app-info;1",
XULAppInfoFactory);

View File

@ -0,0 +1,63 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Ryan Flint <rflint@dslr.net> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
function run_test() {
var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].
getService(Ci.nsIURLFormatter);
var locale = Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIXULChromeRegistry).
getSelectedLocale('global');
var prefs = Cc['@mozilla.org/preferences-service;1'].
getService(Ci.nsIPrefBranch);
var upperUrlRaw = "http://%LOCALE%.%VENDOR%.foo/?name=%NAME%&id=%ID%&version=%VERSION%&platversion=%PLATFORMVERSION%&abid=%APPBUILDID%&pbid=%PLATFORMBUILDID%&app=%APP%";
var lowerUrlRaw = "http://%locale%.%vendor%.foo/?name=%name%&id=%id%&version=%version%&platversion=%platformversion%&abid=%appbuildid%&pbid=%platformbuildid%&app=%app%";
//XXX %APP%'s RegExp is not global, so it only replaces the first space
var ulUrlRef = "http://" + locale + ".Mozilla.foo/?name=Url Formatter Test&id=urlformattertest@test.mozilla.org&version=1&platversion=2.0&abid=2007122405&pbid=2007122406&app=urlformatter test";
var multiUrl = "http://%VENDOR%.%VENDOR%.%NAME%.%VENDOR%.%NAME%";
var multiUrlRef = "http://Mozilla.Mozilla.Url Formatter Test.Mozilla.Url Formatter Test";
var pref = "xpcshell.urlformatter.test";
var str = Cc["@mozilla.org/supports-string;1"].
createInstance(Ci.nsISupportsString);
str.data = upperUrlRaw;
prefs.setComplexValue(pref, Ci.nsISupportsString, str);
do_check_eq(formatter.formatURL(upperUrlRaw), ulUrlRef);
do_check_eq(formatter.formatURLPref(pref), ulUrlRef);
// Keys must be uppercase
do_check_neq(formatter.formatURL(lowerUrlRaw), ulUrlRef);
do_check_eq(formatter.formatURL(multiUrl), multiUrlRef);
}