Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / GardeningServer / lib / test / update-util-tests.html
1 <!--
2 Copyright 2014 The Chromium Authors. All rights reserved.
3 Use of this source code is governed by a BSD-style license that can be
4 found in the LICENSE file.
5 -->
6
7 <link rel="import" href="../update-util.html">
8
9 <script>
10
11 (function () {
12 'use strict';
13
14 var assert = chai.assert;
15
16 describe('update-util', function() {
17   describe('.updateLeft', function() {
18     it('updates an array', function() {
19       var x = {key: 'x', val: 0};
20       var y1 = {key: 'y', val: 1};
21       var y2 = {key: 'y', val: 2};
22       var z = {key: 'z', val: 3};
23       var target = [x, y1];
24       var source = [y2, z];
25       target = updateUtil.updateLeft(target, source);
26       assert.strictEqual(target[0], y1);
27       assert.strictEqual(target[1], z);
28       assert.equal(target[0].val, 2);
29     });
30
31     it('updates and reorders an array', function() {
32       var x = {key: 'x', val: 0};
33       var y1 = {key: 'y', val: 1};
34       var y2 = {key: 'y', val: 2};
35       var z = {key: 'z', val: 3};
36       var target = [x, y1];
37       var source = [z, y2];
38       target = updateUtil.updateLeft(target, source);
39       assert.strictEqual(target[0], z);
40       assert.strictEqual(target[1], y1);
41       assert.equal(target[1].val, 2);
42     });
43
44     it('updates an array to empty', function() {
45       var x = {key: 'x', val: 0};
46       var target = [x];
47       var source = [];
48       assert.deepEqual(updateUtil.updateLeft(target, source), []);
49
50       // Try without a key.
51       var x = {val: 0};
52       var target = [x];
53       var source = [];
54       assert.deepEqual(updateUtil.updateLeft(target, source), []);
55     });
56
57
58     it('calls custom updateLeft members', function() {
59       var y1 = {key: 'y', val: 1,
60                 updateLeft: function(right) {
61                   this.val += right.val;
62                   return this;
63                 }};
64       var y2 = {key: 'y', val: 2};
65       var target = [y1];
66       var source = [y2];
67       target = updateUtil.updateLeft(target, source);
68       assert.strictEqual(target[0], y1);
69       assert.propertyVal(target[0], 'val', 3);
70       assert.property(target[0], 'updateLeft');
71     });
72
73     it('calls custom updateLeft members in custom types', function() {
74       function Updatable(val) {
75         this.key = 'x';
76         this.val = val;
77       };
78       Updatable.prototype.updateLeft = function(right) {
79         this.val += right.val;
80         return this;
81       };
82
83       var y1 = new Updatable(1);
84       var y2 = new Updatable(2);
85       var target = [y1];
86       var source = [y2];
87       target = updateUtil.updateLeft(target, source);
88       assert.strictEqual(target[0], y1);
89       assert.propertyVal(target[0], 'val', 3);
90       assert.property(target[0], 'updateLeft');
91     });
92
93     it('skips transient properties', function() {
94       function Updatable(val, visible) {
95         this.val = val;
96         if (visible !== undefined)
97           this.visible = visible
98       };
99       Updatable.transientProperties = ['visible'];
100
101       var target = new Updatable(1, true);
102       var source = new Updatable(2, false);
103       assert.strictEqual(updateUtil.updateLeft(target, source), target);
104       assert.propertyVal(target, 'val', 2);
105       assert.propertyVal(target, 'visible', true);
106
107       var target = new Updatable(1, undefined);
108       var source = new Updatable(2, true);
109       updateUtil.updateLeft(target, source);
110       assert.notProperty(target, 'visible');
111
112       var target = new Updatable(1, true);
113       var source = new Updatable(2, undefined);
114       updateUtil.updateLeft(target, source);
115       assert.propertyVal(target, 'visible', true);
116     });
117
118     it('updates array containing null', function() {
119       var target = [{key: 'x', a: {b:1}}];
120       var source = [{key: 'x', a: null}];
121       target = updateUtil.updateLeft(target, source);
122       assert.propertyVal(target[0], 'a', null);
123
124       var target = [{key: 'x', a: null}];
125       var source = [{key: 'x', a: {b:1}}];
126       target = updateUtil.updateLeft(target, source);
127       assert.deepEqual(target[0].a, {b:1});
128     });
129
130     it('updates object containing null', function() {
131       var target = {x: {a: 1}};
132       var source = {x: null};
133       assert.strictEqual(updateUtil.updateLeft(target, source), target);
134       assert.propertyVal(target, 'x', null);
135
136       var target = {x: null};
137       var source = {x: {a: 1}};
138       assert.strictEqual(updateUtil.updateLeft(target, source), target);
139       assert.deepEqual(target.x, {a: 1});
140     });
141
142     it('returns source on badly typed arguments', function() {
143       assert.strictEqual(updateUtil.updateLeft(1, 2), 2);
144       var source = [2];
145       assert.strictEqual(updateUtil.updateLeft([1], source), source);
146     });
147   });
148 });
149
150 })();
151
152 </script>