Debian Codegraph @

addons/web/static/tests/legacy/webclient/helpers.js

raw symbols SCIP
1 /** @odoo-module alias=@web/../tests/webclient/helpers default=false */
2
3 import { dialogService } from "@web/core/dialog/dialog_service";
4 import { notificationService } from "@web/core/notifications/notification_service";
5 import { ormService } from "@web/core/orm_service";
6 import { popoverService } from "@web/core/popover/popover_service";
7 import { registry } from "@web/core/registry";
8 import { viewService } from "@web/views/view_service";
9 import { actionService } from "@web/webclient/actions/action_service";
10 import { effectService } from "@web/core/effects/effect_service";
11 import { hotkeyService } from "@web/core/hotkeys/hotkey_service";
12 import { menuService } from "@web/webclient/menus/menu_service";
13 import { WebClient } from "@web/webclient/webclient";
14 import { registerCleanup } from "../helpers/cleanup";
15 import { makeTestEnv } from "../helpers/mock_env";
16 import {
17 fakeTitleService,
18 makeFakePwaService,
19 makeFakeLocalizationService,
20 makeFakeHTTPService,
21 makeFakeBarcodeService,
22 } from "../helpers/mock_services";
23 import { getFixture, mount, nextTick } from "../helpers/utils";
24 import { uiService } from "@web/core/ui/ui_service";
25 import { commandService } from "@web/core/commands/command_service";
26 import { CustomFavoriteItem } from "@web/search/custom_favorite_item/custom_favorite_item";
27 import { overlayService } from "@web/core/overlay/overlay_service";
28
29 import { Component, onMounted, xml } from "@odoo/owl";
30 import { fieldService } from "@web/core/field_service";
31 import { nameService } from "@web/core/name_service";
32 import { datetimePickerService } from "@web/core/datetime/datetimepicker_service";
33 import { treeProcessorService } from "@web/core/tree_editor/tree_processor";
34
35 const actionRegistry = registry.category("actions");
36 const serviceRegistry = registry.category("services");
37 const favoriteMenuRegistry = registry.category("favoriteMenu");
38
39 /**
40 * Builds the required registries for tests using a WebClient.
41 * We use a default version of each required registry item.
42 * If the registry already contains one of those items,
43 * the existing one is kept (it means it has been added in the test
44 * directly, e.g. to have a custom version of the item).
45 */
46 export function setupWebClientRegistries() {
47 const favoriveMenuItems = {
48 "custom-favorite-item": {
49 value: { Component: CustomFavoriteItem, groupNumber: 3 },
50 options: { sequence: 0 },
51 },
52 };
53 for (const [key, { value, options }] of Object.entries(favoriveMenuItems)) {
54 if (!favoriteMenuRegistry.contains(key)) {
55 favoriteMenuRegistry.add(key, value, options);
56 }
57 }
58 const services = {
59 action: () => actionService,
60 barcode: () => makeFakeBarcodeService(),
61 command: () => commandService,
62 dialog: () => dialogService,
63 effect: () => effectService,
64 field: () => fieldService,
65 tree_processor: () => treeProcessorService,
66 hotkey: () => hotkeyService,
67 http: () => makeFakeHTTPService(),
68 pwa: () => makeFakePwaService(),
69 localization: () => makeFakeLocalizationService(),
70 menu: () => menuService,
71 name: () => nameService,
72 notification: () => notificationService,
73 orm: () => ormService,
74 overlay: () => overlayService,
75 popover: () => popoverService,
76 title: () => fakeTitleService,
77 ui: () => uiService,
78 view: () => viewService,
79 datetime_picker: () => datetimePickerService,
80 };
81 for (const serviceName in services) {
82 if (!serviceRegistry.contains(serviceName)) {
83 serviceRegistry.add(serviceName, services[serviceName]());
84 }
85 }
86 }
87
88 /**
89 * This method create a web client instance properly configured.
90 *
91 * Note that the returned web client will be automatically cleaned up after the
92 * end of the test.
93 *
94 * @param {*} params
95 */
96 export async function createWebClient(params) {
97 setupWebClientRegistries();
98
99 params.serverData = params.serverData || {};
100 const mockRPC = params.mockRPC || undefined;
101 const env = await makeTestEnv({
102 serverData: params.serverData,
103 mockRPC,
104 });
105
106 const WebClientClass = params.WebClientClass || WebClient;
107 const target = params && params.target ? params.target : getFixture();
108 const wc = await mount(WebClientClass, target, { env });
109 odoo.__WOWL_DEBUG__ = { root: wc };
110 target.classList.add("o_web_client"); // necessary for the stylesheet
111 registerCleanup(() => {
112 target.classList.remove("o_web_client");
113 });
114 // Wait for visual changes caused by a potential loadState
115 await nextTick();
116 // wait for BlankComponent
117 await nextTick();
118 // wait for the regular rendering
119 await nextTick();
120 return wc;
121 }
122
123 export function doAction(env, ...args) {
124 if (env instanceof Component) {
125 env = env.env;
126 }
127 return env.services.action.doAction(...args);
128 }
129
130 export function getActionManagerServerData() {
131 // additional basic client action
132 class TestClientAction extends Component {
133 static template = xml`
134 <div class="test_client_action">
135 ClientAction_<t t-esc="props.action.params?.description"/>
136 </div>`;
137 static props = ["*"];
138 setup() {
139 onMounted(() =>
140 this.env.config.setDisplayName(`Client action ${this.props.action.id}`)
141 );
142 }
143 }
144 actionRegistry.add("__test__client__action__", TestClientAction);
145
146 const menus = {
147 root: { id: "root", children: [0, 1, 2], name: "root", appID: "root" },
148 // id:0 is a hack to not load anything at webClient mount
149 0: { id: 0, children: [], name: "UglyHack", appID: 0, xmlid: "menu_0" },
150 1: { id: 1, children: [], name: "App1", appID: 1, actionID: 1001, xmlid: "menu_1" },
151 2: { id: 2, children: [], name: "App2", appID: 2, actionID: 1002, xmlid: "menu_2" },
152 };
153 const actionsArray = [
154 {
155 id: 1,
156 xml_id: "action_1",
157 name: "Partners Action 1",
158 res_model: "partner",
159 type: "ir.actions.act_window",
160 views: [[1, "kanban"]],
161 },
162 {
163 id: 2,
164 xml_id: "action_2",
165 type: "ir.actions.server",
166 },
167 {
168 id: 3,
169 xml_id: "action_3",
170 name: "Partners",
171 res_model: "partner",
172 mobile_view_mode: "kanban",
173 type: "ir.actions.act_window",
174 views: [
175 [false, "list"],
176 [1, "kanban"],
177 [false, "form"],
178 ],
179 },
180 {
181 id: 4,
182 xml_id: "action_4",
183 name: "Partners Action 4",
184 res_model: "partner",
185 type: "ir.actions.act_window",
186 views: [
187 [1, "kanban"],
188 [2, "list"],
189 [false, "form"],
190 ],
191 },
192 {
193 id: 5,
194 xml_id: "action_5",
195 name: "Create a Partner",
196 res_model: "partner",
197 target: "new",
198 type: "ir.actions.act_window",
199 views: [[false, "form"]],
200 },
201 {
202 id: 6,
203 xml_id: "action_6",
204 name: "Partner",
205 res_id: 2,
206 res_model: "partner",
207 type: "ir.actions.act_window",
208 views: [[false, "form"]],
209 },
210 {
211 id: 7,
212 xml_id: "action_7",
213 name: "Some Report",
214 report_name: "some_report",
215 report_type: "qweb-pdf",
216 type: "ir.actions.report",
217 },
218 {
219 id: 8,
220 xml_id: "action_8",
221 name: "Favorite Ponies",
222 res_model: "pony",
223 type: "ir.actions.act_window",
224 views: [
225 [false, "list"],
226 [false, "form"],
227 ],
228 },
229 {
230 id: 9,
231 xml_id: "action_9",
232 name: "A Client Action",
233 tag: "ClientAction",
234 type: "ir.actions.client",
235 },
236 {
237 id: 10,
238 type: "ir.actions.act_window_close",
239 },
240 {
241 id: 11,
242 xml_id: "action_11",
243 name: "Another Report",
244 report_name: "another_report",
245 report_type: "qweb-pdf",
246 type: "ir.actions.report",
247 close_on_report_download: true,
248 },
249 {
250 id: 12,
251 xml_id: "action_12",
252 name: "Some HTML Report",
253 report_name: "some_report",
254 report_type: "qweb-html",
255 type: "ir.actions.report",
256 },
257 {
258 id: 24,
259 name: "Partner",
260 res_id: 2,
261 res_model: "partner",
262 type: "ir.actions.act_window",
263 views: [[666, "form"]],
264 },
265 {
266 id: 25,
267 name: "Create a Partner",
268 res_model: "partner",
269 target: "new",
270 type: "ir.actions.act_window",
271 views: [[3, "form"]],
272 },
273 {
274 id: 26,
275 xml_id: "action_26",
276 name: "Partner",
277 res_model: "partner",
278 target: "new",
279 type: "ir.actions.act_window",
280 views: [[false, "list"]],
281 },
282 {
283 id: 27,
284 xml_id: "action_27",
285 name: "Partners Action 27",
286 res_model: "partner",
287 mobile_view_mode: "kanban",
288 type: "ir.actions.act_window",
289 path: "partners",
290 views: [
291 [false, "list"],
292 [1, "kanban"],
293 [false, "form"],
294 ],
295 },
296 {
297 id: 28,
298 xml_id: "action_28",
299 name: "Partners Action 28",
300 res_model: "partner",
301 type: "ir.actions.act_window",
302 views: [
303 [1, "kanban"],
304 [2, "list"],
305 [false, "form"],
306 ],
307 },
308 {
309 id: 1001,
310 tag: "__test__client__action__",
311 target: "main",
312 type: "ir.actions.client",
313 params: { description: "Id 1" },
314 },
315 {
316 id: 1002,
317 tag: "__test__client__action__",
318 target: "main",
319 type: "ir.actions.client",
320 params: { description: "Id 2" },
321 },
322 {
323 xmlId: "wowl.client_action",
324 id: 1099,
325 tag: "__test__client__action__",
326 target: "main",
327 type: "ir.actions.client",
328 params: { description: "xmlId" },
329 },
330 ];
331 const actions = {};
332 actionsArray.forEach((act) => {
333 actions[act.xmlId || act.id] = act;
334 });
335 const archs = {
336 // kanban views
337 "partner,1,kanban":
338 '<kanban><templates><t t-name="card">' +
339 '<field name="foo"/>' +
340 "</t></templates></kanban>",
341 // list views
342 "partner,false,list": '<list><field name="foo"/></list>',
343 "partner,2,list": '<list limit="3"><field name="foo"/></list>',
344 "pony,false,list": '<list><field name="name"/></list>',
345 // form views
346 "partner,false,form":
347 "<form>" +
348 "<header>" +
349 '<button name="object" string="Call method" type="object"/>' +
350 '<button name="4" string="Execute action" type="action"/>' +
351 "</header>" +
352 "<group>" +
353 '<field name="display_name"/>' +
354 '<field name="foo"/>' +
355 "</group>" +
356 "</form>",
357 "partner,3,form": `
358 <form>
359 <footer>
360 <button class="btn-primary" string="Save" special="save"/>
361 </footer>
362 </form>`,
363 "partner,666,form": `<form>
364 <header></header>
365 <sheet>
366 <div class="oe_button_box" name="button_box">
367 <button class="oe_stat_button" type="action" name="1" icon="fa-star" context="{'default_partner': id}">
368 <field string="Partners" name="o2m" widget="statinfo"/>
369 </button>
370 </div>
371 <field name="display_name"/>
372 </sheet>
373 </form>`,
374 "pony,false,form": "<form>" + '<field name="name"/>' + "</form>",
375 // search views
376 "partner,false,search": '<search><field name="foo" string="Foo"/></search>',
377 "partner,4,search":
378 "<search>" +
379 '<filter name="bar" help="Bar" domain="[(\'bar\', \'=\', 1)]"/>' +
380 "</search>",
381 "pony,false,search": "<search></search>",
382 };
383 const models = {
384 partner: {
385 fields: {
386 id: { string: "Id", type: "integer" },
387 foo: { string: "Foo", type: "char" },
388 bar: { string: "Bar", type: "many2one", relation: "partner" },
389 o2m: {
390 string: "One2Many",
391 type: "one2many",
392 relation: "partner",
393 relation_field: "bar",
394 },
395 m2o: { string: "Many2one", type: "many2one", relation: "partner" },
396 },
397 records: [
398 { id: 1, display_name: "First record", foo: "yop", bar: 2, o2m: [2, 3], m2o: 3 },
399 {
400 id: 2,
401 display_name: "Second record",
402 foo: "blip",
403 bar: 1,
404 o2m: [1, 4, 5],
405 m2o: 3,
406 },
407 { id: 3, display_name: "Third record", foo: "gnap", bar: 1, o2m: [], m2o: 1 },
408 { id: 4, display_name: "Fourth record", foo: "plop", bar: 2, o2m: [], m2o: 1 },
409 { id: 5, display_name: "Fifth record", foo: "zoup", bar: 2, o2m: [], m2o: 1 },
410 ],
411 },
412 pony: {
413 fields: {
414 id: { string: "Id", type: "integer" },
415 name: { string: "Name", type: "char" },
416 },
417 records: [
418 { id: 4, name: "Twilight Sparkle" },
419 { id: 6, name: "Applejack" },
420 { id: 9, name: "Fluttershy" },
421 ],
422 },
423 };
424 return {
425 models,
426 views: archs,
427 actions,
428 menus,
429 };
430 }