From d85f5ddeacf8e9cdaf41bfde4ddde916f8b02591 Mon Sep 17 00:00:00 2001 From: Romeo Fragomeli Date: Thu, 4 Feb 2021 16:42:48 +0000 Subject: [PATCH] [FIX] web: client report action mounted called twice MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, the 'mounted' method of the ControlPanel in the client report action was called twice. It happened because the client report action updated the ControlPanel before being actually mounted, so mounted was called once when the traceability report was mounted, and once when the update was applied. Ideally, this should not be an issue (this isn't an issue with owl). However, in Odoo, we mix layers of Owl Components and legacy widgets. In these situations, the above scenario isn't properly handled (and can't be). As a consequence, in mobile (enterprise), it crashed because an handler bound in mounted (thus twice) was only unbound once. This commit avoids the issue as the update was actually useless. Steps to reproduces (Mobile): * Go to Inventory (Stock) * Open the "burger menu" * Select "Products" -> "Products" * Select one product in the list * Click on the "Forecasted" ("stat button") * Select one "SO line" (sale order) to go to the form view * Go back to the previous view using breadcrumb * Optional: Go to another app if the screen can't scroll (e.g. go to Sales) * Scroll the view => Bug closes odoo/odoo#66497 X-original-commit: d3854dbf7a6e0c0f9ac00c11716908bc175808d7 Related: odoo/enterprise#16507 Signed-off-by: Adrien Dieudonné (adr) --- .../web/static/src/js/report/client_action.js | 9 -- .../tests/report/client_action_tests.js | 111 ++++++++++++++++++ addons/web/views/webclient_templates.xml | 2 + 3 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 addons/web/static/tests/report/client_action_tests.js diff --git a/addons/web/static/src/js/report/client_action.js b/addons/web/static/src/js/report/client_action.js index 9b7d1fb6124..d4cb3096aea 100644 --- a/addons/web/static/src/js/report/client_action.js +++ b/addons/web/static/src/js/report/client_action.js @@ -53,15 +53,6 @@ var ReportAction = AbstractAction.extend({ }); }, - do_show: function () { - this.updateControlPanel({ - cp_content: { - $buttons: this.$buttons, - }, - }); - return this._super.apply(this, arguments); - }, - on_attach_callback: function () { // Register now the postMessage event handler. We only want to listen to ~trusted // messages and we can only filter them by their origin, so we chose to ignore the diff --git a/addons/web/static/tests/report/client_action_tests.js b/addons/web/static/tests/report/client_action_tests.js new file mode 100644 index 00000000000..c537f6c2540 --- /dev/null +++ b/addons/web/static/tests/report/client_action_tests.js @@ -0,0 +1,111 @@ +odoo.define('web/static/tests/report/client_action_tests', function (require) { + "use strict"; + + const ControlPanel = require('web.ControlPanel'); + const ReportClientAction = require('report.client_action'); + const testUtils = require("web.test_utils"); + + const { createActionManager, dom, mock } = testUtils; + + QUnit.module('Client Action Report', {}, () => { + QUnit.test("mounted is called once when returning on 'Client Action Report' from breadcrumb", async assert => { + // This test can be removed as soon as we don't mix legacy and owl layers anymore. + assert.expect(7); + + let mountCount = 0; + + // patch the report client action to override its iframe's url so that + // it doesn't trigger an RPC when it is appended to the DOM (for this + // usecase, using removeSRCAttribute doesn't work as the RPC is + // triggered as soon as the iframe is in the DOM, even if its src + // attribute is removed right after) + mock.patch(ReportClientAction, { + start: function () { + var self = this; + return this._super.apply(this, arguments).then(function () { + self._rpc({route: self.iframe.getAttribute('src')}); + self.iframe.setAttribute('src', 'about:blank'); + }); + } + }); + + ControlPanel.patch('test.ControlPanel', T => { + class ControlPanelPatchTest extends T { + mounted() { + mountCount = mountCount + 1; + this.__uniqueId = mountCount; + assert.step(`mounted ${this.__uniqueId}`); + super.mounted(...arguments); + } + willUnmount() { + assert.step(`willUnmount ${this.__uniqueId}`); + super.mounted(...arguments); + } + } + return ControlPanelPatchTest; + }); + const actionManager = await createActionManager({ + actions: [ + { + id: 42, + name: "Client Action Report", + tag: 'report.client_action', + type: 'ir.actions.report', + report_type: 'qweb-html', + }, + { + id: 43, + type: "ir.actions.act_window", + res_id: 1, + res_model: "partner", + views: [ + [false, "form"], + ], + } + ], + archs: { + 'partner,false,form': '
', + 'partner,false,search': '', + }, + data: { + partner: { + fields: { + display_name: { string: "Displayed name", type: "char" }, + }, + records: [ + {id: 1, display_name: "Genda Swami"}, + ], + }, + }, + mockRPC: function (route) { + if (route === '/report/html/undefined?context=%7B%7D') { + return Promise.resolve('Go to detail view'); + } + return this._super.apply(this, arguments); + }, + intercepts: { + do_action: ev => actionManager.doAction(ev.data.action, ev.data.options), + }, + }); + + await actionManager.doAction(42); + // simulate an action as we are not able to reproduce a real doAction using 'Client Action Report' + await actionManager.doAction(43); + await dom.click(actionManager.$('.breadcrumb-item:first')); + actionManager.destroy(); + + assert.verifySteps([ + 'mounted 1', + 'willUnmount 1', + 'mounted 2', + 'willUnmount 2', + 'mounted 3', + 'willUnmount 3', + ]); + + ControlPanel.unpatch('test.ControlPanel'); + mock.unpatch(ReportClientAction); + }); + }); + +}); diff --git a/addons/web/views/webclient_templates.xml b/addons/web/views/webclient_templates.xml index 763117ec968..7effc9952da 100644 --- a/addons/web/views/webclient_templates.xml +++ b/addons/web/views/webclient_templates.xml @@ -778,6 +778,8 @@