Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / webui / array_data_model_test.html
blob0b41f8535d365cb1cb459acc0c2463d9087940dc
1 <!DOCTYPE html>
2 <html>
3 <body>
5 <script>
7 function testSlice() {
8 var m = new cr.ui.ArrayDataModel([0, 1, 2]);
9 assertArrayEquals([0, 1, 2], m.slice());
10 assertArrayEquals([1, 2], m.slice(1));
11 assertArrayEquals([1], m.slice(1, 2));
14 function testPush() {
15 var m = new cr.ui.ArrayDataModel([0, 1, 2]);
17 var count = 0;
18 m.addEventListener('splice', function(e) {
19 count++;
20 assertEquals(3, e.index);
21 assertArrayEquals([], e.removed);
22 assertArrayEquals([3, 4], e.added);
23 });
25 assertEquals(5, m.push(3, 4));
26 var a = m.slice();
27 assertArrayEquals([0, 1, 2, 3, 4], a);
29 assertEquals(1, count, 'The splice event should only fire once');
32 function testSplice() {
33 function compare(array, args) {
34 var m = new cr.ui.ArrayDataModel(array.slice());
35 var expected = array.slice();
36 var result = expected.splice.apply(expected, args);
37 assertArrayEquals(result, m.splice.apply(m, args));
38 assertArrayEquals(expected, m.slice());
41 compare([1, 2, 3], []);
42 compare([1, 2, 3], [0, 0]);
43 compare([1, 2, 3], [0, 1]);
44 compare([1, 2, 3], [1, 1]);
45 compare([1, 2, 3], [0, 3]);
46 compare([1, 2, 3], [0, 1, 5]);
47 compare([1, 2, 3], [0, 3, 1, 2, 3]);
48 compare([1, 2, 3], [5, 3, 1, 2, 3]);
51 function testPermutation() {
52 function doTest(sourceArray, spliceArgs) {
53 var m = new cr.ui.ArrayDataModel(sourceArray.slice());
54 var permutation;
55 m.addEventListener('permuted', function(event) {
56 permutation = event.permutation;
57 });
58 m.splice.apply(m, spliceArgs);
59 var deleted = 0;
60 for (var i = 0; i < sourceArray.length; i++) {
61 if (permutation[i] == -1)
62 deleted++;
63 else
64 assertEquals(sourceArray[i], m.item(permutation[i]));
66 assertEquals(deleted, spliceArgs[1]);
69 doTest([1, 2, 3], [0, 0]);
70 doTest([1, 2, 3], [0, 1]);
71 doTest([1, 2, 3], [1, 1]);
72 doTest([1, 2, 3], [0, 3]);
73 doTest([1, 2, 3], [0, 1, 5]);
74 doTest([1, 2, 3], [0, 3, 1, 2, 3]);
77 function testUpdateIndexes() {
78 var m = new cr.ui.ArrayDataModel([1, 2, 3]);
79 var changedIndexes = [];
80 m.addEventListener('change', function(event) {
81 changedIndexes.push(event.index);
82 });
83 m.updateIndexes([0, 1, 2]);
84 assertArrayEquals([0, 1, 2], changedIndexes);
87 function testReplaceItem() {
88 var m = new cr.ui.ArrayDataModel([1, 2, 3]);
89 var permutation = null;
90 var changeIndex;
91 m.addEventListener('permuted', function(event) {
92 permutation = event.permutation;
93 });
94 m.addEventListener('change', function(event) {
95 changeIndex = event.index;
96 });
97 m.replaceItem(2, 4);
98 assertEquals(null, permutation);
99 assertEquals(1, changeIndex);
102 </script>
104 </body>
105 </html>