diff --git a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js index 73a29a736e..5f43bc63c7 100644 --- a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js +++ b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js @@ -26,6 +26,13 @@ PAGE_LOADED: 'Page Loaded', }; + // Plugin-related reporting constants. + const PLUGINS = { + TYPE: 'lifecycle', + // Reported events - alphabetize below. + INSTALLED: 'Plugins installed', + }; + // Navigation reporting constants. const NAVIGATION = { TYPE: 'nav-report', @@ -214,8 +221,10 @@ } }, - pluginsLoaded() { + pluginsLoaded(pluginsList) { this.timeEnd(TIMER.PLUGINS_LOADED); + this.reporter( + PLUGINS.TYPE, PLUGINS.INSTALLED, (pluginsList || []).join(',')); }, /** diff --git a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html index 3965c7d4e0..de58b6a71f 100644 --- a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html +++ b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html @@ -162,6 +162,14 @@ limitations under the License. )); }); + test('pluginsLoaded reports plugins', () => { + Gerrit._arePluginsLoaded.returns(true); + element.pluginsLoaded(['foo', 'bar']); + assert.isTrue(element.defaultReporter.calledWithExactly( + 'lifecycle', 'Plugins installed', 'foo,bar' + )); + }); + test('caches reports if plugins are not loaded', () => { Gerrit._arePluginsLoaded.returns(false); element.timeEnd('foo'); diff --git a/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html b/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html index 0f0fab5572..31d3150862 100644 --- a/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html +++ b/polygerrit-ui/app/elements/plugins/gr-endpoint-decorator/gr-endpoint-decorator_test.html @@ -56,18 +56,16 @@ limitations under the License. stub('gr-endpoint-decorator', { _import: sandbox.stub().returns(Promise.resolve()), }); - // Since _endpoints are global, must reset state. - Gerrit._endpoints = new GrPluginEndpoints(); + Gerrit._resetPlugins(); container = fixture('basic'); Gerrit.install(p => plugin = p, '0.1', 'http://some/plugin/url.html'); - hooks = []; // Decoration decorationHook = plugin.registerCustomComponent('first', 'some-module'); // Replacement replacementHook = plugin.registerCustomComponent( 'second', 'other-module', {replace: true}); // Mimic all plugins loaded. - Gerrit._setPluginsCount(0); + Gerrit._setPluginsPending([]); flush(done); }); @@ -88,8 +86,10 @@ limitations under the License. test('decoration', () => { const element = container.querySelector('gr-endpoint-decorator[name="first"]'); - const module = Polymer.dom(element.root).children.find( + const modules = Polymer.dom(element.root).children.filter( element => element.nodeName === 'SOME-MODULE'); + assert.equal(modules.length, 1); + const [module] = modules; assert.isOk(module); assert.equal(module['someparam'], 'barbar'); return decorationHook.getLastAttached().then(element => { diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-change-actions-js-api_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-change-actions-js-api_test.html index de1ed41fad..747e6b5375 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-change-actions-js-api_test.html +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-change-actions-js-api_test.html @@ -52,8 +52,11 @@ breaking changes to gr-change-actions won’t be noticed. suite('early init', () => { setup(() => { + Gerrit._resetPlugins(); Gerrit.install(p => { plugin = p; }, '0.1', 'http://test.com/plugins/testplugin/static/test.js'); + // Mimic all plugins loaded. + Gerrit._setPluginsPending([]); changeActions = plugin.changeActions(); element = fixture('basic'); }); @@ -71,6 +74,7 @@ breaking changes to gr-change-actions won’t be noticed. suite('normal init', () => { setup(() => { + Gerrit._resetPlugins(); element = fixture('basic'); sinon.stub(element, '_editStatusChanged'); element.change = {}; @@ -78,6 +82,8 @@ breaking changes to gr-change-actions won’t be noticed. Gerrit.install(p => { plugin = p; }, '0.1', 'http://test.com/plugins/testplugin/static/test.js'); changeActions = plugin.changeActions(); + // Mimic all plugins loaded. + Gerrit._setPluginsPending([]); }); teardown(() => { diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html index 7d91f413e9..bd5e2a27c5 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html @@ -320,15 +320,19 @@ limitations under the License. assert.isTrue(Gerrit._arePluginsLoaded()); }); - test('_pluginInstalled', done => { + test('_pluginInstalled', () => { + const pluginsLoadedStub = sandbox.stub(); stub('gr-reporting', { - pluginsLoaded() { - done(); - }, + pluginsLoaded: (...args) => pluginsLoadedStub(...args), }); - Gerrit._setPluginsCount(2); - Gerrit._pluginInstalled(); - Gerrit._pluginInstalled(); + const plugins = [ + 'http://test.com/plugins/foo/static/test.js', + 'http://test.com/plugins/bar/static/test.js', + ]; + Gerrit._setPluginsPending(plugins); + Gerrit._pluginInstalled(plugins[0]); + Gerrit._pluginInstalled(plugins[1]); + assert.isTrue(pluginsLoadedStub.calledWithExactly(['foo', 'bar'])); }); test('install calls _pluginInstalled', () => { diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js index 0b2133bfec..3719877106 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js @@ -27,8 +27,12 @@ */ let _pluginsPending = {}; + let _pluginsInstalled = []; + let _pluginsPendingCount = -1; + const UNKNOWN_PLUGIN = 'unknown'; + const PANEL_ENDPOINTS_MAPPING = { CHANGE_SCREEN_BELOW_COMMIT_INFO_BLOCK: 'change-view-integration', CHANGE_SCREEN_BELOW_CHANGE_INFO_BLOCK: 'change-metadata-item', @@ -37,6 +41,7 @@ const PLUGIN_LOADING_TIMEOUT_MS = 10000; let _restAPI; + const getRestAPI = () => { if (!_restAPI) { _restAPI = document.createElement('gr-rest-api-interface'); @@ -44,6 +49,14 @@ return _restAPI; }; + let _reporting; + const getReporting = () => { + if (!_reporting) { + _reporting = document.createElement('gr-reporting'); + } + return _reporting; + }; + // TODO (viktard): deprecate in favor of GrPluginRestApi. function send(method, url, opt_callback, opt_payload) { return getRestAPI().send(method, url, opt_payload).then(response => { @@ -420,9 +433,13 @@ if (!app) { // No gr-app found (running tests) Gerrit._resetPlugins = () => { - _resolveAllPluginsLoaded = null; _allPluginsPromise = null; - Gerrit._setPluginsPending([]); + _pluginsInstalled = []; + _pluginsPending = {}; + _pluginsPendingCount = -1; + _reporting = null; + _resolveAllPluginsLoaded = null; + _restAPI = null; Gerrit._endpoints = new GrPluginEndpoints(); for (const k of Object.keys(_plugins)) { delete _plugins[k]; @@ -558,7 +575,8 @@ Gerrit._setPluginsPending = function(plugins) { _pluginsPending = plugins.reduce((o, url) => { - o[getPluginNameFromUrl(url)] = url; + // TODO(viktard): Remove guard (@see Issue 8962) + o[getPluginNameFromUrl(url) || UNKNOWN_PLUGIN] = url; return o; }, {}); Gerrit._setPluginsCount(Object.keys(_pluginsPending).length); @@ -567,7 +585,7 @@ Gerrit._setPluginsCount = function(count) { _pluginsPendingCount = count; if (Gerrit._arePluginsLoaded()) { - document.createElement('gr-reporting').pluginsLoaded(); + getReporting().pluginsLoaded(_pluginsInstalled); if (_resolveAllPluginsLoaded) { _resolveAllPluginsLoaded(); } @@ -585,17 +603,14 @@ }; Gerrit._pluginInstalled = function(url) { - const name = getPluginNameFromUrl(url); - if (name && !_pluginsPending[name]) { - console.warn(`Unexpected plugin from ${url}!`); + const name = getPluginNameFromUrl(url) || UNKNOWN_PLUGIN; + if (!_pluginsPending[name]) { + console.warn(`Unexpected plugin ${name} installed from ${url}.`); } else { - if (name) { - delete _pluginsPending[name]; - console.log(`Plugin ${name} installed`); - } else { - console.log(`Plugin installed from ${url}`); - } + delete _pluginsPending[name]; + _pluginsInstalled.push(name); Gerrit._setPluginsCount(_pluginsPendingCount - 1); + console.log(`Plugin ${name} installed.`); } };