selectionmodel_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. goog.provide('goog.ui.SelectionModelTest');
  15. goog.setTestOnly('goog.ui.SelectionModelTest');
  16. goog.require('goog.array');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.testing.recordFunction');
  19. goog.require('goog.ui.SelectionModel');
  20. var selectionModel, items, addedItem, addedItems;
  21. function setUp() {
  22. items = [1, 2, 3, 4];
  23. addedItem = 5;
  24. addedItems = [6, 7, 8];
  25. selectionModel = new goog.ui.SelectionModel(items);
  26. }
  27. function tearDown() {
  28. goog.dispose(selectionModel);
  29. }
  30. /*
  31. * Checks that the selection model returns the correct item count.
  32. */
  33. function testGetItemCount() {
  34. assertEquals(items.length, selectionModel.getItemCount());
  35. }
  36. /*
  37. * Checks that the correct first element is returned by the selection model.
  38. */
  39. function testGetFirst() {
  40. assertEquals(items[0], selectionModel.getFirst());
  41. }
  42. /*
  43. * Checks that the correct last element is returned by the selection model.
  44. */
  45. function testGetLast() {
  46. assertEquals(items[items.length - 1], selectionModel.getLast());
  47. }
  48. /*
  49. * Tests the behavior of goog.ui.SelectionModel.getItemAt(index).
  50. */
  51. function testGetItemAt() {
  52. goog.array.forEach(items, function(item, i) {
  53. assertEquals(item, selectionModel.getItemAt(i));
  54. });
  55. }
  56. /*
  57. * Checks that an item can be correctly added to the selection model.
  58. */
  59. function testAddItem() {
  60. assertEquals(items.length, selectionModel.getItemCount());
  61. selectionModel.addItem(addedItem);
  62. assertEquals(items.length + 1, selectionModel.getItemCount());
  63. assertEquals(addedItem, selectionModel.getLast());
  64. }
  65. /*
  66. * Checks that an item can be added to the selection model at a specific index.
  67. */
  68. function testAddItemAt() {
  69. assertEquals(items.length, selectionModel.getItemCount());
  70. var insertIndex = 2;
  71. assertEquals(items[insertIndex], selectionModel.getItemAt(insertIndex));
  72. selectionModel.addItemAt(addedItem, insertIndex);
  73. var resultArray = goog.array.clone(items);
  74. goog.array.insertAt(resultArray, addedItem, insertIndex);
  75. assertEquals(items.length + 1, selectionModel.getItemCount());
  76. assertEquals(addedItem, selectionModel.getItemAt(insertIndex));
  77. assertArrayEquals(resultArray, selectionModel.getItems());
  78. }
  79. /*
  80. * Checks that multiple items can be correctly added to the selection model.
  81. */
  82. function testAddItems() {
  83. assertEquals(items.length, selectionModel.getItemCount());
  84. selectionModel.addItems(addedItems);
  85. assertEquals(items.length + addedItems.length, selectionModel.getItemCount());
  86. var resultArray = goog.array.concat(items, addedItems);
  87. assertArrayEquals(resultArray, selectionModel.getItems());
  88. }
  89. /*
  90. * Checks that all elements can be removed from the selection model.
  91. */
  92. function testClear() {
  93. assertArrayEquals(items, selectionModel.getItems());
  94. selectionModel.clear();
  95. assertArrayEquals([], selectionModel.getItems());
  96. }
  97. /*
  98. * Checks that all items can be obtained from the selection model.
  99. */
  100. function testGetItems() {
  101. assertArrayEquals(items, selectionModel.getItems());
  102. }
  103. /*
  104. * Checks that an item's index can be found in the selection model.
  105. */
  106. function testIndexOfItem() {
  107. goog.array.forEach(items, function(item, i) {
  108. assertEquals(i, selectionModel.indexOfItem(item));
  109. });
  110. }
  111. /*
  112. * Checks that an item can be removed from the selection model.
  113. */
  114. function testRemoveItem() {
  115. assertEquals(items.length, selectionModel.getItemCount());
  116. var resultArray = goog.array.clone(items);
  117. goog.array.removeAt(resultArray, 2);
  118. selectionModel.removeItem(items[2]);
  119. assertEquals(items.length - 1, selectionModel.getItemCount());
  120. assertArrayEquals(resultArray, selectionModel.getItems());
  121. }
  122. /*
  123. * Checks that an item at a particular index can be removed from the selection
  124. * model.
  125. */
  126. function testRemoveItemAt() {
  127. assertEquals(items.length, selectionModel.getItemCount());
  128. var resultArray = goog.array.clone(items);
  129. var removeIndex = 2;
  130. goog.array.removeAt(resultArray, removeIndex);
  131. selectionModel.removeItemAt(removeIndex);
  132. assertEquals(items.length - 1, selectionModel.getItemCount());
  133. assertNotEquals(items[removeIndex], selectionModel.getItemAt(removeIndex));
  134. assertArrayEquals(resultArray, selectionModel.getItems());
  135. }
  136. /*
  137. * Checks that item selection at a particular index works.
  138. */
  139. function testSelectedIndex() {
  140. // Default selected index is -1
  141. assertEquals(-1, selectionModel.getSelectedIndex());
  142. selectionModel.setSelectedIndex(2);
  143. assertEquals(2, selectionModel.getSelectedIndex());
  144. assertEquals(items[2], selectionModel.getSelectedItem());
  145. }
  146. /*
  147. * Checks that items can be selected in the selection model.
  148. */
  149. function testSelectedItem() {
  150. assertNull(selectionModel.getSelectedItem());
  151. selectionModel.setSelectedItem(items[1]);
  152. assertNotNull(selectionModel.getSelectedItem());
  153. assertEquals(items[1], selectionModel.getSelectedItem());
  154. assertEquals(1, selectionModel.getSelectedIndex());
  155. }
  156. /*
  157. * Checks that an installed handler is called on selection change.
  158. */
  159. function testSelectionHandler() {
  160. var myRecordFunction = new goog.testing.recordFunction();
  161. selectionModel.setSelectionHandler(myRecordFunction);
  162. // Select index 2
  163. selectionModel.setSelectedIndex(2);
  164. // De-select 2 and select 3
  165. selectionModel.setSelectedIndex(3);
  166. var recordCalls = myRecordFunction.getCalls();
  167. assertEquals(3, recordCalls.length);
  168. // Calls: Select items[2], de-select items[2], select items[3]
  169. assertArrayEquals([items[2], true], recordCalls[0].getArguments());
  170. assertArrayEquals([items[2], false], recordCalls[1].getArguments());
  171. assertArrayEquals([items[3], true], recordCalls[2].getArguments());
  172. }