commit 3f0f7af71ad6957a521c8665b1628ec2bec1cb7d
parent 50cd396918dc56e10a43e860e5f9d969e02e4446
Author: Simon Kornblith <simon@simonster.com>
Date: Sun, 8 Mar 2015 15:59:53 -0400
Add unit test for PDF recognition with DOI
Also some support code that was useful here and will probably be
useful for other tests. This is a pretty complicated thing to test, but
it seems to work.
Diffstat:
6 files changed, 150 insertions(+), 0 deletions(-)
diff --git a/test/content/runtests.html b/test/content/runtests.html
@@ -9,6 +9,7 @@
<script src="resource://zotero-unit/chai/chai.js"></script>
<script src="resource://zotero-unit/mocha/mocha.js"></script>
<script src="resource://zotero-unit-tests/index.js"></script>
+ <script src="support.js" version="application/javascript;version=1.8"></script>
<script src="runtests.js" version="application/javascript;version=1.8"></script>
</body>
</html>
\ No newline at end of file
diff --git a/test/content/runtests.js b/test/content/runtests.js
@@ -1,5 +1,6 @@
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/osfile.jsm");
+Components.utils.import("resource://zotero/q.js");
var ZoteroUnit = Components.classes["@mozilla.org/commandlinehandler/general-startup;1?type=zotero-unit"].
getService(Components.interfaces.nsISupports).
diff --git a/test/content/support.js b/test/content/support.js
@@ -0,0 +1,111 @@
+/**
+ * Open a window. Returns a promise for the window.
+ */
+function loadWindow(winurl, argument) {
+ var deferred = Q.defer();
+ var win = window.openDialog(winurl, "_blank", "chrome", argument);
+ var func = function() {
+ win.removeEventListener("load", func, false);
+ deferred.resolve(win);
+ };
+ win.addEventListener("load", func, false);
+ return deferred.promise;
+}
+
+/**
+ * Loads a Zotero pane in a new window. Returns the containing window.
+ */
+function loadZoteroPane() {
+ return loadWindow("chrome://browser/content/browser.xul").then(function(win) {
+ win.ZoteroOverlay.toggleDisplay(true);
+
+ // Hack to wait for pane load to finish. This is the same hack
+ // we use in ZoteroPane.js, so either it's not good enough
+ // there or it should be good enough here.
+ return Q.delay(52).then(function() {
+ return win;
+ });
+ });
+}
+
+/**
+ * Waits for a window with a specific URL to open. Returns a promise for the window.
+ */
+function waitForWindow(uri) {
+ var deferred = Q.defer();
+ Components.utils.import("resource://gre/modules/Services.jsm");
+ var loadobserver = function(ev) {
+ ev.originalTarget.removeEventListener("load", loadobserver, false);
+ if(ev.target.location == uri) {
+ Services.ww.unregisterNotification(winobserver);
+ deferred.resolve(ev.target.docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
+ getInterface(Components.interfaces.nsIDOMWindow));
+ }
+ };
+ var winobserver = {"observe":function(subject, topic, data) {
+ if(topic != "domwindowopened") return;
+ var win = subject.QueryInterface(Components.interfaces.nsIDOMWindow);
+ win.addEventListener("load", loadobserver, false);
+ }};
+ var enumerator = Services.ww.registerNotification(winobserver);
+ return deferred.promise;
+}
+
+/**
+ * Waits for a single item event. Returns a promise for the item ID(s).
+ */
+function waitForItemEvent(event) {
+ var deferred = Q.defer();
+ var notifierID = Zotero.Notifier.registerObserver({notify:function(ev, type, ids, extraData) {
+ if(ev == event) {
+ Zotero.Notifier.unregisterObserver(notifierID);
+ deferred.resolve(ids);
+ }
+ }}, ["item"]);
+ return deferred.promise;
+}
+
+/**
+ * Ensures that the PDF tools are installed, or installs them if not. Returns a promise.
+ */
+function installPDFTools() {
+ if(Zotero.Fulltext.pdfConverterIsRegistered() && Zotero.Fulltext.pdfInfoIsRegistered()) {
+ return Q(true);
+ }
+
+ // Begin install procedure
+ return loadWindow("chrome://zotero/content/preferences/preferences.xul", {
+ pane: 'zotero-prefpane-search',
+ action: 'pdftools-install'
+ }).then(function(win) {
+ // Wait for confirmation dialog
+ return waitForWindow("chrome://global/content/commonDialog.xul").then(function(dlg) {
+ // Accept confirmation dialog
+ dlg.document.documentElement.acceptDialog();
+
+ // Wait for install to finish
+ var deferred = Q.defer();
+ var id = setInterval(function() {
+ if(Zotero.Fulltext.pdfConverterIsRegistered() && Zotero.Fulltext.pdfInfoIsRegistered()) {
+ win.close();
+ clearInterval(id);
+ deferred.resolve(true);
+ }
+ }, 500);
+ return deferred.promise;
+ });
+ });
+}
+
+/**
+ * Returns a promise for the nsIFile corresponding to the test data
+ * directory (i.e., test/tests/data)
+ */
+function getTestDataDirectory() {
+ Components.utils.import("resource://gre/modules/Services.jsm");
+ var resource = Services.io.getProtocolHandler("resource").
+ QueryInterface(Components.interfaces.nsIResProtocolHandler),
+ 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;
+}
+\ No newline at end of file
diff --git a/test/tests/data/recognizePDF_test_DOI.pdf b/test/tests/data/recognizePDF_test_DOI.pdf
Binary files differ.
diff --git a/test/tests/index.js b/test/tests/index.js
@@ -1,3 +1,4 @@
var TESTS = {
+ "recognizePDF":["recognizePDF.js"],
"utilities":["utilities.js"]
};
diff --git a/test/tests/recognizePDF.js b/test/tests/recognizePDF.js
@@ -0,0 +1,34 @@
+describe("PDF Recognition", function() {
+ Components.utils.import("resource://gre/modules/FileUtils.jsm");
+
+ var win;
+ before(function() {
+ this.timeout(60000);
+ return installPDFTools().then(function() {
+ return loadZoteroPane();
+ }).then(function(w) {
+ win = w;
+ });
+ });
+ after(function() {
+ win.close();
+ });
+
+ it("should recognize a PDF with a DOI", function() {
+ this.timeout(30000);
+ // Import the PDF
+ var testdir = getTestDataDirectory();
+ testdir.append("recognizePDF_test_DOI.pdf");
+ var id = Zotero.Attachments.importFromFile(testdir);
+
+ // Recognize the PDF
+ win.ZoteroPane.selectItem(id);
+ win.Zotero_RecognizePDF.recognizeSelected();
+
+ return waitForItemEvent("add").then(function(ids) {
+ var item = Zotero.Items.get(ids[0]);
+ assert.equal(item.getField("title"), "Shaping the Research Agenda");
+ assert.equal(item.getField("libraryCatalog"), "CrossRef");
+ });
+ });
+});
+\ No newline at end of file