toolkit/components/urlformatter/tests/unit/test_urlformatter.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/urlformatter/tests/unit/test_urlformatter.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,65 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +function run_test() {
     1.8 +  var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].
     1.9 +                  getService(Ci.nsIURLFormatter);
    1.10 +  var locale = Cc["@mozilla.org/chrome/chrome-registry;1"].
    1.11 +               getService(Ci.nsIXULChromeRegistry).
    1.12 +               getSelectedLocale('global');
    1.13 +  var prefs = Cc['@mozilla.org/preferences-service;1'].
    1.14 +              getService(Ci.nsIPrefBranch);
    1.15 +  var sysInfo = Cc["@mozilla.org/system-info;1"].
    1.16 +                getService(Ci.nsIPropertyBag2);
    1.17 +  var OSVersion = sysInfo.getProperty("name") + " " +
    1.18 +                  sysInfo.getProperty("version");
    1.19 +  try {
    1.20 +    OSVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
    1.21 +  } catch (e) {}
    1.22 +  OSVersion = encodeURIComponent(OSVersion);
    1.23 +  var macutils = null;
    1.24 +  try {
    1.25 +    macutils = Cc["@mozilla.org/xpcom/mac-utils;1"].
    1.26 +               getService(Ci.nsIMacUtils);
    1.27 +  } catch (e) {}
    1.28 +  var appInfo = Cc["@mozilla.org/xre/app-info;1"].
    1.29 +                getService(Ci.nsIXULAppInfo).
    1.30 +                QueryInterface(Ci.nsIXULRuntime);
    1.31 +  var abi = macutils && macutils.isUniversalBinary ? "Universal-gcc3" : appInfo.XPCOMABI;
    1.32 +
    1.33 +  let channel = "default";
    1.34 +  let defaults = prefs.QueryInterface(Ci.nsIPrefService).getDefaultBranch(null);
    1.35 +  try {
    1.36 +    channel = defaults.getCharPref("app.update.channel");
    1.37 +  } catch (e) {}
    1.38 +  // Set distribution values.
    1.39 +  defaults.setCharPref("distribution.id", "bacon");
    1.40 +  defaults.setCharPref("distribution.version", "1.0");
    1.41 +
    1.42 +  var upperUrlRaw = "http://%LOCALE%.%VENDOR%.foo/?name=%NAME%&id=%ID%&version=%VERSION%&platversion=%PLATFORMVERSION%&abid=%APPBUILDID%&pbid=%PLATFORMBUILDID%&app=%APP%&os=%OS%&abi=%XPCOMABI%";
    1.43 +  var lowerUrlRaw = "http://%locale%.%vendor%.foo/?name=%name%&id=%id%&version=%version%&platversion=%platformversion%&abid=%appbuildid%&pbid=%platformbuildid%&app=%app%&os=%os%&abi=%xpcomabi%";
    1.44 +  //XXX %APP%'s RegExp is not global, so it only replaces the first space
    1.45 +  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;
    1.46 +  var multiUrl = "http://%VENDOR%.%VENDOR%.%NAME%.%VENDOR%.%NAME%";
    1.47 +  var multiUrlRef = "http://Mozilla.Mozilla.Url Formatter Test.Mozilla.Url Formatter Test";
    1.48 +  var encodedUrl = "https://%LOCALE%.%VENDOR%.foo/?q=%E3%82%BF%E3%83%96&app=%NAME%&ver=%PLATFORMVERSION%";
    1.49 +  var encodedUrlRef = "https://" + locale + ".Mozilla.foo/?q=%E3%82%BF%E3%83%96&app=Url Formatter Test&ver=2.0";
    1.50 +  var advancedUrl = "http://test.mozilla.com/%NAME%/%VERSION%/%APPBUILDID%/%BUILD_TARGET%/%LOCALE%/%CHANNEL%/%OS_VERSION%/%DISTRIBUTION%/%DISTRIBUTION_VERSION%/";
    1.51 +  var advancedUrlRef = "http://test.mozilla.com/Url Formatter Test/1/2007122405/XPCShell_" + abi + "/" + locale + "/" + channel + "/" + OSVersion + "/bacon/1.0/";
    1.52 +
    1.53 +  var pref = "xpcshell.urlformatter.test";
    1.54 +  var str = Cc["@mozilla.org/supports-string;1"].
    1.55 +            createInstance(Ci.nsISupportsString);
    1.56 +  str.data = upperUrlRaw;
    1.57 +  prefs.setComplexValue(pref, Ci.nsISupportsString, str);
    1.58 +
    1.59 +  do_check_eq(formatter.formatURL(upperUrlRaw), ulUrlRef);
    1.60 +  do_check_eq(formatter.formatURLPref(pref), ulUrlRef);
    1.61 +  // Keys must be uppercase
    1.62 +  do_check_neq(formatter.formatURL(lowerUrlRaw), ulUrlRef);
    1.63 +  do_check_eq(formatter.formatURL(multiUrl), multiUrlRef);
    1.64 +  // Encoded strings must be kept as is (Bug 427304)
    1.65 +  do_check_eq(formatter.formatURL(encodedUrl), encodedUrlRef);
    1.66 +
    1.67 +  do_check_eq(formatter.formatURL(advancedUrl), advancedUrlRef);
    1.68 +}

mercurial