Bug 1372899 - provide scriptable MUTF-7 converter and use it in tests. r=aceman
authorJorg K <jorgk@jorgk.com>
Sat, 01 Jul 2017 15:32:47 +0200
changeset 28404 df37b1887f4dc4170cc657bf4317f5870209485c
parent 28403 ceb1142708c762273ba9436887b5ff7b8ef60f5d
child 28405 8f2f953f2ed32a1eb84344ea69a7cb9d09957301
push id1986
push userclokep@gmail.com
push dateWed, 02 Aug 2017 14:43:31 +0000
treeherdercomm-beta@b51c9adf2c9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaceman
bugs1372899
Bug 1372899 - provide scriptable MUTF-7 converter and use it in tests. r=aceman
mailnews/imap/test/unit/test_mailboxes.js
mailnews/intl/nsCharsetConverterManager.cpp
mailnews/intl/nsICharsetConverterManager.idl
mailnews/test/fakeserver/imapd.js
--- a/mailnews/imap/test/unit/test_mailboxes.js
+++ b/mailnews/imap/test/unit/test_mailboxes.js
@@ -1,21 +1,19 @@
 /**
  * Tests basic mailbox handling of IMAP, like discovery, rename and empty folder.
  */
 
 load("../../../resources/logHelper.js");
 load("../../../resources/asyncTestUtils.js");
 load("../../../resources/messageGenerator.js");
 
-// Due to bug 1363281 we currently can't decode UTF-7 during tests.
-// const folderName1 = "I18N box\u00E1";
-const folderName1 = "I18N box";
-// const folderName2 = "test \u00E4";
-const folderName2 = "test box";
+// The following folder names are not pure ASCII and will be MUTF-7 encoded.
+const folderName1 = "I18N box\u00E1"; // I18N boxá
+const folderName2 = "test \u00E4";    // test ä
 
 function* setup() {
   setupIMAPPump();
 
   IMAPPump.daemon.createMailbox(folderName1, {subscribed : true});
   IMAPPump.daemon.createMailbox("Unsubscribed box");
   // Create an all upper case trash folder name to make sure
   // we handle special folder names case-insensitively.
--- a/mailnews/intl/nsCharsetConverterManager.cpp
+++ b/mailnews/intl/nsCharsetConverterManager.cpp
@@ -12,16 +12,17 @@
 #include "nsIStringBundle.h"
 #include "nsTArray.h"
 #include "nsStringEnumerator.h"
 #include "mozilla/Services.h"
 
 #include "nsComponentManagerUtils.h"
 #include "nsISupportsPrimitives.h"
 #include "nsServiceManagerUtils.h"
+#include "../base/util/nsMsgI18N.h"
 
 // just for CONTRACTIDs
 #include "nsCharsetConverterManager.h"
 
 static nsIStringBundle * sDataBundle;
 static nsIStringBundle * sTitleBundle;
 
 // Class nsCharsetConverterManager [implementation]
@@ -191,8 +192,20 @@ nsCharsetConverterManager::GetCharsetLan
 
   if (NS_SUCCEEDED(rv)) {
     ToLowerCase(langGroup); // use lowercase for all language atoms
     *aResult = NS_Atomize(langGroup).take();
   }
 
   return rv;
 }
+
+NS_IMETHODIMP
+nsCharsetConverterManager::Mutf7ToUnicode(const char* aSrc, nsAString& aDest)
+{
+  return CopyMUTF7toUTF16(nsDependentCString(aSrc), aDest);
+}
+
+NS_IMETHODIMP
+nsCharsetConverterManager::UnicodeToMutf7(const char16_t* aSrc, nsACString& aDest)
+{
+  return CopyUTF16toMUTF7(nsDependentString(aSrc), aDest);
+}
--- a/mailnews/intl/nsICharsetConverterManager.idl
+++ b/mailnews/intl/nsICharsetConverterManager.idl
@@ -55,9 +55,15 @@ interface nsICharsetConverterManager : n
      * calling <tt>getCharsetData</tt> with the <tt>prop</tt> "LangGroup".
      * 
      * @param aCharset name of the character encoding, e.g. 'iso-8859-15'.
      * @throws if aCharset is an unknown charset.
      * @return the language code for the character encoding.
      */
     nsIAtom getCharsetLangGroup(in string aCharset);
     nsIAtom getCharsetLangGroupRaw(in string aCharset);
+
+    /**
+     * Support for Modified UTF-7 (MUTF-7) used by IMAP.
+     */
+    AString mutf7ToUnicode(in string aMutf7);
+    ACString unicodeToMutf7(in wstring aUnicode);
 };
--- a/mailnews/test/fakeserver/imapd.js
+++ b/mailnews/test/fakeserver/imapd.js
@@ -296,21 +296,20 @@ imapMailbox.prototype = {
     }
     return matching;
   },
   get fullName () {
     return (this._parent ? this._parent.fullName + this.delimiter : "") +
            this.name;
   },
   get displayName() {
-    // We should decode the folder name from the modified UTF-7 used by IMAP,
-    // but after bug 1363281 / bug 1261841 there isn't a scriptable
-    // converter any more. Since folder names are mostly ASCII, we just
-    // skip the conversion for now.
-    return this.fullName;
+    let manager = Cc['@mozilla.org/charset-converter-manager;1']
+                    .getService(Ci.nsICharsetConverterManager);
+    // Escape backslash and double-quote with another backslash before encoding.
+    return manager.unicodeToMutf7(this.fullName.replace(/([\\"])/g, '\\$1'));
   },
   get allChildren() {
     return this._children.reduce(function (arr, elem) {
       return arr.concat(elem._allChildrenInternal);
     }, []);
   },
   get _allChildrenInternal() {
     return this._children.reduce(function (arr, elem) {
@@ -579,20 +578,19 @@ function formatArg(argument, spec) {
 
   // By now, we know that the input should be generated from an atom or string.
   if (typeof argument != "string")
     throw "Expected argument of type " + spec + "!";
 
   if (spec == "atom") {
     argument = argument.toUpperCase();
   } else if (spec == "mailbox") {
-    // We should decode the argument from the modified UTF-7 used by IMAP,
-    // but after bug 1363281 / bug 1261841 there isn't a scriptable
-    // converter any more. Since folder names are mostly ASCII, we just
-    // skip the conversion for now.
+    let manager = Cc['@mozilla.org/charset-converter-manager;1']
+                    .getService(Ci.nsICharsetConverterManager);
+    argument = manager.mutf7ToUnicode(argument);
   } else if (spec == "string") {
     // Do nothing
   } else if (spec == "flag") {
     argument = argument.toLowerCase();
     if (!('a' <= argument[0] && argument[0] <= 'z') &&
         !('A' <= argument[0] && argument[0] <= 'Z')) {
       argument = argument[0] + argument[1].toUpperCase() + argument.substr(2);
     } else {