From 832ce8d1f985eadd04adaf2abaf62964c59fea40 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Mon, 31 May 2021 16:49:44 +0200 Subject: [PATCH] [FIX] web, mail: test helpers: fix legacy interactions The new execution environment of wowl tests has to take into account that legacy objects can be spawned below it and around it, in particular in mail tests where mail has to create an owl environment with the right keys. This commit aims at making this work namely by being to pass an already formed legacyEnv to the createWebClient helper --- addons/mail/static/src/utils/test_utils.js | 37 +++++++++++++++----- addons/web/static/tests/setup.js | 8 ----- addons/web/static/tests/webclient/helpers.js | 29 +++++++++------ 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/addons/mail/static/src/utils/test_utils.js b/addons/mail/static/src/utils/test_utils.js index 9982a4fb0eb..b013a03018f 100644 --- a/addons/mail/static/src/utils/test_utils.js +++ b/addons/mail/static/src/utils/test_utils.js @@ -23,7 +23,11 @@ import { mock, } from 'web.test_utils'; import Widget from 'web.Widget'; -import { createWebClient } from "@web/../tests/webclient/helpers"; +import { createWebClient, getActionManagerServerData } from "@web/../tests/webclient/helpers"; + +import { ComponentAdapter } from "web.OwlCompatibility"; +import LegacyMockServer from "web.MockServer"; +import LegacyRegistry from "web.Registry"; const { addMockEnvironment, @@ -384,8 +388,8 @@ async function createRootComponent(self, Component, { props = {}, target }) { * @param {Object} [param0.env={}] * @param {function} [param0.mockFetch] * @param {function} [param0.mockRPC] - * @param {boolean} [param0.hasActionManager=false] if set, use - * createActionManager. + * @param {boolean} [param0.hasWebClient=false] if set, use + * createWebClient * @param {boolean} [param0.hasChatWindow=false] if set, mount chat window * service. * @param {boolean} [param0.hasDiscuss=false] if set, mount discuss app. @@ -441,7 +445,7 @@ async function start(param0 = {}) { }; const { env: providedEnv, - hasActionManager = false, + hasWebClient = false, hasChatWindow = false, hasDialog = false, hasDiscuss = false, @@ -456,7 +460,7 @@ async function start(param0 = {}) { throw Error(`Unknown parameter value ${waitUntilMessagingCondition} for 'waitUntilMessaging'.`); } delete param0.env; - delete param0.hasActionManager; + delete param0.hasWebClient; delete param0.hasChatWindow; delete param0.hasDiscuss; delete param0.hasMessagingMenu; @@ -545,9 +549,14 @@ async function start(param0 = {}) { } } }); - } else if (hasActionManager) { - let serverData = kwargs.serverData || {}; - delete kwargs.serverData; + } else if (hasWebClient) { + let serverData; + if (!kwargs.serverData) { + serverData = getActionManagerServerData(); + } else { + serverData = kwargs.serverData; + delete kwargs.serverData; + } if (kwargs.actions) { const actions = {}; @@ -567,9 +576,19 @@ async function start(param0 = {}) { const mockRPC = kwargs.mockRPC; delete kwargs.mockRPC; + if (kwargs.services) { + const serviceRegistry = kwargs.serviceRegistry = new LegacyRegistry(); + for (const sname in kwargs.services) { + serviceRegistry.add(sname, kwargs.services[sname]); + } + delete kwargs.services; + } + const legacyParams = kwargs; legacyParams.withLegacyMockServer = true; - const widget = await createWebClient({ serverData, mockRPC, legacyParams }); + legacyParams.env = env; + + widget = await createWebClient({ serverData, mockRPC, legacyParams }); legacyPatch(widget, { destroy() { diff --git a/addons/web/static/tests/setup.js b/addons/web/static/tests/setup.js index b44be18b306..8f8fa75a6c8 100644 --- a/addons/web/static/tests/setup.js +++ b/addons/web/static/tests/setup.js @@ -128,13 +128,6 @@ function patchLegacyCoreBus() { }); } -function patchLegacySession() { - const userContext = Object.getOwnPropertyDescriptor(session, "user_context"); - registerCleanup(() => { - Object.defineProperty(session, "user_context", userContext); - }); -} - function patchOdoo() { patchWithCleanup(odoo, { debug: "", @@ -179,7 +172,6 @@ export async function setupTests() { forceLocaleAndTimezoneWithCleanup(); patchBrowserWithCleanup(); patchLegacyCoreBus(); - patchLegacySession(); patchOdoo(); }); diff --git a/addons/web/static/tests/webclient/helpers.js b/addons/web/static/tests/webclient/helpers.js index 9529226d73d..58272c21e04 100644 --- a/addons/web/static/tests/webclient/helpers.js +++ b/addons/web/static/tests/webclient/helpers.js @@ -22,8 +22,6 @@ import { WebClient } from "@web/webclient/webclient"; // web.test_utils easyload xml templates at line : 124:130. // Also it set the autocomplete delay time for the field Many2One at 0 for the tests at line : 132:137 import "web.test_legacy"; -import AbstractAction from "web.AbstractAction"; -import AbstractController from "web.AbstractController"; import AbstractService from "web.AbstractService"; import ActionMenus from "web.ActionMenus"; import basicFields from "web.basic_fields"; @@ -44,6 +42,7 @@ import LegacyMockServer from "web.MockServer"; import Widget from "web.Widget"; import { userService } from "@web/core/user_service"; import { uiService } from "@web/core/ui/ui_service"; +import { ClientActionAdapter, ViewAdapter } from "@web/legacy/action_adapters"; const { Component, mount, tags } = owl; @@ -139,7 +138,8 @@ function addLegacyMockEnvironment(env, legacyParams = {}) { localSession = { getTZOffset: legacyParams.getTZOffset }; } - const legacyEnv = makeTestEnvironment({ dataManager, bus: core.bus, session: localSession }); + const baseEnv = { dataManager, bus: core.bus, session: localSession }; + const legacyEnv = makeTestEnvironment(Object.assign(baseEnv, legacyParams.env)); if (legacyParams.serviceRegistry) { const legacyServiceMap = core.serviceRegistry.map; @@ -155,6 +155,13 @@ function addLegacyMockEnvironment(env, legacyParams = {}) { Component.env = legacyEnv; mapLegacyEnvToWowlEnv(legacyEnv, env); + function patchLegacySession() { + const userContext = Object.getOwnPropertyDescriptor(session, "user_context"); + registerCleanup(() => { + Object.defineProperty(session, "user_context", userContext); + }); + } + patchLegacySession(); serviceRegistry.add("legacy_session", makeLegacySessionService(legacyEnv, session)); // deploy the legacyActionManagerService (in Wowl env) const legacyActionManagerService = makeLegacyActionManagerService(legacyEnv); @@ -206,16 +213,16 @@ export async function createWebClient(params) { // we destroy the webclient and expect every legacy that has been instantiated // to be destroyed. We thus need to manually destroy them here. const controllers = []; - patchWithCleanup(AbstractAction.prototype, { - init() { - this._super(...arguments); - controllers.push(this); + patchWithCleanup(ClientActionAdapter.prototype, { + mounted() { + this._super(); + controllers.push(this.widget); }, }); - patchWithCleanup(AbstractController.prototype, { - init() { - this._super(...arguments); - controllers.push(this); + patchWithCleanup(ViewAdapter.prototype, { + mounted() { + this._super(); + controllers.push(this.widget); }, });