1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
odoo.define('web.widget_tests', function (require) {
"use strict";
var AjaxService = require('web.AjaxService');
var core = require('web.core');
var Dialog = require('web.Dialog');
var QWeb = require('web.QWeb');
var Widget = require('web.Widget');
var testUtils = require('web.test_utils');
QUnit.module('core', {}, function () {
QUnit.module('Widget');
QUnit.test('proxy (String)', function (assert) {
assert.expect(1);
var W = Widget.extend({
exec: function () {
this.executed = true;
}
});
var w = new W();
var fn = w.proxy('exec');
fn();
assert.ok(w.executed, 'should execute the named method in the right context');
w.destroy();
});
QUnit.test('proxy (String)(*args)', function (assert) {
assert.expect(2);
var W = Widget.extend({
exec: function (arg) {
this.executed = arg;
}
});
var w = new W();
var fn = w.proxy('exec');
fn(42);
assert.ok(w.executed, "should execute the named method in the right context");
assert.strictEqual(w.executed, 42, "should be passed the proxy's arguments");
w.destroy();
});
QUnit.test('proxy (String), include', function (assert) {
assert.expect(1);
// the proxy function should handle methods being changed on the class
// and should always proxy "by name", to the most recent one
var W = Widget.extend({
exec: function () {
this.executed = 1;
}
});
var w = new W();
var fn = w.proxy('exec');
W.include({
exec: function () { this.executed = 2; }
});
fn();
assert.strictEqual(w.executed, 2, "should be lazily resolved");
w.destroy();
});
QUnit.test('proxy (Function)', function (assert) {
assert.expect(1);
var w = new (Widget.extend({ }))();
var fn = w.proxy(function () { this.executed = true; });
fn();
assert.ok(w.executed, "should set the function's context (like Function#bind)");
w.destroy();
});
QUnit.test('proxy (Function)(*args)', function (assert) {
assert.expect(1);
var w = new (Widget.extend({ }))();
var fn = w.proxy(function (arg) { this.executed = arg; });
fn(42);
assert.strictEqual(w.executed, 42, "should be passed the proxy's arguments");
w.destroy();
});
QUnit.test('renderElement, no template, default', function (assert) {
assert.expect(7);
var widget = new (Widget.extend({ }))();
assert.strictEqual(widget.$el, undefined, "should not have a root element");
widget.renderElement();
assert.ok(widget.$el, "should have generated a root element");
assert.strictEqual(widget.$el, widget.$el, "should provide $el alias");
assert.ok(widget.$el.is(widget.el), "should provide raw DOM alias");
assert.strictEqual(widget.el.nodeName, 'DIV', "should have generated the default element");
assert.strictEqual(widget.el.attributes.length, 0, "should not have generated any attribute");
assert.ok(_.isEmpty(widget.$el.html(), "should not have generated any content"));
widget.destroy();
});
QUnit.test('no template, custom tag', function (assert) {
assert.expect(1);
var widget = new (Widget.extend({
tagName: 'ul'
}))();
widget.renderElement();
assert.strictEqual(widget.el.nodeName, 'UL', "should have generated the custom element tag");
widget.destroy();
});
QUnit.test('no template, @id', function (assert) {
assert.expect(3);
var widget = new (Widget.extend({
id: 'foo'
}))();
widget.renderElement();
assert.strictEqual(widget.el.attributes.length, 1, "should have one attribute");
assert.hasAttrValue(widget.$el, 'id', 'foo', "should have generated the id attribute");
assert.strictEqual(widget.el.id, 'foo', "should also be available via property");
widget.destroy();
});
QUnit.test('no template, @className', function (assert) {
assert.expect(2);
var widget = new (Widget.extend({
className: 'oe_some_class'
}))();
widget.renderElement();
assert.strictEqual(widget.el.className, 'oe_some_class', "should have the right property");
assert.hasAttrValue(widget.$el, 'class', 'oe_some_class', "should have the right attribute");
widget.destroy();
});
QUnit.test('no template, bunch of attributes', function (assert) {
assert.expect(9);
var widget = new (Widget.extend({
attributes: {
'id': 'some_id',
'class': 'some_class',
'data-foo': 'data attribute',
'clark': 'gable',
'spoiler': // don't read the next line if you care about Harry Potter...
'snape kills dumbledore'
}
}))();
widget.renderElement();
assert.strictEqual(widget.el.attributes.length, 5, "should have all the specified attributes");
assert.strictEqual(widget.el.id, 'some_id');
assert.hasAttrValue(widget.$el, 'id', 'some_id');
assert.strictEqual(widget.el.className, 'some_class');
assert.hasAttrValue(widget.$el, 'class', 'some_class');
assert.hasAttrValue(widget.$el, 'data-foo', 'data attribute');
assert.strictEqual(widget.$el.data('foo'), 'data attribute');
assert.hasAttrValue(widget.$el, 'clark', 'gable');
assert.hasAttrValue(widget.$el, 'spoiler', 'snape kills dumbledore');
widget.destroy();
});
QUnit.test('template', function (assert) {
assert.expect(3);
core.qweb.add_template(
'<no>' +
'<t t-name="test.widget.template">' +
'<ol>' +
'<li t-foreach="5" t-as="counter" ' +
't-attf-class="class-#{counter}">' +
'<input/>' +
'<t t-esc="counter"/>' +
'</li>' +
'</ol>' +
'</t>' +
'</no>'
);
var widget = new (Widget.extend({
template: 'test.widget.template'
}))();
widget.renderElement();
assert.strictEqual(widget.el.nodeName, 'OL');
assert.strictEqual(widget.$el.children().length, 5);
assert.strictEqual(widget.el.textContent, '01234');
widget.destroy();
});
QUnit.test('repeated', async function (assert) {
assert.expect(4);
var $fix = $( "#qunit-fixture");
core.qweb.add_template(
'<no>' +
'<t t-name="test.widget.template">' +
'<p><t t-esc="widget.value"/></p>' +
'</t>' +
'</no>'
);
var widget = new (Widget.extend({
template: 'test.widget.template'
}))();
widget.value = 42;
await widget.appendTo($fix)
.then(function () {
assert.strictEqual($fix.find('p').text(), '42', "DOM fixture should contain initial value");
assert.strictEqual(widget.$el.text(), '42', "should set initial value");
widget.value = 36;
widget.renderElement();
assert.strictEqual($fix.find('p').text(), '36', "DOM fixture should use new value");
assert.strictEqual(widget.$el.text(), '36', "should set new value");
});
widget.destroy();
});
QUnit.module('Widgets, with QWeb', {
beforeEach: function() {
this.oldQWeb = core.qweb;
core.qweb = new QWeb();
core.qweb.add_template(
'<no>' +
'<t t-name="test.widget.template">' +
'<ol>' +
'<li t-foreach="5" t-as="counter" ' +
't-attf-class="class-#{counter}">' +
'<input/>' +
'<t t-esc="counter"/>' +
'</li>' +
'</ol>' +
'</t>' +
'</no>'
);
},
afterEach: function() {
core.qweb = this.oldQWeb;
},
});
QUnit.test('basic-alias', function (assert) {
assert.expect(1);
var widget = new (Widget.extend({
template: 'test.widget.template'
}))();
widget.renderElement();
assert.ok(widget.$('li:eq(3)').is(widget.$el.find('li:eq(3)')),
"should do the same thing as calling find on the widget root");
widget.destroy();
});
QUnit.test('delegate', async function (assert) {
assert.expect(5);
var a = [];
var widget = new (Widget.extend({
template: 'test.widget.template',
events: {
'click': function () {
a[0] = true;
assert.strictEqual(this, widget, "should trigger events in widget");
},
'click li.class-3': 'class3',
'change input': function () { a[2] = true; }
},
class3: function () { a[1] = true; }
}))();
widget.renderElement();
await testUtils.dom.click(widget.$el, {allowInvisible: true});
await testUtils.dom.click(widget.$('li:eq(3)'), {allowInvisible: true});
await testUtils.fields.editAndTrigger(widget.$('input:last'), 'foo', 'change');
for(var i=0; i<3; ++i) {
assert.ok(a[i], "should pass test " + i);
}
widget.destroy();
});
QUnit.test('undelegate', async function (assert) {
assert.expect(4);
var clicked = false;
var newclicked = false;
var widget = new (Widget.extend({
template: 'test.widget.template',
events: { 'click li': function () { clicked = true; } }
}))();
widget.renderElement();
widget.$el.on('click', 'li', function () { newclicked = true; });
await testUtils.dom.clickFirst(widget.$('li'), {allowInvisible: true});
assert.ok(clicked, "should trigger bound events");
assert.ok(newclicked, "should trigger bound events");
clicked = newclicked = false;
widget._undelegateEvents();
await testUtils.dom.clickFirst(widget.$('li'), {allowInvisible: true});
assert.ok(!clicked, "undelegate should unbind events delegated");
assert.ok(newclicked, "undelegate should only unbind events it created");
widget.destroy();
});
QUnit.module('Widget, and async stuff');
QUnit.test("alive(alive)", async function (assert) {
assert.expect(1);
var widget = new (Widget.extend({}));
await widget.start()
.then(function () {return widget.alive(Promise.resolve()) ;})
.then(function () { assert.ok(true); });
widget.destroy();
});
QUnit.test("alive(dead)", function (assert) {
assert.expect(1);
var widget = new (Widget.extend({}));
return new Promise(function (resolve, reject) {
widget.start()
.then(function () {
// destroy widget
widget.destroy();
var promise = Promise.resolve();
// leave time for alive() to do its stuff
promise.then(function () {
return Promise.resolve();
}).then(function () {
assert.ok(true);
resolve();
});
// ensure that widget.alive() refuses to resolve or reject
return widget.alive(promise);
}).then(function () {
reject();
assert.ok(false, "alive() should not terminate by default");
}).catch(function() {
reject();
assert.ok(false, "alive() should not terminate by default");
});
});
});
QUnit.test("alive(alive, true)", async function (assert) {
assert.expect(1);
var widget = new (Widget.extend({}));
await widget.start()
.then(function () { return widget.alive(Promise.resolve(), true); })
.then(function () { assert.ok(true); });
widget.destroy();
});
QUnit.test("alive(dead, true)", function (assert) {
assert.expect(1);
var done = assert.async();
var widget = new (Widget.extend({}));
widget.start()
.then(function () {
// destroy widget
widget.destroy();
return widget.alive(Promise.resolve(), true);
}).then(function () {
assert.ok(false, "alive(p, true) should fail its promise");
done();
}, function () {
assert.ok(true, "alive(p, true) should fail its promise");
done();
});
});
QUnit.test("calling _rpc on destroyed widgets", async function (assert) {
assert.expect(3);
var def;
var parent = new Widget();
await testUtils.mock.addMockEnvironment(parent, {
session: {
rpc: function () {
def = testUtils.makeTestPromise();
def.abort = def.reject;
return def;
},
},
services: {
ajax: AjaxService
},
});
var widget = new Widget(parent);
widget._rpc({route: '/a/route'}).then(function () {
assert.ok(true, "The ajax call should be resolve");
});
def.resolve();
await testUtils.nextMicrotaskTick();
def = null;
widget._rpc({route: '/a/route'}).then(function () {
throw Error("Calling _rpc on a destroyed widget should return a " +
"promise that remains pending forever");
}).catch(function () {
throw Error("Calling _rpc on a destroyed widget should return a " +
"promise that remains pending forever");
});
widget.destroy();
def.resolve();
await testUtils.nextMicrotaskTick();
def = null;
widget._rpc({route: '/a/route'}).then(function () {
throw Error("Calling _rpc on a destroyed widget should return a " +
"promise that remains pending forever");
}).catch(function () {
throw Error("Calling _rpc on a destroyed widget should return a " +
"promise that remains pending forever");
});
assert.ok(!def, "trigger_up is not performed and the call returns a " +
"promise that remains pending forever");
assert.ok(true,
"there should be no crash when calling _rpc on a destroyed widget");
parent.destroy();
});
QUnit.test("calling do_hide on a widget destroyed before being rendered", async function (assert) {
assert.expect(1);
const MyWidget = Widget.extend({
willStart() {
return new Promise(() => {});
}
});
const widget = new MyWidget();
widget.appendTo(document.createDocumentFragment());
widget.destroy();
// those calls should not crash
widget.do_hide();
widget.do_show();
widget.do_toggle(true);
assert.ok(true);
});
QUnit.test('start is not called when widget is destroyed', function (assert) {
assert.expect(0);
const $fix = $("#qunit-fixture");
// Note: willStart is always async
const MyWidget = Widget.extend({
start: function () {
assert.ok(false, 'Should not call start method');
},
});
const widget = new MyWidget();
widget.appendTo($fix);
widget.destroy();
const divEl = document.createElement('div');
$fix[0].appendChild(divEl);
const widget2 = new MyWidget();
widget2.attachTo(divEl);
widget2.destroy();
});
QUnit.test("don't destroy twice widget's children", function (assert) {
assert.expect(2);
var parent = new Widget();
var child = new (Widget.extend({
destroy: function () {
assert.step('destroy');
}
}))(parent);
parent.destroy();
assert.verifySteps(['destroy'], "child should have been detroyed only once");
});
QUnit.module('Widgets, Dialog');
QUnit.test("don't close dialog on backdrop click", async function (assert) {
assert.expect(3);
var dialog = new Dialog(null);
dialog.open();
await dialog.opened();
assert.strictEqual($('.modal.show').length, 1, "a dialog should have opened");
var $backdrop = $('.modal-backdrop');
assert.strictEqual($backdrop.length, 1, "the dialog should have a modal backdrop");
testUtils.dom.click('.modal.show'); // Click on backdrop is in fact a direct click on the .modal element
assert.strictEqual($('.modal.show').length, 1, "the dialog should still be opened");
dialog.close();
});
});
});
|