Debian Codegraph @

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

raw
1 import { NameAndSignature } from "@web/core/signature/name_and_signature";
2
3 import { expect, test } from "@odoo/hoot";
4 import { animationFrame, runAllTimers } from "@odoo/hoot-mock";
5 import { click, drag, edit, queryFirst, waitFor } from "@odoo/hoot-dom";
6 import {
7 clickSave,
8 defineModels,
9 fields,
10 models,
11 mountView,
12 onRpc,
13 patchWithCleanup,
14 } from "@web/../tests/web_test_helpers";
15
16 function getUnique(target) {
17 const src = target.dataset.src;
18 return new URL(src).searchParams.get("unique");
19 }
20
21 class Partner extends models.Model {
22 name = fields.Char();
23 product_id = fields.Many2one({
24 string: "Product Name",
25 relation: "product",
26 });
27 sign = fields.Binary({ string: "Signature" });
28 _records = [
29 {
30 id: 1,
31 name: "Pop's Chock'lit",
32 product_id: 7,
33 },
34 ];
35 }
36 class Product extends models.Model {
37 name = fields.Char({ string: "Product Name" });
38 _records = [
39 {
40 id: 7,
41 name: "Veggie Burger",
42 },
43 ];
44 }
45 defineModels([Partner, Product]);
46
47 test("signature can be drawn", async () => {
48 onRpc("/web/sign/get_fonts/", () => ({}));
49 await mountView({
50 type: "form",
51 resModel: "partner",
52 resId: 1,
53 arch: /* xml */ `<form><field name="sign" widget="signature" /></form>`,
54 });
55
56 expect("div[name=sign] img.o_signature").toHaveCount(0);
57 expect("div[name=sign] div.o_signature svg").toHaveCount(1, {
58 message: "should have a valid signature widget",
59 });
60
61 // Click on the widget to open signature modal
62 await click("div[name=sign] div.o_signature");
63 await waitFor(".modal .modal-body");
64 expect(".modal .modal-body .o_web_sign_name_and_signature").toHaveCount(1);
65 expect(".modal .btn.btn-primary:not([disabled])").toHaveCount(0);
66
67 // Use a drag&drop simulation to draw a signature
68 const { drop } = await drag(".modal .o_web_sign_signature", {
69 position: {
70 x: 1,
71 y: 1,
72 },
73 relative: true,
74 });
75 await drop(".modal .o_web_sign_signature", {
76 position: {
77 x: 10, // Arbitrary value
78 y: 10, // Arbitrary value
79 },
80 relative: true,
81 });
82 await animationFrame(); // await owl rendering
83 expect(".modal .btn.btn-primary:not([disabled])").toHaveCount(1);
84
85 // Click on "Adopt and Sign" button
86 await click(".modal .btn.btn-primary:not([disabled])");
87 await animationFrame();
88 expect(".modal").toHaveCount(0);
89
90 // The signature widget should now display the signature img
91 expect("div[name=sign] div.o_signature svg").toHaveCount(0);
92 expect("div[name=sign] img.o_signature").toHaveCount(1);
93
94 const signImgSrc = queryFirst("div[name=sign] img.o_signature").dataset.src;
95 expect(signImgSrc).not.toMatch("placeholder");
96 expect(signImgSrc).toMatch(/^data:image\/png;base64,/);
97 });
98
99 test("Set simple field in 'full_name' node option", async () => {
100 patchWithCleanup(NameAndSignature.prototype, {
101 setup() {
102 super.setup(...arguments);
103 expect.step(this.props.signature.name);
104 },
105 });
106 onRpc("/web/sign/get_fonts/", () => ({}));
107 await mountView({
108 type: "form",
109 resModel: "partner",
110 resId: 1,
111 arch: /* xml */ `
112 <form>
113 <field name="name"/>
114 <field name="sign" widget="signature" options="{'full_name': 'name'}" />
115 </form>`,
116 });
117
118 expect("div[name=sign] div.o_signature svg").toHaveCount(1, {
119 message: "should have a valid signature widget",
120 });
121 // Click on the widget to open signature modal
122 await click("div[name=sign] div.o_signature");
123 await animationFrame();
124 expect(".modal .modal-body a.o_web_sign_auto_button").toHaveCount(1, {
125 message: 'should open a modal with "Auto" button',
126 });
127 expect(".o_web_sign_auto_button").toHaveClass("active", {
128 message: "'Auto' panel is visible by default",
129 });
130 expect.verifySteps(["Pop's Chock'lit"]);
131 });
132
133 test("Set m2o field in 'full_name' node option", async () => {
134 patchWithCleanup(NameAndSignature.prototype, {
135 setup() {
136 super.setup(...arguments);
137 expect.step(this.props.signature.name);
138 },
139 });
140 onRpc("/web/sign/get_fonts/", () => ({}));
141
142 await mountView({
143 type: "form",
144 resModel: "partner",
145 resId: 1,
146 arch: /* xml */ `
147 <form>
148 <field name="product_id"/>
149 <field name="sign" widget="signature" options="{'full_name': 'product_id'}" />
150 </form>`,
151 });
152
153 expect("div[name=sign] div.o_signature svg").toHaveCount(1, {
154 message: "should have a valid signature widget",
155 });
156
157 // Click on the widget to open signature modal
158 await click("div[name=sign] div.o_signature");
159 await waitFor(".modal .modal-body");
160
161 expect(".modal .modal-body a.o_web_sign_auto_button").toHaveCount(1, {
162 message: 'should open a modal with "Auto" button',
163 });
164 expect.verifySteps(["Veggie Burger"]);
165 });
166
167 test("Set size (width and height) in node option", async () => {
168 Partner._fields.sign2 = fields.Binary({ string: "Signature" });
169 Partner._fields.sign3 = fields.Binary({ string: "Signature" });
170 onRpc("/web/sign/get_fonts/", () => ({}));
171
172 await mountView({
173 type: "form",
174 resModel: "partner",
175 resId: 1,
176 arch: /* xml */ `
177 <form>
178 <field name="sign" widget="signature" options="{'size': [150,'']}" />
179 <field name="sign2" widget="signature" options="{'size': ['',100]}" />
180 <field name="sign3" widget="signature" options="{'size': [120,130]}" />
181 </form>`,
182 });
183
184 expect(".o_signature").toHaveCount(3);
185
186 expect("[name='sign'] .o_signature").toHaveStyle({
187 width: "150px",
188 height: "50px",
189 });
190 expect("[name='sign2'] .o_signature").toHaveStyle({
191 width: "300px",
192 height: "100px",
193 });
194 expect("[name='sign3'] .o_signature").toHaveStyle({
195 width: "120px",
196 height: "40px",
197 });
198 });
199
200 test("clicking save manually after changing signature should change the unique of the image src", async () => {
201 Partner._fields.foo = fields.Char({
202 onChange() {},
203 });
204
205 const rec = Partner._records.find((rec) => rec.id === 1);
206 rec.sign = "3 kb";
207 rec.write_date = "2022-08-05 08:37:00"; // 1659688620000
208 const fillSignatureField = async (lineToX, lineToY) => {
209 await click(".o_field_signature img", { visible: false });
210 await waitFor(".modal .modal-body");
211 expect(".modal canvas").toHaveCount(1);
212 const { drop } = await drag(".modal .o_web_sign_signature", {
213 position: {
214 x: 1,
215 y: 1,
216 },
217 relative: true,
218 });
219 await drop(".modal .o_web_sign_signature", {
220 position: {
221 x: lineToX,
222 y: lineToY,
223 },
224 relative: true,
225 });
226 await animationFrame();
227 await click(".modal-footer .btn-primary");
228 await animationFrame();
229 };
230 // 1659692220000, 1659695820000
231 const lastUpdates = ["2022-08-05 09:37:00", "2022-08-05 10:37:00"];
232 let index = 0;
233
234 onRpc("/web/sign/get_fonts/", () => ({}));
235 onRpc("web_save", ({ args }) => {
236 expect.step("web_save");
237 args[1].write_date = lastUpdates[index];
238 args[1].sign = "4 kb";
239 index++;
240 });
241
242 await mountView({
243 type: "form",
244 resModel: "partner",
245 resId: 1,
246 arch: /* xml */ `
247 <form>
248 <field name="foo" />
249 <field name="sign" widget="signature" />
250 </form>`,
251 });
252 expect(getUnique(queryFirst(".o_field_signature img"))).toBe("1659688620000");
253
254 await fillSignatureField(0, 2);
255 await click(".o_field_widget[name='foo'] input");
256 await edit("grrr", { confirm: "Enter" });
257 await runAllTimers();
258 await animationFrame();
259 await clickSave();
260 expect.verifySteps(["web_save"]);
261 expect(getUnique(queryFirst(".o_field_signature img"))).toBe("1659692220000");
262
263 await fillSignatureField(2, 0);
264 await clickSave();
265 expect.verifySteps(["web_save"]);
266 expect(getUnique(queryFirst(".o_field_signature img"))).toBe("1659695820000");
267 });
268
269 test("save record with signature field modified by onchange", async () => {
270 const MYB64 = `iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABRJREFUGFdjZGD438DAwNjACGMAACQlBAMW7JulAAAAAElFTkSuQmCC`;
271
272 Partner._fields.foo = fields.Char({
273 onChange(data) {
274 data.sign = MYB64;
275 },
276 });
277
278 const rec = Partner._records.find((rec) => rec.id === 1);
279 rec.sign = "3 kb";
280 rec.write_date = "2022-08-05 08:37:00"; // 1659688620000
281
282 // 1659692220000, 1659695820000
283 const lastUpdates = ["2022-08-05 09:37:00", "2022-08-05 10:37:00"];
284 let index = 0;
285 onRpc("web_save", ({ args }) => {
286 expect.step("web_save");
287 args[1].write_date = lastUpdates[index];
288 args[1].sign = "4 kb";
289 index++;
290 });
291 await mountView({
292 type: "form",
293 resModel: "partner",
294 resId: 1,
295 arch: /* xml */ `
296 <form>
297 <field name="foo" />
298 <field name="sign" widget="signature" />
299 </form>`,
300 });
301 expect(getUnique(queryFirst(".o_field_signature img"))).toBe("1659688620000");
302 await click("[name='foo'] input");
303 await edit("grrr", { confirm: "Enter" });
304 await runAllTimers();
305 await animationFrame();
306 expect(queryFirst("div[name=sign] img").dataset.src).toBe(`data:image/png;base64,${MYB64}`);
307
308 await clickSave();
309 expect(getUnique(queryFirst(".o_field_signature img"))).toBe("1659692220000");
310 expect.verifySteps(["web_save"]);
311 });
312
313 test("signature field should render initials", async () => {
314 patchWithCleanup(NameAndSignature.prototype, {
315 setup() {
316 super.setup(...arguments);
317 expect.step(this.getCleanedName());
318 },
319 });
320 onRpc("/web/sign/get_fonts/", () => ({}));
321
322 await mountView({
323 type: "form",
324 resModel: "partner",
325 resId: 1,
326 arch: /* xml */ `
327 <form>
328 <field name="product_id"/>
329 <field name="sign" widget="signature" options="{'full_name': 'product_id', 'type': 'initial'}" />
330 </form>`,
331 });
332
333 expect("div[name=sign] div.o_signature svg").toHaveCount(1, {
334 message: "should have a valid signature widget",
335 });
336
337 // Click on the widget to open signature modal
338 await click("div[name=sign] div.o_signature");
339 await animationFrame();
340 expect(".modal .modal-body a.o_web_sign_auto_button").toHaveCount(1, {
341 message: 'should open a modal with "Auto" button',
342 });
343 expect.verifySteps(["V.B."]);
344 });