commit c7c58f83438acd959afa0d9d33102102088dd5ec
parent b6673511f748be5e1d8b7a826a97ed115066df5e
Author: Simon Kornblith <simon@simonster.com>
Date: Mon, 9 Mar 2015 14:25:49 -0400
Add support function to reset the DB and a test that it works.
Diffstat:
5 files changed, 41 insertions(+), 2 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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");
+ });
+ });
+ });
+});