Debian Codegraph @

closure-library/closure/goog/ui/selectionmodel_test.js

raw
1 // Copyright 2011 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.ui.SelectionModelTest');
16 goog.setTestOnly('goog.ui.SelectionModelTest');
17
18 goog.require('goog.array');
19 goog.require('goog.testing.jsunit');
20 goog.require('goog.testing.recordFunction');
21 goog.require('goog.ui.SelectionModel');
22
23 var selectionModel, items, addedItem, addedItems;
24
25 function setUp() {
26 items = [1, 2, 3, 4];
27 addedItem = 5;
28 addedItems = [6, 7, 8];
29 selectionModel = new goog.ui.SelectionModel(items);
30 }
31
32 function tearDown() {
33 goog.dispose(selectionModel);
34 }
35
36 /*
37 * Checks that the selection model returns the correct item count.
38 */
39 function testGetItemCount() {
40 assertEquals(items.length, selectionModel.getItemCount());
41 }
42
43 /*
44 * Checks that the correct first element is returned by the selection model.
45 */
46 function testGetFirst() {
47 assertEquals(items[0], selectionModel.getFirst());
48 }
49
50 /*
51 * Checks that the correct last element is returned by the selection model.
52 */
53 function testGetLast() {
54 assertEquals(items[items.length - 1], selectionModel.getLast());
55 }
56
57 /*
58 * Tests the behavior of goog.ui.SelectionModel.getItemAt(index).
59 */
60 function testGetItemAt() {
61 goog.array.forEach(items, function(item, i) {
62 assertEquals(item, selectionModel.getItemAt(i));
63 });
64 }
65
66 /*
67 * Checks that an item can be correctly added to the selection model.
68 */
69 function testAddItem() {
70 assertEquals(items.length, selectionModel.getItemCount());
71
72 selectionModel.addItem(addedItem);
73
74 assertEquals(items.length + 1, selectionModel.getItemCount());
75 assertEquals(addedItem, selectionModel.getLast());
76 }
77
78 /*
79 * Checks that an item can be added to the selection model at a specific index.
80 */
81 function testAddItemAt() {
82 assertEquals(items.length, selectionModel.getItemCount());
83
84 var insertIndex = 2;
85 assertEquals(items[insertIndex], selectionModel.getItemAt(insertIndex));
86
87 selectionModel.addItemAt(addedItem, insertIndex);
88
89 var resultArray = goog.array.clone(items);
90 goog.array.insertAt(resultArray, addedItem, insertIndex);
91
92 assertEquals(items.length + 1, selectionModel.getItemCount());
93 assertEquals(addedItem, selectionModel.getItemAt(insertIndex));
94 assertArrayEquals(resultArray, selectionModel.getItems());
95 }
96
97 /*
98 * Checks that multiple items can be correctly added to the selection model.
99 */
100 function testAddItems() {
101 assertEquals(items.length, selectionModel.getItemCount());
102
103 selectionModel.addItems(addedItems);
104
105 assertEquals(items.length + addedItems.length, selectionModel.getItemCount());
106
107 var resultArray = goog.array.concat(items, addedItems);
108 assertArrayEquals(resultArray, selectionModel.getItems());
109 }
110
111 /*
112 * Checks that all elements can be removed from the selection model.
113 */
114 function testClear() {
115 assertArrayEquals(items, selectionModel.getItems());
116
117 selectionModel.clear();
118
119 assertArrayEquals([], selectionModel.getItems());
120 }
121
122 /*
123 * Checks that all items can be obtained from the selection model.
124 */
125 function testGetItems() {
126 assertArrayEquals(items, selectionModel.getItems());
127 }
128
129 /*
130 * Checks that an item's index can be found in the selection model.
131 */
132 function testIndexOfItem() {
133 goog.array.forEach(items, function(item, i) {
134 assertEquals(i, selectionModel.indexOfItem(item));
135 });
136 }
137
138 /*
139 * Checks that an item can be removed from the selection model.
140 */
141 function testRemoveItem() {
142 assertEquals(items.length, selectionModel.getItemCount());
143
144 var resultArray = goog.array.clone(items);
145 goog.array.removeAt(resultArray, 2);
146
147 selectionModel.removeItem(items[2]);
148
149 assertEquals(items.length - 1, selectionModel.getItemCount());
150 assertArrayEquals(resultArray, selectionModel.getItems());
151 }
152
153 /*
154 * Checks that an item at a particular index can be removed from the selection
155 * model.
156 */
157 function testRemoveItemAt() {
158 assertEquals(items.length, selectionModel.getItemCount());
159
160 var resultArray = goog.array.clone(items);
161 var removeIndex = 2;
162
163 goog.array.removeAt(resultArray, removeIndex);
164
165 selectionModel.removeItemAt(removeIndex);
166
167 assertEquals(items.length - 1, selectionModel.getItemCount());
168 assertNotEquals(items[removeIndex], selectionModel.getItemAt(removeIndex));
169 assertArrayEquals(resultArray, selectionModel.getItems());
170 }
171
172 /*
173 * Checks that item selection at a particular index works.
174 */
175 function testSelectedIndex() {
176 // Default selected index is -1
177 assertEquals(-1, selectionModel.getSelectedIndex());
178
179 selectionModel.setSelectedIndex(2);
180
181 assertEquals(2, selectionModel.getSelectedIndex());
182 assertEquals(items[2], selectionModel.getSelectedItem());
183 }
184
185 /*
186 * Checks that items can be selected in the selection model.
187 */
188 function testSelectedItem() {
189 assertNull(selectionModel.getSelectedItem());
190
191 selectionModel.setSelectedItem(items[1]);
192
193 assertNotNull(selectionModel.getSelectedItem());
194 assertEquals(items[1], selectionModel.getSelectedItem());
195 assertEquals(1, selectionModel.getSelectedIndex());
196 }
197
198 /*
199 * Checks that an installed handler is called on selection change.
200 */
201 function testSelectionHandler() {
202 var myRecordFunction = new goog.testing.recordFunction();
203
204 selectionModel.setSelectionHandler(myRecordFunction);
205
206 // Select index 2
207 selectionModel.setSelectedIndex(2);
208 // De-select 2 and select 3
209 selectionModel.setSelectedIndex(3);
210
211 var recordCalls = myRecordFunction.getCalls();
212
213 assertEquals(3, recordCalls.length);
214 // Calls: Select items[2], de-select items[2], select items[3]
215 assertArrayEquals([items[2], true], recordCalls[0].getArguments());
216 assertArrayEquals([items[2], false], recordCalls[1].getArguments());
217 assertArrayEquals([items[3], true], recordCalls[2].getArguments());
218 }