odoo.define('web_editor.test_utils', function (require) { "use strict"; var ajax = require('web.ajax'); var MockServer = require('web.MockServer'); var testUtils = require('web.test_utils'); var OdooEditorLib = require('@web_editor/js/editor/odoo-editor/src/OdooEditor'); var Widget = require('web.Widget'); var Wysiwyg = require('web_editor.wysiwyg'); var options = require('web_editor.snippets.options'); const { TABLE_ATTRIBUTES, TABLE_STYLES } = require('@web_editor/js/backend/convert_inline'); const COLOR_PICKER_TEMPLATE = `
`; const SNIPPETS_TEMPLATE = `

Add blocks

First Panel

Title

Content

Background Image
Top Middle Bottom Equal height
`; MockServer.include({ //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- /** * @override * @private * @returns {Promise} */ async _performRpc(route, args) { if (args.model === "ir.ui.view" && args.method === 'render_public_asset') { if (args.args[0] === "web_editor.colorpicker") { return COLOR_PICKER_TEMPLATE; } if (args.args[0] === "web_editor.snippets") { return SNIPPETS_TEMPLATE; } } return this._super(...arguments); }, }); /** * Options with animation and edition for test. */ options.registry.option_test = options.Class.extend({ cleanForSave: function () { this.$target.addClass('cleanForSave'); }, onBuilt: function () { this.$target.addClass('built'); }, onBlur: function () { this.$target.removeClass('focus'); }, onClone: function () { this.$target.addClass('clone'); this.$target.removeClass('focus'); }, onFocus: function () { this.$target.addClass('focus'); }, onMove: function () { this.$target.addClass('move'); }, onRemove: function () { this.$target.closest('.note-editable').addClass('snippet_has_removed'); }, }); /** * Constructor WysiwygTest why editable and unbreakable node used in test. */ var WysiwygTest = Wysiwyg.extend({ _parentToDestroyForTest: null, /** * Override 'destroy' of discuss so that it calls 'destroy' on the parent. * * @override */ destroy: function () { unpatch(); this._super(); this.$target.remove(); this._parentToDestroyForTest.destroy(); }, }); function patch() { testUtils.mock.patch(ajax, { loadAsset: function (xmlId) { if (xmlId === 'template.assets') { return Promise.resolve({ cssLibs: [], cssContents: ['body {background-color: red;}'] }); } if (xmlId === 'template.assets_all_style') { return Promise.resolve({ cssLibs: $('link[href]:not([type="image/x-icon"])').map(function () { return $(this).attr('href'); }).get(), cssContents: ['body {background-color: red;}'] }); } throw 'Wrong template'; }, }); } function unpatch() { testUtils.mock.unpatch(ajax); } /** * @param {object} data * @returns {object} */ function wysiwygData(data) { return _.defaults({}, data, { 'ir.ui.view': { fields: { display_name: { string: "Displayed name", type: "char", }, }, records: [], render_template(args) { if (args[0] === 'web_editor.colorpicker') { return COLOR_PICKER_TEMPLATE; } if (args[0] === 'web_editor.snippets') { return SNIPPETS_TEMPLATE; } }, }, 'ir.attachment': { fields: { display_name: { string: "display_name", type: 'char', }, description: { string: "description", type: 'char', }, mimetype: { string: "mimetype", type: 'char', }, checksum: { string: "checksum", type: 'char', }, url: { string: "url", type: 'char', }, type: { string: "type", type: 'char', }, res_id: { string: "res_id", type: 'integer', }, res_model: { string: "res_model", type: 'char', }, public: { string: "public", type: 'boolean', }, access_token: { string: "access_token", type: 'char', }, image_src: { string: "image_src", type: 'char', }, image_width: { string: "image_width", type: 'integer', }, image_height: { string: "image_height", type: 'integer', }, original_id: { string: "original_id", type: 'many2one', relation: 'ir.attachment', }, }, records: [{ id: 1, name: 'image', description: '', mimetype: 'image/png', checksum: false, url: '/web/image/123/transparent.png', type: 'url', res_id: 0, res_model: false, public: true, access_token: false, image_src: '/web/image/123/transparent.png', image_width: 256, image_height: 256, }], generate_access_token: function () { return; }, }, }); } /** * Create the wysiwyg instance for test (contains patch, usefull ir.ui.view, snippets). * * @param {object} params */ async function createWysiwyg(params) { patch(); params.data = wysiwygData(params.data); var parent = new Widget(); await testUtils.mock.addMockEnvironment(parent, params); var wysiwygOptions = _.extend({}, params.wysiwygOptions, { recordInfo: { context: {}, res_model: 'module.test', res_id: 1, }, useOnlyTestUnbreakable: params.useOnlyTestUnbreakable, }); var wysiwyg = new WysiwygTest(parent, wysiwygOptions); wysiwyg._parentToDestroyForTest = parent; var $textarea = $('