Bug 1208761: [webext] Support l10n.getMessage in content scripts. r=billm
authorKris Maglione <maglione.k@gmail.com>
Mon, 23 Nov 2015 17:52:23 -0800
changeset 273797 4dca10306d58d2e87cbed46a72472b62c17b0ec9
parent 273796 58d49370eb712781f5b555e2a39b969a58ce77f0
child 273798 c6d9dafc91a1c76fd0a79c5650e84a58569b5af7
child 274032 45273bbed8efaface6f5ec56d984cb9faf4fbb6a
push id16369
push usermaglione.k@gmail.com
push dateTue, 24 Nov 2015 07:00:46 +0000
treeherderfx-team@4dca10306d58 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm
bugs1208761
milestone45.0a1
Bug 1208761: [webext] Support l10n.getMessage in content scripts. r=billm
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/test/mochitest/test_ext_i18n.html
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -89,16 +89,22 @@ var api = context => { return {
 
   extension: {
     getURL: function(url) {
       return context.extension.baseURI.resolve(url);
     },
 
     inIncognitoContext: PrivateBrowsingUtils.isContentWindowPrivate(context.contentWindow),
   },
+
+  i18n: {
+    getMessage: function(messageName, substitutions) {
+      return context.extension.localizeMessage(messageName, substitutions);
+    },
+  },
 }};
 
 // Represents a content script.
 function Script(options)
 {
   this.options = options;
   this.run_at = this.options.run_at;
   this.js = this.options.js || [];
--- a/toolkit/components/extensions/test/mochitest/test_ext_i18n.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_i18n.html
@@ -8,21 +8,59 @@
   <script type="text/javascript" src="head.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 
 <script type="application/javascript;version=1.8">
 
 add_task(function* test_i18n() {
+  function runTests(assertEq) {
+    let _ = browser.i18n.getMessage.bind(browser.i18n);
+
+    assertEq("Foo.", _("Foo"), "Simple message in selected locale.");
+
+    assertEq("(bar)", _("bar"), "Simple message fallback in default locale.");
+
+    let substitutions = [];
+    substitutions[4] = "5";
+    substitutions[13] = "14";
+
+    assertEq("'$0' '14' '' '5' '$$$$' '$'.", _("basic_substitutions", substitutions),
+             "Basic numeric substitutions");
+
+    assertEq("'$0' '' 'just a string' '' '$$$$' '$'.", _("basic_substitutions", "just a string"),
+             "Basic numeric substitutions, with non-array value");
+
+    let values = _("named_placeholder_substitutions", ["(subst $1 $2)", "(2 $1 $2)"]).split("\n");
+
+    assertEq("_foo_ (subst $1 $2) _bar_", values[0], "Named and numeric substitution");
+
+    assertEq("(2 $1 $2)", values[1], "Numeric substitution amid named placeholders");
+
+    assertEq("$bad name$", values[2], "Named placeholder with invalid key");
+
+    assertEq("", values[3], "Named placeholder with an invalid value");
+
+    assertEq("Accepted, but shouldn't break.", values[4], "Named placeholder with a strange content value");
+
+    assertEq("$foo", values[5], "Non-placeholder token that should be ignored");
+  };
+
   let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       "default_locale": "jp",
+
+      content_scripts: [
+        { "matches": ["http://mochi.test/*/file_sample.html"],
+          "js": ["content.js"] },
+      ],
     },
 
+
     files: {
       "_locales/en_US/messages.json": {
         "foo": {
           "message": "Foo.",
           "description": "foo",
         },
 
         "basic_substitutions": {
@@ -66,56 +104,47 @@ add_task(function* test_i18n() {
           "description": "foo",
         },
 
         "bar": {
           "message": "(bar)",
           "description": "bar",
         },
       },
+
+      "content.js": "new " + function(runTests) {
+        runTests((...args) => {
+          browser.runtime.sendMessage(["assertEq", ...args]);
+        });
+
+        browser.runtime.sendMessage(["content-script-finished"]);
+      } + `(${runTests})`,
     },
 
-    background: "new " + function() {
-      let _ = browser.i18n.getMessage.bind(browser.i18n);
-
-      browser.test.assertEq("Foo.", _("Foo"), "Simple message in selected locale.");
-
-      browser.test.assertEq("(bar)", _("bar"), "Simple message fallback in default locale.");
-
-      let substitutions = [];
-      substitutions[4] = "5";
-      substitutions[13] = "14";
-
-      browser.test.assertEq("'$0' '14' '' '5' '$$$$' '$'.",
-                            _("basic_substitutions", substitutions),
-                            "Basic numeric substitutions");
+    background: "new " + function(runTests) {
+      browser.runtime.onMessage.addListener(([msg, ...args]) => {
+        if (msg == "assertEq") {
+          browser.test.assertEq(...args);
+        } else {
+          browser.test.sendMessage(msg, ...args);
+        }
+      });
 
-      browser.test.assertEq("'$0' '' 'just a string' '' '$$$$' '$'.",
-                            _("basic_substitutions", "just a string"),
-                            "Basic numeric substitutions, with non-array value");
-
-      let values = _("named_placeholder_substitutions", ["(subst $1 $2)", "(2 $1 $2)"]).split("\n");
-
-      browser.test.assertEq("_foo_ (subst $1 $2) _bar_", values[0], "Named and numeric substitution");
-
-      browser.test.assertEq("(2 $1 $2)", values[1], "Numeric substitution amid named placeholders");
-
-      browser.test.assertEq("$bad name$", values[2], "Named placeholder with invalid key");
-
-      browser.test.assertEq("", values[3], "Named placeholder with an invalid value");
-
-      browser.test.assertEq("Accepted, but shouldn't break.", values[4], "Named placeholder with a strange content value");
-
-      browser.test.assertEq("$foo", values[5], "Non-placeholder token that should be ignored");
+      runTests(browser.test.assertEq.bind(browser.test));
 
       browser.test.notifyPass("l10n");
-    },
+    } + `(${runTests})`,
   });
 
   yield extension.startup();
+
+  let win = window.open("file_sample.html");
+  yield extension.awaitMessage("content-script-finished");
+  win.close();
+
   yield extension.awaitFinish("l10n");
   yield extension.unload();
 });
 
 </script>
 
 </body>
 </html>