Debian Codegraph @

addons/web/static/tests/views/fields/image_field.test.js

raw
1 import { expect, test } from "@odoo/hoot";
2 import {
3 click,
4 edit,
5 manuallyDispatchProgrammaticEvent,
6 queryAll,
7 queryAllProperties,
8 queryFirst,
9 setInputFiles,
10 waitFor,
11 } from "@odoo/hoot-dom";
12 import { animationFrame, runAllTimers, mockDate } from "@odoo/hoot-mock";
13 import {
14 clickSave,
15 defineModels,
16 fields,
17 models,
18 mountView,
19 onRpc,
20 pagerNext,
21 contains,
22 webModels,
23 } from "@web/../tests/web_test_helpers";
24
25 import { getOrigin } from "@web/core/utils/urls";
26
27 const { DateTime } = luxon;
28
29 const MY_IMAGE =
30 "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
31 const PRODUCT_IMAGE =
32 "R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7";
33
34 function getUnique(target) {
35 const src = target.dataset.src;
36 return new URL(src).searchParams.get("unique");
37 }
38
39 async function setFiles(files, name = "document") {
40 await click("input[type=file]", { visible: false });
41 await setInputFiles(files);
42 await waitFor(`div[name=${name}] img[data-src^="data:image/"]`, { timeout: 1000 });
43 }
44
45 class Partner extends models.Model {
46 name = fields.Char();
47 timmy = fields.Many2many({ relation: "partner.type" });
48 foo = fields.Char();
49 document = fields.Binary();
50
51 _records = [
52 { id: 1, name: "first record", timmy: [], document: "coucou==" },
53 { id: 2, name: "second record", timmy: [] },
54 { id: 4, name: "aaa" },
55 ];
56 }
57
58 class PartnerType extends models.Model {
59 _name = "partner.type";
60
61 name = fields.Char();
62 color = fields.Integer();
63
64 _records = [
65 { id: 12, name: "gold", color: 2 },
66 { id: 14, name: "silver", color: 5 },
67 ];
68 }
69
70 defineModels([Partner, PartnerType]);
71
72 test("ImageField is correctly rendered", async () => {
73 expect.assertions(10);
74
75 Partner._records[0].write_date = "2017-02-08 10:00:00";
76 Partner._records[0].document = MY_IMAGE;
77
78 onRpc("web_read", ({ kwargs }) => {
79 expect(kwargs.specification).toEqual(
80 {
81 display_name: {},
82 document: {},
83 write_date: {},
84 },
85 {
86 message:
87 "The fields document, name and write_date should be present when reading an image",
88 }
89 );
90 });
91 await mountView({
92 type: "form",
93 resModel: "partner",
94 resId: 1,
95 arch: /* xml */ `
96 <form>
97 <field name="document" widget="image" options="{'size': [90, 90]}"/>
98 </form>
99 `,
100 });
101
102 expect(".o_field_widget[name='document']").toHaveClass("o_field_image", {
103 message: "the widget should have the correct class",
104 });
105 expect(".o_field_widget[name='document'] img").toHaveCount(1, {
106 message: "the widget should contain an image",
107 });
108 expect('div[name="document"] img').toHaveAttribute(
109 "data-src",
110 `data:image/png;base64,${MY_IMAGE}`,
111 { message: "the image should have the correct src" }
112 );
113 expect(".o_field_widget[name='document'] img").toHaveClass("img-fluid", {
114 message: "the image should have the correct class",
115 });
116 expect(".o_field_widget[name='document'] img").toHaveAttribute("width", "90", {
117 message: "the image should correctly set its attributes",
118 });
119 expect(".o_field_widget[name='document'] img").toHaveStyle(
120 {
121 maxWidth: "90px",
122 width: "90px",
123 height: "90px",
124 },
125 {
126 message: "the image should correctly set its attributes",
127 }
128 );
129 expect(".o_field_image .o_select_file_button").toHaveCount(1, {
130 message: "the image can be edited",
131 });
132 expect(".o_field_image .o_clear_file_button").toHaveCount(1, {
133 message: "the image can be deleted",
134 });
135 expect("input.o_input_file").toHaveAttribute("accept", "image/*", {
136 message:
137 'the default value for the attribute "accept" on the "image" widget must be "image/*"',
138 });
139 });
140
141 test("ImageField with img_class option", async () => {
142 await mountView({
143 type: "form",
144 resModel: "partner",
145 resId: 1,
146 arch: `
147 <form>
148 <field name="document" widget="image" options="{'img_class': 'my_custom_class'}"/>
149 </form>`,
150 });
151
152 expect(".o_field_image img").toHaveClass("my_custom_class");
153 });
154
155 test("ImageField with alt attribute", async () => {
156 await mountView({
157 type: "form",
158 resModel: "partner",
159 resId: 1,
160 arch: `
161 <form>
162 <field name="document" widget="image" alt="something"/>
163 </form>`,
164 });
165
166 expect(".o_field_widget[name='document'] img").toHaveAttribute("alt", "something", {
167 message: "the image should correctly set its alt attribute",
168 });
169 });
170
171 test("ImageField on a many2one", async () => {
172 Partner._fields.parent_id = fields.Many2one({ relation: "partner" });
173 Partner._records[1].parent_id = 1;
174
175 mockDate("2017-02-06 10:00:00");
176
177 await mountView({
178 type: "form",
179 resModel: "partner",
180 resId: 2,
181 arch: /* xml */ `
182 <form>
183 <field name="parent_id" widget="image" options="{'preview_image': 'document'}"/>
184 </form>`,
185 });
186
187 expect(".o_field_widget[name=parent_id] img").toHaveCount(1);
188 expect('div[name="parent_id"] img').toHaveAttribute(
189 "data-src",
190 `${getOrigin()}/web/image/partner/1/document?unique=1486375200000`
191 );
192 expect(".o_field_widget[name='parent_id'] img").toHaveAttribute("alt", "first record");
193 });
194
195 test("url should not use the record last updated date when the field is related", async () => {
196 Partner._fields.related = fields.Binary({ related: "parent_id.document" });
197 Partner._fields.parent_id = fields.Many2one({ relation: "partner" });
198 Partner._records[1].parent_id = 1;
199 Partner._records[0].write_date = "2017-02-04 10:00:00";
200 Partner._records[0].document = "3 kb";
201
202 mockDate("2017-02-06 10:00:00");
203
204 await mountView({
205 type: "form",
206 resModel: "partner",
207 resId: 2,
208 arch: `
209 <form>
210 <field name="foo"/>
211 <field name="related" widget="image" readonly="0"/>
212 </form>`,
213 });
214
215 const initialUnique = Number(getUnique(queryFirst('div[name="related"] img')));
216 expect(DateTime.fromMillis(initialUnique).hasSame(DateTime.fromISO("2017-02-06"), "days")).toBe(
217 true
218 );
219
220 await click(".o_field_widget[name='foo'] input");
221 await edit("grrr");
222 await animationFrame();
223
224 expect(Number(getUnique(queryFirst('div[name="related"] img')))).toBe(initialUnique);
225
226 mockDate("2017-02-09 10:00:00");
227
228 await click("input[type=file]", { visible: false });
229 await setFiles(
230 new File(
231 [Uint8Array.from([...atob(MY_IMAGE)].map((c) => c.charCodeAt(0)))],
232 "fake_file.png",
233 { type: "png" }
234 ),
235 "related"
236 );
237
238 expect("div[name=related] img").toHaveAttribute(
239 "data-src",
240 `data:image/png;base64,${MY_IMAGE}`
241 );
242
243 await clickSave();
244
245 const unique = Number(getUnique(queryFirst('div[name="related"] img')));
246 expect(DateTime.fromMillis(unique).hasSame(DateTime.fromISO("2017-02-09"), "days")).toBe(true);
247 });
248
249 test("url should use the record last updated date when the field is related on the same model", async () => {
250 Partner._fields.related = fields.Binary({ related: "document" });
251 Partner._records[0].write_date = "2017-02-04 10:00:00"; // 1486202400000
252 Partner._records[0].document = "3 kb";
253
254 await mountView({
255 type: "form",
256 resModel: "partner",
257 resId: 1,
258 arch: `
259 <form>
260 <field name="related" widget="image"/>
261 </form>`,
262 });
263 expect('div[name="related"] img').toHaveAttribute(
264 "data-src",
265 `${getOrigin()}/web/image/partner/1/related?unique=1486202400000`
266 );
267 });
268
269 test("ImageField is correctly replaced when given an incorrect value", async () => {
270 Partner._records[0].document = "incorrect_base64_value";
271
272 await mountView({
273 type: "form",
274 resModel: "partner",
275 resId: 1,
276 arch: /* xml */ `
277 <form>
278 <field name="document" widget="image" options="{'size': [90, 90]}"/>
279 </form>
280 `,
281 });
282
283 expect(`div[name="document"] img`).toHaveAttribute(
284 "data-src",
285 "data:image/png;base64,incorrect_base64_value",
286 {
287 message: "the image has the invalid src by default",
288 }
289 );
290
291 // As GET requests can't occur in tests, we must generate an error
292 // on the img element to check whether the data-src is replaced with
293 // a placeholder, here knowing that the GET request would fail
294 manuallyDispatchProgrammaticEvent(queryFirst('div[name="document"] img'), "error");
295 await animationFrame();
296
297 expect('.o_field_widget[name="document"]').toHaveClass("o_field_image", {
298 message: "the widget should have the correct class",
299 });
300 expect(".o_field_widget[name='document'] img").toHaveCount(1, {
301 message: "the widget should contain an image",
302 });
303 expect('div[name="document"] img').toHaveAttribute(
304 "data-src",
305 "/web/static/img/placeholder.png",
306 { message: "the image should have the correct src" }
307 );
308 expect(".o_field_widget[name='document'] img").toHaveClass("img-fluid", {
309 message: "the image should have the correct class",
310 });
311 expect(".o_field_widget[name='document'] img").toHaveAttribute("width", "90", {
312 message: "the image should correctly set its attributes",
313 });
314 expect(".o_field_widget[name='document'] img").toHaveStyle("maxWidth: 90px", {
315 message: "the image should correctly set its attributes",
316 });
317
318 expect(".o_field_image .o_select_file_button").toHaveCount(1, {
319 message: "the image can be edited",
320 });
321 expect(".o_field_image .o_clear_file_button").toHaveCount(0, {
322 message: "the image cannot be deleted as it has not been uploaded",
323 });
324 });
325
326 test("ImageField preview is updated when an image is uploaded", async () => {
327 const imageFile = new File(
328 [Uint8Array.from([...atob(MY_IMAGE)].map((c) => c.charCodeAt(0)))],
329 "fake_file.png",
330 { type: "png" }
331 );
332 await mountView({
333 type: "form",
334 resModel: "partner",
335 resId: 1,
336 arch: /* xml */ `
337 <form>
338 <field name="document" widget="image" options="{'size': [90, 90]}"/>
339 </form>
340 `,
341 });
342
343 expect('div[name="document"] img').toHaveAttribute(
344 "data-src",
345 "data:image/png;base64,coucou==",
346 { message: "the image should have the initial src" }
347 );
348 // Whitebox: replace the event target before the event is handled by the field so that we can modify
349 // the files that it will take into account. This relies on the fact that it reads the files from
350 // event.target and not from a direct reference to the input element.
351 await click(".o_select_file_button");
352 await setInputFiles(imageFile);
353 // It can take some time to encode the data as a base64 url
354 await runAllTimers();
355 // Wait for a render
356 await animationFrame();
357 expect("div[name=document] img").toHaveAttribute(
358 "data-src",
359 `data:image/png;base64,${MY_IMAGE}`,
360 { message: "the image should have the new src" }
361 );
362 });
363
364 test("clicking save manually after uploading new image should change the unique of the image src", async () => {
365 Partner._onChanges.foo = () => {};
366
367 const rec = Partner._records.find((rec) => rec.id === 1);
368 rec.document = "3 kb";
369 rec.write_date = "2022-08-05 08:37:00"; // 1659688620000
370
371 // 1659692220000, 1659695820000
372 const lastUpdates = ["2022-08-05 09:37:00", "2022-08-05 10:37:00"];
373 let index = 0;
374
375 onRpc("web_save", ({ args }) => {
376 args[1].write_date = lastUpdates[index];
377 args[1].document = "4 kb";
378 index++;
379 });
380 await mountView({
381 type: "form",
382 resModel: "partner",
383 resId: 1,
384 arch: /* xml */ `
385 <form>
386 <field name="foo"/>
387 <field name="document" widget="image" />
388 </form>
389 `,
390 });
391 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
392
393 await click("input[type=file]", { visible: false });
394 await setFiles(
395 new File(
396 [Uint8Array.from([...atob(MY_IMAGE)].map((c) => c.charCodeAt(0)))],
397 "fake_file.png",
398 { type: "png" }
399 )
400 );
401 expect("div[name=document] img").toHaveAttribute(
402 "data-src",
403 `data:image/png;base64,${MY_IMAGE}`
404 );
405
406 await click(".o_field_widget[name='foo'] input");
407 await edit("grrr");
408 await animationFrame();
409 expect("div[name=document] img").toHaveAttribute(
410 "data-src",
411 `data:image/png;base64,${MY_IMAGE}`
412 );
413
414 await clickSave();
415 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659692220000");
416
417 // Change the image again. After clicking save, it should have the correct new url.
418 await click("input[type=file]", { visible: false });
419 await setFiles(
420 new File(
421 [Uint8Array.from([...atob(PRODUCT_IMAGE)].map((c) => c.charCodeAt(0)))],
422 "fake_file2.gif",
423 { type: "gif" }
424 )
425 );
426 expect("div[name=document] img").toHaveAttribute(
427 "data-src",
428 `data:image/gif;base64,${PRODUCT_IMAGE}`
429 );
430
431 await clickSave();
432 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659695820000");
433 });
434
435 test("save record with image field modified by onchange", async () => {
436 Partner._onChanges.foo = (data) => {
437 data.document = MY_IMAGE;
438 };
439 const rec = Partner._records.find((rec) => rec.id === 1);
440 rec.document = "3 kb";
441 rec.write_date = "2022-08-05 08:37:00"; // 1659688620000
442
443 // 1659692220000
444 const lastUpdates = ["2022-08-05 09:37:00"];
445 let index = 0;
446
447 onRpc("web_save", ({ args }) => {
448 args[1].write_date = lastUpdates[index];
449 args[1].document = "3 kb";
450 index++;
451 });
452 await mountView({
453 type: "form",
454 resModel: "partner",
455 resId: 1,
456 arch: /* xml */ `
457 <form>
458 <field name="foo"/>
459 <field name="document" widget="image" />
460 </form>
461 `,
462 });
463 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
464 await click("[name='foo'] input");
465 await edit("grrr", { confirm: "enter" });
466 await animationFrame();
467 expect("div[name=document] img").toHaveAttribute(
468 "data-src",
469 `data:image/png;base64,${MY_IMAGE}`
470 );
471
472 await clickSave();
473 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659692220000");
474 });
475
476 test("ImageField: option accepted_file_extensions", async () => {
477 await mountView({
478 type: "form",
479 resModel: "partner",
480 resId: 1,
481 arch: /* xml */ `
482 <form>
483 <field name="document" widget="image" options="{'accepted_file_extensions': '.png,.jpeg'}" />
484 </form>
485 `,
486 });
487 // The view must be in edit mode
488 expect("input.o_input_file").toHaveAttribute("accept", ".png,.jpeg", {
489 message: "the input should have the correct ``accept`` attribute",
490 });
491 });
492
493 test("ImageField: set 0 width/height in the size option", async () => {
494 await mountView({
495 type: "form",
496 resModel: "partner",
497 resId: 1,
498 arch: /* xml */ `
499 <form>
500 <field name="document" widget="image" options="{'size': [0, 0]}" />
501 <field name="document" widget="image" options="{'size': [0, 50]}" />
502 <field name="document" widget="image" options="{'size': [50, 0]}" />
503 </form>
504 `,
505 });
506
507 const imgs = queryAll(".o_field_widget img");
508
509 expect([imgs[0].attributes.width, imgs[0].attributes.height]).toEqual([undefined, undefined], {
510 message: "if both size are set to 0, both attributes are undefined",
511 });
512
513 expect([imgs[1].attributes.width, imgs[1].attributes.height.value]).toEqual([undefined, "50"], {
514 message: "if only the width is set to 0, the width attribute is not set on the img",
515 });
516 expect([
517 imgs[1].style.width,
518 imgs[1].style.maxWidth,
519 imgs[1].style.height,
520 imgs[1].style.maxHeight,
521 ]).toEqual(["auto", "100%", "", "50px"], {
522 message: "the image should correctly set its attributes",
523 });
524
525 expect([imgs[2].attributes.width.value, imgs[2].attributes.height]).toEqual(["50", undefined], {
526 message: "if only the height is set to 0, the height attribute is not set on the img",
527 });
528 expect([
529 imgs[2].style.width,
530 imgs[2].style.maxWidth,
531 imgs[2].style.height,
532 imgs[2].style.maxHeight,
533 ]).toEqual(["", "50px", "auto", "100%"], {
534 message: "the image should correctly set its attributes",
535 });
536 });
537
538 test("ImageField: zoom and zoom_delay options (readonly)", async () => {
539 Partner._records[0].document = MY_IMAGE;
540
541 await mountView({
542 type: "form",
543 resModel: "partner",
544 resId: 1,
545 arch: /* xml */ `
546 <form>
547 <field name="document" widget="image" options="{'zoom': true, 'zoom_delay': 600}" readonly="1" />
548 </form>
549 `,
550 });
551 // data-tooltip attribute is used by the tooltip service
552 expect(".o_field_image img").toHaveAttribute(
553 "data-tooltip-info",
554 `{"url":"data:image/png;base64,${MY_IMAGE}"}`,
555 { message: "shows a tooltip on hover" }
556 );
557 expect(".o_field_image img").toHaveAttribute("data-tooltip-delay", "600", {
558 message: "tooltip has the right delay",
559 });
560 });
561
562 test("ImageField: zoom and zoom_delay options (edit)", async () => {
563 Partner._records[0].document = "3 kb";
564 Partner._records[0].write_date = "2022-08-05 08:37:00";
565
566 await mountView({
567 type: "form",
568 resModel: "partner",
569 resId: 1,
570 arch: /* xml */ `
571 <form>
572 <field name="document" widget="image" options="{'zoom': true, 'zoom_delay': 600}" />
573 </form>
574 `,
575 });
576
577 expect(".o_field_image img").toHaveAttribute(
578 "data-tooltip-info",
579 `{"url":"${getOrigin()}/web/image/partner/1/document?unique=1659688620000"}`,
580 { message: "tooltip show the full image from the field value" }
581 );
582 expect(".o_field_image img").toHaveAttribute("data-tooltip-delay", "600", {
583 message: "tooltip has the right delay",
584 });
585 });
586
587 test("ImageField displays the right images with zoom and preview_image options (readonly)", async () => {
588 Partner._records[0].document = "3 kb";
589 Partner._records[0].write_date = "2022-08-05 08:37:00";
590
591 await mountView({
592 type: "form",
593 resModel: "partner",
594 resId: 1,
595 arch: /* xml */ `
596 <form>
597 <field name="document" widget="image" options="{'zoom': true, 'preview_image': 'document_preview', 'zoom_delay': 600}" readonly="1" />
598 </form>
599 `,
600 });
601 expect(".o_field_image img").toHaveAttribute(
602 "data-tooltip-info",
603 `{"url":"${getOrigin()}/web/image/partner/1/document?unique=1659688620000"}`,
604 { message: "tooltip show the full image from the field value" }
605 );
606 expect(".o_field_image img").toHaveAttribute("data-tooltip-delay", "600", {
607 message: "tooltip has the right delay",
608 });
609 });
610
611 test("ImageField in subviews is loaded correctly", async () => {
612 Partner._records[0].write_date = "2017-02-08 10:00:00";
613 Partner._records[0].document = MY_IMAGE;
614 PartnerType._fields.image = fields.Binary({});
615 PartnerType._records[0].image = PRODUCT_IMAGE;
616 Partner._records[0].timmy = [12];
617
618 await mountView({
619 type: "form",
620 resModel: "partner",
621 resId: 1,
622 arch: /* xml */ `
623 <form>
624 <field name="document" widget="image" options="{'size': [90, 90]}" />
625 <field name="timmy" widget="many2many" mode="kanban">
626 <kanban>
627 <templates>
628 <t t-name="card">
629 <field name="name" />
630 </t>
631 </templates>
632 </kanban>
633 <form>
634 <field name="image" widget="image" />
635 </form>
636 </field>
637 </form>
638 `,
639 });
640
641 expect(`img[data-src="data:image/png;base64,${MY_IMAGE}"]`).toHaveCount(1);
642 expect(".o_kanban_record:not(.o_kanban_ghost):not(.o-kanban-button-new)").toHaveCount(1);
643
644 // Actual flow: click on an element of the m2m to get its form view
645 await click(".o_kanban_record:not(.o_kanban_ghost):not(.o-kanban-button-new)");
646 await animationFrame();
647 expect(".modal").toHaveCount(1, { message: "The modal should have opened" });
648
649 expect(`img[data-src="data:image/gif;base64,${PRODUCT_IMAGE}"]`).toHaveCount(1);
650 });
651
652 test("ImageField in x2many list is loaded correctly", async () => {
653 PartnerType._fields.image = fields.Binary({});
654 PartnerType._records[0].image = PRODUCT_IMAGE;
655 Partner._records[0].timmy = [12];
656
657 await mountView({
658 type: "form",
659 resModel: "partner",
660 resId: 1,
661 arch: /* xml */ `
662 <form>
663 <field name="timmy" widget="many2many">
664 <list>
665 <field name="image" widget="image" />
666 </list>
667 </field>
668 </form>
669 `,
670 });
671
672 expect("tr.o_data_row").toHaveCount(1, {
673 message: "There should be one record in the many2many",
674 });
675 expect(`img[data-src="data:image/gif;base64,${PRODUCT_IMAGE}"]`).toHaveCount(1, {
676 message: "The list's image is in the DOM",
677 });
678 });
679
680 test("ImageField with required attribute", async () => {
681 onRpc("create", () => {
682 throw new Error("Should not do a create RPC with unset required image field");
683 });
684 await mountView({
685 type: "form",
686 resModel: "partner",
687 arch: /* xml */ `
688 <form>
689 <field name="document" widget="image" required="1" />
690 </form>
691 `,
692 });
693
694 await clickSave();
695
696 expect(".o_form_view .o_form_editable").toHaveCount(1, {
697 message: "form view should still be editable",
698 });
699 expect(".o_field_widget").toHaveClass("o_field_invalid", {
700 message: "image field should be displayed as invalid",
701 });
702 });
703
704 test("ImageField is reset when changing record", async () => {
705 const imageData = Uint8Array.from([...atob(MY_IMAGE)].map((c) => c.charCodeAt(0)));
706 await mountView({
707 type: "form",
708 resModel: "partner",
709 arch: /* xml */ `
710 <form>
711 <field name="document" widget="image" options="{'size': [90, 90]}"/>
712 </form>
713 `,
714 });
715
716 const imageFile = new File([imageData], "fake_file.png", { type: "png" });
717 expect("img[alt='Binary file']").toHaveAttribute(
718 "data-src",
719 "/web/static/img/placeholder.png",
720 { message: "image field should not be set" }
721 );
722
723 await setFiles(imageFile);
724 expect("img[alt='Binary file']").toHaveAttribute(
725 "data-src",
726 `data:image/png;base64,${MY_IMAGE}`,
727 {
728 message: "image field should be set",
729 }
730 );
731
732 await clickSave();
733 await click(".o_control_panel_main_buttons .o_form_button_create");
734 await runAllTimers();
735 await animationFrame();
736 expect("img[alt='Binary file']").toHaveAttribute(
737 "data-src",
738 "/web/static/img/placeholder.png",
739 { message: "image field should be reset" }
740 );
741
742 await setFiles(imageFile);
743 expect("img[alt='Binary file']").toHaveAttribute(
744 "data-src",
745 `data:image/png;base64,${MY_IMAGE}`,
746 {
747 message: "image field should be set",
748 }
749 );
750 });
751 test("unique in url doesn't change on onchange", async () => {
752 Partner._onChanges.foo = () => {};
753
754 const rec = Partner._records.find((rec) => rec.id === 1);
755 rec.document = "3 kb";
756 rec.write_date = "2022-08-05 08:37:00";
757
758 onRpc(({ method, args }) => {
759 expect.step(method);
760 if (method === "web_save") {
761 args[1].write_date = "2022-08-05 09:37:00"; // 1659692220000
762 }
763 });
764 await mountView({
765 resId: 1,
766 type: "form",
767 resModel: "partner",
768 arch: /* xml */ `
769 <form>
770 <field name="foo" />
771 <field name="document" widget="image" required="1" />
772 </form>
773 `,
774 });
775
776 expect.verifySteps(["get_views", "web_read"]);
777 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
778
779 expect.verifySteps([]);
780 // same unique as before
781 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
782
783 await click(".o_field_widget[name='foo'] input");
784 await edit("grrr", { confirm: "enter" });
785 await animationFrame();
786 expect.verifySteps(["onchange"]);
787 // also same unique
788 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
789
790 await clickSave();
791 expect.verifySteps(["web_save"]);
792
793 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659692220000");
794 });
795
796 test("unique in url change on record change", async () => {
797 const rec = Partner._records.find((rec) => rec.id === 1);
798 rec.document = "3 kb";
799 rec.write_date = "2022-08-05 08:37:00";
800
801 const rec2 = Partner._records.find((rec) => rec.id === 2);
802 rec2.document = "3 kb";
803 rec2.write_date = "2022-08-05 09:37:00";
804
805 await mountView({
806 resIds: [1, 2],
807 resId: 1,
808 type: "form",
809 resModel: "partner",
810 arch: /* xml */ `
811 <form>
812 <field name="document" widget="image" required="1" />
813 </form>
814 `,
815 });
816
817 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
818 await pagerNext();
819 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659692220000");
820 });
821
822 test("unique in url does not change on record change if reload option is set to false", async () => {
823 const rec = Partner._records.find((rec) => rec.id === 1);
824 rec.document = "3 kb";
825 rec.write_date = "2022-08-05 08:37:00";
826
827 await mountView({
828 resIds: [1, 2],
829 resId: 1,
830 type: "form",
831 resModel: "partner",
832 arch: /* xml */ `
833 <form>
834 <field name="document" widget="image" required="1" options="{'reload': false}" />
835 <field name="write_date" readonly="0"/>
836 </form>
837 `,
838 });
839 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
840 await contains("div[name='write_date'] > div > button").click();
841 await edit("2022-08-05 08:39:00", { confirm: "enter" });
842 await animationFrame();
843 await clickSave();
844 expect(getUnique(queryFirst(".o_field_image img"))).toBe("1659688620000");
845 });
846
847 test("convert image to webp", async () => {
848 onRpc("ir.attachment", "create_unique", ({ args }) => {
849 // This RPC call is done two times - once for storing webp and once for storing jpeg
850 // This handles first RPC call to store webp
851 if (!args[0][0].res_id) {
852 // Here we check the image data we pass and generated data.
853 // Also we check the file type
854 expect(args[0][0].datas).not.toBe(imageData);
855 expect(args[0][0].mimetype).toBe("image/webp");
856 return [1];
857 }
858 // This handles second RPC call to store jpeg
859 expect(args[0][0].datas).not.toBe(imageData);
860 expect(args[0][0].mimetype).toBe("image/jpeg");
861 return true;
862 });
863
864 const imageData = Uint8Array.from([...atob(MY_IMAGE)].map((c) => c.charCodeAt(0)));
865 await mountView({
866 type: "form",
867 resModel: "partner",
868 arch: /* xml */ `
869 <form>
870 <field name="document" widget="image" required="1" options="{'convert_to_webp': True}" />
871 </form>
872 `,
873 });
874
875 const imageFile = new File([imageData], "fake_file.jpeg", { type: "jpeg" });
876 expect("img[alt='Binary file']").toHaveAttribute(
877 "data-src",
878 "/web/static/img/placeholder.png",
879 { message: "image field should not be set" }
880 );
881 await setFiles(imageFile);
882 });
883
884 test.tags("desktop");
885 test("ImageField with width attribute in list", async () => {
886 const { ResCompany, ResPartner, ResUsers } = webModels;
887 defineModels([ResCompany, ResPartner, ResUsers]);
888
889 await mountView({
890 type: "list",
891 resModel: "partner",
892 arch: /* xml */ `
893 <list>
894 <field name="document" widget="image" width="30"/>
895 <field name="foo"/>
896 </list>
897 `,
898 });
899
900 expect(".o_data_row").toHaveCount(3);
901 expect(".o_field_widget[name=document] img").toHaveCount(3);
902 expect(queryAllProperties(".o_list_table th[data-name=document]", "offsetWidth")).toEqual([39]);
903 });