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));
15 var m
= new cr
.ui
.ArrayDataModel([0, 1, 2]);
18 m
.addEventListener('splice', function(e
) {
20 assertEquals(3, e
.index
);
21 assertArrayEquals([], e
.removed
);
22 assertArrayEquals([3, 4], e
.added
);
25 assertEquals(5, m
.push(3, 4));
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());
55 m
.addEventListener('permuted', function(event
) {
56 permutation
= event
.permutation
;
58 m
.splice
.apply(m
, spliceArgs
);
60 for (var i
= 0; i
< sourceArray
.length
; i
++) {
61 if (permutation
[i
] == -1)
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
);
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;
91 m
.addEventListener('permuted', function(event
) {
92 permutation
= event
.permutation
;
94 m
.addEventListener('change', function(event
) {
95 changeIndex
= event
.index
;
98 assertEquals(null, permutation
);
99 assertEquals(1, changeIndex
);