diff --git a/services/sync/tests/unit/head_first.js b/services/sync/tests/unit/head_first.js index ac1d1543c1d3..7af151bfd8f4 100644 --- a/services/sync/tests/unit/head_first.js +++ b/services/sync/tests/unit/head_first.js @@ -3,6 +3,7 @@ version(180); const Cc = Components.classes; const Ci = Components.interfaces; const Cr = Components.results; +const Cu = Components.utils; let ds = Cc["@mozilla.org/file/directory_service;1"] .getService(Ci.nsIProperties); @@ -40,37 +41,69 @@ function loadInSandbox(aUri) { return sandbox; } -function makeAsyncTestRunner(generator) { - const Cu = Components.utils; +function FakeTimerService() { + Cu.import("resource://weave/util.js"); + this.callbackQueue = []; + + var self = this; + + this.__proto__ = { + makeTimerForCall: function FTS_makeTimerForCall(cb) { + // Just add the callback to our queue and we'll call it later, so + // as to simulate a real nsITimer. + self.callbackQueue.push(cb); + return "fake nsITimer"; + }, + processCallback: function FTS_processCallbacks() { + var cb = self.callbackQueue.pop(); + if (cb) { + cb(); + return true; + } + return false; + } + }; + + Utils.makeTimerForCall = self.makeTimerForCall; +}; + +function initTestLogging() { Cu.import("resource://weave/log4moz.js"); - Cu.import("resource://weave/async.js"); - var errorsLogged = 0; - - function _TestFormatter() {} - _TestFormatter.prototype = { + function LogStats() { + this.errorsLogged = 0; + } + LogStats.prototype = { format: function BF_format(message) { if (message.level == Log4Moz.Level.Error) - errorsLogged += 1; + this.errorsLogged += 1; return message.loggerName + "\t" + message.levelDesc + "\t" + message.message + "\n"; } }; - _TestFormatter.prototype.__proto__ = new Log4Moz.Formatter(); + LogStats.prototype.__proto__ = new Log4Moz.Formatter(); var log = Log4Moz.Service.rootLogger; - var formatter = new _TestFormatter(); - var appender = new Log4Moz.DumpAppender(formatter); + var logStats = new LogStats(); + var appender = new Log4Moz.DumpAppender(logStats); log.level = Log4Moz.Level.Debug; appender.level = Log4Moz.Level.Debug; log.addAppender(appender); + return logStats; +} + +function makeAsyncTestRunner(generator) { + Cu.import("resource://weave/async.js"); + + var logStats = initTestLogging(); + function run_test() { do_test_pending(); let onComplete = function() { - if (errorsLogged) + if (logStats.errorsLogged) do_throw("Errors were logged."); else do_test_finished(); diff --git a/services/sync/tests/unit/test_async.js b/services/sync/tests/unit/test_async.js index 36795e9948a4..746033ac4754 100644 --- a/services/sync/tests/unit/test_async.js +++ b/services/sync/tests/unit/test_async.js @@ -1,20 +1,11 @@ -const Cu = Components.utils; - Cu.import("resource://weave/util.js"); Cu.import("resource://weave/async.js"); function run_test() { - var callbackQueue = []; + var fts = new FakeTimerService(); Function.prototype.async = Async.sugar; - Utils.makeTimerForCall = function fake_makeTimerForCall(cb) { - // Just add the callback to our queue and we'll call it later, so - // as to simulate a real nsITimer. - callbackQueue.push(cb); - return "fake nsITimer"; - }; - var onCompleteCalled = false; function onComplete() { @@ -33,9 +24,7 @@ function run_test() { // Ensure that 'this' is set properly. do_check_eq(this.sampleProperty, true); - // Simulate the calling of an asynchronous function that will call - // our callback. - callbackQueue.push(self.cb); + fts.makeTimerForCall(self.cb); yield; timesYielded++; @@ -47,15 +36,11 @@ function run_test() { do_check_eq(timesYielded, 1); - let func = callbackQueue.pop(); - do_check_eq(typeof func, "function"); - func(); + do_check_true(fts.processCallback()); do_check_eq(timesYielded, 2); - func = callbackQueue.pop(); - do_check_eq(typeof func, "function"); - func(); + do_check_true(fts.processCallback()); - do_check_eq(callbackQueue.length, 0); + do_check_false(fts.processCallback()); } diff --git a/services/sync/tests/unit/test_async_exceptions.js b/services/sync/tests/unit/test_async_exceptions.js index f1fe78eadb28..b15a2e8dd666 100644 --- a/services/sync/tests/unit/test_async_exceptions.js +++ b/services/sync/tests/unit/test_async_exceptions.js @@ -1,19 +1,8 @@ -const Cu = Components.utils; - Cu.import("resource://weave/util.js"); Cu.import("resource://weave/async.js"); Function.prototype.async = Async.sugar; -var callbackQueue = []; - -Utils.makeTimerForCall = function fake_makeTimerForCall(cb) { - // Just add the callback to our queue and we'll call it later, so - // as to simulate a real nsITimer. - callbackQueue.push(cb); - return "fake nsITimer"; -}; - function thirdGen() { let self = yield; @@ -54,12 +43,8 @@ function runTestGenerator() { } function run_test() { + var fts = new FakeTimerService(); runTestGenerator.async({}); - let i = 1; - while (callbackQueue.length > 0) { - let cb = callbackQueue.pop(); - cb(); - i += 1; - } - do_check_eq(i, 5); + for (var i = 0; fts.processCallback(); i++) {} + do_check_eq(i, 4); } diff --git a/services/sync/tests/unit/test_async_missing_yield.js b/services/sync/tests/unit/test_async_missing_yield.js index 14347ae518b9..b3ae785627fe 100644 --- a/services/sync/tests/unit/test_async_missing_yield.js +++ b/services/sync/tests/unit/test_async_missing_yield.js @@ -1,23 +1,12 @@ -const Cu = Components.utils; - Cu.import("resource://weave/util.js"); Cu.import("resource://weave/async.js"); Function.prototype.async = Async.sugar; -var callbackQueue = []; - -Utils.makeTimerForCall = function fake_makeTimerForCall(cb) { - // Just add the callback to our queue and we'll call it later, so - // as to simulate a real nsITimer. - callbackQueue.push(cb); - return "fake nsITimer"; -}; - function secondGen() { let self = yield; - callbackQueue.push(self.cb); + Utils.makeTimerForCall(self.cb); self.done(); } @@ -32,38 +21,13 @@ function runTestGenerator() { } function run_test() { - const Cu = Components.utils; - Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/async.js"); - var errorsLogged = 0; - - function _TestFormatter() {} - _TestFormatter.prototype = { - format: function BF_format(message) { - if (message.level == Log4Moz.Level.Error) - errorsLogged += 1; - return message.loggerName + "\t" + message.levelDesc + "\t" + - message.message + "\n"; - } - }; - _TestFormatter.prototype.__proto__ = new Log4Moz.Formatter(); - - var log = Log4Moz.Service.rootLogger; - var formatter = new _TestFormatter(); - var appender = new Log4Moz.DumpAppender(formatter); - log.level = Log4Moz.Level.Trace; - appender.level = Log4Moz.Level.Trace; - log.addAppender(appender); + var fts = new FakeTimerService(); + var logStats = initTestLogging(); runTestGenerator.async({}); - let i = 1; - while (callbackQueue.length > 0) { - let cb = callbackQueue.pop(); - cb(); - i += 1; - } - - do_check_eq(errorsLogged, 3); + while (fts.processCallback()) {}; + do_check_eq(logStats.errorsLogged, 3); } diff --git a/services/sync/tests/unit/test_sharing.js b/services/sync/tests/unit/test_sharing.js index b962404e9d37..e18f6de3be95 100644 --- a/services/sync/tests/unit/test_sharing.js +++ b/services/sync/tests/unit/test_sharing.js @@ -1,5 +1,3 @@ -const Cu = Components.utils; - Cu.import("resource://weave/sharing.js"); Cu.import("resource://weave/util.js"); diff --git a/services/sync/tests/unit/test_xmpp.js b/services/sync/tests/unit/test_xmpp.js index 60396b2e054c..9c76eed9308a 100644 --- a/services/sync/tests/unit/test_xmpp.js +++ b/services/sync/tests/unit/test_xmpp.js @@ -1,5 +1,3 @@ -var Cu = Components.utils; - Cu.import( "resource://weave/xmpp/xmppClient.js" ); function LOG(aMsg) {