From 52d6089e1a696914266cc83b8a7b835e57c00df2 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Tue, 17 May 2022 13:49:48 +0000 Subject: [PATCH] [IMP] mail: replace env.browser by wowl browser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR prepares the ground for the one introducing the new environment in the discuss app. Indeed, switching from `env.browser` to the wowl `browser` makes a lot of noise in the main PR. In order to reduce this noise, this change is done now. task-2582313 closes odoo/odoo#91622 Signed-off-by: Sébastien Theys (seb) --- addons/mail/__manifest__.py | 1 - addons/mail/static/src/env/test_env.js | 25 ------- addons/mail/static/src/models/messaging.js | 4 +- .../mail/static/tests/helpers/test_utils.js | 6 +- .../mail/static/tests/helpers/time_control.js | 5 +- .../discuss_mobile_mailbox_selection_tests.js | 19 +++-- .../components/chat_window_manager_tests.js | 73 +++++++------------ .../components/messaging_menu_tests.js | 55 +++++++------- .../widgets/notification_alert_tests.js | 37 ++++------ addons/web/static/src/core/browser/browser.js | 10 +++ 10 files changed, 100 insertions(+), 135 deletions(-) delete mode 100644 addons/mail/static/src/env/test_env.js diff --git a/addons/mail/__manifest__.py b/addons/mail/__manifest__.py index 40cf762c007..7f7894f143b 100644 --- a/addons/mail/__manifest__.py +++ b/addons/mail/__manifest__.py @@ -137,7 +137,6 @@ 'mail/static/tests/tours/**/*', ], 'web.tests_assets': [ - 'mail/static/src/env/test_env.js', 'mail/static/tests/helpers/*.js', 'mail/static/tests/models/*.js', ], diff --git a/addons/mail/static/src/env/test_env.js b/addons/mail/static/src/env/test_env.js deleted file mode 100644 index 9b83ffabf31..00000000000 --- a/addons/mail/static/src/env/test_env.js +++ /dev/null @@ -1,25 +0,0 @@ -/** @odoo-module **/ - -/** - * @param {Object} [providedEnv={}] - * @returns {Object} - */ -export function addMessagingToEnv(providedEnv = {}) { - const env = { ...providedEnv }; - /** - * Environment keys used in messaging. - */ - Object.assign(env, { - browser: Object.assign({ - innerHeight: 1080, - innerWidth: 1920, - Notification: Object.assign({ - permission: 'denied', - async requestPermission() { - return this.permission; - }, - }, (env.browser && env.browser.Notification) || {}), - }, env.browser), - }); - return env; -} diff --git a/addons/mail/static/src/models/messaging.js b/addons/mail/static/src/models/messaging.js index c4e55a4d8b3..250409d6b65 100644 --- a/addons/mail/static/src/models/messaging.js +++ b/addons/mail/static/src/models/messaging.js @@ -6,6 +6,8 @@ import { OnChange } from '@mail/model/model_onchange'; import { clear, insertAndReplace, replace } from '@mail/model/model_field_command'; import { makeDeferred } from '@mail/utils/deferred'; +import { browser } from '@web/core/browser/browser'; + const { EventBus } = owl; registerModel({ @@ -190,7 +192,7 @@ registerModel({ * @returns {Object} browser */ _computeBrowser() { - return this.env.browser; + return browser; }, /** * @private diff --git a/addons/mail/static/tests/helpers/test_utils.js b/addons/mail/static/tests/helpers/test_utils.js index b7d4b934622..39e7dc8e873 100644 --- a/addons/mail/static/tests/helpers/test_utils.js +++ b/addons/mail/static/tests/helpers/test_utils.js @@ -3,7 +3,6 @@ import BusService from 'bus.BusService'; import { MessagingMenuContainer } from '@mail/components/messaging_menu_container/messaging_menu_container'; -import { addMessagingToEnv } from '@mail/env/test_env'; import { insertAndReplace, replace } from '@mail/model/model_field_command'; import { ChatWindowService } from '@mail/services/chat_window_service/chat_window_service'; import { MessagingService } from '@mail/services/messaging/messaging'; @@ -22,6 +21,7 @@ import { mock, } from 'web.test_utils'; import Widget from 'web.Widget'; +import { browser } from '@web/core/browser/browser'; import { registry } from '@web/core/registry'; import { registerCleanup } from "@web/../tests/helpers/cleanup"; import { getFixture, patchWithCleanup } from "@web/../tests/helpers/utils"; @@ -693,7 +693,6 @@ async function start(param0 = {}) { env.session ); env.isDebug = env.isDebug || (() => true); - env = addMessagingToEnv(env); if (hasTimeControl) { env = addTimeControlToEnv(env); } @@ -851,6 +850,9 @@ async function start(param0 = {}) { } // get the final test env after execution of createView/createWebClient/addMockEnvironment testEnv = Component.env; + patchWithCleanup(browser, { + fetch: testEnv.browser.fetch, + }); // link the pyEnv to the actual mockServer after execution of createView/createWebClient/addMockEnvironment if (pyEnv) { pyEnv.mockServer = MockServer.currentMockServer; diff --git a/addons/mail/static/tests/helpers/time_control.js b/addons/mail/static/tests/helpers/time_control.js index bcd50809cef..78ca6a14a61 100644 --- a/addons/mail/static/tests/helpers/time_control.js +++ b/addons/mail/static/tests/helpers/time_control.js @@ -2,6 +2,9 @@ import { nextTick } from '@mail/utils/utils'; +import { browser } from '@web/core/browser/browser'; +import { patchWithCleanup } from '@web/../tests/helpers/utils'; + /** * @param {Object} [providedEnv={}] * @returns {Object} @@ -15,7 +18,7 @@ import { nextTick } from '@mail/utils/utils'; let timedOutIds = []; // key: timeoutId, value: func + remaining duration const timeouts = new Map(); - Object.assign(env.browser, { + patchWithCleanup(browser, { clearTimeout: id => { timeouts.delete(id); timedOutIds = timedOutIds.filter(i => i !== id); diff --git a/addons/mail/static/tests/qunit_mobile_suite_tests/components/discuss_mobile_mailbox_selection_tests.js b/addons/mail/static/tests/qunit_mobile_suite_tests/components/discuss_mobile_mailbox_selection_tests.js index f093a76286e..7d4d0b38017 100644 --- a/addons/mail/static/tests/qunit_mobile_suite_tests/components/discuss_mobile_mailbox_selection_tests.js +++ b/addons/mail/static/tests/qunit_mobile_suite_tests/components/discuss_mobile_mailbox_selection_tests.js @@ -5,6 +5,9 @@ import { startServer, } from '@mail/../tests/helpers/test_utils'; +import { browser } from '@web/core/browser/browser'; +import { patchWithCleanup } from '@web/../tests/helpers/utils'; + QUnit.module('mail', {}, function () { QUnit.module('components', {}, function () { QUnit.module('discuss_mobile_mailbox_selection', {}, function () { @@ -13,13 +16,13 @@ QUnit.module('discuss_mobile_mailbox_selection_tests.js'); QUnit.test('select another mailbox', async function (assert) { assert.expect(7); + patchWithCleanup(browser, { + innerHeight: 640, + innerWidth: 360, + }); const { click, messaging } = await start({ autoOpenDiscuss: true, env: { - browser: { - innerHeight: 640, - innerWidth: 360, - }, device: { isMobile: true, }, @@ -74,6 +77,10 @@ QUnit.test('auto-select "Inbox" when discuss had channel as active thread', asyn const pyEnv = await startServer(); const mailChannelId1 = pyEnv['mail.channel'].create(); + patchWithCleanup(browser, { + innerHeight: 640, + innerWidth: 360, + }); const { click, messaging } = await start({ autoOpenDiscuss: true, discuss: { @@ -82,10 +89,6 @@ QUnit.test('auto-select "Inbox" when discuss had channel as active thread', asyn }, }, env: { - browser: { - innerHeight: 640, - innerWidth: 360, - }, device: { isMobile: true, }, diff --git a/addons/mail/static/tests/qunit_suite_tests/components/chat_window_manager_tests.js b/addons/mail/static/tests/qunit_suite_tests/components/chat_window_manager_tests.js index 3733c6ba029..0b31c25cb5c 100644 --- a/addons/mail/static/tests/qunit_suite_tests/components/chat_window_manager_tests.js +++ b/addons/mail/static/tests/qunit_suite_tests/components/chat_window_manager_tests.js @@ -8,6 +8,9 @@ import { startServer, } from '@mail/../tests/helpers/test_utils'; +import { browser } from '@web/core/browser/browser'; +import { patchWithCleanup } from '@web/../tests/helpers/utils'; + import { file, dom } from 'web.test_utils'; const { createFile, inputFiles } = file; const { triggerEvent } = dom; @@ -233,12 +236,10 @@ QUnit.test('open chat from "new message" chat window should open chat in place o }, ]); const imSearchDef = makeDeferred(); + patchWithCleanup(browser, { + innerWidth: 1920, + }); const { click, createMessagingMenuComponent } = await start({ - env: { - browser: { - innerWidth: 1920, - }, - }, async mockRPC(route, args) { const res = await this._super(...arguments); if (args.method === 'im_search') { @@ -857,13 +858,10 @@ QUnit.test('focus next visible chat window when closing current chat window with ], }, ]); - await start({ - env: { - browser: { - innerWidth: 1920, - }, - }, + patchWithCleanup(browser, { + innerWidth: 1920, }); + await start(); assert.containsN( document.body, '.o_ChatWindow .o_ComposerTextInput_textarea', @@ -1043,13 +1041,10 @@ QUnit.test('open 2 different chat windows: enough screen width [REQUIRE FOCUS]', const pyEnv = await startServer(); const [mailChannelId1, mailChannelId2] = pyEnv['mail.channel'].create([{ name: 'mailChannel1' }, { name: 'mailChannel2' }]); - const { click, createMessagingMenuComponent, messaging } = await start({ - env: { - browser: { - innerWidth: 1920, // enough to fit at least 2 chat windows - }, - }, + patchWithCleanup(browser, { + innerWidth: 1920, // enough to fit at least 2 chat windows }); + const { click, createMessagingMenuComponent, messaging } = await start(); await createMessagingMenuComponent(); await click(`.o_MessagingMenu_toggler`); await click(` @@ -1272,13 +1267,10 @@ QUnit.test('open 2 folded chat windows: check shift operations are available', a channel_type: "chat", }; pyEnv['mail.channel'].create([channel, chat]); - const { click } = await start({ - env: { - browser: { - innerWidth: 900, - }, - }, + patchWithCleanup(browser, { + innerWidth: 900, }); + const { click } = await start(); assert.containsN( document.body, @@ -1385,13 +1377,10 @@ QUnit.test('open 3 different chat windows: not enough screen width', async funct { name: 'mailChannel2' }, { name: 'mailChannel3' }, ]); - const { click, createMessagingMenuComponent, messaging } = await start({ - env: { - browser: { - innerWidth: 900, // enough to fit 2 chat windows but not 3 - }, - }, + patchWithCleanup(browser, { + innerWidth: 900, // enough to fit 2 chat windows but not 3 }); + const { click, createMessagingMenuComponent, messaging } = await start(); await createMessagingMenuComponent(); // open, from systray menu, chat windows of channels with Id 1, 2, then 3 @@ -1644,13 +1633,10 @@ QUnit.test('chat window: TAB cycle with 3 open chat windows [REQUIRE FOCUS]', as ], }, ]); - await start({ - env: { - browser: { - innerWidth: 1920, - }, - }, + patchWithCleanup(browser, { + innerWidth: 1920, }); + await start(); assert.containsN( document.body, '.o_ChatWindow .o_ComposerTextInput_textarea', @@ -2060,12 +2046,10 @@ QUnit.test('chat window does not fetch messages if hidden', async function (asse name: "Channel #12", }, ]); + patchWithCleanup(browser, { + innerWidth: 900, + }); const { click, messaging } = await start({ - env: { - browser: { - innerWidth: 900, - }, - }, mockRPC(route, args) { if (route === '/mail/channel/messages') { const { channel_id } = args; @@ -2465,13 +2449,10 @@ QUnit.test('should not have chat window hidden menu in mobile (transition from 2 const pyEnv = await startServer(); const [mailChannelId1, mailChannelId2] = pyEnv['mail.channel'].create([{ name: 'mailChannel1' }, { name: 'mailChannel1' }]); - const { click, createMessagingMenuComponent, messaging } = await start({ - env: { - browser: { - innerWidth: 600, // enough to fit 1 chat window + hidden menu - }, - }, + patchWithCleanup(browser, { + innerWidth: 600, // enough to fit 1 chat window + hidden menu }); + const { click, createMessagingMenuComponent, messaging } = await start(); await createMessagingMenuComponent(); // open, from systray menu, chat windows of channels with id 1, 2 await click('.o_MessagingMenu_toggler'); diff --git a/addons/mail/static/tests/qunit_suite_tests/components/messaging_menu_tests.js b/addons/mail/static/tests/qunit_suite_tests/components/messaging_menu_tests.js index 995438c96df..f58edc302b0 100644 --- a/addons/mail/static/tests/qunit_suite_tests/components/messaging_menu_tests.js +++ b/addons/mail/static/tests/qunit_suite_tests/components/messaging_menu_tests.js @@ -6,6 +6,9 @@ import { startServer, } from '@mail/../tests/helpers/test_utils'; +import { browser } from '@web/core/browser/browser'; +import { patchWithCleanup } from '@web/../tests/helpers/utils'; + import { makeTestPromise } from 'web.test_utils'; QUnit.module('mail', {}, function () { @@ -806,15 +809,13 @@ QUnit.test('
tags in message body preview are transformed in spaces', async QUnit.test('rendering with OdooBot has a request (default)', async function (assert) { assert.expect(4); - const { click, createMessagingMenuComponent } = await start({ - env: { - browser: { - Notification: { - permission: 'default', - }, - }, + patchWithCleanup(browser, { + Notification: { + ...browser.Notification, + permission: 'default', }, }); + const { click, createMessagingMenuComponent } = await start(); await createMessagingMenuComponent(); assert.ok( @@ -843,15 +844,12 @@ QUnit.test('rendering with OdooBot has a request (default)', async function (ass QUnit.test('rendering without OdooBot has a request (denied)', async function (assert) { assert.expect(2); - const { click, createMessagingMenuComponent } = await start({ - env: { - browser: { - Notification: { - permission: 'denied', - }, - }, + patchWithCleanup(browser, { + Notification: { + permission: 'denied', }, }); + const { click, createMessagingMenuComponent } = await start(); await createMessagingMenuComponent(); assert.containsNone( @@ -871,15 +869,12 @@ QUnit.test('rendering without OdooBot has a request (denied)', async function (a QUnit.test('rendering without OdooBot has a request (accepted)', async function (assert) { assert.expect(2); - const { click, createMessagingMenuComponent } = await start({ - env: { - browser: { - Notification: { - permission: 'granted', - }, - }, + patchWithCleanup(browser, { + Notification: { + permission: 'granted', }, }); + const { click, createMessagingMenuComponent } = await start(); await createMessagingMenuComponent(); assert.containsNone( @@ -899,17 +894,17 @@ QUnit.test('rendering without OdooBot has a request (accepted)', async function QUnit.test('respond to notification prompt (denied)', async function (assert) { assert.expect(4); + patchWithCleanup(browser, { + Notification: { + permission: 'default', + async requestPermission() { + this.permission = 'denied'; + return this.permission; + }, + }, + }); const { click, createMessagingMenuComponent } = await start({ env: { - browser: { - Notification: { - permission: 'default', - async requestPermission() { - this.permission = 'denied'; - return this.permission; - }, - }, - }, services: { notification: { notify() { diff --git a/addons/mail/static/tests/qunit_suite_tests/widgets/notification_alert_tests.js b/addons/mail/static/tests/qunit_suite_tests/widgets/notification_alert_tests.js index 3c00f832544..7eca2f869ad 100644 --- a/addons/mail/static/tests/qunit_suite_tests/widgets/notification_alert_tests.js +++ b/addons/mail/static/tests/qunit_suite_tests/widgets/notification_alert_tests.js @@ -2,6 +2,9 @@ import { start } from '@mail/../tests/helpers/test_utils'; +import { browser } from '@web/core/browser/browser'; +import { patchWithCleanup } from '@web/../tests/helpers/utils'; + import FormView from 'web.FormView'; QUnit.module('mail', {}, function () { @@ -14,19 +17,15 @@ QUnit.skip('notification_alert widget: display blocked notification alert', asyn // is good for rendering... task-227947 assert.expect(1); + patchWithCleanup(browser, { + Notification: 'denied', + }); await start({ arch: `
`, - env: { - browser: { - Notification: { - permission: 'denied', - }, - }, - }, hasView: true, model: 'mail.message', // View params @@ -43,19 +42,17 @@ QUnit.skip('notification_alert widget: display blocked notification alert', asyn QUnit.test('notification_alert widget: no notification alert when granted', async function (assert) { assert.expect(1); + patchWithCleanup(browser, { + Notification: { + permission: 'granted', + }, + }); await start({ arch: `
`, - env: { - browser: { - Notification: { - permission: 'granted', - }, - }, - }, hasView: true, model: 'mail.message', // View params @@ -72,19 +69,17 @@ QUnit.test('notification_alert widget: no notification alert when granted', asyn QUnit.test('notification_alert widget: no notification alert when default', async function (assert) { assert.expect(1); + patchWithCleanup(browser, { + Notification: { + permission: 'default', + }, + }); await start({ arch: `
`, - env: { - browser: { - Notification: { - permission: 'default', - }, - }, - }, hasView: true, model: 'mail.message', // View params diff --git a/addons/web/static/src/core/browser/browser.js b/addons/web/static/src/core/browser/browser.js index 12ae06da488..d587af041fa 100644 --- a/addons/web/static/src/core/browser/browser.js +++ b/addons/web/static/src/core/browser/browser.js @@ -32,6 +32,7 @@ export const browser = { console: window.console, history: window.history, navigator: navigator, + Notification: window.Notification, open: window.open.bind(window), XMLHttpRequest: window.XMLHttpRequest, localStorage, @@ -49,6 +50,15 @@ Object.defineProperty(browser, "location", { configurable: true, }); +Object.defineProperty(browser, 'innerHeight', { + get: () => window.innerHeight, + configurable: true, +}); +Object.defineProperty(browser, 'innerWidth', { + get: () => window.innerWidth, + configurable: true, +}); + // ----------------------------------------------------------------------------- // memory localStorage // -----------------------------------------------------------------------------