From c7c58f83438acd959afa0d9d33102102088dd5ec Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Mon, 9 Mar 2015 14:25:49 -0400 Subject: [PATCH] Add support function to reset the DB and a test that it works. --- chrome/content/zotero/xpcom/zotero.js | 1 + components/zotero-service.js | 15 +++++++++++++++ test/content/support.js | 11 +++++++++++ test/tests/index.js | 5 +++-- test/tests/support.js | 11 +++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/tests/support.js diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 4b8f1a3848..431fdd5aa1 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -54,6 +54,7 @@ Components.utils.import("resource://gre/modules/Services.jsm"); this.join = join; this.randomString = randomString; this.moveToUnique = moveToUnique; + this.reinit = reinit; // defined in zotero-service.js // Public properties this.initialized = false; diff --git a/components/zotero-service.js b/components/zotero-service.js index e80bb0e7e3..736a35db1a 100644 --- a/components/zotero-service.js +++ b/components/zotero-service.js @@ -177,6 +177,21 @@ ZoteroContext.prototype = { } return zContext; + }, + + /** + * Shuts down Zotero, calls a callback (that may return a promise), + * then reinitializes Zotero. Returns a promise that is resolved + * when this process completes. + */ + "reinit":function(cb, isConnector) { + Services.obs.notifyObservers(zContext.Zotero, "zotero-before-reload", isConnector ? "connector" : "full"); + return zContext.Zotero.shutdown().then(function() { + return cb ? cb() : false; + }).finally(function() { + makeZoteroContext(isConnector); + zContext.Zotero.init(zInitOptions); + }); } }; diff --git a/test/content/support.js b/test/content/support.js index fabf535f89..8e6b801cad 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -164,4 +164,15 @@ function getTestDataDirectory() { resURI = Services.io.newURI("resource://zotero-unit-tests/data", null, null); return Services.io.newURI(resource.resolveURI(resURI), null, null). QueryInterface(Components.interfaces.nsIFileURL).file; +} + +/** + * Resets the Zotero DB and restarts Zotero. Returns a promise resolved + * when this finishes. + */ +function resetDB() { + var db = Zotero.getZoteroDatabase(); + return Zotero.reinit(function() { + db.remove(false); + }); } \ No newline at end of file diff --git a/test/tests/index.js b/test/tests/index.js index 8af1a146db..9ea8d06ba9 100644 --- a/test/tests/index.js +++ b/test/tests/index.js @@ -1,5 +1,6 @@ var TESTS = { - "recognizePDF":["recognizePDF.js"], + "support":["support.js"], + "utilities":["utilities.js"], "lookup":["lookup.js"], - "utilities":["utilities.js"] + "recognizePDF":["recognizePDF.js"] }; diff --git a/test/tests/support.js b/test/tests/support.js new file mode 100644 index 0000000000..f8c91f6f3c --- /dev/null +++ b/test/tests/support.js @@ -0,0 +1,11 @@ +describe("Support Functions for Unit Testing", function() { + describe("resetDB", function() { + it("should restore the DB to factory settings", function() { + var quickstart = Zotero.Items.erase(1); + assert.equal(Zotero.Items.get(1), false); + return resetDB().then(function() { + assert.equal(Zotero.Items.get(1).getField("url"), "http://zotero.org/support/quick_start_guide"); + }); + }); + }); +});