From 269a250b4f950bf917c3a680f1d2c5aad2c75cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Fri, 31 Mar 2017 14:27:22 +0300 Subject: [PATCH] Fetch a style if it is not installed on document preferences load --- chrome/content/zotero/bibliography.js | 14 +-- chrome/content/zotero/xpcom/file.js | 4 +- chrome/content/zotero/xpcom/integration.js | 101 +++++++++--------- chrome/content/zotero/xpcom/style.js | 32 ++++-- test/tests/data/cell.csl | 116 +++++++++++++++++++++ test/tests/styleTest.js | 45 ++++++++ 6 files changed, 245 insertions(+), 67 deletions(-) create mode 100644 test/tests/data/cell.csl create mode 100644 test/tests/styleTest.js diff --git a/chrome/content/zotero/bibliography.js b/chrome/content/zotero/bibliography.js index 0ff60b8f4..13c591a54 100644 --- a/chrome/content/zotero/bibliography.js +++ b/chrome/content/zotero/bibliography.js @@ -67,23 +67,25 @@ var Zotero_File_Interface_Bibliography = new function() { _io.style = Zotero.Prefs.get("export.lastStyle"); } + // Initialize styles and try to load the style, attempting a download + yield Zotero.Styles.init(); + if (!Zotero.Styles.get(_io.style)) { + yield Zotero.Styles.install({url: _io.style}, data.style.styleID, true); + } + // add styles to list - yield Zotero.Styles.init(); var styles = Zotero.Styles.getVisible(); - var index = 0; - var nStyles = styles.length; var selectIndex = null; - for(var i=0; i + diff --git a/test/tests/styleTest.js b/test/tests/styleTest.js new file mode 100644 index 000000000..1e627a401 --- /dev/null +++ b/test/tests/styleTest.js @@ -0,0 +1,45 @@ +"use strict"; + +describe("Zotero.Styles", function() { + var styleID = "http://www.zotero.org/styles/cell"; + var stylePath = OS.Path.join(getTestDataDirectory().path, 'cell.csl'); + var styleFile = Zotero.File.pathToFile(stylePath); + var style; + + before(function* () { + yield Zotero.Styles.init(); + style = yield Zotero.File.getContentsAsync(stylePath); + }); + + describe("Zotero.Styles.install", function() { + afterEach(function* (){ + assert.isOk(Zotero.Styles.get(styleID)); + yield Zotero.Styles.get(styleID).remove(); + }); + + it("should install the style from string", function* () { + yield Zotero.Styles.install(style, styleID, true); + }); + + it("should install the style from nsIFile", function* () { + yield Zotero.Styles.install(styleFile, styleID, true); + }); + + it("should install the style from url", function* () { + var getContentsFromURLAsync = Zotero.File.getContentsFromURLAsync; + sinon.stub(Zotero.File, 'getContentsFromURLAsync', function(style) { + if (style.url == styleID) { + return Zotero.Promise.resolve(style); + } else { + return getContentsFromURLAsync.apply(Zotero.File, arguments); + } + }); + yield Zotero.Styles.install({url: styleID}, styleID, true); + Zotero.File.getContentsFromURLAsync.restore(); + }); + + it("should install the style from file path", function* () { + yield Zotero.Styles.install({file: stylePath}, styleID, true); + }) + }); +});