Bug 1011597 - Use promise-based listener in test_imapPump, r=jcranmer
authorKent James <kent@caspia.com>
Mon, 19 May 2014 13:21:36 -0700
changeset 16209 8200f186be4653d8762fd056f548c1bf6b8110e1
parent 16208 3cb370e6812d4a42337f53d55be8f10ec82b6930
child 16210 83d9d43c13c5bc908af30be54a8c6ce249e5b61a
push id10135
push userkent@caspia.com
push dateMon, 19 May 2014 20:22:28 +0000
treeherdercomm-central@8200f186be46 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjcranmer
bugs1011597
Bug 1011597 - Use promise-based listener in test_imapPump, r=jcranmer
mailnews/Makefile.in
mailnews/base/test/unit/test_imapPump.js
mailnews/test/resources/PromiseTestUtils.jsm
--- a/mailnews/Makefile.in
+++ b/mailnews/Makefile.in
@@ -16,16 +16,17 @@ ifdef ENABLE_TESTS
 RESDIR = $(srcdir)/test/resources
 FAKESERVER := $(srcdir)/test/fakeserver
 TESTING_JS_MODULE_DIR = mailnews
 TESTING_JS_MODULES += \
 	$(RESDIR)/IMAPpump.js \
 	$(RESDIR)/localAccountUtils.js \
 	$(RESDIR)/mailTestUtils.js \
 	$(RESDIR)/MockFactory.js \
+	$(RESDIR)/PromiseTestUtils.jsm \
 	$(FAKESERVER)/auth.js \
 	$(FAKESERVER)/imapd.js \
 	$(FAKESERVER)/maild.js \
 	$(FAKESERVER)/nntpd.js \
 	$(FAKESERVER)/pop3d.js \
 	$(FAKESERVER)/smtpd.js \
 	$(NULL)
 endif
--- a/mailnews/base/test/unit/test_imapPump.js
+++ b/mailnews/base/test/unit/test_imapPump.js
@@ -3,66 +3,57 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /**
  * Simple demonstration of the imap pump test method.
  */
 
 // async support 
 load("../../../resources/logHelper.js");
-load("../../../resources/asyncTestUtils.js");
 load("../../../resources/alertTestUtils.js");
+Components.utils.import("resource://testing-common/mailnews/PromiseTestUtils.jsm");
 
 // IMAP pump
 Components.utils.import("resource://testing-common/mailnews/IMAPpump.js");
 Components.utils.import("resource://testing-common/mailnews/imapd.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
 
 setupIMAPPump();
 
 // Definition of tests
-var tests = [
-  loadImapMessage,
-  endTest
-]
 
 // load and update a message in the imap fake server
-function loadImapMessage()
-{
+add_task(function* loadImapMessage() {
   IMAPPump.mailbox.addMessage(new imapMessage(specForFileName(gMessage),
                           IMAPPump.mailbox.uidnext++, []));
-  IMAPPump.inbox.updateFolderWithListener(gDummyMsgWindow, asyncUrlListener);
-  yield false;
+  let promiseUrlListener = new PromiseTestUtils.PromiseUrlListener();
+  IMAPPump.inbox.updateFolderWithListener(gDummyMsgWindow, promiseUrlListener);
+  yield promiseUrlListener.promise;
+
   do_check_eq(1, IMAPPump.inbox.getTotalMessages(false));
   let msgHdr = mailTestUtils.firstMsgHdr(IMAPPump.inbox);
   do_check_true(msgHdr instanceof Ci.nsIMsgDBHdr);
-  yield true;
-}
+});
 
 // Cleanup at end
-function endTest()
-{
-  teardownIMAPPump();
-}
+add_task(teardownIMAPPump);
 
-function run_test()
-{
+function run_test() {
   Services.prefs.setBoolPref("mail.server.server1.autosync_offline_stores", false);
-  async_run_tests(tests);
+  run_next_test();
 }
 
 /*
  * helper functions
  */
 
 // given a test file, return the file uri spec
-function specForFileName(aFileName)
-{
+function specForFileName(aFileName) {
   let file = do_get_file("../../../data/" + aFileName);
   let msgfileuri = Services.io.newFileURI(file).QueryInterface(Ci.nsIFileURL);
   return msgfileuri.spec;
 }
new file mode 100644
--- /dev/null
+++ b/mailnews/test/resources/PromiseTestUtils.jsm
@@ -0,0 +1,51 @@
+/* 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/. */
+
+/**
+ * This file provides utilities useful in using Promises and Task.jsm
+ * with mailnews tests.
+ */
+
+const EXPORTED_SYMBOLS = ['PromiseTestUtils'];
+
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var Cr = Components.results;
+var CC = Components.Constructor;
+var Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Promise.jsm");
+
+/**
+ * Url listener that can wrap another listener and trigger a callback.
+ *
+ * @param [aWrapped] The nsIUrlListener to pass all notifications through to.
+ *     This gets called prior to the callback (or async resumption).
+ */
+
+var PromiseTestUtils = {};
+
+PromiseTestUtils.PromiseUrlListener = function(aWrapped) {
+  this.wrapped = aWrapped ? aWrapped.QueryInterface(Ci.nsIUrlListener) : null;
+  this._deferred = Promise.defer();
+};
+
+PromiseTestUtils.PromiseUrlListener.prototype = {
+  QueryInterface:   XPCOMUtils.generateQI([Ci.nsIUrlListener]),
+
+  OnStartRunningUrl: function(aUrl) {
+    if (this.wrapped)
+      this.wrapped.OnStartRunningUrl(aUrl);
+  },
+  OnStopRunningUrl: function(aUrl, aExitCode) {
+    if (this.wrapped)
+      this.wrapped.OnStopRunningUrl(aUrl, aExitCode);
+    if (aExitCode == Cr.NS_OK)
+      this._deferred.resolve();
+    else
+      this._deferred.reject(aExitCode);
+  },
+  get promise() { return this._deferred.promise; },
+};