diff --git a/chrome/content/zotero/tools/testTranslators/testTranslators.html b/chrome/content/zotero/tools/testTranslators/testTranslators.html index 14fab8e09..e407b8f9e 100644 --- a/chrome/content/zotero/tools/testTranslators/testTranslators.html +++ b/chrome/content/zotero/tools/testTranslators/testTranslators.html @@ -26,6 +26,8 @@ + + Zotero Translator Tester diff --git a/chrome/content/zotero/tools/testTranslators/testTranslators.js b/chrome/content/zotero/tools/testTranslators/testTranslators.js index eaaa7dba8..91c473136 100644 --- a/chrome/content/zotero/tools/testTranslators/testTranslators.js +++ b/chrome/content/zotero/tools/testTranslators/testTranslators.js @@ -23,24 +23,10 @@ ***** END LICENSE BLOCK ***** */ -const CHROME_SAFARI_SCRIPTS = [ - "zotero.js", - "zotero/date.js", - "zotero/debug.js", - "zotero/inject/translator.js", - "zotero/openurl.js", - "zotero/translate.js", - "zotero/utilities.js", - "zotero/messages.js", - "messaging_inject.js", - "translatorTests.js" -]; - const TRANSLATOR_TYPES = ["Web", "Import", "Export", "Search"]; const TABLE_COLUMNS = ["Translator", "Supported", "Status", "Pending", "Succeeded", "Failed", "Unknown"]; var translatorTables = {}; var translatorTestViewsToRun = {}; -var Zotero; /** * Encapsulates a set of tests for a specific translator and type @@ -48,6 +34,7 @@ var Zotero; */ var TranslatorTestView = function(translator, type) { this._translator = translator; + this._type = type; var row = document.createElement("tr"); @@ -87,23 +74,23 @@ var TranslatorTestView = function(translator, type) { translatorTables[type].appendChild(row); // create translator tester and update status based on what it knows - this._translatorTester = new Zotero_TranslatorTester(translator, type); - this.updateStatus(); + this._translatorTester = new Zotero.TranslatorTester(translator, type); + this.updateStatus(this._translatorTester); this.hasTests = !!this._translatorTester.tests.length; } /** * Changes the displayed status of a translator */ -TranslatorTestView.prototype.updateStatus = function() { - if(this._translatorTester.tests.length) { - if(this._translatorTester.pending.length) { +TranslatorTestView.prototype.updateStatus = function(obj) { + if(obj.tests.length) { + if(obj.pending.length) { this._status.className = "status-pending"; this._status.textContent = "Pending"; - } else if(this._translatorTester.failed.length) { + } else if(obj.failed.length) { this._status.className = "status-failed"; this._status.textContent = "Failed"; - } else if(this._translatorTester.unknown.length) { + } else if(obj.unknown.length) { this._status.className = "status-unknown"; this._status.textContent = "Unknown"; } else { @@ -115,10 +102,10 @@ TranslatorTestView.prototype.updateStatus = function() { this._status.textContent = "Untested"; } - this._pending.textContent = this._translatorTester.pending.length; - this._succeeded.textContent = this._translatorTester.succeeded.length; - this._failed.textContent = this._translatorTester.failed.length; - this._unknown.textContent = this._translatorTester.unknown.length; + this._pending.textContent = obj.pending.length; + this._succeeded.textContent = obj.succeeded.length; + this._failed.textContent = obj.failed.length; + this._unknown.textContent = obj.unknown.length; } /** @@ -126,15 +113,17 @@ TranslatorTestView.prototype.updateStatus = function() { */ TranslatorTestView.prototype.runTests = function(doneCallback) { var me = this; + var newCallback = function(obj, status, message) { + me.updateStatus(obj); + if(obj.pending.length === 0) { + doneCallback(); + } + }; + if(Zotero.isFx) { - // yay, no message passing - var i = 1; - this._translatorTester.runTests(function(status, message) { - me.updateStatus(); - if(me._translatorTester.pending.length === 0) { - doneCallback(); - } - }); + this._translatorTester.runTests(newCallback); + } else { + Zotero.TranslatorTester.runTests(this._translator, this._type, newCallback); } } @@ -143,24 +132,8 @@ TranslatorTestView.prototype.runTests = function(doneCallback) { */ function load(event) { if(window.chrome || window.safari) { - // load scripts - for(var i in CHROME_SAFARI_SCRIPTS) { - var script = document.createElement("script"); - script.setAttribute("type", "text/javascript"); - script.setAttribute("src", CHROME_SAFARI_SCRIPTS[i]); - document.head.appendChild(script); - } - // initialize Zotero.initInject(); - } else { - // load scripts - Zotero = Components.classes["@zotero.org/Zotero;1"] - .getService(Components.interfaces.nsISupports) - .wrappedJSObject; - Components.classes["@mozilla.org/moz/jssubscript-loader;1"] - .getService(Components.interfaces.mozIJSSubScriptLoader) - .loadSubScript("chrome://zotero/content/tools/testTranslators/translatorTester.js"); } for(var i in TRANSLATOR_TYPES) { diff --git a/chrome/content/zotero/tools/testTranslators/translatorTester.js b/chrome/content/zotero/tools/testTranslators/translatorTester.js index 35bc7b623..03ea1cd0c 100644 --- a/chrome/content/zotero/tools/testTranslators/translatorTester.js +++ b/chrome/content/zotero/tools/testTranslators/translatorTester.js @@ -37,7 +37,7 @@ const Zotero_TranslatorTester_IGNORE_FIELDS = ["complete", "accessDate", "checkF * @param {Zotero.Translator[]} translator The translator for which to run tests * @param {String} type The type of tests to run (web, import, export, or search) */ -var Zotero_TranslatorTester = function(translator, type, debug) { +Zotero.TranslatorTester = function(translator, type, debug) { this._type = type; this._translator = translator; this._debug = (debug ? debug : function(a, b) { Zotero.debug(a, b) }); @@ -77,13 +77,13 @@ var Zotero_TranslatorTester = function(translator, type, debug) { * Executes tests for this translator * @param {Function} testDoneCallback A callback to be executed each time a test is complete */ -Zotero_TranslatorTester.prototype.runTests = function(testDoneCallback, recursiveRun) { +Zotero.TranslatorTester.prototype.runTests = function(testDoneCallback, recursiveRun) { if(!recursiveRun) { this._debug("TranslatorTester: Running "+this.pending.length+" tests for "+this._translator.label); } if(!this.pending.length) { // always call testDoneCallback once if there are no tests - if(!recursiveRun) testDoneCallback("unknown", "No tests present"); + if(!recursiveRun) testDoneCallback(this, "unknown", "No tests present"); return; } @@ -91,21 +91,33 @@ Zotero_TranslatorTester.prototype.runTests = function(testDoneCallback, recursiv var testNumber = this.tests.length-this.pending.length; var me = this; - var callback = function(status, message) { + var callback = function(obj, status, message) { me._debug("TranslatorTester: "+me._translator.label+" Test "+testNumber+": "+status+" ("+message+")"); me[status].push(test); - if(testDoneCallback) testDoneCallback(status, message); + if(testDoneCallback) testDoneCallback(me, status, message); me.runTests(testDoneCallback, true); }; + this.fetchPageAndRunTest(test, callback); +} + +/** + * Fetches the page for a given test and runs it + * @param {Object} test Test to execute + * @param {Document} doc DOM document to test against + * @param {Function} testDoneCallback A callback to be executed when test is complete + */ +Zotero.TranslatorTester.prototype.fetchPageAndRunTest = function(test, testDoneCallback) { + var me = this; Zotero.HTTP.processDocuments(test.url, function(doc) { - me.runTest(test, doc, callback); + me.runTest(test, doc, testDoneCallback); }, null, function(e) { - callback("failed", "Translation failed to initialize: "+e); - }); + testDoneCallback(this, "failed", "Translation failed to initialize: "+e); + } + ); } /** @@ -114,7 +126,7 @@ Zotero_TranslatorTester.prototype.runTests = function(testDoneCallback, recursiv * @param {Document} doc DOM document to test against * @param {Function} testDoneCallback A callback to be executed when test is complete */ -Zotero_TranslatorTester.prototype.runTest = function(test, doc, testDoneCallback) { +Zotero.TranslatorTester.prototype.runTest = function(test, doc, testDoneCallback) { this._debug(test); var me = this; var translate = Zotero.Translate.newInstance(this._type); @@ -131,19 +143,19 @@ Zotero_TranslatorTester.prototype.runTest = function(test, doc, testDoneCallback * @param {Boolean} returnValue Whether translation completed successfully * @param {Function} testDoneCallback A callback to be executed when test is complete */ -Zotero_TranslatorTester.prototype._checkResult = function(test, translate, returnValue, testDoneCallback) { +Zotero.TranslatorTester.prototype._checkResult = function(test, translate, returnValue, testDoneCallback) { if(!returnValue) { - testDoneCallback("failed", "Translation failed; examine debug output for errors"); + testDoneCallback(this, "failed", "Translation failed; examine debug output for errors"); return; } if(!translate.newItems.length) { - testDoneCallback("failed", "Translation failed; no items returned"); + testDoneCallback(this, "failed", "Translation failed; no items returned"); return; } if(translate.newItems.length !== test.items.length) { - testDoneCallback("unknown", "Expected "+test.items.length+" items; got "+translate.newItems.length); + testDoneCallback(this, "unknown", "Expected "+test.items.length+" items; got "+translate.newItems.length); return; } @@ -159,11 +171,11 @@ Zotero_TranslatorTester.prototype._checkResult = function(test, translate, retur var testItemJSON = JSON.stringify(testItem); var translatedItemJSON = JSON.stringify(translatedItem); if(testItemJSON != translatedItemJSON) { - testDoneCallback("unknown", "Item "+i+" does not match"); + testDoneCallback(this, "unknown", "Item "+i+" does not match"); this._debug("TranslatorTester: Mismatch between "+testItemJSON+" and "+translatedItemJSON); return; } } - testDoneCallback("succeeded", "Test succeeded"); + testDoneCallback(this, "succeeded", "Test succeeded"); } \ No newline at end of file diff --git a/chrome/content/zotero/xpcom/connector/translator.js b/chrome/content/zotero/xpcom/connector/translator.js index 88e851462..236ea1580 100644 --- a/chrome/content/zotero/xpcom/connector/translator.js +++ b/chrome/content/zotero/xpcom/connector/translator.js @@ -99,10 +99,10 @@ Zotero.Translators = new function() { * retrieved. If no callback is specified, translators are * returned. */ - this.getAllForType = function(type, callback) { + this.getAllForType = function(type, callback, includeUnsupported) { if(!_initialized) Zotero.Translators.init() var translators = _cache[type].slice(0); - new Zotero.Translators.CodeGetter(translators, callback, translators); + new Zotero.Translators.CodeGetter(translators, callback, translators, includeUnsupported); return true; } @@ -207,11 +207,17 @@ Zotero.Translators = new function() { /** * A class to get the code for a set of translators at once + * + * @param {Zotero.Translator[]} translators Translators for which to retrieve code + * @param {Function} callback Callback to call once code has been retrieved + * @param {Function} callbackArgs All arguments to be passed to callback (including translators) + * @param {Boolean} [includeUnsupported] If true, include code for unsupported translators */ -Zotero.Translators.CodeGetter = function(translators, callback, callbackArgs) { +Zotero.Translators.CodeGetter = function(translators, callback, callbackArgs, includeUnsupported) { this._translators = translators; this._callbackArgs = callbackArgs; this._callback = callback; + this._includeUnsupported = includeUnsupported; this.getCodeFor(0); } @@ -224,7 +230,7 @@ Zotero.Translators.CodeGetter.prototype.getCodeFor = function(i) { return; } - if(this._translators[i].runMode === Zotero.Translator.RUN_MODE_IN_BROWSER) { + if(this._translators[i].runMode === Zotero.Translator.RUN_MODE_IN_BROWSER || this._includeUnsupported) { // get next translator this._translators[i].getCode(function() { me.getCodeFor(i+1) }); return;