diff --git a/test/content/runtests.html b/test/content/runtests.html index 047e7a5e1..173dff3c5 100644 --- a/test/content/runtests.html +++ b/test/content/runtests.html @@ -9,6 +9,7 @@ + \ No newline at end of file diff --git a/test/content/runtests.js b/test/content/runtests.js index b751b8f85..7e131dfc8 100644 --- 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 new file mode 100644 index 000000000..9d8fee3a5 --- /dev/null +++ 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 new file mode 100644 index 000000000..324e83cee Binary files /dev/null and b/test/tests/data/recognizePDF_test_DOI.pdf differ diff --git a/test/tests/index.js b/test/tests/index.js index 125298956..4b5b947cc 100644 --- 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 new file mode 100644 index 000000000..df0dedf70 --- /dev/null +++ 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