2015-06-12 20:57:38 +00:00
|
|
|
describe("Zotero_File_Interface", function() {
|
|
|
|
let win;
|
|
|
|
before(function* () {
|
2015-06-12 21:06:07 +00:00
|
|
|
win = yield loadZoteroPane();
|
2015-06-12 20:57:38 +00:00
|
|
|
yield OS.File.copy(OS.Path.join(getTestDataDirectory().path, "Test Import Translator.js"),
|
|
|
|
OS.Path.join(Zotero.getTranslatorsDirectory().path, "Test Import Translator.js"));
|
|
|
|
yield Zotero.Translators.reinit();
|
|
|
|
});
|
|
|
|
after(function () {
|
|
|
|
win.close();
|
|
|
|
});
|
|
|
|
|
2020-06-25 05:59:57 +00:00
|
|
|
it('should import all types and fields into a new collection', async function () {
|
2015-06-12 21:08:10 +00:00
|
|
|
this.timeout(10000);
|
2015-06-12 20:57:38 +00:00
|
|
|
let testFile = getTestDataDirectory();
|
|
|
|
testFile.append("allTypesAndFields.js");
|
2020-06-25 05:59:57 +00:00
|
|
|
await win.Zotero_File_Interface.importFile({
|
|
|
|
file: testFile
|
|
|
|
});
|
2015-06-12 20:57:38 +00:00
|
|
|
|
2016-03-25 22:40:47 +00:00
|
|
|
let importedCollection = Zotero.Collections.getByLibrary(
|
|
|
|
Zotero.Libraries.userLibraryID
|
|
|
|
).filter(x => x.name == 'allTypesAndFields');
|
2015-06-12 20:57:38 +00:00
|
|
|
assert.equal(importedCollection.length, 1);
|
|
|
|
let childItems = importedCollection[0].getChildItems();
|
|
|
|
let savedItems = {};
|
|
|
|
for (let i=0; i<childItems.length; i++) {
|
Deasyncification :back: :cry:
While trying to get translation and citing working with asynchronously
generated data, we realized that drag-and-drop support was going to
be...problematic. Firefox only supports synchronous methods for
providing drag data (unlike, it seems, the DataTransferItem interface
supported by Chrome), which means that we'd need to preload all relevant
data on item selection (bounded by export.quickCopy.dragLimit) and keep
the translate/cite methods synchronous (or maintain two separate
versions).
What we're trying instead is doing what I said in #518 we weren't going
to do: loading most object data on startup and leaving many more
functions synchronous. Essentially, this takes the various load*()
methods described in #518, moves them to startup, and makes them operate
on entire libraries rather than individual objects.
The obvious downside here (other than undoing much of the work of the
last many months) is that it increases startup time, potentially quite a
lot for larger libraries. On my laptop, with a 3,000-item library, this
adds about 3 seconds to startup time. I haven't yet tested with larger
libraries. But I'm hoping that we can optimize this further to reduce
that delay. Among other things, this is loading data for all libraries,
when it should be able to load data only for the library being viewed.
But this is also fundamentally just doing some SELECT queries and
storing the results, so it really shouldn't need to be that slow (though
performance may be bounded a bit here by XPCOM overhead).
If we can make this fast enough, it means that third-party plugins
should be able to remain much closer to their current designs. (Some
things, including saving, will still need to be made asynchronous.)
2016-03-07 21:05:51 +00:00
|
|
|
let savedItem = childItems[i].toJSON();
|
2015-06-12 20:57:38 +00:00
|
|
|
delete savedItem.dateAdded;
|
|
|
|
delete savedItem.dateModified;
|
|
|
|
delete savedItem.key;
|
|
|
|
delete savedItem.collections;
|
|
|
|
savedItems[Zotero.ItemTypes.getName(childItems[i].itemTypeID)] = savedItem;
|
|
|
|
}
|
|
|
|
let trueItems = loadSampleData('itemJSON');
|
|
|
|
for (let itemType in trueItems) {
|
|
|
|
let trueItem = trueItems[itemType];
|
|
|
|
delete trueItem.dateAdded;
|
|
|
|
delete trueItem.dateModified;
|
|
|
|
delete trueItem.key;
|
|
|
|
delete trueItem.collections;
|
|
|
|
}
|
2020-06-25 05:59:57 +00:00
|
|
|
assert.deepEqual(savedItems, trueItems, "saved items match inputs");
|
2015-06-12 20:57:38 +00:00
|
|
|
});
|
2016-03-22 04:40:59 +00:00
|
|
|
|
2020-06-25 05:59:57 +00:00
|
|
|
|
|
|
|
it("should import RIS into selected collection", async function () {
|
|
|
|
var collection = await createDataObject('collection');
|
|
|
|
|
|
|
|
var testFile = OS.Path.join(getTestDataDirectory().path, 'book_and_child_note.ris');
|
|
|
|
await win.Zotero_File_Interface.importFile({
|
|
|
|
file: testFile,
|
|
|
|
createNewCollection: false
|
|
|
|
});
|
|
|
|
|
|
|
|
var items = collection.getChildItems();
|
|
|
|
assert.lengthOf(items, 1);
|
|
|
|
var childNotes = items[0].getNotes();
|
|
|
|
assert.lengthOf(childNotes, 1);
|
|
|
|
assert.equal(Zotero.Items.get(childNotes[0]).getNote(), '<p>Child</p>');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-03-22 04:40:59 +00:00
|
|
|
it('should import an item and snapshot from Zotero RDF', function* () {
|
|
|
|
var tmpDir = yield getTempDirectory();
|
|
|
|
var rdfFile = OS.Path.join(tmpDir, 'test.rdf');
|
|
|
|
yield OS.File.copy(OS.Path.join(getTestDataDirectory().path, 'book_and_snapshot.rdf'), rdfFile);
|
|
|
|
yield OS.File.makeDir(OS.Path.join(tmpDir, 'files'));
|
|
|
|
yield OS.File.makeDir(OS.Path.join(tmpDir, 'files', 2));
|
|
|
|
yield OS.File.copy(
|
|
|
|
OS.Path.join(getTestDataDirectory().path, 'test.html'),
|
|
|
|
OS.Path.join(tmpDir, 'files', 2, 'test.html')
|
|
|
|
);
|
|
|
|
|
|
|
|
var promise = waitForItemEvent('add');
|
2020-06-25 05:59:57 +00:00
|
|
|
yield win.Zotero_File_Interface.importFile({
|
|
|
|
file: rdfFile
|
|
|
|
});
|
2016-03-22 04:40:59 +00:00
|
|
|
var ids = yield promise;
|
2020-03-24 21:53:13 +00:00
|
|
|
// Notifications are batched
|
|
|
|
assert.lengthOf(ids, 2);
|
2016-03-22 04:40:59 +00:00
|
|
|
|
|
|
|
// Check book
|
|
|
|
var item = Zotero.Items.get(ids[0]);
|
|
|
|
assert.equal(item.itemTypeID, Zotero.ItemTypes.getID('book'));
|
|
|
|
|
|
|
|
// Check attachment
|
|
|
|
var ids = item.getAttachments();
|
|
|
|
assert.lengthOf(ids, 1);
|
|
|
|
var attachment = Zotero.Items.get(ids[0]);
|
|
|
|
assert.equal(attachment.attachmentCharset, 'utf-8');
|
|
|
|
|
|
|
|
// Check indexing
|
|
|
|
var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test');
|
|
|
|
assert.lengthOf(matches, 1);
|
|
|
|
assert.propertyVal(matches[0], 'id', attachment.id);
|
|
|
|
});
|
2016-08-23 01:28:34 +00:00
|
|
|
|
|
|
|
it('should import a MODS file', function* () {
|
|
|
|
var modsFile = OS.Path.join(getTestDataDirectory().path, "mods.xml");
|
|
|
|
|
|
|
|
var promise = waitForItemEvent('add');
|
2020-06-25 05:59:57 +00:00
|
|
|
yield win.Zotero_File_Interface.importFile({
|
|
|
|
file: modsFile
|
|
|
|
});
|
2016-08-23 01:28:34 +00:00
|
|
|
var ids = yield promise;
|
|
|
|
assert.lengthOf(ids, 1);
|
|
|
|
|
|
|
|
var item = Zotero.Items.get(ids[0]);
|
|
|
|
assert.equal(item.itemTypeID, Zotero.ItemTypes.getID('journalArticle'));
|
|
|
|
assert.equal(item.getField('title'), "Test");
|
|
|
|
});
|
2016-08-26 06:42:56 +00:00
|
|
|
|
|
|
|
describe("#copyItemsToClipboard()", function () {
|
|
|
|
var clipboardService, item1, item2;
|
|
|
|
|
|
|
|
before(function* () {
|
|
|
|
yield Zotero.Styles.init();
|
|
|
|
|
|
|
|
clipboardService = Components.classes["@mozilla.org/widget/clipboard;1"]
|
|
|
|
.getService(Components.interfaces.nsIClipboard);
|
|
|
|
|
|
|
|
item1 = createUnsavedDataObject('item', { title: "A" });
|
|
|
|
item1.setField('date', '2016');
|
|
|
|
yield item1.saveTx();
|
|
|
|
item2 = createUnsavedDataObject('item', { title: "B" });
|
|
|
|
item2.setField('date', '2016');
|
|
|
|
yield item2.saveTx();
|
|
|
|
});
|
|
|
|
|
|
|
|
function getDataForFlavor(flavor) {
|
|
|
|
var transferable = Components.classes["@mozilla.org/widget/transferable;1"]
|
|
|
|
.createInstance(Components.interfaces.nsITransferable);
|
|
|
|
transferable.init(null);
|
|
|
|
transferable.addDataFlavor(flavor);
|
|
|
|
clipboardService.getData(transferable, Components.interfaces.nsIClipboard.kGlobalClipboard);
|
|
|
|
var str = {};
|
|
|
|
transferable.getTransferData(flavor, str, {})
|
|
|
|
return str.value.QueryInterface(Components.interfaces.nsISupportsString).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Non-"Copy as HTML" mode
|
|
|
|
//
|
|
|
|
it("should copy HTML and text citations to the clipboard", function* () {
|
|
|
|
win.Zotero_File_Interface.copyItemsToClipboard(
|
|
|
|
[item1, item2],
|
|
|
|
'http://www.zotero.org/styles/apa',
|
|
|
|
'en-US',
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
// HTML
|
|
|
|
var str = getDataForFlavor('text/html');
|
2017-04-15 08:34:27 +00:00
|
|
|
assert.equal(str, '(<i>A</i>, 2016; <i>B</i>, 2016)');
|
2016-08-26 06:42:56 +00:00
|
|
|
|
|
|
|
// Plain text
|
|
|
|
str = getDataForFlavor('text/unicode');
|
2017-04-15 08:34:27 +00:00
|
|
|
assert.equal(str, '(A, 2016; B, 2016)');
|
2016-08-26 06:42:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should copy HTML and text bibliography to the clipboard", function* () {
|
|
|
|
win.Zotero_File_Interface.copyItemsToClipboard(
|
|
|
|
[item1, item2],
|
|
|
|
'http://www.zotero.org/styles/apa',
|
|
|
|
'en-US'
|
|
|
|
);
|
|
|
|
|
|
|
|
var str = getDataForFlavor('text/html');
|
|
|
|
assert.include(str, 'line-height');
|
|
|
|
assert.include(str, '<i>A</i>');
|
|
|
|
assert.include(str, '<i>B</i>');
|
|
|
|
|
|
|
|
// Plain text
|
|
|
|
str = getDataForFlavor('text/unicode');
|
|
|
|
assert.equal(str, 'A. (2016).\nB. (2016).\n');
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// "Copy as HTML" mode
|
|
|
|
//
|
|
|
|
it("should copy HTML and HTML source citations to the clipboard", function* () {
|
|
|
|
win.Zotero_File_Interface.copyItemsToClipboard(
|
|
|
|
[item1, item2],
|
|
|
|
'http://www.zotero.org/styles/apa',
|
|
|
|
'en-US',
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
var str = getDataForFlavor('text/html');
|
2017-04-15 08:34:27 +00:00
|
|
|
assert.equal(str, '(<i>A</i>, 2016; <i>B</i>, 2016)');
|
2016-08-26 06:42:56 +00:00
|
|
|
|
|
|
|
// Plain text
|
|
|
|
str = getDataForFlavor('text/unicode');
|
2017-04-15 08:34:27 +00:00
|
|
|
assert.equal(str, '(<i>A</i>, 2016; <i>B</i>, 2016)');
|
2016-08-26 06:42:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should copy HTML and HTML source bibliography to the clipboard", function* () {
|
|
|
|
win.Zotero_File_Interface.copyItemsToClipboard(
|
|
|
|
[item1, item2],
|
|
|
|
'http://www.zotero.org/styles/apa',
|
|
|
|
'en-US',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
var str = getDataForFlavor('text/html');
|
|
|
|
assert.include(str, 'line-height');
|
|
|
|
assert.include(str, '<i>A</i>');
|
|
|
|
assert.include(str, '<i>B</i>');
|
|
|
|
|
|
|
|
// Plain text
|
|
|
|
str = getDataForFlavor('text/unicode');
|
|
|
|
assert.include(str, 'line-height');
|
|
|
|
assert.include(str, '<i>A</i>');
|
|
|
|
assert.include(str, '<i>B</i>');
|
|
|
|
});
|
|
|
|
});
|
2015-06-12 20:57:38 +00:00
|
|
|
});
|