4 <script src=
"list_selection_model_test_util.js"></script>
9 function createSelectionModel(len
, opt_dependentLeadItem
) {
10 var sm
= new cr
.ui
.ListSelectionModel(len
);
11 sm
.independentLeadItem_
= !opt_dependentLeadItem
;
15 function testAdjust1() {
16 var sm
= createSelectionModel(200);
18 sm
.leadIndex
= sm
.anchorIndex
= sm
.selectedIndex
= 100;
21 assertEquals(90, sm
.leadIndex
);
22 assertEquals(90, sm
.anchorIndex
);
23 assertEquals(90, sm
.selectedIndex
);
26 function testAdjust2() {
27 var sm
= createSelectionModel(200);
29 sm
.leadIndex
= sm
.anchorIndex
= sm
.selectedIndex
= 50;
30 adjust(sm
, 60, 10, 0);
32 assertEquals(50, sm
.leadIndex
);
33 assertEquals(50, sm
.anchorIndex
);
34 assertEquals(50, sm
.selectedIndex
);
37 function testAdjust3() {
38 var sm
= createSelectionModel(200);
40 sm
.leadIndex
= sm
.anchorIndex
= sm
.selectedIndex
= 100;
43 assertEquals(110, sm
.leadIndex
);
44 assertEquals(110, sm
.anchorIndex
);
45 assertEquals(110, sm
.selectedIndex
);
48 function testAdjust4() {
49 var sm
= createSelectionModel(200);
51 sm
.leadIndex
= sm
.anchorIndex
= 100;
52 sm
.selectRange(100, 110);
56 assertEquals(95, sm
.leadIndex
);
57 assertEquals(95, sm
.anchorIndex
);
58 assertArrayEquals(range(95, 105), sm
.selectedIndexes
);
61 function testAdjust5() {
62 var sm
= createSelectionModel(100);
64 sm
.leadIndex
= sm
.anchorIndex
= sm
.selectedIndex
= 99;
68 assertEquals(98, sm
.leadIndex
, 'lead');
69 assertEquals(98, sm
.anchorIndex
, 'anchor');
70 assertArrayEquals([98], sm
.selectedIndexes
);
73 function testAdjust6() {
74 var sm
= createSelectionModel(200);
76 sm
.leadIndex
= sm
.anchorIndex
= 105;
77 sm
.selectRange(100, 110);
80 adjust(sm
, 100, 5, 0);
82 assertEquals(100, sm
.leadIndex
, 'lead');
83 assertEquals(100, sm
.anchorIndex
, 'anchor');
84 assertArrayEquals(range(100, 105), sm
.selectedIndexes
);
87 function testAdjust7() {
88 var sm
= createSelectionModel(1);
90 sm
.leadIndex
= sm
.anchorIndex
= sm
.selectedIndex
= 0;
94 assertEquals(10, sm
.leadIndex
, 'lead');
95 assertEquals(10, sm
.anchorIndex
, 'anchor');
96 assertArrayEquals([10], sm
.selectedIndexes
);
99 function testAdjust8() {
100 var sm
= createSelectionModel(100);
102 sm
.leadIndex
= sm
.anchorIndex
= 50;
105 adjust(sm
, 10, 80, 0);
107 assertEquals(-1, sm
.leadIndex
, 'lead');
108 assertEquals(-1, sm
.anchorIndex
, 'anchor');
109 assertArrayEquals(range(0, 19), sm
.selectedIndexes
);
112 function testAdjust9() {
113 var sm
= createSelectionModel(10);
115 sm
.leadIndex
= sm
.anchorIndex
= 5;
119 adjust(sm
, 0, 10, 0);
121 assertEquals(-1, sm
.leadIndex
, 'lead');
122 assertEquals(-1, sm
.anchorIndex
, 'anchor');
123 assertArrayEquals([], sm
.selectedIndexes
);
126 function testAdjust10() {
127 var sm
= createSelectionModel(10);
129 sm
.leadIndex
= sm
.anchorIndex
= 5;
132 adjust(sm
, 0, 10, 20);
134 assertEquals(5, sm
.leadIndex
, 'lead');
135 assertEquals(5, sm
.anchorIndex
, 'anchor');
136 assertArrayEquals([5], sm
.selectedIndexes
);
139 function testAdjust11() {
140 var sm
= createSelectionModel(20);
142 sm
.leadIndex
= sm
.anchorIndex
= 10;
145 adjust(sm
, 5, 20, 10);
147 assertEquals(-1, sm
.leadIndex
, 'lead');
148 assertEquals(-1, sm
.anchorIndex
, 'anchor');
149 assertArrayEquals(range(0, 4), sm
.selectedIndexes
);
152 function testAdjust12() {
153 var sm
= createSelectionModel(20, true);
156 sm
.leadIndex
= sm
.anchorIndex
= 10;
158 adjust(sm
, 5, 20, 10);
160 assertEquals(-1, sm
.leadIndex
, 'lead');
161 assertEquals(-1, sm
.anchorIndex
, 'anchor');
162 assertArrayEquals(range(0, 4), sm
.selectedIndexes
);
165 function testAdjust13() {
166 var sm
= createSelectionModel(20, true);
169 sm
.leadIndex
= sm
.anchorIndex
= 15;
173 assertEquals(10, sm
.leadIndex
, 'lead');
174 assertEquals(10, sm
.anchorIndex
, 'anchor');
175 assertArrayEquals(range(0, 14), sm
.selectedIndexes
);
178 function testLeadAndAnchor1() {
179 var sm
= createSelectionModel(20, true);
182 sm
.leadIndex
= sm
.anchorIndex
= 10;
184 assertEquals(10, sm
.leadIndex
, 'lead');
185 assertEquals(10, sm
.anchorIndex
, 'anchor');
188 function testLeadAndAnchor2() {
189 var sm
= createSelectionModel(20, true);
191 sm
.leadIndex
= sm
.anchorIndex
= 10;
194 assertEquals(-1, sm
.leadIndex
, 'lead');
195 assertEquals(-1, sm
.anchorIndex
, 'anchor');
198 function testSelectAll() {
199 var sm
= createSelectionModel(10);
202 sm
.addEventListener('change', function(e
) {
208 assertArrayEquals(range(0, 9), sm
.selectedIndexes
);
209 assertArrayEquals(range(0, 9),
210 changes
.map(function(change
) { return change
.index
; }));
213 function testSelectAllOnEmptyList() {
214 var sm
= createSelectionModel(0);
217 sm
.addEventListener('change', function(e
) {
223 assertArrayEquals([], sm
.selectedIndexes
);
224 assertEquals(null, changes
);