diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 17fc69104..8dabe324a 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -1226,10 +1226,14 @@ var ZoteroPane = new function() /** - * @return {Promise} + * @return {Promise} - Promise that resolves to true if an item was selected, + * or false if not (used for tests, though there could possibly + * be a better test for whether the item pane changed) */ this.itemSelected = function (event) { return Zotero.spawn(function* () { + yield Zotero.DB.waitForTransaction(); + var selectedItems = this.itemsView.getSelectedItems(); // Check if selection has actually changed. The onselect event that calls this @@ -1237,13 +1241,11 @@ var ZoteroPane = new function() // such as whenever selectEventsSuppressed is set to false. var ids = selectedItems.map(item => item.id); ids.sort(); - if (Zotero.Utilities.arrayEquals(_lastSelectedItems, ids)) { + if (ids.length && Zotero.Utilities.arrayEquals(_lastSelectedItems, ids)) { return false; } _lastSelectedItems = ids; - yield Zotero.DB.waitForTransaction(); - if (!this.itemsView) { Zotero.debug("Items view not available in itemSelected", 2); return false; @@ -1405,6 +1407,8 @@ var ZoteroPane = new function() } this.setItemPaneMessage(msg); + + return false; } } @@ -1424,7 +1428,7 @@ var ZoteroPane = new function() } throw e; }.bind(this)); - }; + } /** diff --git a/test/content/support.js b/test/content/support.js index 04b23b1d9..d9edf57c6 100644 --- a/test/content/support.js +++ b/test/content/support.js @@ -48,16 +48,7 @@ var loadZoteroPane = Zotero.Promise.coroutine(function* () { // there or it should be good enough here. yield Zotero.Promise.delay(52); - var zp = win.ZoteroPane; - var cv = zp.collectionsView; - var resolve1, resolve2; - var promise1 = new Zotero.Promise(() => resolve1 = arguments[0]); - var promise2 = new Zotero.Promise(() => resolve2 = arguments[0]); - cv.addEventListener('load', () => resolve1()) - yield promise1; - cv.selection.select(0); - zp.addEventListener('itemsLoaded', () => resolve2()); - yield promise2; + yield waitForItemsLoad(win, 0); return win; }); @@ -85,6 +76,22 @@ function waitForWindow(uri) { return deferred.promise; } +var waitForItemsLoad = function (win, collectionRowToSelect) { + var resolve; + var promise = new Zotero.Promise(() => resolve = arguments[0]); + var zp = win.ZoteroPane; + var cv = zp.collectionsView; + cv.addEventListener('load', function () { + if (collectionRowToSelect !== undefined) { + cv.selection.select(collectionRowToSelect); + } + zp.addEventListener('itemsLoaded', function () { + resolve(); + }); + }); + return promise; +} + /** * Waits for a single item event. Returns a promise for the item ID(s). */ diff --git a/test/tests/zoteroPaneTest.js b/test/tests/zoteroPaneTest.js index 092e698da..6815634c3 100644 --- a/test/tests/zoteroPaneTest.js +++ b/test/tests/zoteroPaneTest.js @@ -34,4 +34,50 @@ describe("ZoteroPane", function() { assert.equal(item.getField('title'), value); }) }); + + describe("#itemSelected()", function () { + it("should update the item count", function* () { + var collection = new Zotero.Collection; + collection.name = "Count Test"; + var id = yield collection.save(); + 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.save({ + 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.save({ + 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) + ); + }) + }) })