From 30722d0b82917b228a2335ced593ada40bc7f531 Mon Sep 17 00:00:00 2001 From: Atul Varma Date: Mon, 16 Jun 2008 16:42:32 -0700 Subject: [PATCH] Factored out fake-timer code into a separate class and moved it to head.js. --- services/sync/tests/unit/head_first.js | 27 ++++++++++++++++++++++++++ services/sync/tests/unit/test_async.js | 23 +++++----------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/services/sync/tests/unit/head_first.js b/services/sync/tests/unit/head_first.js index 5c7ed021c3d5..7af151bfd8f4 100644 --- a/services/sync/tests/unit/head_first.js +++ b/services/sync/tests/unit/head_first.js @@ -41,6 +41,33 @@ function loadInSandbox(aUri) { return sandbox; } +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"); diff --git a/services/sync/tests/unit/test_async.js b/services/sync/tests/unit/test_async.js index ed742d25abeb..746033ac4754 100644 --- a/services/sync/tests/unit/test_async.js +++ b/services/sync/tests/unit/test_async.js @@ -2,17 +2,10 @@ 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() { @@ -31,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++; @@ -45,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()); }