From cd3fda98d13a5aa2731a38b3a34c616207b7fe7c Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 4 Mar 2024 01:06:31 -0500 Subject: [PATCH] Fix broken tests due to protocol handler and autocomplete re-registration For the protocol handler, unregister on reinit(). For autocomplete, just skip registration if already registered. --- .../content/zotero/ZoteroProtocolHandler.jsm | 2 ++ chrome/content/zotero/zotero-autocomplete.js | 18 ++++++++++-------- chrome/content/zotero/zotero.mjs | 3 +++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/chrome/content/zotero/ZoteroProtocolHandler.jsm b/chrome/content/zotero/ZoteroProtocolHandler.jsm index 8131c4c1cb..c50628f3e1 100644 --- a/chrome/content/zotero/ZoteroProtocolHandler.jsm +++ b/chrome/content/zotero/ZoteroProtocolHandler.jsm @@ -1366,6 +1366,8 @@ ZoteroProtocolHandler.prototype = { /** * @static + * + * Unregistered in Zotero.reinit() for tests */ ZoteroProtocolHandler.init = function () { Services.io.registerProtocolHandler( diff --git a/chrome/content/zotero/zotero-autocomplete.js b/chrome/content/zotero/zotero-autocomplete.js index a607b888c8..5b569dfdc3 100644 --- a/chrome/content/zotero/zotero-autocomplete.js +++ b/chrome/content/zotero/zotero-autocomplete.js @@ -23,7 +23,8 @@ ***** END LICENSE BLOCK ***** */ -const ZOTERO_AC_CID = Components.ID('{06a2ed11-d0a4-4ff0-a56f-a44545eee6ea}'); +const ZOTERO_AC_CLASS_ID = Components.ID('{06a2ed11-d0a4-4ff0-a56f-a44545eee6ea}'); +const ZOTERO_AC_CONTRACT_ID = "@mozilla.org/autocomplete/search;1?name=zotero"; const EXPORTED_SYMBOLS = ['ZoteroAutoComplete']; const Cc = Components.classes; @@ -351,19 +352,20 @@ ZoteroAutoComplete.prototype.stopSearch = function(){ // Static ZoteroAutoComplete.init = function () { - let search = new ZoteroAutoComplete(); - var name = "@mozilla.org/autocomplete/search;1?name=zotero"; - var componentManager = Components.manager.QueryInterface( - Ci.nsIComponentRegistrar - ); - componentManager.registerFactory(ZOTERO_AC_CID, "", name, search); + // If already registered (e.g., after a Zotero.reinit() in tests), skip + var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); + if (registrar.isCIDRegistered(ZOTERO_AC_CLASS_ID)) { + return; + } + var search = new ZoteroAutoComplete(); + registrar.registerFactory(ZOTERO_AC_CLASS_ID, "", ZOTERO_AC_CONTRACT_ID, search); }; // // XPCOM goop // -ZoteroAutoComplete.prototype.classID = ZOTERO_AC_CID; +ZoteroAutoComplete.prototype.classID = ZOTERO_AC_CLASS_ID; ZoteroAutoComplete.prototype.QueryInterface = ChromeUtils.generateQI([ "nsIFactory", "nsIAutoCompleteSearch" diff --git a/chrome/content/zotero/zotero.mjs b/chrome/content/zotero/zotero.mjs index 71fad6312d..84b0999f07 100644 --- a/chrome/content/zotero/zotero.mjs +++ b/chrome/content/zotero/zotero.mjs @@ -184,6 +184,9 @@ ZoteroContext.prototype = { reinit: function (cb, options = {}) { Services.obs.notifyObservers(zContext.Zotero, "zotero-before-reload"); return zContext.Zotero.shutdown().then(function() { + // Unregister custom protocol handler + Services.io.unregisterProtocolHandler('zotero'); + return cb ? cb() : false; }).finally(function() { makeZoteroContext();