www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 0471a393ebed00acda7567c65659be73d3669018
parent 1179d4c142054cf3119b76efa969c0bb5b1a5190
Author: Dan Stillman <dstillman@zotero.org>
Date:   Sun, 26 Apr 2015 17:42:29 -0400

Always return promise from Zotero.Attachments._postProcessFile()

Diffstat:
Mchrome/content/zotero/xpcom/attachments.js | 4++--
Atest/tests/attachmentsTest.js | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/chrome/content/zotero/xpcom/attachments.js b/chrome/content/zotero/xpcom/attachments.js @@ -1327,7 +1327,7 @@ Zotero.Attachments = new function(){ * * @return {Promise} */ - function _postProcessFile(itemID, file, contentType){ + var _postProcessFile = Zotero.Promise.coroutine(function* (itemID, file, contentType) { // Don't try to process if MIME type is unknown if (!contentType) { return; @@ -1387,7 +1387,7 @@ Zotero.Attachments = new function(){ browser.loadURI(url); return deferred.promise; - } + }); /** * Determines if a given document is an instance of PDFJS diff --git a/test/tests/attachmentsTest.js b/test/tests/attachmentsTest.js @@ -0,0 +1,58 @@ +describe("Zotero.Attachments", function() { + var win; + + before(function () { + // Hidden browser, which requires a browser window, needed for charset detection + // (until we figure out a better way) + if (!Zotero.isStandalone) { + return loadBrowserWindow().then(window => win = window); + } + }); + after(function () { + if (win) { + win.close(); + } + }); + + describe("#importFromFile()", function () { + it("should create a child attachment from a text file", function* () { + // Create test file + var contents = "Test"; + var tmpFile = Zotero.getTempDirectory(); + tmpFile.append('test.txt'); + yield Zotero.File.putContentsAsync(tmpFile, contents); + + // Create parent item + var item = new Zotero.Item('book'); + var parentItemID = yield item.save(); + + // Create attachment and compare content + var itemID = yield Zotero.Attachments.importFromFile(tmpFile, parentItemID); + var item = yield Zotero.Items.getAsync(itemID); + var storedFile = item.getFile(); + assert.equal((yield Zotero.File.getContentsAsync(storedFile)), contents); + + // Clean up + yield Zotero.Items.erase(itemID); + }); + + it("should create a child attachment from a PNG file", function* () { + var file = getTestDataDirectory(); + file.append('test.png'); + var contents = yield Zotero.File.getBinaryContentsAsync(file); + + // Create parent item + var item = new Zotero.Item('book'); + var parentItemID = yield item.save(); + + // Create attachment and compare content + var itemID = yield Zotero.Attachments.importFromFile(file, parentItemID); + var item = yield Zotero.Items.getAsync(itemID); + var storedFile = item.getFile(); + assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents); + + // Clean up + yield Zotero.Items.erase(itemID); + }); + }) +})