aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.5/packages/api-utils/tests/test-app-strings.js
blob: 902c31f8649369664255f5c96e03ba41c94034cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { Cc,Ci } = require("chrome");

let StringBundle = require("app-strings").StringBundle;
exports.testStringBundle = function(test) {
  let url = "chrome://global/locale/security/caps.properties";

  let strings = StringBundle(url);

  test.assertEqual(strings.url, url,
                   "'url' property contains correct URL of string bundle");

  let appLocale = Cc["@mozilla.org/intl/nslocaleservice;1"].
                  getService(Ci.nsILocaleService).
                  getApplicationLocale();

  let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"].
                     getService(Ci.nsIStringBundleService).
                     createBundle(url, appLocale);

  let (name = "Yes") {
    test.assertEqual(strings.get(name), stringBundle.GetStringFromName(name),
                     "getting a string returns the string");
  }

  let (name = "ExtensionCapability", args = ["foo"]) {
    test.assertEqual(strings.get(name, args),
                     stringBundle.formatStringFromName(name, args, args.length),
                     "getting a formatted string returns the formatted string");
  }

  test.assertRaises(function () strings.get("nonexistentString"),
                    "String 'nonexistentString' could not be retrieved from " +
                    "the bundle due to an unknown error (it doesn't exist?).",
                    "retrieving a nonexistent string throws an exception");

  let a = [], b = [];
  let enumerator = stringBundle.getSimpleEnumeration();
  while (enumerator.hasMoreElements()) {
    let elem = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement);
    a.push([elem.key, elem.value]);
  }
  for (let keyVal in Iterator(strings))
    b.push(keyVal);

  // Sort the arrays, because we don't assume enumeration has a set order.
  // Sort compares [key, val] as string "key,val", which sorts the way we want
  // it to, so there is no need to provide a custom compare function.
  a.sort();
  b.sort();

  test.assertEqual(a.length, b.length,
                   "the iterator returns the correct number of items");
  for (let i = 0; i < a.length; i++) {
    test.assertEqual(a[i][0], b[i][0], "the iterated string's name is correct");
    test.assertEqual(a[i][1], b[i][1],
                     "the iterated string's value is correct");
  }
};