Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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 function run_test() {
5 var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].
6 getService(Ci.nsIURLFormatter);
7 var locale = Cc["@mozilla.org/chrome/chrome-registry;1"].
8 getService(Ci.nsIXULChromeRegistry).
9 getSelectedLocale('global');
10 var prefs = Cc['@mozilla.org/preferences-service;1'].
11 getService(Ci.nsIPrefBranch);
12 var sysInfo = Cc["@mozilla.org/system-info;1"].
13 getService(Ci.nsIPropertyBag2);
14 var OSVersion = sysInfo.getProperty("name") + " " +
15 sysInfo.getProperty("version");
16 try {
17 OSVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
18 } catch (e) {}
19 OSVersion = encodeURIComponent(OSVersion);
20 var macutils = null;
21 try {
22 macutils = Cc["@mozilla.org/xpcom/mac-utils;1"].
23 getService(Ci.nsIMacUtils);
24 } catch (e) {}
25 var appInfo = Cc["@mozilla.org/xre/app-info;1"].
26 getService(Ci.nsIXULAppInfo).
27 QueryInterface(Ci.nsIXULRuntime);
28 var abi = macutils && macutils.isUniversalBinary ? "Universal-gcc3" : appInfo.XPCOMABI;
30 let channel = "default";
31 let defaults = prefs.QueryInterface(Ci.nsIPrefService).getDefaultBranch(null);
32 try {
33 channel = defaults.getCharPref("app.update.channel");
34 } catch (e) {}
35 // Set distribution values.
36 defaults.setCharPref("distribution.id", "bacon");
37 defaults.setCharPref("distribution.version", "1.0");
39 var upperUrlRaw = "http://%LOCALE%.%VENDOR%.foo/?name=%NAME%&id=%ID%&version=%VERSION%&platversion=%PLATFORMVERSION%&abid=%APPBUILDID%&pbid=%PLATFORMBUILDID%&app=%APP%&os=%OS%&abi=%XPCOMABI%";
40 var lowerUrlRaw = "http://%locale%.%vendor%.foo/?name=%name%&id=%id%&version=%version%&platversion=%platformversion%&abid=%appbuildid%&pbid=%platformbuildid%&app=%app%&os=%os%&abi=%xpcomabi%";
41 //XXX %APP%'s RegExp is not global, so it only replaces the first space
42 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&os=XPCShell&abi=" + abi;
43 var multiUrl = "http://%VENDOR%.%VENDOR%.%NAME%.%VENDOR%.%NAME%";
44 var multiUrlRef = "http://Mozilla.Mozilla.Url Formatter Test.Mozilla.Url Formatter Test";
45 var encodedUrl = "https://%LOCALE%.%VENDOR%.foo/?q=%E3%82%BF%E3%83%96&app=%NAME%&ver=%PLATFORMVERSION%";
46 var encodedUrlRef = "https://" + locale + ".Mozilla.foo/?q=%E3%82%BF%E3%83%96&app=Url Formatter Test&ver=2.0";
47 var advancedUrl = "http://test.mozilla.com/%NAME%/%VERSION%/%APPBUILDID%/%BUILD_TARGET%/%LOCALE%/%CHANNEL%/%OS_VERSION%/%DISTRIBUTION%/%DISTRIBUTION_VERSION%/";
48 var advancedUrlRef = "http://test.mozilla.com/Url Formatter Test/1/2007122405/XPCShell_" + abi + "/" + locale + "/" + channel + "/" + OSVersion + "/bacon/1.0/";
50 var pref = "xpcshell.urlformatter.test";
51 var str = Cc["@mozilla.org/supports-string;1"].
52 createInstance(Ci.nsISupportsString);
53 str.data = upperUrlRaw;
54 prefs.setComplexValue(pref, Ci.nsISupportsString, str);
56 do_check_eq(formatter.formatURL(upperUrlRaw), ulUrlRef);
57 do_check_eq(formatter.formatURLPref(pref), ulUrlRef);
58 // Keys must be uppercase
59 do_check_neq(formatter.formatURL(lowerUrlRaw), ulUrlRef);
60 do_check_eq(formatter.formatURL(multiUrl), multiUrlRef);
61 // Encoded strings must be kept as is (Bug 427304)
62 do_check_eq(formatter.formatURL(encodedUrl), encodedUrlRef);
64 do_check_eq(formatter.formatURL(advancedUrl), advancedUrlRef);
65 }