1 openerp.testing.section('list.events', {
2 dependencies: ['web.list']
4 var create = function (o) {
5 if (typeof Object.create === 'function') {
6 return Object.create(o);
12 test('Simple event triggering', function (instance) {
13 var e = create(instance.web.list.Events), passed = false;
14 e.bind('foo', function () { passed = true; });
18 test('Bind all', function (instance) {
19 var e = create(instance.web.list.Events), event = null;
20 e.bind(null, function (ev) { event = ev; });
22 strictEqual(event, 'foo');
24 test('Propagate trigger params', function (instance) {
25 var e = create(instance.web.list.Events), p = false;
26 e.bind(null, function (_, param) { p = param; });
27 e.trigger('foo', true);
30 test('Bind multiple callbacks', function (instance) {
31 var e = create(instance.web.list.Events), count;
32 e.bind('foo', function () { count++; })
33 .bind('bar', function () { count++; })
34 .bind(null, function () { count++; })
35 .bind('foo', function () { count++; })
36 .bind(null, function () { count++; })
37 .bind(null, function () { count++; });
41 strictEqual(count, 5);
45 strictEqual(count, 4);
49 strictEqual(count, 3);
51 test('Mixin events', function (instance) {
52 var cls = instance.web.Class.extend({
53 method: function () { this.trigger('e'); }
55 cls.include(instance.web.list.Events);
56 var i = new cls(), triggered = false;
58 i.bind('e', function () { triggered = true; });
63 test('Unbind all handlers', function (instance) {
64 var e = create(instance.web.list.Events), passed = 0;
65 e.bind('foo', function () { passed++; });
67 strictEqual(passed, 1);
70 strictEqual(passed, 1);
72 test('Unbind one handler', function (instance) {
73 var e = create(instance.web.list.Events), p1 = 0, p2 = 0,
74 h1 = function () { p1++; }, h2 = function () { p2++; };
86 openerp.testing.section('list.records', {
87 dependencies: ['web.list']
89 test('Basic record initialization', function (instance) {
90 var r = new instance.web.list.Record({qux: 3});
93 strictEqual(r.get('foo'), 1);
94 strictEqual(r.get('bar'), 2);
95 strictEqual(r.get('qux'), 3);
97 test('Change all the things', function (instance) {
98 var r = new instance.web.list.Record(), changed = false, field;
99 r.bind('change', function () { changed = true; });
100 r.bind(null, function (e) { field = field || e.split(':')[1]; });
102 strictEqual(r.get('foo'), 1);
104 strictEqual(field, 'foo');
106 test('Change single field', function (instance) {
107 var r = new instance.web.list.Record(), changed = 0;
108 r.bind('change:foo', function () { changed++; });
111 strictEqual(r.get('foo'), 1);
112 strictEqual(r.get('bar'), 1);
113 strictEqual(changed, 1);
116 openerp.testing.section('list.collections', {
117 dependencies: ['web.list']
119 test('degenerate-fetch', function (instance) {
120 var c = new instance.web.list.Collection();
121 strictEqual(c.length, 0);
122 c.add({id: 1, value: 2});
123 c.add({id: 2, value: 3});
124 c.add({id: 3, value: 5});
125 c.add({id: 4, value: 7});
126 strictEqual(c.length, 4);
127 var r = c.at(2), r2 = c.get(1);
129 ok(r instanceof instance.web.list.Record);
130 strictEqual(r.get('id'), 3);
131 strictEqual(r.get('value'), 5);
133 ok(r2 instanceof instance.web.list.Record);
134 strictEqual(r2.get('id'), 1);
135 strictEqual(r2.get('value'), 2);
137 test('degenerate-indexed-add', function (instance) {
138 var c = new instance.web.list.Collection([
143 strictEqual(c.at(1).get('value'), 10);
144 equal(c.at(3), undefined);
145 c.add({id:4, value: 55}, {at: 1});
146 strictEqual(c.at(1).get('value'), 55);
147 strictEqual(c.at(3).get('value'), 20);
149 test('degenerate-remove', function (instance) {
150 var c = new instance.web.list.Collection([
155 var record = c.get(2);
156 strictEqual(c.length, 3);
158 strictEqual(c.length, 2);
159 equal(c.get(2), undefined);
160 strictEqual(c.at(1).get('value'), 20);
162 test('degenerate-remove-bound', function (instance) {
164 c = new instance.web.list.Collection([ {id: 1, value: 5} ]);
165 c.bind('change', function () { changed = true; });
166 var record = c.get(1);
168 record.set('value', 42);
169 ok(!changed, 'removed records should not trigger events in their ' +
170 'parent collection');
172 test('degenerate-reset', function (instance) {
173 var event, obj, c = new instance.web.list.Collection([
178 c.bind(null, function (e, instance) { event = e; obj = instance; });
180 strictEqual(c.length, 0);
181 strictEqual(event, 'reset');
188 c.reset([{id: 42, value: 55}]);
189 strictEqual(c.length, 1);
190 strictEqual(c.get(42).get('value'), 55);
192 test('degenerate-reset-bound', function (instance) {
194 c = new instance.web.list.Collection([ {id: 1, value: 5} ]);
195 c.bind('change', function () { changed = true; });
196 var record = c.get(1);
198 record.set('value', 42);
199 ok(!changed, 'removed records should not trigger events in their ' +
200 'parent collection');
203 test('degenerate-propagations', function (instance) {
205 var c = new instance.web.list.Collection([
210 c.bind('change:value', function (e, record, value) {
213 c.get(1).set('value', 6);
214 c.get(2).set('value', 11);
215 c.get(3).set('value', 21);
216 deepEqual(values, [6, 11, 21]);
218 test('BTree', function (instance) {
219 var root = new instance.web.list.Collection(),
220 c = root.proxy('admin'),
222 c.add({id: 1, name: "Administrator", login: 'admin'});
223 c.add({id: 3, name: "Demo", login: 'demo'});
224 root.bind('change:wealth', function () {
225 total = (root.get(1).get('wealth') || 0) + (root.get(3).get('wealth') || 0);
228 strictEqual(total, 0);
229 c.at(0).set('wealth', 42);
230 strictEqual(total, 42);
231 c.at(1).set('wealth', 5);
232 strictEqual(total, 47);
234 test('degenerate-successor', function (instance) {
235 var root = new instance.web.list.Collection([
243 deepEqual(root.succ(root.at(2)).attributes,
244 root.at(3).attributes,
245 "should return the record at (index + 1) from the pivot");
246 equal(root.succ(root.at(4)), null,
247 "should return null as successor to last record");
248 deepEqual(root.succ(root.at(4), {wraparound: true}).attributes,
249 root.at(0).attributes,
250 "should return index 0 as successor to last record if" +
251 " wraparound is set");
252 deepEqual(root.succ(root.at(2), {wraparound: true}).attributes,
253 root.at(3).attributes,
254 "wraparound should have no effect if not succ(last_record)");
256 test('successor', function (instance) {
257 var root = new instance.web.list.Collection();
258 root.proxy('first').add([{id: 1, value: 1}, {id: 2, value: 2}]);
259 root.proxy('second').add([{id: 3, value: 3}, {id: 4, value: 5}]);
260 root.proxy('third').add([{id: 5, value: 8}, {id: 6, value: 13}]);
262 deepEqual(root.succ(root.get(3)).attributes,
263 root.get(4).attributes,
264 "should get successor");
265 equal(root.succ(root.get(4)),
267 "successors do not cross collections");
268 deepEqual(root.succ(root.get(4), {wraparound: true}).attributes,
269 root.get(3).attributes,
270 "should wraparound within a collection");
272 test('degenerate-predecessor', function (instance) {
273 var root = new instance.web.list.Collection([
281 deepEqual(root.pred(root.at(2)).attributes,
282 root.at(1).attributes,
283 "should return the record at (index - 1) from the pivot");
284 equal(root.pred(root.at(0)), null,
285 "should return null as predecessor to first record");
286 deepEqual(root.pred(root.at(0), {wraparound: true}).attributes,
287 root.at(4).attributes,
288 "should return last record as predecessor to first record" +
289 " if wraparound is set");
290 deepEqual(root.pred(root.at(1), {wraparound: true}).attributes,
291 root.at(0).attributes,
292 "wraparound should have no effect if not pred(first_record)");
294 test('predecessor', function (instance) {
295 var root = new instance.web.list.Collection();
296 root.proxy('first').add([{id: 1, value: 1}, {id: 2, value: 2}]);
297 root.proxy('second').add([{id: 3, value: 3}, {id: 4, value: 5}]);
298 root.proxy('third').add([{id: 5, value: 8}, {id: 6, value: 13}]);
300 deepEqual(root.pred(root.get(4)).attributes,
301 root.get(3).attributes,
302 "should get predecessor");
303 equal(root.pred(root.get(3)),
305 "predecessor do not cross collections");
306 deepEqual(root.pred(root.get(3), {wraparound: true}).attributes,
307 root.get(4).attributes,
308 "should wraparound within a collection");
311 openerp.testing.section('list.collections.hom', {
312 dependencies: ['web.list']
314 test('each, degenerate', function (instance) {
315 var c = new instance.web.list.Collection([
320 c.each(function (record) {
321 ids.push(record.get('id'));
325 'degenerate collections should be iterated in record order');
327 test('each, deep', function (instance) {
328 var root = new instance.web.list.Collection(),
330 root.proxy('foo').add([
333 {id: 3, value: 20}]);
334 root.proxy('bar').add([
337 {id: 30, value: 20}]);
338 root.each(function (record) {
339 ids.push(record.get('id'));
341 // No contract on sub-collection iteration order (for now anyway)
342 ids.sort(function (a, b) { return a - b; });
344 ids, [1, 2, 3, 10, 20, 30],
345 'tree collections should be deeply iterated');
347 test('map, degenerate', function (instance) {
348 var c = new instance.web.list.Collection([
353 var ids = c.map(function (record) {
354 return record.get('id');
358 'degenerate collections should be iterated in record order');
360 test('map, deep', function (instance) {
361 var root = new instance.web.list.Collection();
362 root.proxy('foo').add([
365 {id: 3, value: 20}]);
366 root.proxy('bar').add([
369 {id: 30, value: 20}]);
370 var ids = root.map(function (record) {
371 return record.get('id');
373 // No contract on sub-collection iteration order (for now anyway)
374 ids.sort(function (a, b) { return a - b; });
376 ids, [1, 2, 3, 10, 20, 30],
377 'tree collections should be deeply iterated');
380 openerp.testing.section('list.collection.weirdoes', {
381 dependencies: ['web.list']
383 test('set-from-noid', function (instance) {
384 var root = new instance.web.list.Collection();
386 root.at(0).set('id', 42);
387 var record = root.get(42);
388 equal(root.length, 1);
389 equal(record.get('v'), 3, "should have fetched the original record");
391 test('set-from-previd', function (instance) {
392 var root = new instance.web.list.Collection();
393 root.add({id: 1, v: 2});
394 root.get(1).set('id', 42);
395 var record = root.get(42);
396 equal(root.length, 1);
397 equal(record.get('v'), 2, "should have fetched the original record");