dragdropgroup_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2010 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.fx.DragDropGroupTest');
  15. goog.setTestOnly('goog.fx.DragDropGroupTest');
  16. goog.require('goog.events');
  17. goog.require('goog.fx.DragDropGroup');
  18. goog.require('goog.testing.jsunit');
  19. var s1;
  20. var s2;
  21. var t1;
  22. var t2;
  23. var source = null;
  24. var target = null;
  25. function setUpPage() {
  26. s1 = document.getElementById('s1');
  27. s2 = document.getElementById('s2');
  28. t1 = document.getElementById('t1');
  29. t2 = document.getElementById('t2');
  30. }
  31. function setUp() {
  32. source = new goog.fx.DragDropGroup();
  33. source.setSourceClass('ss');
  34. source.setTargetClass('st');
  35. target = new goog.fx.DragDropGroup();
  36. target.setSourceClass('ts');
  37. target.setTargetClass('tt');
  38. source.addTarget(target);
  39. }
  40. function tearDown() {
  41. source.removeItems();
  42. target.removeItems();
  43. }
  44. function addElementsToGroups() {
  45. source.addItem(s1);
  46. source.addItem(s2);
  47. target.addItem(t1);
  48. target.addItem(t2);
  49. }
  50. function testAddItemsBeforeInit() {
  51. addElementsToGroups();
  52. source.init();
  53. target.init();
  54. assertEquals(2, source.items_.length);
  55. assertEquals(2, target.items_.length);
  56. assertEquals('s ss', s1.className);
  57. assertEquals('s ss', s2.className);
  58. assertEquals('t tt', t1.className);
  59. assertEquals('t tt', t2.className);
  60. assertTrue(goog.events.hasListener(s1));
  61. assertTrue(goog.events.hasListener(s2));
  62. assertFalse(goog.events.hasListener(t1));
  63. assertFalse(goog.events.hasListener(t2));
  64. }
  65. function testAddItemsAfterInit() {
  66. source.init();
  67. target.init();
  68. addElementsToGroups();
  69. assertEquals(2, source.items_.length);
  70. assertEquals(2, target.items_.length);
  71. assertEquals('s ss', s1.className);
  72. assertEquals('s ss', s2.className);
  73. assertEquals('t tt', t1.className);
  74. assertEquals('t tt', t2.className);
  75. assertTrue(goog.events.hasListener(s1));
  76. assertTrue(goog.events.hasListener(s2));
  77. assertFalse(goog.events.hasListener(t1));
  78. assertFalse(goog.events.hasListener(t2));
  79. }
  80. function testRemoveItems() {
  81. source.init();
  82. target.init();
  83. addElementsToGroups();
  84. assertEquals(2, source.items_.length);
  85. assertEquals(s1, source.items_[0].element);
  86. assertEquals(s2, source.items_[1].element);
  87. assertEquals('s ss', s1.className);
  88. assertEquals('s ss', s2.className);
  89. assertTrue(goog.events.hasListener(s1));
  90. assertTrue(goog.events.hasListener(s2));
  91. source.removeItems();
  92. assertEquals(0, source.items_.length);
  93. assertEquals('s', s1.className);
  94. assertEquals('s', s2.className);
  95. assertFalse(goog.events.hasListener(s1));
  96. assertFalse(goog.events.hasListener(s2));
  97. }
  98. function testRemoveSourceItem1() {
  99. source.init();
  100. target.init();
  101. addElementsToGroups();
  102. assertEquals(2, source.items_.length);
  103. assertEquals(s1, source.items_[0].element);
  104. assertEquals(s2, source.items_[1].element);
  105. assertEquals('s ss', s1.className);
  106. assertEquals('s ss', s2.className);
  107. assertTrue(goog.events.hasListener(s1));
  108. assertTrue(goog.events.hasListener(s2));
  109. source.removeItem(s1);
  110. assertEquals(1, source.items_.length);
  111. assertEquals(s2, source.items_[0].element);
  112. assertEquals('s', s1.className);
  113. assertEquals('s ss', s2.className);
  114. assertFalse(goog.events.hasListener(s1));
  115. assertTrue(goog.events.hasListener(s2));
  116. }
  117. function testRemoveSourceItem2() {
  118. source.init();
  119. target.init();
  120. addElementsToGroups();
  121. assertEquals(2, source.items_.length);
  122. assertEquals(s1, source.items_[0].element);
  123. assertEquals(s2, source.items_[1].element);
  124. assertEquals('s ss', s1.className);
  125. assertEquals('s ss', s2.className);
  126. assertTrue(goog.events.hasListener(s1));
  127. assertTrue(goog.events.hasListener(s2));
  128. source.removeItem(s2);
  129. assertEquals(1, source.items_.length);
  130. assertEquals(s1, source.items_[0].element);
  131. assertEquals('s ss', s1.className);
  132. assertEquals('s', s2.className);
  133. assertTrue(goog.events.hasListener(s1));
  134. assertFalse(goog.events.hasListener(s2));
  135. }
  136. function testRemoveTargetItem1() {
  137. source.init();
  138. target.init();
  139. addElementsToGroups();
  140. assertEquals(2, target.items_.length);
  141. assertEquals(t1, target.items_[0].element);
  142. assertEquals(t2, target.items_[1].element);
  143. assertEquals('t tt', t1.className);
  144. assertEquals('t tt', t2.className);
  145. assertFalse(goog.events.hasListener(t1));
  146. assertFalse(goog.events.hasListener(t2));
  147. target.removeItem(t1);
  148. assertEquals(1, target.items_.length);
  149. assertEquals(t2, target.items_[0].element);
  150. assertEquals('t', t1.className);
  151. assertEquals('t tt', t2.className);
  152. assertFalse(goog.events.hasListener(t1));
  153. assertFalse(goog.events.hasListener(t2));
  154. }
  155. function testRemoveTargetItem2() {
  156. source.init();
  157. target.init();
  158. addElementsToGroups();
  159. assertEquals(2, target.items_.length);
  160. assertEquals(t1, target.items_[0].element);
  161. assertEquals(t2, target.items_[1].element);
  162. assertEquals('t tt', t1.className);
  163. assertEquals('t tt', t2.className);
  164. assertFalse(goog.events.hasListener(t1));
  165. assertFalse(goog.events.hasListener(t2));
  166. target.removeItem(t2);
  167. assertEquals(1, target.items_.length);
  168. assertEquals(t1, target.items_[0].element);
  169. assertEquals('t tt', t1.className);
  170. assertEquals('t', t2.className);
  171. assertFalse(goog.events.hasListener(t1));
  172. assertFalse(goog.events.hasListener(t2));
  173. }