zotero/test/tests/zoteroPaneTest.js
Dan Stillman daf4a8fe4d Deasyncification 🔙 😢
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 17:03:58 -05:00

204 lines
6.3 KiB
JavaScript

"use strict";
describe("ZoteroPane", function() {
var win, doc, zp;
// Load Zotero pane and select library
before(function* () {
win = yield loadZoteroPane();
doc = win.document;
zp = win.ZoteroPane;
});
after(function () {
win.close();
});
describe("#newItem", function () {
it("should create an item and focus the title field", function* () {
yield zp.newItem(Zotero.ItemTypes.getID('book'), {}, null, true);
var itemBox = doc.getElementById('zotero-editpane-item-box');
var textboxes = doc.getAnonymousNodes(itemBox)[0].getElementsByTagName('textbox');
assert.lengthOf(textboxes, 1);
assert.equal(textboxes[0].getAttribute('fieldname'), 'title');
textboxes[0].blur();
yield Zotero.Promise.delay(1);
})
it("should save an entered value when New Item is used", function* () {
var value = "Test";
var item = yield zp.newItem(Zotero.ItemTypes.getID('book'), {}, null, true);
var itemBox = doc.getElementById('zotero-editpane-item-box');
var textbox = doc.getAnonymousNodes(itemBox)[0].getElementsByTagName('textbox')[0];
textbox.value = value;
yield itemBox.blurOpenField();
item = yield Zotero.Items.getAsync(item.id);
assert.equal(item.getField('title'), value);
})
});
describe("#newNote()", function () {
it("should create a child note and select it", function* () {
var item = yield createDataObject('item');
var noteID = yield zp.newNote(false, item.key, "Test");
var selected = zp.itemsView.getSelectedItems(true);
assert.lengthOf(selected, 1);
assert.equal(selected, noteID);
})
it("should create a standalone note within a collection and select it", function* () {
var collection = yield createDataObject('collection');
var noteID = yield zp.newNote(false, false, "Test");
assert.equal(zp.collectionsView.getSelectedCollection(), collection);
var selected = zp.itemsView.getSelectedItems(true);
assert.lengthOf(selected, 1);
assert.equal(selected, noteID);
})
})
describe("#newCollection()", function () {
it("should create a collection", function* () {
var promise = waitForDialog();
var id = yield zp.newCollection();
yield promise;
var collection = Zotero.Collections.get(id);
assert.isTrue(collection.name.startsWith(Zotero.getString('pane.collections.untitled')));
});
});
describe("#itemSelected()", function () {
it.skip("should update the item count", function* () {
var collection = new Zotero.Collection;
collection.name = "Count Test";
var id = yield collection.saveTx();
yield waitForItemsLoad(win);
// Unselected, with no items in view
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.unselected.zero', 0)
);
// Unselected, with one item in view
var item = new Zotero.Item('newspaperArticle');
item.setCollections([id]);
var itemID1 = yield item.saveTx({
skipSelect: true
});
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.unselected.singular', 1)
);
// Unselected, with multiple items in view
var item = new Zotero.Item('audioRecording');
item.setCollections([id]);
var itemID2 = yield item.saveTx({
skipSelect: true
});
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.unselected.plural', 2)
);
// Multiple items selected
var promise = zp.itemsView._getItemSelectedPromise();
zp.itemsView.rememberSelection([itemID1, itemID2]);
yield promise;
assert.equal(
doc.getElementById('zotero-item-pane-message').value,
Zotero.getString('pane.item.selected.multiple', 2)
);
})
})
describe("#viewAttachment", function () {
Components.utils.import("resource://zotero-unit/httpd.js");
var apiKey = Zotero.Utilities.randomString(24);
var port = 16213;
var baseURL = `http://localhost:${port}/`;
var server;
var responses = {};
var setup = Zotero.Promise.coroutine(function* (options = {}) {
server = sinon.fakeServer.create();
server.autoRespond = true;
});
function setResponse(response) {
setHTTPResponse(server, baseURL, response, responses);
}
before(function () {
Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
Zotero.Sync.Runner.apiKey = apiKey;
Zotero.Sync.Runner.baseURL = baseURL;
})
beforeEach(function* () {
this.httpd = new HttpServer();
this.httpd.start(port);
yield Zotero.Users.setCurrentUserID(1);
yield Zotero.Users.setCurrentUsername("testuser");
})
afterEach(function* () {
var defer = new Zotero.Promise.defer();
this.httpd.stop(() => defer.resolve());
yield defer.promise;
})
it("should download an attachment on-demand", function* () {
yield setup();
Zotero.Sync.Storage.Local.downloadAsNeeded(Zotero.Libraries.userLibraryID, true);
var item = new Zotero.Item("attachment");
item.attachmentLinkMode = 'imported_file';
item.attachmentPath = 'storage:test.txt';
// TODO: Test binary data
var text = Zotero.Utilities.randomString();
item.attachmentSyncState = "to_download";
yield item.saveTx();
var mtime = "1441252524000";
var md5 = Zotero.Utilities.Internal.md5(text)
var newStorageSyncTime = Math.round(new Date().getTime() / 1000);
setResponse({
method: "GET",
url: "users/1/laststoragesync",
status: 200,
text: "" + newStorageSyncTime
});
var s3Path = `pretend-s3/${item.key}`;
this.httpd.registerPathHandler(
`/users/1/items/${item.key}/file`,
{
handle: function (request, response) {
response.setStatusLine(null, 302, "Found");
response.setHeader("Zotero-File-Modification-Time", mtime, false);
response.setHeader("Zotero-File-MD5", md5, false);
response.setHeader("Zotero-File-Compressed", "No", false);
response.setHeader("Location", baseURL + s3Path, false);
}
}
);
this.httpd.registerPathHandler(
"/" + s3Path,
{
handle: function (request, response) {
response.setStatusLine(null, 200, "OK");
response.write(text);
}
}
);
yield zp.viewAttachment(item.id);
assert.equal((yield item.attachmentHash), md5);
assert.equal((yield item.attachmentModificationTime), mtime);
var path = yield item.getFilePathAsync();
assert.equal((yield Zotero.File.getContentsAsync(path)), text);
})
})
})