Refactored some things in the test framework to make tests easier to write, and to make certain things possible to write.
authorAtul Varma <varmaa@toolness.com>
Thu, 26 Jun 2008 15:01:34 -0700
changeset 44770 fd39885d34e6f388865412eaba7fb62c071e3a1b
parent 44761 19a88c70f9e2aa2584b8db629f9a188e94379a6a
child 44771 ecc6163e17aaeec265787783cbd99df3058cba29
push idunknown
push userunknown
push dateunknown
Refactored some things in the test framework to make tests easier to write, and to make certain things possible to write.
services/sync/tests/unit/head_first.js
services/sync/tests/unit/test_bookmark_syncing.js
services/sync/tests/unit/test_bookmark_syncing.log.expected
services/sync/tests/unit/test_password_syncing.js
--- a/services/sync/tests/unit/head_first.js
+++ b/services/sync/tests/unit/head_first.js
@@ -269,17 +269,17 @@ function FakeFilesystemService(contents)
 function FakeGUIDService() {
   let latestGUID = 0;
 
   Utils.makeGUID = function fake_makeGUID() {
     return "fake-guid-" + latestGUID++;
   };
 }
 
-function SyncTestingInfrastructure(engineCtor) {
+function SyncTestingInfrastructure(engineFactory) {
   let __fakePasswords = {
     'Mozilla Services Password': {foo: "bar"},
     'Mozilla Services Encryption Passphrase': {foo: "passphrase"}
   };
 
   let __fakePrefs = {
     "encryption" : "none",
     "log.logger.service.crypto" : "Debug",
@@ -298,42 +298,48 @@ function SyncTestingInfrastructure(engin
   this.fakePrefService = new FakePrefService(__fakePrefs);
   this.fakeDAVService = new FakeDAVService({});
   this.fakeTimerService = new FakeTimerService();
   this.logStats = initTestLogging();
   this.fakeFilesystem = new FakeFilesystemService({});
   this.fakeGUIDService = new FakeGUIDService();
 
   this._logger = getTestLogger();
-  this._Engine = engineCtor;
+  this._engineFactory = engineFactory;
   this._clientStates = [];
 
   this.saveClientState = function pushClientState(label) {
     let state = Utils.deepCopy(this.fakeFilesystem.fakeContents);
-    this._clientStates[label] = state;
+    let currContents = this.fakeFilesystem.fakeContents;
+    this.fakeFilesystem.fakeContents = [];
+    let engine = this._engineFactory();
+    let snapshot = Utils.deepCopy(engine._store.wrap());
+    this._clientStates[label] = {state: state, snapshot: snapshot};
+    this.fakeFilesystem.fakeContents = currContents;
   };
 
   this.restoreClientState = function restoreClientState(label) {
-    let state = this._clientStates[label];
+    let state = this._clientStates[label].state;
+    let snapshot = this._clientStates[label].snapshot;
 
     function _restoreState() {
       let self = yield;
 
-      this.fakeFilesystem.fakeContents = Utils.deepCopy(state);
-      let engine = new this._Engine();
+      this.fakeFilesystem.fakeContents = [];
+      let engine = this._engineFactory();
       engine._store.wipe();
       let originalSnapshot = Utils.deepCopy(engine._store.wrap());
-      engine._snapshot.load();
-      let snapshot = engine._snapshot.data;
 
       engine._core.detectUpdates(self.cb, originalSnapshot, snapshot);
       let commands = yield;
 
       engine._store.applyCommands.async(engine._store, self.cb, commands);
       yield;
+
+      this.fakeFilesystem.fakeContents = Utils.deepCopy(state);
     }
 
     let self = this;
 
     function restoreState(cb) {
       _restoreState.async(self, cb);
     }
 
@@ -344,16 +350,27 @@ function SyncTestingInfrastructure(engin
   this.__makeCallback = function __makeCallback() {
     this.__callbackCalled = false;
     let self = this;
     return function callback() {
       self.__callbackCalled = true;
     };
   };
 
+  this.doSync = function doSync(name) {
+    let self = this;
+
+    function freshEngineSync(cb) {
+      let engine = self._engineFactory();
+      engine.sync(cb);
+    }
+
+    this.runAsyncFunc(name, freshEngineSync);
+  };
+
   this.runAsyncFunc = function runAsyncFunc(name, func) {
     let logger = this._logger;
 
     logger.info("-----------------------------------------");
     logger.info("Step '" + name + "' starting.");
     logger.info("-----------------------------------------");
     func(this.__makeCallback());
     while (this.fakeTimerService.processCallback()) {}
@@ -362,12 +379,12 @@ function SyncTestingInfrastructure(engin
       logger.warn("Outstanding generator exists: " + name);
     do_check_eq(this.logStats.errorsLogged, 0);
     do_check_eq(Async.outstandingGenerators.length, 0);
     logger.info("Step '" + name + "' succeeded.");
   };
 
   this.resetClientState = function resetClientState() {
     this.fakeFilesystem.fakeContents = {};
-    let engine = new this._Engine();
+    let engine = this._engineFactory();
     engine._store.wipe();
   };
 }
--- a/services/sync/tests/unit/test_bookmark_syncing.js
+++ b/services/sync/tests/unit/test_bookmark_syncing.js
@@ -9,79 +9,77 @@ load("bookmark_setup.js");
 // ----------------------------------------
 // Test Logic
 // ----------------------------------------
 
 function FakeMicrosummaryService() {
   return {hasMicrosummary: function() { return false; }};
 }
 
+function makeBookmarksEngine() {
+  let engine = new BookmarksEngine();
+  engine._store.__ms = new FakeMicrosummaryService();
+  return engine;
+}
+
 function run_test() {
   // -----
   // Setup
   // -----
 
-  var syncTesting = new SyncTestingInfrastructure(BookmarksEngine);
-
-  function freshEngineSync(cb) {
-    let engine = new BookmarksEngine();
-    engine._store.__ms = new FakeMicrosummaryService();
-    engine.sync(cb);
-  };
+  var syncTesting = new SyncTestingInfrastructure(makeBookmarksEngine);
 
   let bms = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
     getService(Ci.nsINavBookmarksService);
 
   cleanUp();
 
   // -----------
   // Test Proper
   // -----------
 
   let boogleBm = bms.insertBookmark(bms.bookmarksMenuFolder,
                                     uri("http://www.boogle.com"),
                                     -1,
                                     "Boogle");
   bms.setItemGUID(boogleBm, "boogle-bookmark-guid");
 
-  syncTesting.runAsyncFunc("initial sync w/ one bookmark", freshEngineSync);
+  syncTesting.doSync("initial sync w/ one bookmark");
 
-  syncTesting.runAsyncFunc("trivial re-sync", freshEngineSync);
+  syncTesting.doSync("trivial re-sync");
 
   let yoogleBm = bms.insertBookmark(bms.bookmarksMenuFolder,
                                     uri("http://www.yoogle.com"),
                                     -1,
                                     "Yoogle");
   bms.setItemGUID(yoogleBm, "yoogle-bookmark-guid");
 
-  syncTesting.runAsyncFunc("add bookmark and re-sync", freshEngineSync);
+  syncTesting.doSync("add bookmark and re-sync");
 
   bms.moveItem(yoogleBm,
                bms.bookmarksMenuFolder,
                0);
 
-  syncTesting.runAsyncFunc("swap bookmark order and re-sync",
-                           freshEngineSync);
+  syncTesting.doSync("swap bookmark order and re-sync");
 
   syncTesting.saveClientState("first computer");
 
   syncTesting.resetClientState();
 
-  syncTesting.runAsyncFunc("re-sync on second computer", freshEngineSync);
+  syncTesting.doSync("re-sync on second computer");
 
   let zoogleBm = bms.insertBookmark(bms.bookmarksMenuFolder,
                                     uri("http://www.zoogle.com"),
                                     -1,
                                     "Zoogle");
   bms.setItemGUID(zoogleBm, "zoogle-bookmark-guid");
 
-  syncTesting.runAsyncFunc("add bookmark on second computer and resync",
-                           freshEngineSync);
+  syncTesting.doSync("add bookmark on second computer and resync");
 
   syncTesting.restoreClientState("first computer");
-  syncTesting.runAsyncFunc("re-sync on first computer", freshEngineSync);
+  syncTesting.doSync("re-sync on first computer");
 
   // --------
   // Teardown
   // --------
 
   cleanUp();
 }
--- a/services/sync/tests/unit/test_bookmark_syncing.log.expected
+++ b/services/sync/tests/unit/test_bookmark_syncing.log.expected
@@ -213,22 +213,16 @@ Service.BmkEngine	INFO	Successfully upda
 Service.SnapStore	INFO	Saving snapshot to disk
 Testing	INFO	Opening 'weave/snapshots/bookmarks.json' for writing.
 Testing	INFO	Writing data to local file 'weave/snapshots/bookmarks.json': {"version":3,"GUID":"fake-guid-0","snapshot":{"yoogle-bookmark-guid":{"parentGUID":"menu","index":0,"type":"bookmark","title":"Yoogle","URI":"http://www.yoogle.com/","tags":[],"keyword":null},"boogle-bookmark-guid":{"parentGUID":"menu","index":1,"type":"bookmark","title":"Boogle","URI":"http://www.boogle.com/","tags":[],"keyword":null},"zoogle-bookmark-guid":{"parentGUID":"menu","index":2,"type":"bookmark","title":"Zoogle","URI":"http://www.zoogle.com/","tags":[],"keyword":null},"menu":{"type":"folder"},"toolbar":{"type":"folder"},"unfiled":{"type":"folder"}}}
 Service.BmkEngine	INFO	Sync complete
 Testing	INFO	Step 'add bookmark on second computer and resync' succeeded.
 Testing	INFO	-----------------------------------------
 Testing	INFO	Step 'restore client state of first computer' starting.
 Testing	INFO	-----------------------------------------
-Testing	INFO	Opening 'weave/snapshots/bookmarks.json' for reading.
-Testing	INFO	Reading from stream.
-Service.SnapStore	INFO	Read saved snapshot from disk
-Testing	INFO	Opening 'weave/snapshots/bookmarks.json' for reading.
-Testing	INFO	Reading from stream.
-Service.SnapStore	INFO	Read saved snapshot from disk
 Service.BStore	TRACE	Processing command: {"action":"create","GUID":"yoogle-bookmark-guid","depth":1,"parents":["menu"],"data":{"parentGUID":"menu","index":0,"type":"bookmark","title":"Yoogle","URI":"http://www.yoogle.com/","tags":[],"keyword":null}}
 Service.BStore	DEBUG	 -> creating bookmark "Yoogle"
 Service.BStore	TRACE	Processing command: {"action":"create","GUID":"boogle-bookmark-guid","depth":1,"parents":["menu"],"data":{"parentGUID":"menu","index":1,"type":"bookmark","title":"Boogle","URI":"http://www.boogle.com/","tags":[],"keyword":null}}
 Service.BStore	DEBUG	 -> creating bookmark "Boogle"
 Testing	INFO	Step 'restore client state of first computer' succeeded.
 Testing	INFO	-----------------------------------------
 Testing	INFO	Step 're-sync on first computer' starting.
 Testing	INFO	-----------------------------------------
--- a/services/sync/tests/unit/test_password_syncing.js
+++ b/services/sync/tests/unit/test_password_syncing.js
@@ -2,40 +2,36 @@ Cu.import("resource://weave/engines/pass
 
 load("fake_login_manager.js");
 
 // ----------------------------------------
 // Test Logic
 // ----------------------------------------
 
 function run_test() {
-  var syncTesting = new SyncTestingInfrastructure(PasswordEngine);
+  function passwdFactory() { return new PasswordEngine(); }
+  var syncTesting = new SyncTestingInfrastructure(passwdFactory);
   var fakeLoginManager = new FakeLoginManager(fakeSampleLogins);
 
-  function freshEngineSync(cb) {
-    let engine = new PasswordEngine();
-    engine.sync(cb);
-  };
+  syncTesting.doSync("initial sync");
 
-  syncTesting.runAsyncFunc("initial sync", freshEngineSync);
-
-  syncTesting.runAsyncFunc("trivial re-sync", freshEngineSync);
+  syncTesting.doSync("trivial re-sync");
 
   fakeLoginManager.fakeLogins.push(
     {hostname: "www.yoogle.com",
      formSubmitURL: "http://www.yoogle.com/search",
      httpRealm: "",
      username: "",
      password: "",
      usernameField: "test_person2",
      passwordField: "test_password2"}
   );
 
-  syncTesting.runAsyncFunc("add user and re-sync", freshEngineSync);
+  syncTesting.doSync("add user and re-sync");
 
   fakeLoginManager.fakeLogins.pop();
 
-  syncTesting.runAsyncFunc("remove user and re-sync", freshEngineSync);
+  syncTesting.doSync("remove user and re-sync");
 
   syncTesting.resetClientState();
 
-  syncTesting.runAsyncFunc("resync on second computer", freshEngineSync);
+  syncTesting.doSync("resync on second computer");
 }