3 var ropenerp = window.openerp;
5 var openerp = ropenerp.declare($, _, QWeb2);
7 ropenerp.testing.section('class', {
8 dependencies: ['web.corelib']
10 test('Basic class creation', function () {
11 var C = openerp.web.Class.extend({
20 strictEqual(i.foo(), 3);
22 test('Class initialization', function () {
23 var C1 = openerp.web.Class.extend({
28 var C2 = openerp.web.Class.extend({
29 init: function (arg) {
37 strictEqual(i1.foo, 3);
38 strictEqual(i2.foo, 42);
40 test('Inheritance', function () {
41 var C0 = openerp.web.Class.extend({
48 return 1 + this._super();
53 return 1 + this._super();
57 strictEqual(new C0().foo(), 1);
58 strictEqual(new C1().foo(), 2);
59 strictEqual(new C2().foo(), 3);
61 test('In-place extension', function () {
62 var C0 = openerp.web.Class.extend({
76 return 2 + this._super();
82 strictEqual(new C0().bar, 5);
83 strictEqual(new C0().baz, 5);
84 strictEqual(new C0().foo(), 5);
85 strictEqual(new C0().qux(), 5);
87 test('In-place extension and inheritance', function () {
88 var C0 = openerp.web.Class.extend({
89 foo: function () { return 1; },
90 bar: function () { return 1; }
93 foo: function () { return 1 + this._super(); }
95 strictEqual(new C1().foo(), 2);
96 strictEqual(new C1().bar(), 1);
99 foo: function () { return 2 + this._super(); },
100 bar: function () { return 1 + this._super(); }
102 strictEqual(new C1().foo(), 4);
103 strictEqual(new C1().bar(), 2);
105 test('In-place extensions alter existing instances', function () {
106 var C0 = openerp.web.Class.extend({
107 foo: function () { return 1; },
108 bar: function () { return 1; }
111 strictEqual(i.foo(), 1);
112 strictEqual(i.bar(), 1);
115 foo: function () { return 2; },
116 bar: function () { return 2 + this._super(); }
118 strictEqual(i.foo(), 2);
119 strictEqual(i.bar(), 3);
121 test('In-place extension of subclassed types', function () {
122 var C0 = openerp.web.Class.extend({
123 foo: function () { return 1; },
124 bar: function () { return 1; }
127 foo: function () { return 1 + this._super(); },
128 bar: function () { return 1 + this._super(); }
131 strictEqual(i.foo(), 2);
133 foo: function () { return 2; },
134 bar: function () { return 2 + this._super(); }
136 strictEqual(i.foo(), 3);
137 strictEqual(i.bar(), 4);
142 ropenerp.testing.section('Widget.proxy', {
144 test('(String)', function () {
145 var W = openerp.web.Widget.extend({
147 this.executed = true;
151 var fn = w.proxy('exec');
153 ok(w.executed, 'should execute the named method in the right context');
155 test('(String)(*args)', function () {
156 var W = openerp.web.Widget.extend({
157 exec: function (arg) {
162 var fn = w.proxy('exec');
164 ok(w.executed, "should execute the named method in the right context");
165 equal(w.executed, 42, "should be passed the proxy's arguments");
167 test('(String), include', function () {
168 // the proxy function should handle methods being changed on the class
169 // and should always proxy "by name", to the most recent one
170 var W = openerp.web.Widget.extend({
176 var fn = w.proxy('exec');
178 exec: function () { this.executed = 2; }
182 equal(w.executed, 2, "should be lazily resolved");
185 test('(Function)', function () {
186 var w = new (openerp.web.Widget.extend({ }))();
188 var fn = w.proxy(function () { this.executed = true; });
190 ok(w.executed, "should set the function's context (like Function#bind)");
192 test('(Function)(*args)', function () {
193 var w = new (openerp.web.Widget.extend({ }))();
195 var fn = w.proxy(function (arg) { this.executed = arg; });
197 equal(w.executed, 42, "should be passed the proxy's arguments");
200 ropenerp.testing.section('Widget.renderElement', {
202 openerp.web.qweb = new QWeb2.Engine();
203 openerp.web.qweb.add_template(
205 '<t t-name="test.widget.template">' +
207 '<li t-foreach="5" t-as="counter" ' +
208 't-attf-class="class-#{counter}">' +
210 '<t t-esc="counter"/>' +
214 '<t t-name="test.widget.template-value">' +
215 '<p><t t-esc="widget.value"/></p>' +
220 test('no template, default', function () {
221 var w = new (openerp.web.Widget.extend({ }))();
223 var $original = w.$el;
224 ok($original, "should initially have a root element");
226 ok(w.$el, "should have generated a root element");
227 ok($original !== w.$el, "should have generated a new root element");
228 strictEqual(w.$el, w.$el, "should provide $el alias");
229 ok(w.$el.is(w.el), "should provide raw DOM alias");
231 equal(w.el.nodeName, 'DIV', "should have generated the default element");
232 equal(w.el.attributes.length, 0, "should not have generated any attribute");
233 ok(_.isEmpty(w.$el.html(), "should not have generated any content"));
235 test('no template, custom tag', function () {
236 var w = new (openerp.web.Widget.extend({
241 equal(w.el.nodeName, 'UL', "should have generated the custom element tag");
243 test('no template, @id', function () {
244 var w = new (openerp.web.Widget.extend({
249 equal(w.el.attributes.length, 1, "should have one attribute");
250 equal(w.$el.attr('id'), 'foo', "should have generated the id attribute");
251 equal(w.el.id, 'foo', "should also be available via property");
253 test('no template, @className', function () {
254 var w = new (openerp.web.Widget.extend({
255 className: 'oe_some_class'
259 equal(w.el.className, 'oe_some_class', "should have the right property");
260 equal(w.$el.attr('class'), 'oe_some_class', "should have the right attribute");
262 test('no template, bunch of attributes', function () {
263 var w = new (openerp.web.Widget.extend({
266 'class': 'some_class',
267 'data-foo': 'data attribute',
269 'spoiler': 'snape kills dumbledore'
274 equal(w.el.attributes.length, 5, "should have all the specified attributes");
276 equal(w.el.id, 'some_id');
277 equal(w.$el.attr('id'), 'some_id');
279 equal(w.el.className, 'some_class');
280 equal(w.$el.attr('class'), 'some_class');
282 equal(w.$el.attr('data-foo'), 'data attribute');
283 equal(w.$el.data('foo'), 'data attribute');
285 equal(w.$el.attr('clark'), 'gable');
286 equal(w.$el.attr('spoiler'), 'snape kills dumbledore');
289 test('template', function () {
290 var w = new (openerp.web.Widget.extend({
291 template: 'test.widget.template'
295 equal(w.el.nodeName, 'OL');
296 equal(w.$el.children().length, 5);
297 equal(w.el.textContent, '01234');
299 test('repeated', { asserts: 4 }, function (_unused, $fix) {
300 var w = new (openerp.web.Widget.extend({
301 template: 'test.widget.template-value'
304 return w.appendTo($fix)
306 equal($fix.find('p').text(), '42', "DOM fixture should contain initial value");
307 equal(w.$el.text(), '42', "should set initial value");
310 equal($fix.find('p').text(), '36', "DOM fixture should use new value");
311 equal(w.$el.text(), '36', "should set new value");
315 ropenerp.testing.section('Widget.$', {
317 openerp.web.qweb = new QWeb2.Engine();
318 openerp.web.qweb.add_template(
320 '<t t-name="test.widget.template">' +
322 '<li t-foreach="5" t-as="counter" ' +
323 't-attf-class="class-#{counter}">' +
325 '<t t-esc="counter"/>' +
332 test('basic-alias', function () {
333 var w = new (openerp.web.Widget.extend({
334 template: 'test.widget.template'
338 ok(w.$('li:eq(3)').is(w.$el.find('li:eq(3)')),
339 "should do the same thing as calling find on the widget root");
342 ropenerp.testing.section('Widget.events', {
344 openerp.web.qweb = new QWeb2.Engine();
345 openerp.web.qweb.add_template(
347 '<t t-name="test.widget.template">' +
349 '<li t-foreach="5" t-as="counter" ' +
350 't-attf-class="class-#{counter}">' +
352 '<t t-esc="counter"/>' +
359 test('delegate', function () {
361 var w = new (openerp.web.Widget.extend({
362 template: 'test.widget.template',
364 'click': function () {
366 strictEqual(this, w, "should trigger events in widget");
368 'click li.class-3': 'class3',
369 'change input': function () { a[2] = true; }
371 class3: function () { a[1] = true; }
376 w.$('li:eq(3)').click();
377 w.$('input:last').val('foo').change();
379 for(var i=0; i<3; ++i) {
380 ok(a[i], "should pass test " + i);
383 test('undelegate', function () {
384 var clicked = false, newclicked = false;
385 var w = new (openerp.web.Widget.extend({
386 template: 'test.widget.template',
387 events: { 'click li': function () { clicked = true; } }
390 w.$el.on('click', 'li', function () { newclicked = true; });
393 ok(clicked, "should trigger bound events");
394 ok(newclicked, "should trigger bound events");
395 clicked = newclicked = false;
397 w.undelegateEvents();
399 ok(!clicked, "undelegate should unbind events delegated");
400 ok(newclicked, "undelegate should only unbind events it created");