fix archiving of messages in multiple years, and add mozmill test for archiving, r=sid0, bug 531829
authorDavid Bienvenu <bienvenu@nventure.com>
Mon, 21 Dec 2009 10:47:08 -0800
changeset 4575 55cdbbf52d48a8df05fa80a83cb52fd277dc5c1a
parent 4574 eaa8dc210e53c868e33dff71aeedea1e779a3294
child 4576 a02bc393512e1af9322180f2b151b01b4a40d96e
push idunknown
push userunknown
push dateunknown
reviewerssid0, bug
bugs531829
fix archiving of messages in multiple years, and add mozmill test for archiving, r=sid0, bug 531829
mail/base/content/mailWindowOverlay.js
mail/test/mozmill/folder-display/test-message-commands.js
mail/test/mozmill/shared-modules/test-folder-display-helpers.js
--- a/mail/base/content/mailWindowOverlay.js
+++ b/mail/base/content/mailWindowOverlay.js
@@ -1333,18 +1333,21 @@ BatchMessageMover.prototype = {
         if (!identity)
           identity = getIdentityForHeader(msgHdr);
         archiveFolderUri = identity.archiveFolder;
       }
       let archiveFolder = GetMsgFolderFromUri(archiveFolderUri, false);
       let granularity = archiveFolder.server.archiveGranularity;
 
       let copyBatchKey = msgHdr.folder.URI + '\000';
-      if (granularity >=  Components.interfaces.nsIMsgIncomingServer
-                                    .perMonthArchiveFolders)
+      if (granularity >= Components.interfaces.nsIMsgIncomingServer
+                                   .perYearArchiveFolders)
+        copyBatchKey += msgYear;
+      if (granularity >= Components.interfaces.nsIMsgIncomingServer
+                                   .perMonthArchiveFolders)
         copyBatchKey += monthFolderName;
 
        if (! (copyBatchKey in this._batches)) {
         this._batches[copyBatchKey] = [msgHdr.folder, archiveFolderUri,
                                        granularity, msgYear, monthFolderName];
       }
       this._batches[copyBatchKey].push(msgHdr);
     }
new file mode 100644
--- /dev/null
+++ b/mail/test/mozmill/folder-display/test-message-commands.js
@@ -0,0 +1,143 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Thunderbird Mail Client.
+ *
+ * The Initial Developer of the Original Code is
+ * Mozilla Messaging, Inc.
+ * Portions created by the Initial Developer are Copyright (C) 2009
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   David Bienvenu <bienvenu@nventure.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+/**
+ * This tests various commands on messages. This is primarily for commands
+ * that can't be tested with xpcshell tests because they're handling in the
+ * front end - which is why Archive is the only command currently tested.
+ */
+var MODULE_NAME = 'test-message-commands';
+
+var RELATIVE_ROOT = '../shared-modules';
+var MODULE_REQUIRES = ['folder-display-helpers'];
+
+Components.utils.import("resource://app/modules/MailUtils.js");
+
+var archiveSrcFolder = null;
+var archiveURI;
+
+var setupModule = function(module) {
+  let fdh = collector.getModule('folder-display-helpers');
+  fdh.installInto(module);
+
+  archiveSrcFolder = create_folder("ArchiveSrc");
+  // Create messages from 13 different months, which will mean 2 different
+  // years as well.
+  make_new_sets_in_folder(archiveSrcFolder, [{count: 13, age_incr: {weeks: 5}}]);
+};
+
+function test_yearly_archive() {
+  be_in_folder(archiveSrcFolder);
+  make_display_unthreaded();
+  mc.folderDisplay.view.sort(Ci.nsMsgViewSortType.byDate, Ci.nsMsgViewSortOrder.ascending);
+
+  // We need to get all the info about the messages before we do the archive,
+  // because deleting the headers could make extracting values from them fail.
+  let firstMsgHdr = mc.folderDisplay.view.dbView.getMsgHdrAt(0);
+  let lastMsgHdr = mc.folderDisplay.view.dbView.getMsgHdrAt(12);
+  let firstMsgHdrMsgId = firstMsgHdr.messageId;
+  let lastMsgHdrMsgId = lastMsgHdr.messageId;
+  let firstMsgDate = new Date(firstMsgHdr.date / 1000);
+  let firstMsgYear = firstMsgDate.getFullYear().toString();
+  let lastMsgDate = new Date(lastMsgHdr.date / 1000);
+  let lastMsgYear = lastMsgDate.getFullYear().toString();
+
+  select_click_row(0);
+  select_control_click_row(12);
+
+  // Press the archive key. The results should go into two separate years.
+  archive_selected_messages();
+
+  // Figure out where the messages should have gone.
+  let archiveRoot = "mailbox://nobody@Local%20Folders/Archives";
+  let firstArchiveUri = archiveRoot + "/" + firstMsgYear;
+  let lastArchiveUri = archiveRoot + "/" + lastMsgYear;
+  let firstArchiveFolder =  MailUtils.getFolderForURI(firstArchiveUri);
+  let lastArchiveFolder = MailUtils.getFolderForURI(lastArchiveUri);
+  be_in_folder(firstArchiveFolder);
+  assert_true(mc.dbView.getMsgHdrAt(0).messageId == firstMsgHdrMsgId,
+              "Message should have been archived to Local Folders/Archives/" + firstMsgYear + ", but it isn't present there");
+  be_in_folder(lastArchiveFolder);
+
+  assert_true(mc.dbView.getMsgHdrAt(0).messageId == lastMsgHdrMsgId,
+              "Message should have been archived to Local Folders/Archives/" + lastMsgYear + ", but it isn't present there");
+}
+
+function test_monthly_archive() {
+  be_in_folder(archiveSrcFolder);
+  let server = mc.folderDisplay.view.dbView.getMsgHdrAt(0).folder.server;
+  server.archiveGranularity = Ci.nsIMsgIncomingServer.perMonthArchiveFolders;
+  select_click_row(0);
+  select_control_click_row(1);
+
+  let firstMsgHdr = mc.folderDisplay.view.dbView.getMsgHdrAt(0);
+  let lastMsgHdr = mc.folderDisplay.view.dbView.getMsgHdrAt(1);
+  let firstMsgHdrMsgId = firstMsgHdr.messageId;
+  let lastMsgHdrMsgId = lastMsgHdr.messageId;
+  let firstMsgDate = new Date(firstMsgHdr.date / 1000);
+  let firstMsgYear = firstMsgDate.getFullYear().toString();
+  let firstMonthFolderName = firstMsgDate.toLocaleFormat("%Y-%m")
+  let lastMsgDate = new Date(lastMsgHdr.date / 1000);
+  let lastMsgYear = lastMsgDate.getFullYear().toString();
+  let lastMonthFolderName = lastMsgDate.toLocaleFormat("%Y-%m")
+
+  // Press the archive key. The results should go into two separate months.
+  archive_selected_messages();
+
+  // Figure out where the messages should have gone.
+  let archiveRoot = "mailbox://nobody@Local%20Folders/Archives";
+  let firstArchiveUri = archiveRoot + "/" + firstMsgYear +
+                        "/" + firstMonthFolderName;
+  let lastArchiveUri = archiveRoot + "/" + lastMsgYear +
+                        "/" + lastMonthFolderName;;
+  let firstArchiveFolder =  MailUtils.getFolderForURI(firstArchiveUri);
+  let lastArchiveFolder = MailUtils.getFolderForURI(lastArchiveUri);
+  be_in_folder(firstArchiveFolder);
+  assert_true(mc.dbView.getMsgHdrAt(0).messageId == firstMsgHdrMsgId,
+              "Message should have been archived to Local Folders/Archives/" + 
+              firstMsgYear + "/" + firstMonthFolderName + ", but it isn't present there");
+  be_in_folder(lastArchiveFolder);
+  assert_true(mc.dbView.getMsgHdrAt(0).messageId == lastMsgHdrMsgId,
+              "Message should have been archived to Local Folders/Archives/" + 
+              lastMsgYear + "/" + lastMonthFolderName + ", but it isn't present there");
+}
+
+// something less sucky than do_check_true
+function assert_true(aBeTrue, aWhy) {
+  if (!aBeTrue)
+    throw new Error(aWhy);
+}
--- a/mail/test/mozmill/shared-modules/test-folder-display-helpers.js
+++ b/mail/test/mozmill/shared-modules/test-folder-display-helpers.js
@@ -896,16 +896,43 @@ function press_delete(aController) {
   plan_to_wait_for_folder_events("DeleteOrMoveMsgCompleted",
                                  "DeleteOrMoveMsgFailed");
   aController.keypress(aController == mc ? mc.eThreadTree : null,
                        "VK_DELETE", {});
   wait_for_folder_events();
 }
 
 /**
+ * Archive the selected messages, and wait for it to complete.
+ *
+ * @param aController The controller in whose context to do this, defaults to
+ *     |mc| if omitted.
+ */
+function archive_selected_messages(aController) {
+  if (aController === undefined)
+    aController = mc;
+  let expectedCount = aController.dbView.rowCount - aController.dbView.numSelected;
+  // XXX We seem to sometimes have focus issues on trunk; this works around it.
+  focus_thread_tree();
+  aController.keypress(null, "a", {});
+
+  // Wait for the view rowCount to decrease by the number of selected messages.
+  let messagesDeletedFromView = function() {
+    return aController.dbView.rowCount == expectedCount;
+  };
+  controller.waitForEval('subject()',
+                         NORMAL_TIMEOUT,
+                         FAST_INTERVAL, messagesDeletedFromView);
+  // The above may return immediately, meaning the event queue might not get a
+  //  chance.  give it a chance now.
+  aController.sleep(0);
+
+}
+
+/**
  * Pretend we are pressing the Enter key, triggering opening selected messages.
  * Note that since we don't know where this is going to trigger a message load,
  * you're going to have to wait for message display completion yourself.
  *
  * @param aController The controller in whose context to do this, defaults to
  *     |mc| if omitted.
  */
 function press_enter(aController) {