From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/web_editor/static/tests/test_utils.js | 722 +++++++++++++++++++++++++++ 1 file changed, 722 insertions(+) create mode 100644 addons/web_editor/static/tests/test_utils.js (limited to 'addons/web_editor/static/tests/test_utils.js') diff --git a/addons/web_editor/static/tests/test_utils.js b/addons/web_editor/static/tests/test_utils.js new file mode 100644 index 00000000..1aa4a79c --- /dev/null +++ b/addons/web_editor/static/tests/test_utils.js @@ -0,0 +1,722 @@ +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 Widget = require('web.Widget'); +var Wysiwyg = require('web_editor.wysiwyg'); +var options = require('web_editor.snippets.options'); + +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") { + if (args.method === 'read_template' && args.args[0] === "web_editor.colorpicker") { + return COLOR_PICKER_TEMPLATE; + } + if (args.method === 'render_public_asset' && 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: [], + read_template(args) { + if (args[0] === 'web_editor.colorpicker') { + return COLOR_PICKER_TEMPLATE; + } + }, + render_template(args) { + 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 = $('