www

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

fileInterfaceTest.js (6558B)


      1 describe("Zotero_File_Interface", function() {
      2     let win;
      3     before(function* () {
      4         win = yield loadZoteroPane();
      5         yield OS.File.copy(OS.Path.join(getTestDataDirectory().path, "Test Import Translator.js"),
      6                            OS.Path.join(Zotero.getTranslatorsDirectory().path, "Test Import Translator.js"));
      7         yield Zotero.Translators.reinit();
      8     });
      9     after(function () {
     10         win.close();
     11     });
     12 
     13     it('should import a file into a collection', function* () {
     14         this.timeout(10000);
     15         let testFile = getTestDataDirectory();
     16         testFile.append("allTypesAndFields.js");
     17         yield win.Zotero_File_Interface.importFile(testFile);
     18 
     19         let importedCollection = Zotero.Collections.getByLibrary(
     20 			Zotero.Libraries.userLibraryID
     21 		).filter(x => x.name == 'allTypesAndFields');
     22         assert.equal(importedCollection.length, 1);
     23         let childItems = importedCollection[0].getChildItems();
     24         let savedItems = {};
     25         for (let i=0; i<childItems.length; i++) {
     26             let savedItem = childItems[i].toJSON();
     27             delete savedItem.dateAdded;
     28             delete savedItem.dateModified;
     29             delete savedItem.key;
     30             delete savedItem.collections;
     31             savedItems[Zotero.ItemTypes.getName(childItems[i].itemTypeID)] = savedItem;
     32         }
     33         let trueItems = loadSampleData('itemJSON');
     34         for (let itemType in trueItems) {
     35             let trueItem = trueItems[itemType];
     36             delete trueItem.dateAdded;
     37             delete trueItem.dateModified;
     38             delete trueItem.key;
     39             delete trueItem.collections;
     40         }
     41         assert.deepEqual(savedItems, trueItems, "saved items match inputs")
     42     });
     43     
     44 	it('should import an item and snapshot from Zotero RDF', function* () {
     45 		var tmpDir = yield getTempDirectory();
     46 		var rdfFile = OS.Path.join(tmpDir, 'test.rdf');
     47 		yield OS.File.copy(OS.Path.join(getTestDataDirectory().path, 'book_and_snapshot.rdf'), rdfFile);
     48 		yield OS.File.makeDir(OS.Path.join(tmpDir, 'files'));
     49 		yield OS.File.makeDir(OS.Path.join(tmpDir, 'files', 2));
     50 		yield OS.File.copy(
     51 			OS.Path.join(getTestDataDirectory().path, 'test.html'),
     52 			OS.Path.join(tmpDir, 'files', 2, 'test.html')
     53 		);
     54 		
     55 		var promise = waitForItemEvent('add');
     56 		Zotero.debug(yield Zotero.File.getContentsAsync(rdfFile));
     57 		yield win.Zotero_File_Interface.importFile(Zotero.File.pathToFile(rdfFile))
     58 		var ids = yield promise;
     59 		assert.lengthOf(ids, 1);
     60 		
     61 		// Check book
     62 		var item = Zotero.Items.get(ids[0]);
     63 		assert.equal(item.itemTypeID, Zotero.ItemTypes.getID('book'));
     64 		
     65 		// Check attachment
     66 		var ids = item.getAttachments();
     67 		assert.lengthOf(ids, 1);
     68 		var attachment = Zotero.Items.get(ids[0]);
     69 		assert.equal(attachment.attachmentCharset, 'utf-8');
     70 		
     71 		// Check indexing
     72 		var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test');
     73 		assert.lengthOf(matches, 1);
     74 		assert.propertyVal(matches[0], 'id', attachment.id);
     75 	});
     76 	
     77 	it('should import a MODS file', function* () {
     78 		var modsFile = OS.Path.join(getTestDataDirectory().path, "mods.xml");
     79 		
     80 		var promise = waitForItemEvent('add');
     81 		yield win.Zotero_File_Interface.importFile(Zotero.File.pathToFile(modsFile));
     82 		var ids = yield promise;
     83 		assert.lengthOf(ids, 1);
     84 		
     85 		var item = Zotero.Items.get(ids[0]);
     86 		assert.equal(item.itemTypeID, Zotero.ItemTypes.getID('journalArticle'));
     87 		assert.equal(item.getField('title'), "Test");
     88 	});
     89 	
     90 	describe("#copyItemsToClipboard()", function () {
     91 		var clipboardService, item1, item2;
     92 		
     93 		before(function* () {
     94 			yield Zotero.Styles.init();
     95 			
     96 			clipboardService = Components.classes["@mozilla.org/widget/clipboard;1"]
     97 				.getService(Components.interfaces.nsIClipboard);
     98 			
     99 			item1 = createUnsavedDataObject('item', { title: "A" });
    100 			item1.setField('date', '2016');
    101 			yield item1.saveTx();
    102 			item2 = createUnsavedDataObject('item', { title: "B" });
    103 			item2.setField('date', '2016');
    104 			yield item2.saveTx();
    105 		});
    106 		
    107 		function getDataForFlavor(flavor) {
    108 			var transferable = Components.classes["@mozilla.org/widget/transferable;1"]
    109 				.createInstance(Components.interfaces.nsITransferable);
    110 			transferable.init(null);
    111 			transferable.addDataFlavor(flavor);
    112 			clipboardService.getData(transferable, Components.interfaces.nsIClipboard.kGlobalClipboard);
    113 			var str = {};
    114 			transferable.getTransferData(flavor, str, {})
    115 			return str.value.QueryInterface(Components.interfaces.nsISupportsString).data;
    116 		}
    117 		
    118 		//
    119 		// Non-"Copy as HTML" mode
    120 		//
    121 		it("should copy HTML and text citations to the clipboard", function* () {
    122 			win.Zotero_File_Interface.copyItemsToClipboard(
    123 				[item1, item2],
    124 				'http://www.zotero.org/styles/apa',
    125 				'en-US',
    126 				false,
    127 				true
    128 			);
    129 			
    130 			// HTML
    131 			var str = getDataForFlavor('text/html');
    132 			assert.equal(str, '(<i>A</i>, 2016; <i>B</i>, 2016)');
    133 			
    134 			// Plain text
    135 			str = getDataForFlavor('text/unicode');
    136 			assert.equal(str, '(A, 2016; B, 2016)');
    137 		});
    138 		
    139 		it("should copy HTML and text bibliography to the clipboard", function* () {
    140 			win.Zotero_File_Interface.copyItemsToClipboard(
    141 				[item1, item2],
    142 				'http://www.zotero.org/styles/apa',
    143 				'en-US'
    144 			);
    145 			
    146 			var str = getDataForFlavor('text/html');
    147 			assert.include(str, 'line-height');
    148 			assert.include(str, '<i>A</i>');
    149 			assert.include(str, '<i>B</i>');
    150 			
    151 			// Plain text
    152 			str = getDataForFlavor('text/unicode');
    153 			assert.equal(str, 'A. (2016).\nB. (2016).\n');
    154 		});
    155 		
    156 		//
    157 		// "Copy as HTML" mode
    158 		//
    159 		it("should copy HTML and HTML source citations to the clipboard", function* () {
    160 			win.Zotero_File_Interface.copyItemsToClipboard(
    161 				[item1, item2],
    162 				'http://www.zotero.org/styles/apa',
    163 				'en-US',
    164 				true,
    165 				true
    166 			);
    167 			
    168 			var str = getDataForFlavor('text/html');
    169 			assert.equal(str, '(<i>A</i>, 2016; <i>B</i>, 2016)');
    170 			
    171 			// Plain text
    172 			str = getDataForFlavor('text/unicode');
    173 			assert.equal(str, '(<i>A</i>, 2016; <i>B</i>, 2016)');
    174 		});
    175 		
    176 		it("should copy HTML and HTML source bibliography to the clipboard", function* () {
    177 			win.Zotero_File_Interface.copyItemsToClipboard(
    178 				[item1, item2],
    179 				'http://www.zotero.org/styles/apa',
    180 				'en-US',
    181 				true
    182 			);
    183 			
    184 			var str = getDataForFlavor('text/html');
    185 			assert.include(str, 'line-height');
    186 			assert.include(str, '<i>A</i>');
    187 			assert.include(str, '<i>B</i>');
    188 			
    189 			// Plain text
    190 			str = getDataForFlavor('text/unicode');
    191 			assert.include(str, 'line-height');
    192 			assert.include(str, '<i>A</i>');
    193 			assert.include(str, '<i>B</i>');
    194 		});
    195 	});
    196 });