PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/qooxdoo/framework/source/class/qx/test/ui/selection/AbstractMultiSelectonTest.js

https://github.com/Wkasel/qooxdoo
JavaScript | 250 lines | 171 code | 35 blank | 44 comment | 1 complexity | 8d7df7dc8a4b0f9e9e2b50406557974c MD5 | raw file
  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2007-2008 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Christian Hagendorn (chris_schmidt)
  12. ************************************************************************ */
  13. qx.Class.define("qx.test.ui.selection.AbstractMultiSelectonTest",
  14. {
  15. extend : qx.test.ui.selection.AbstractSingleSelectonTest,
  16. type : "abstract",
  17. members :
  18. {
  19. testSetSelectionWithTooMuchElements : function()
  20. {
  21. // Do nothing
  22. },
  23. // overridden
  24. testSetSelection : function()
  25. {
  26. this.base(arguments);
  27. // Sets up the new selection
  28. var selection = [];
  29. selection[0] = this._notInSelection[0];
  30. selection[1] = this._notInSelection[1];
  31. selection[2] = this._notInSelection[2];
  32. selection[3] = this._notInSelection[3];
  33. selection[4] = this._notInSelection[4];
  34. this._testSetSelection(selection);
  35. this.flush();
  36. },
  37. testSetSelectionOverrideWithLess : function()
  38. {
  39. // Sets up the new selection
  40. this._selection = [];
  41. this._selection[0] = this._notInSelection[0];
  42. this._selection[1] = this._notInSelection[1];
  43. this._selection[2] = this._notInSelection[2];
  44. this._selection[3] = this._notInSelection[3];
  45. this._selection[4] = this._notInSelection[4];
  46. this._widget.setSelection(this._selection);
  47. this.flush();
  48. // Test setSelection() with the same elements, but less
  49. var expected = [this._selection[0], this._selection[2],
  50. this._selection[4]];
  51. this._widget.setSelection(expected);
  52. // Tests the result from "getSelection"
  53. var result = this._widget.getSelection();
  54. this._assertArrayEquals(expected, result, "Selection is wrong");
  55. // Test setSelection(), with one element from the selection before
  56. this._widget.setSelection(this._selection);
  57. this._widget.setSelection([this._selection[0]]);
  58. result = this._widget.getSelection();
  59. expected = [this._selection[0]];
  60. this._assertArrayEquals(expected, result, "Selection is wrong");
  61. this.flush();
  62. },
  63. testSelectAll : function()
  64. {
  65. // Resets the selection to compare the results.
  66. this._widget.resetSelection();
  67. // Tests event and select all items
  68. var widget = this._widget;
  69. var that = this;
  70. this.assertEventFired(widget, "changeSelection", function () {
  71. widget.selectAll();
  72. that.flush();
  73. }, function(event) {
  74. // Tests the result from the event
  75. that._assertArrayEquals(that._getChildren(), event.getData(),
  76. "Selection is wrong!");
  77. }, "'changeSelection' event not fired!");
  78. // A second selectAll() shouldn't fire an event
  79. this.assertEventNotFired(widget, "changeSelection", function () {
  80. widget.selectAll();
  81. }, function(event) {}, "'changeSelection' event fired!");
  82. // Tests the result from "getSelection"
  83. this._selection = this._widget.getSelection();
  84. this._assertArrayEquals(this._getChildren(), this._selection,
  85. "Selection is wrong!");
  86. },
  87. testAddToSelection : function()
  88. {
  89. // Sets up a new item for selection
  90. var newValue = this._notInSelection[0];
  91. this._selection[this._selection.length] = newValue;
  92. // Tests event and adds item to the selection
  93. var widget = this._widget;
  94. var that = this;
  95. this.assertEventFired(widget, "changeSelection", function () {
  96. widget.addToSelection(newValue);
  97. that.flush();
  98. }, function(event) {
  99. // Tests the result from the event
  100. that._assertArrayEquals(that._selection, event.getData(),
  101. "The result of the selection is wrong");
  102. }, "'changeSelection' event not fired!");
  103. // A second selection shouldn't fire an event
  104. this.assertEventNotFired(widget, "changeSelection", function () {
  105. widget.addToSelection(newValue);
  106. }, function(event) {}, "'changeSelection' event fired!");
  107. // Tests the result from "getSelection"
  108. this._assertArrayEquals(this._selection, this._widget.getSelection(),
  109. "Selection is wrong");
  110. },
  111. testAddToSelectionWithNotChildElement : function()
  112. {
  113. var that = this;
  114. var testElement = this._createTestElement("Test Element");
  115. this.assertException(function() {
  116. that._widget.addToSelection(testElement);
  117. }, Error, null, "No error occurs by trying to add an element" +
  118. " to the selection which isn't a child element!");
  119. testElement.destroy();
  120. },
  121. testRemoveFromSelection : function()
  122. {
  123. // Sets up the item to remove and update the selection array
  124. var selection = this._selection
  125. var itemToRemove = selection[selection.length - 1];
  126. delete selection[selection.length - 1];
  127. selection.length = selection.length - 1;
  128. // Tests event and removes the item
  129. var widget = this._widget;
  130. var that = this;
  131. this.assertEventFired(widget, "changeSelection", function () {
  132. widget.removeFromSelection(itemToRemove);
  133. that.flush();
  134. }, function(event) {
  135. // Tests the result from the event
  136. that._assertArrayEquals(that._selection, event.getData(),
  137. "The result of the selection is wrong");
  138. }, "'changeSelection' event not fired!");
  139. // Tests the result from "getSelection"
  140. this._assertArrayEquals(this._selection, this._widget.getSelection(),
  141. "Selection is wrong");
  142. },
  143. testRemoveFromSelectionWithNotChildElement : function()
  144. {
  145. var that = this;
  146. var testElement = this._createTestElement("Test Element");
  147. this.assertException(function() {
  148. that._widget.removeFromSelection(testElement);
  149. }, Error, null, "No error occurs by trying to remove an element" +
  150. " which isn't a child element!");
  151. testElement.destroy();
  152. },
  153. testRemoveFromSelectionWithNotSelectedElement : function()
  154. {
  155. var itemToRemove = this._notInSelection[0];
  156. var widget = this._widget;
  157. this.assertEventNotFired(widget, "changeSelection", function () {
  158. widget.removeFromSelection(itemToRemove);
  159. }, function(event) {}, "'changeSelection' event fired!");
  160. },
  161. testInvertSelection : function()
  162. {
  163. var that = this;
  164. var widget = this._widget;
  165. this.assertEventFired(widget, "changeSelection", function () {
  166. widget.invertSelection();
  167. that.flush();
  168. }, function(event) {
  169. // Tests the result from the event
  170. that._assertArrayEquals(that._notInSelection, event.getData(),
  171. "The result of the selection is wrong");
  172. }, "'changeSelection' event not fired!");
  173. // Tests the result from "getSelection"
  174. this._assertArrayEquals(this._notInSelection, this._widget.getSelection(),
  175. "Selection is wrong");
  176. },
  177. testInvertSelectionWithErrors : function()
  178. {
  179. var widget = this._widget;
  180. widget.setSelectionMode("single");
  181. this.assertException(function() {
  182. widget.invertSelection();
  183. }, Error, null, "No error occurs by trying to invert elements" +
  184. " in 'single' selection mode!");
  185. },
  186. testInvertSelectionWithDisabledChildElements : function()
  187. {
  188. // test setup
  189. var tempNotInSelection = [];
  190. for (var i = 0; i < this._notInSelection.length; i++) {
  191. tempNotInSelection.push(this._notInSelection[i]);
  192. }
  193. this._notInSelection = tempNotInSelection;
  194. var that = this;
  195. var widget = this._widget;
  196. this.assertEventFired(widget, "changeSelection", function () {
  197. widget.invertSelection();
  198. that.flush();
  199. }, function(event) {
  200. // Tests the result from the event
  201. that._assertArrayEquals(that._notInSelection, event.getData(),
  202. "The result of the selection is wrong");
  203. }, "'changeSelection' event not fired!");
  204. // Tests the result from "getSelection"
  205. this._assertArrayEquals(this._notInSelection, this._widget.getSelection(),
  206. "Selection is wrong");
  207. }
  208. }
  209. });