Bug 1288684 - Add basic xpcshell tests for MessageContext and L10nRegistry. r?stas
MozReview-Commit-ID: KETTx1e7QVG
--- a/toolkit/modules/L10nRegistry.jsm
+++ b/toolkit/modules/L10nRegistry.jsm
@@ -228,16 +228,18 @@ const L10nRegistry = {
sources,
order: sourcesOrder
};
},
requestCacheInfo() {
return cache;
},
+
+ FileSource
};
const platformFileSource =
new FileSource('platform', 'gre/localization/{locale}');
const appFileSource =
new FileSource('app', '/localization/{locale}');
new file mode 100644
--- /dev/null
+++ b/toolkit/modules/tests/xpcshell/test_IntlMessageContext.js
@@ -0,0 +1,69 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * This file tests the IntlMessageContext.jsm module.
+ */
+
+
+// Globals
+
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var Cu = Components.utils;
+var Cr = Components.results;
+
+Cu.import("resource://gre/modules/IntlMessageContext.jsm");
+
+add_test(function test_object_exists() {
+
+ do_check_true(this.hasOwnProperty('MessageContext'));
+ run_next_test();
+});
+
+
+add_test(function test_simpleCase() {
+ const mc = new MessageContext('en-US');
+
+ mc.addMessages('key = Value');
+
+ const entity = mc.messages.get('key');
+
+ equal(entity, 'Value');
+ equal(mc.format(entity), 'Value');
+
+ run_next_test();
+});
+
+add_test(function test_withArgument() {
+ const mc = new MessageContext('en-US');
+
+ mc.addMessages('key = Hello, { $name }');
+
+ const entity = mc.messages.get('key');
+
+ equal(mc.format(entity, {name: "Mary"}), 'Hello, Mary');
+
+ run_next_test();
+});
+
+add_test(function test_withError() {
+ const mc = new MessageContext('en-US');
+
+ const errors = [];
+
+ mc.addMessages('key = Hello, { $name2 }');
+
+ const entity = mc.messages.get('key');
+
+ equal(mc.format(entity, {name: "Mary"}, errors), 'Hello, name2');
+
+ equal(errors.length, 1);
+
+ run_next_test();
+});
+
+
+function run_test() {
+ run_next_test();
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/modules/tests/xpcshell/test_L10nRegistry.js
@@ -0,0 +1,74 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * This file tests the L10nRegistry.jsm module.
+ */
+
+
+// Globals
+
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var Cu = Components.utils;
+var Cr = Components.results;
+
+Cu.import("resource://gre/modules/L10nRegistry.jsm");
+
+add_test(function test_object_exists() {
+
+ do_check_true(this.hasOwnProperty('L10nRegistry'));
+ run_next_test();
+});
+
+
+add_test(function test_default_resources_added() {
+ const sourceInfo = L10nRegistry.requestSourceInfo();
+
+ const sourceOrder = Array.from(sourceInfo.order);
+ equal(sourceOrder[0], 'app');
+ equal(sourceOrder[1], 'platform');
+ equal(sourceOrder.length, 2);
+
+ run_next_test();
+});
+
+add_task(function* test_getResources() {
+ const result = yield L10nRegistry.getResources(['en-US'], [
+ 'browser/brand.ftl'
+ ]);
+
+ equal(result.bundles.length, 2);
+
+ equal(Array.from(result.supportedLocales).length, 1);
+ equal(result.bundles[0].locale, 'en-US');
+ equal(result.bundles[0].resources['browser/brand.ftl'].locale, 'en-US');
+ equal(result.bundles[0].resources['browser/brand.ftl'].source, 'app');
+ equal(result.bundles[0].resources['browser/brand.ftl'].data, null);
+});
+
+add_task(function* test_fetchResource() {
+ const result =
+ yield L10nRegistry.fetchResource('app', 'browser/brand.ftl', 'en-US');
+
+ equal(result, null);
+});
+
+add_test(function test_registerSource() {
+ const langpackSource = new L10nRegistry.FileSource('langpack', '/path/foo');
+ L10nRegistry.registerSource(langpackSource);
+
+ const sourceInfo = L10nRegistry.requestSourceInfo();
+
+ const sourceOrder = Array.from(sourceInfo.order);
+
+ equal(sourceOrder[0], 'langpack');
+ equal(sourceOrder.length, 3);
+
+ run_next_test();
+});
+
+
+function run_test() {
+ run_next_test();
+}
--- a/toolkit/modules/tests/xpcshell/xpcshell.ini
+++ b/toolkit/modules/tests/xpcshell/xpcshell.ini
@@ -18,20 +18,22 @@ skip-if = toolkit == 'android'
[test_FileUtils.js]
skip-if = toolkit == 'android'
[test_FinderIterator.js]
[test_GMPInstallManager.js]
skip-if = toolkit == 'android'
[test_Http.js]
skip-if = toolkit == 'android'
[test_Integration.js]
+[test_IntlMessageContext.js]
[test_jsesc.js]
skip-if = toolkit == 'android'
[test_JSONFile.js]
skip-if = toolkit == 'android'
+[test_L10nRegistry.js]
[test_Log.js]
skip-if = toolkit == 'android'
[test_MatchPattern.js]
skip-if = toolkit == 'android'
[test_MatchGlobs.js]
skip-if = toolkit == 'android'
[test_MatchURLFilters.js]
skip-if = toolkit == 'android'