Bug 1209184: Part 3 - [webext] Add tests for locale stream converter. r=billm
☠☠ backed out by cbf641f8da0a ☠ ☠
authorKris Maglione <maglione.k@gmail.com>
Fri, 27 Nov 2015 23:02:41 -0800
changeset 308634 399404ff25e40003c620cd1d6193790127a5c121
parent 308633 a517959befe9b9e426380771c8712e568bb62832
child 308635 24282235336dd3dd3c725bcd6e8025dcf9fe0fb4
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm
bugs1209184
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1209184: Part 3 - [webext] Add tests for locale stream converter. r=billm
toolkit/components/extensions/moz.build
toolkit/components/extensions/test/xpcshell/head.js
toolkit/components/extensions/test/xpcshell/test_locale_converter.js
toolkit/components/extensions/test/xpcshell/xpcshell.ini
--- a/toolkit/components/extensions/moz.build
+++ b/toolkit/components/extensions/moz.build
@@ -10,8 +10,10 @@ EXTRA_JS_MODULES += [
     'ExtensionManagement.jsm',
     'ExtensionStorage.jsm',
     'ExtensionUtils.jsm',
 ]
 
 JAR_MANIFESTS += ['jar.mn']
 
 MOCHITEST_MANIFESTS += ['test/mochitest/mochitest.ini']
+
+XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/head.js
@@ -0,0 +1,10 @@
+"use strict";
+
+const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
+
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
+                                  "resource://gre/modules/NetUtil.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+                                  "resource://gre/modules/Services.jsm");
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_locale_converter.js
@@ -0,0 +1,118 @@
+"use strict";
+
+const convService = Cc["@mozilla.org/streamConverters;1"]
+  .getService(Ci.nsIStreamConverterService);
+
+const UUID = "72b61ee3-aceb-476c-be1b-0822b036c9f1";
+const ADDON_ID = "test@web.extension";
+const URI = NetUtil.newURI(`moz-extension://${UUID}/file.css`);
+
+const FROM_TYPE = "application/vnd.mozilla.webext.unlocalized";
+const TO_TYPE = "text/css";
+
+
+function StringStream(string) {
+  let stream = Cc["@mozilla.org/io/string-input-stream;1"]
+    .createInstance(Ci.nsIStringInputStream);
+
+  stream.data = string;
+  return stream;
+}
+
+
+// Initialize the policy service with a stub localizer for our
+// add-on ID.
+add_task(function* init() {
+  const aps = Cc["@mozilla.org/addons/policy-service;1"]
+    .getService(Ci.nsIAddonPolicyService).wrappedJSObject;
+
+  let oldCallback = aps.setExtensionURIToAddonIdCallback(uri => {
+    if (uri.host == UUID) {
+      return ADDON_ID;
+    }
+  });
+
+  aps.setAddonLocalizeCallback(ADDON_ID, string => {
+    return string.replace(/__MSG_(.*?)__/g, "<localized-$1>");
+  });
+
+  do_register_cleanup(() => {
+    aps.setExtensionURIToAddonIdCallback(oldCallback);
+    aps.setAddonLocalizeCallback(ADDON_ID, null);
+  });
+});
+
+
+// Test that the synchronous converter works as expected with a
+// simple string.
+add_task(function* testSynchronousConvert() {
+  let stream = StringStream("Foo __MSG_xxx__ bar __MSG_yyy__ baz");
+
+  let resultStream = convService.convert(stream, FROM_TYPE, TO_TYPE, URI);
+
+  let result = NetUtil.readInputStreamToString(resultStream, resultStream.available());
+
+  equal(result, "Foo <localized-xxx> bar <localized-yyy> baz");
+});
+
+
+// Test that the asynchronous converter works as expected with input
+// split into multiple chunks, and a boundary in the middle of a
+// replacement token.
+add_task(function* testAsyncConvert() {
+  let listener;
+  let awaitResult = new Promise((resolve, reject) => {
+    listener = {
+      QueryInterface: XPCOMUtils.generateQI([Ci.nsIStreamListener]),
+
+      onDataAvailable(request, context, inputStream, offset, count) {
+        this.resultParts.push(NetUtil.readInputStreamToString(inputStream, count));
+      },
+
+      onStartRequest() {
+        ok(!("resultParts" in this));
+        this.resultParts = [];
+      },
+
+      onStopRequest(request, context, statusCode) {
+        if (!Components.isSuccessCode(statusCode)) {
+          reject(new Error(statusCode));
+        }
+
+        resolve(this.resultParts.join("\n"));
+      },
+    };
+  });
+
+  let parts = ["Foo __MSG_x", "xx__ bar __MSG_yyy__ baz"];
+
+  let converter = convService.asyncConvertData(FROM_TYPE, TO_TYPE, listener, URI);
+  converter.onStartRequest(null, null);
+
+  for (let part of parts) {
+    converter.onDataAvailable(null, null, StringStream(part), 0, part.length);
+  }
+
+  converter.onStopRequest(null, null, Cr.NS_OK);
+
+
+  let result = yield awaitResult;
+  equal(result, "Foo <localized-xxx> bar <localized-yyy> baz");
+});
+
+
+// Test that attempting to initialize a converter with the URI of a
+// nonexistent WebExtension fails.
+add_task(function* testInvalidUUID() {
+  let uri = NetUtil.newURI("moz-extension://eb4f3be8-41c9-4970-aa6d-b84d1ecc02b2/file.css");
+
+  Assert.throws(() => {
+    convService.convert(stream, FROM_TYPE, TO_TYPE, uri);
+  });
+
+  Assert.throws(() => {
+    let listener = { QueryInterface: XPCOMUtils.generateQI([Ci.nsIStreamListener]) };
+
+    convService.asyncConvertData(FROM_TYPE, TO_TYPE, listener, uri);
+  });
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell.ini
@@ -0,0 +1,5 @@
+[DEFAULT]
+head = head.js
+tail =
+
+[test_locale_converter.js]