Skip to content

Commit

Permalink
Add initial locale unit tests for the MozLoopService
Browse files Browse the repository at this point in the history
  • Loading branch information
Standard8 committed May 6, 2014
1 parent 05eab0b commit 886c1d3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
40 changes: 32 additions & 8 deletions browser/components/loop/MozLoopService.jsm
Expand Up @@ -9,8 +9,16 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");

this.EXPORTED_SYMBOLS = ["MozLoopService"];

// Internal helper methods and state
/**
* Internal helper methods and state
*/
let MozLoopServiceInternal = {
/**
* A getter to obtain and store the strings for loop. This is structured
* for use by l10n.js.
*
* @returns {Object} a map of element ids with attributes to set.
*/
get localizedStrings() {
var stringBundle =
Services.strings.createBundle('chrome://browser/locale/loop/loop.properties');
Expand All @@ -36,17 +44,33 @@ let MozLoopServiceInternal = {
};


// Public API
/**
* Public API
*/
this.MozLoopService = {
/**
* Returns the strings for the specified element. Designed for use
* with l10n.js.
*
* @param {key} The element id to get strings for.
* @return {String} A JSON string containing the localized
* attribute/value pairs for the element.
*/
getStrings: function(key) {
try {
return JSON.stringify(MozLoopServiceInternal.localizedStrings[key]);
} catch (ex) {
Cu.reportError('Unable to retrive localized strings: ' + e);
return null;
}
var stringData = MozLoopServiceInternal.localizedStrings;
if (!(key in stringData)) {
Cu.reportError('No string for key: ' + key + 'found');
return "";
}

return JSON.stringify(stringData[key]);
},

/**
* Returns the current locale
*
* @return {String} The code of the current locale.
*/
get locale() {
try {
return Services.prefs.getComplexValue("general.useragent.locale",
Expand Down
2 changes: 2 additions & 0 deletions browser/components/loop/moz.build
Expand Up @@ -8,6 +8,8 @@ JAR_MANIFESTS += ['jar.mn']

JS_MODULES_PATH = 'modules/loop'

XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']

EXTRA_JS_MODULES += [
'MozLoopService.jsm',
]
36 changes: 36 additions & 0 deletions browser/components/loop/test/xpcshell/test_loopservice_locales.js
@@ -0,0 +1,36 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "MozLoopService",
"resource:///modules/loop/MozLoopService.jsm");


function test_locale() {
// Set the pref to something controlled.
Services.prefs.setCharPref("general.useragent.locale", "ab-CD");

do_check_eq(MozLoopService.locale, "ab-CD");

Services.prefs.clearUserPref("general.useragent.locale");
}

function test_getStrings() {
// Try an invalid string
do_check_eq(MozLoopService.getStrings("invalid_not_found_string"), "");

// Get a string that has sub-items to test the function more fully.
// XXX This depends on the L10n values, which I'd prefer not to do, but is the
// simplest way for now.
do_check_eq(MozLoopService.getStrings("caller"), '{"placeholder":"Identify this call"}');

This comment has been minimized.

Copy link
@mhammond

mhammond May 6, 2014

It's hard to keep track of every "new hotness" that comes along, but the current one for xpcshell tests is to use the Assert module (which is already injected into tests) - so instead of do_check_eq, use Assert.equal()

}

function run_test()
{
test_locale();
test_getStrings();
}
6 changes: 6 additions & 0 deletions browser/components/loop/test/xpcshell/xpcshell.ini
@@ -0,0 +1,6 @@
[DEFAULT]
head =
tail =
firefox-appdir = browser

[test_loopservice_locales.js]

0 comments on commit 886c1d3

Please sign in to comment.