Bug 750012 - xpcshell test. r=dbienvenu
authorDavid Lechner <gismho@gmail.com>
Mon, 18 Jun 2012 11:25:20 -0500
changeset 10478 7631dde8734bd28ef9454d259faa6516c138426f
parent 10477 3cc2a51baba8892b77b83b344c3c8a48f1d28567
child 10479 2b69382950af9bd0b83387554050f90077e2d6d9
push id7925
push userryanvm@gmail.com
push dateTue, 19 Jun 2012 22:23:46 +0000
treeherdercomm-central@2b69382950af [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbienvenu
bugs750012
Bug 750012 - xpcshell test. r=dbienvenu
mailnews/imap/test/unit/test_fetchCustomAttribute.js
mailnews/imap/test/unit/xpcshell.ini
mailnews/test/fakeserver/imapd.js
new file mode 100644
--- /dev/null
+++ b/mailnews/imap/test/unit/test_fetchCustomAttribute.js
@@ -0,0 +1,142 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * Test to ensure that imap fetchCustomMsgAttribute function works properly
+ * Bug 750012 
+ * uses Gmail extensions as test case - also useful for bug 721316
+ */
+
+// async support 
+load("../../../resources/logHelper.js");
+load("../../../resources/mailTestUtils.js");
+load("../../../resources/asyncTestUtils.js");
+
+// IMAP pump
+load("../../../resources/IMAPpump.js");
+
+Components.utils.import("resource://gre/modules/Services.jsm");
+
+// Globals
+
+// Messages to load must have CRLF line endings, that is Windows style
+const gMessage = "bugmail10"; // message file used as the test message
+
+const gXGmMsgid = "1278455344230334865";
+const gXGmThrid = "1266894439832287888";
+const gXGmLabels = '(\\Inbox \\Sent Important "Muy Importante" foo)';
+
+var gMsgWindow = Cc["@mozilla.org/messenger/msgwindow;1"]
+  .createInstance(Ci.nsIMsgWindow);
+
+setupIMAPPump();
+
+// Definition of tests
+var tests = [
+  loadImapMessage,
+  testFetchXGmMsgid,
+  testFetchXGmThrid,
+  testFetchXGmLabels,
+  endTest
+]
+
+// load and update a message in the imap fake server
+function loadImapMessage()
+{
+  let message = new imapMessage(specForFileName(gMessage),
+                          gIMAPMailbox.uidnext++, []);
+  message.xGmMsgid = gXGmMsgid;
+  message.xGmThrid = gXGmThrid;
+  message.xGmLabels = gXGmLabels;
+  gIMAPMailbox.addMessage(message);
+  gIMAPInbox.updateFolderWithListener(null, asyncUrlListener);
+  yield false;
+}
+
+function testFetchXGmMsgid()
+{
+  let msgHdr = firstMsgHdr(gIMAPInbox);
+  let uri = gIMAPInbox.fetchCustomMsgAttribute("X-GM-MSGID", msgHdr.messageKey, gMsgWindow);
+  uri.QueryInterface(Ci.nsIMsgMailNewsUrl);
+  uri.RegisterListener(xGmMsgidListener);
+  yield false;
+}
+
+// listens for respone from fetchCustomMsgAttribute request for X-GM-MSGID
+var xGmMsgidListener = {
+  OnStartRunningUrl: function (aUrl) {},
+
+  OnStopRunningUrl: function (aUrl, aExitCode) {
+    aUrl.QueryInterface(Ci.nsIImapUrl);
+    do_check_eq(aUrl.customAttributeResult, gXGmMsgid);
+    async_driver();
+  }
+};
+
+function testFetchXGmThrid()
+{
+  let msgHdr = firstMsgHdr(gIMAPInbox);
+  let uri = gIMAPInbox.fetchCustomMsgAttribute("X-GM-THRID", msgHdr.messageKey, gMsgWindow);
+  uri.QueryInterface(Ci.nsIMsgMailNewsUrl);
+  uri.RegisterListener(xGmThridListener);
+  yield false;
+}
+
+// listens for respone from fetchCustomMsgAttribute request for X-GM-THRID
+var xGmThridListener = {
+  OnStartRunningUrl: function (aUrl) {},
+
+  OnStopRunningUrl: function (aUrl, aExitCode) {
+    aUrl.QueryInterface(Ci.nsIImapUrl);
+    do_check_eq(aUrl.customAttributeResult, gXGmThrid);
+    async_driver();
+  }
+};
+
+function testFetchXGmLabels()
+{
+  let msgHdr = firstMsgHdr(gIMAPInbox);
+  let uri = gIMAPInbox.fetchCustomMsgAttribute("X-GM-LABELS", msgHdr.messageKey, gMsgWindow);
+  uri.QueryInterface(Ci.nsIMsgMailNewsUrl);
+  uri.RegisterListener(xGmLabelsListener);
+  yield false;
+}
+
+// listens for respone from fetchCustomMsgAttribute request for X-GM-LABELS
+var xGmLabelsListener = {
+  OnStartRunningUrl: function (aUrl) {},
+
+  OnStopRunningUrl: function (aUrl, aExitCode) {
+    aUrl.QueryInterface(Ci.nsIImapUrl);
+    do_check_eq(aUrl.customAttributeResult, gXGmLabels);
+    async_driver();
+  }
+};
+
+// Cleanup at end
+function endTest()
+{
+  teardownIMAPPump();
+}
+
+function run_test()
+{
+  Services.prefs.setBoolPref("mail.server.server1.autosync_offline_stores", false);
+  async_run_tests(tests);
+}
+
+/*
+ * helper functions
+ */
+
+// given a test file, return the file uri spec
+function specForFileName(aFileName)
+{
+  let file = do_get_file("../../../data/" + aFileName);
+  let msgfileuri = Cc["@mozilla.org/network/io-service;1"]
+                     .getService(Ci.nsIIOService)
+                     .newFileURI(file)
+                     .QueryInterface(Ci.nsIFileURL);
+  return msgfileuri.spec;
+}
\ No newline at end of file
--- a/mailnews/imap/test/unit/xpcshell.ini
+++ b/mailnews/imap/test/unit/xpcshell.ini
@@ -5,16 +5,17 @@ tail = tail_imap.js
 [test_autosync_date_constraints.js]
 [test_bccProperty.js]
 [test_bug460636.js]
 [test_compactOfflineStore.js]
 [test_copyThenMove.js]
 [test_dod.js]
 [test_dontStatNoSelect.js]
 [test_downloadOffline.js]
+[test_fetchCustomAttribute.js]
 [test_filterCustomHeaders.js]
 [test_filterNeedsBody.js]
 [test_imapAttachmentSaves.js]
 [test_imapAuthMethods.js]
 [test_imapAutoSync.js]
 [test_imapContentLength.js]
 [test_imapCopyTimeout.js]
 [test_imapFilterActions.js]
--- a/mailnews/test/fakeserver/imapd.js
+++ b/mailnews/test/fakeserver/imapd.js
@@ -1209,18 +1209,19 @@ IMAP_RFC3501_handler.prototype = {
     for (var i = 0; i < messages.length; i++) {
       response += "* " + ids[i] + " FETCH (";
       var parts = [];
       for each (var item in items) {
 
         // Brief explanation: an item like BODY[]<> can't be hardcoded easily,
         // so we go for the initial alphanumeric substring, passing in the
         // actual string as an optional second part.
-        var front = item.split(/[^A-Z0-9]/, 1)[0];
-        var functionName = "_FETCH_" + front;
+        var front = item.split(/[^A-Z0-9-]/, 1)[0];
+        var functionName = "_FETCH_" + front.replace(/-/g, "_"); // '-' is not allowed in js identifiers;     
+       
         if (!(functionName in this))
           return "BAD can't fetch " + front;
         try {
           parts.push(this[functionName](messages[i], item));
         } catch (ex) {
 
           return "BAD error in fetching: "+ex;
         }
@@ -1571,16 +1572,37 @@ IMAP_RFC3501_handler.prototype = {
       }
       return "RFC822.SIZE " + length;
     } else {
       throw "Unknown item "+query;
     }
   },
   _FETCH_UID : function (message) {
     return "UID " + message.uid;
+  },
+  _FETCH_X_GM_MSGID : function (message) {
+    if (message.xGmMsgid) {
+        return "X-GM-MSGID " + message.xGmMsgid;
+    } else {
+        return "BAD can't fetch X-GM-MSGID";
+    }
+  },
+  _FETCH_X_GM_THRID : function (message) {
+    if (message.xGmThrid) {
+        return "X-GM-THRID " + message.xGmThrid;
+    } else {
+        return "BAD can't fetch X-GM-THRID";
+    }
+  },
+  _FETCH_X_GM_LABELS : function (message) {
+    if (message.xGmLabels) {
+        return "X-GM-LABELS " + message.xGmLabels;
+    } else {
+        return "BAD can't fetch X-GM-LABELS";
+    }
   }
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //                            IMAP4 RFC extensions                            //
 ////////////////////////////////////////////////////////////////////////////////
 // Since there are so many extensions to IMAP, and since these extensions are //
 // not strictly hierarchial (e.g., an RFC 2342-compliant server can also be   //