diff --git a/chrome/content/zotero/xpcom/fileHandlers.js b/chrome/content/zotero/xpcom/fileHandlers.js index b8b5f1be69..f9027233d9 100644 --- a/chrome/content/zotero/xpcom/fileHandlers.js +++ b/chrome/content/zotero/xpcom/fileHandlers.js @@ -90,17 +90,19 @@ Zotero.FileHandlers = { // If there are handlers for this platform and this reader type... if (handlers) { // First try to open with the custom handler - try { - for (let [i, { name, open }] of handlers.entries()) { - if (name.test(handler)) { - Zotero.debug('Opening with handler ' + i); - await open(handler, { filePath: path, location, page }); - return true; + if (handler) { + try { + for (let [i, { name, open }] of handlers.entries()) { + if (name.test(handler)) { + Zotero.debug('Opening with handler ' + i); + await open(handler, { filePath: path, location, page }); + return true; + } } } - } - catch (e) { - Zotero.logError(e); + catch (e) { + Zotero.logError(e); + } } // If we get here, we don't have special handling for the custom diff --git a/test/tests/fileHandlersTest.js b/test/tests/fileHandlersTest.js index a7549a5ead..622d4be28d 100644 --- a/test/tests/fileHandlersTest.js +++ b/test/tests/fileHandlersTest.js @@ -109,5 +109,39 @@ describe("Zotero.FileHandlers", () => { readerOpenSpy.restore(); getSystemHandlerStub.restore(); }); + + it("should fall back when handler is set to system and we can't retrieve the system handler", async function () { + let pdf = await importFileAttachment('wonderland_short.pdf'); + let wasRun = false; + let readerOpenSpy = sinon.spy(Zotero.Reader, 'open'); + let launchFileStub = sinon.stub(Zotero, 'launchFile'); + Zotero.FileHandlers._mockHandlers = { + pdf: [ + { + name: new RegExp(''), + async open() { + wasRun = true; + } + } + ] + }; + + // Set our custom handler to something nonexistent, + // and stub the system handler to something nonexistent as well + Zotero.Prefs.set('fileHandler.pdf', 'system'); + let getSystemHandlerStub = sinon.stub(Zotero.FileHandlers, '_getSystemHandler'); + getSystemHandlerStub.returns(false); + + await Zotero.FileHandlers.open(pdf, { location: {} }); + assert.isFalse(wasRun); + assert.isFalse(readerOpenSpy.called); + assert.isTrue(launchFileStub.called); + assert.notOk(Zotero.Reader.getByTabID(win.Zotero_Tabs.selectedID)); + assert.isEmpty(Zotero.Reader.getWindowStates()); + + readerOpenSpy.restore(); + launchFileStub.restore(); + getSystemHandlerStub.restore(); + }); }); });