Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / test / data / webui / array_data_model_test.html
blob22614e660230a88f31ef75a0d0f992356b139a87
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 </script>
79 </body>
80 </html>