Bug 1288684 - Add basic xpcshell tests for MessageContext and L10nRegistry. r?stas
authorZibi Braniecki <gandalf@mozilla.com>
Tue, 08 Nov 2016 00:33:39 -0800
changeset 435277 4bf4d2d21fb5ea638f76bdcb28b87930bddc35f6
parent 435276 7ccf894aacb0840b8e06e028abe1d494c5c001c5
child 435669 9dd788eea93029bcac0c7a19432a9ab884e943df
child 435672 dea4830b729c34e72563ebf8b96d72cfb4e31bf2
child 435673 52b2b261a08b9467df6218d839c1120a91c9d636
child 435709 43c4c05c35e6424ae3eac2495756ede50b7337b6
child 437260 2a8c3049b464a92b878b23a866649c0fac9cede2
push id34980
push userzbraniecki@mozilla.com
push dateTue, 08 Nov 2016 08:35:15 +0000
reviewersstas
bugs1288684
milestone52.0a1
Bug 1288684 - Add basic xpcshell tests for MessageContext and L10nRegistry. r?stas MozReview-Commit-ID: KETTx1e7QVG
toolkit/modules/L10nRegistry.jsm
toolkit/modules/tests/xpcshell/test_IntlMessageContext.js
toolkit/modules/tests/xpcshell/test_L10nRegistry.js
toolkit/modules/tests/xpcshell/xpcshell.ini
--- 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'