dragscrollsupport_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Copyright 2008 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.DragScrollSupportTest');
  15. goog.setTestOnly('goog.fx.DragScrollSupportTest');
  16. goog.require('goog.fx.DragScrollSupport');
  17. goog.require('goog.math.Coordinate');
  18. goog.require('goog.math.Rect');
  19. goog.require('goog.testing.MockClock');
  20. goog.require('goog.testing.events');
  21. goog.require('goog.testing.jsunit');
  22. var vContainerDiv;
  23. var vContentDiv;
  24. var hContainerDiv;
  25. var hContentDiv;
  26. var clock;
  27. function setUpPage() {
  28. vContainerDiv = document.getElementById('vContainerDiv');
  29. vContentDiv = document.getElementById('vContentDiv');
  30. hContainerDiv = document.getElementById('hContainerDiv');
  31. hContentDiv = document.getElementById('hContentDiv');
  32. }
  33. function setUp() {
  34. clock = new goog.testing.MockClock(true);
  35. }
  36. function tearDown() {
  37. clock.dispose();
  38. }
  39. function testDragZeroMarginDivVContainer() {
  40. var dsc = new goog.fx.DragScrollSupport(vContainerDiv);
  41. // Set initial scroll state.
  42. var scrollTop = 50;
  43. vContainerDiv.scrollTop = scrollTop;
  44. goog.testing.events.fireMouseMoveEvent(
  45. vContainerDiv, new goog.math.Coordinate(50, 20 + 50));
  46. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  47. assertEquals(
  48. 'Mousing inside the vContainer should not trigger scrolling.', scrollTop,
  49. vContainerDiv.scrollTop);
  50. assertEquals('Scroll timer should not tick yet', 0, clock.getTimeoutsMade());
  51. scrollTop = vContainerDiv.scrollTop;
  52. goog.testing.events.fireMouseMoveEvent(
  53. vContainerDiv, new goog.math.Coordinate(50, 10));
  54. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  55. assertTrue(
  56. 'Mousing above the vContainer should trigger scrolling up.',
  57. scrollTop > vContainerDiv.scrollTop);
  58. scrollTop = vContainerDiv.scrollTop;
  59. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  60. assertTrue(
  61. 'Mousing above the vContainer should trigger scrolling up.',
  62. scrollTop > vContainerDiv.scrollTop);
  63. scrollTop = vContainerDiv.scrollTop;
  64. goog.testing.events.fireMouseMoveEvent(
  65. vContainerDiv, new goog.math.Coordinate(50, 20 + 110));
  66. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  67. assertTrue(
  68. 'Mousing below the vContainer should trigger scrolling down.',
  69. scrollTop < vContainerDiv.scrollTop);
  70. scrollTop = vContainerDiv.scrollTop;
  71. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  72. assertTrue(
  73. 'Mousing below the vContainer should trigger scrolling down.',
  74. scrollTop < vContainerDiv.scrollTop);
  75. scrollTop = vContainerDiv.scrollTop;
  76. goog.testing.events.fireMouseMoveEvent(
  77. vContainerDiv, new goog.math.Coordinate(50, 20 + 50));
  78. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  79. assertEquals(
  80. 'Mousing inside the vContainer should stop scrolling.', scrollTop,
  81. vContainerDiv.scrollTop);
  82. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  83. dsc.dispose();
  84. }
  85. function testDragZeroMarginDivHContainer() {
  86. var dsc = new goog.fx.DragScrollSupport(hContainerDiv);
  87. // Set initial scroll state.
  88. var scrollLeft = 50;
  89. hContainerDiv.scrollLeft = scrollLeft;
  90. goog.testing.events.fireMouseMoveEvent(
  91. hContainerDiv, new goog.math.Coordinate(200 + 50, 20 + 50));
  92. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  93. assertEquals(
  94. 'Mousing inside the hContainer should not trigger scrolling.', scrollLeft,
  95. hContainerDiv.scrollLeft);
  96. assertEquals('Scroll timer should not tick yet', 0, clock.getTimeoutsMade());
  97. scrollLeft = hContainerDiv.scrollLeft;
  98. goog.testing.events.fireMouseMoveEvent(
  99. hContainerDiv, new goog.math.Coordinate(200 - 10, 20 + 50));
  100. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  101. assertTrue(
  102. 'Mousing left of the hContainer should trigger scrolling left.',
  103. scrollLeft > hContainerDiv.scrollLeft);
  104. scrollLeft = hContainerDiv.scrollLeft;
  105. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  106. assertTrue(
  107. 'Mousing left of the hContainer should trigger scrolling left.',
  108. scrollLeft > hContainerDiv.scrollLeft);
  109. scrollLeft = hContainerDiv.scrollLeft;
  110. goog.testing.events.fireMouseMoveEvent(
  111. hContainerDiv, new goog.math.Coordinate(200 + 110, 20 + 50));
  112. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  113. assertTrue(
  114. 'Mousing right of the hContainer should trigger scrolling right.',
  115. scrollLeft < hContainerDiv.scrollLeft);
  116. scrollLeft = hContainerDiv.scrollLeft;
  117. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  118. assertTrue(
  119. 'Mousing right of the hContainer should trigger scrolling right.',
  120. scrollLeft < hContainerDiv.scrollLeft);
  121. scrollLeft = hContainerDiv.scrollLeft;
  122. goog.testing.events.fireMouseMoveEvent(
  123. hContainerDiv, new goog.math.Coordinate(200 + 50, 20 + 50));
  124. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  125. assertEquals(
  126. 'Mousing inside the hContainer should stop scrolling.', scrollLeft,
  127. hContainerDiv.scrollLeft);
  128. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  129. dsc.dispose();
  130. }
  131. function testDragMarginDivVContainer() {
  132. var dsc = new goog.fx.DragScrollSupport(vContainerDiv, 20);
  133. // Set initial scroll state.
  134. var scrollTop = 50;
  135. vContainerDiv.scrollTop = scrollTop;
  136. goog.testing.events.fireMouseMoveEvent(
  137. vContainerDiv, new goog.math.Coordinate(50, 20 + 50));
  138. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  139. assertEquals(
  140. 'Mousing inside the vContainer should not trigger scrolling.', scrollTop,
  141. vContainerDiv.scrollTop);
  142. assertEquals('Scroll timer should not tick yet', 0, clock.getTimeoutsMade());
  143. scrollTop = vContainerDiv.scrollTop;
  144. goog.testing.events.fireMouseMoveEvent(
  145. vContainerDiv, new goog.math.Coordinate(50, 30));
  146. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  147. assertTrue(
  148. 'Mousing above the margin should trigger scrolling up.',
  149. scrollTop > vContainerDiv.scrollTop);
  150. scrollTop = vContainerDiv.scrollTop;
  151. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  152. assertTrue(
  153. 'Mousing above the margin should trigger scrolling up.',
  154. scrollTop > vContainerDiv.scrollTop);
  155. scrollTop = vContainerDiv.scrollTop;
  156. goog.testing.events.fireMouseMoveEvent(
  157. vContainerDiv, new goog.math.Coordinate(50, 20 + 90));
  158. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  159. assertTrue(
  160. 'Mousing below the margin should trigger scrolling down.',
  161. scrollTop < vContainerDiv.scrollTop);
  162. scrollTop = vContainerDiv.scrollTop;
  163. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  164. assertTrue(
  165. 'Mousing above the margin should trigger scrolling down.',
  166. scrollTop < vContainerDiv.scrollTop);
  167. scrollTop = vContainerDiv.scrollTop;
  168. goog.testing.events.fireMouseMoveEvent(
  169. vContainerDiv, new goog.math.Coordinate(50, 20 + 50));
  170. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  171. assertEquals(
  172. 'Mousing inside the margin should stop scrolling.', scrollTop,
  173. vContainerDiv.scrollTop);
  174. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  175. // 5 timeouts are scheduled, but the last one is cancelled.
  176. assertEquals(
  177. 'Scroll timer should have ticked 4 times',
  178. 4,
  179. clock.getCallbacksTriggered());
  180. dsc.dispose();
  181. }
  182. function testDragMarginScrollConstrainedDivVContainer() {
  183. var dsc = new goog.fx.DragScrollSupport(vContainerDiv, 20);
  184. dsc.setConstrainScroll(true);
  185. // Set initial scroll state.
  186. var scrollTop = 50;
  187. vContainerDiv.scrollTop = scrollTop;
  188. goog.testing.events.fireMouseMoveEvent(
  189. vContainerDiv, new goog.math.Coordinate(50, 20 + 50));
  190. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  191. assertEquals(
  192. 'Mousing inside the vContainer should not trigger scrolling.', scrollTop,
  193. vContainerDiv.scrollTop);
  194. assertEquals('Scroll timer should not tick yet', 0, clock.getTimeoutsMade());
  195. scrollTop = vContainerDiv.scrollTop;
  196. goog.testing.events.fireMouseMoveEvent(
  197. vContainerDiv, new goog.math.Coordinate(50, 30));
  198. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  199. assertTrue(
  200. 'Mousing above the margin should trigger scrolling up.',
  201. scrollTop > vContainerDiv.scrollTop);
  202. scrollTop = vContainerDiv.scrollTop;
  203. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  204. assertTrue(
  205. 'Mousing above the margin should trigger scrolling up.',
  206. scrollTop > vContainerDiv.scrollTop);
  207. scrollTop = vContainerDiv.scrollTop;
  208. goog.testing.events.fireMouseMoveEvent(
  209. vContainerDiv, new goog.math.Coordinate(50, 20 + 90));
  210. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  211. assertTrue(
  212. 'Mousing below the margin should trigger scrolling down.',
  213. scrollTop < vContainerDiv.scrollTop);
  214. scrollTop = vContainerDiv.scrollTop;
  215. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  216. assertTrue(
  217. 'Mousing above the margin should trigger scrolling down.',
  218. scrollTop < vContainerDiv.scrollTop);
  219. scrollTop = vContainerDiv.scrollTop;
  220. goog.testing.events.fireMouseMoveEvent(
  221. vContainerDiv, new goog.math.Coordinate(50, 20 + 50));
  222. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  223. assertEquals(
  224. 'Mousing inside the margin should stop scrolling.', scrollTop,
  225. vContainerDiv.scrollTop);
  226. scrollTop = vContainerDiv.scrollTop;
  227. goog.testing.events.fireMouseMoveEvent(
  228. vContainerDiv, new goog.math.Coordinate(50, 10));
  229. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  230. assertEquals(
  231. 'Mousing above the vContainer should not trigger scrolling up.',
  232. scrollTop, vContainerDiv.scrollTop);
  233. scrollTop = vContainerDiv.scrollTop;
  234. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  235. assertEquals(
  236. 'Mousing above the vContainer should not trigger scrolling up.',
  237. scrollTop, vContainerDiv.scrollTop);
  238. scrollTop = vContainerDiv.scrollTop;
  239. goog.testing.events.fireMouseMoveEvent(
  240. vContainerDiv, new goog.math.Coordinate(50, 20 + 110));
  241. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  242. assertEquals(
  243. 'Mousing below the vContainer should not trigger scrolling down.',
  244. scrollTop, vContainerDiv.scrollTop);
  245. scrollTop = vContainerDiv.scrollTop;
  246. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  247. assertEquals(
  248. 'Mousing below the vContainer should not trigger scrolling down.',
  249. scrollTop, vContainerDiv.scrollTop);
  250. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  251. scrollTop = vContainerDiv.scrollTop;
  252. goog.testing.events.fireMouseMoveEvent(
  253. vContainerDiv, new goog.math.Coordinate(150, 20 + 90));
  254. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  255. assertEquals(
  256. 'Mousing to the right of the vContainer should not trigger ' +
  257. 'scrolling up.',
  258. scrollTop, vContainerDiv.scrollTop);
  259. scrollTop = vContainerDiv.scrollTop;
  260. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  261. assertEquals(
  262. 'Mousing to the right of the vContainer should not trigger ' +
  263. 'scrolling up.',
  264. scrollTop, vContainerDiv.scrollTop);
  265. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  266. // 5 timeouts are scheduled, but the last one is cancelled.
  267. assertEquals(
  268. 'Scroll timer should have ticked 4 times',
  269. 4,
  270. clock.getCallbacksTriggered());
  271. dsc.dispose();
  272. }
  273. function testSetHorizontalScrolling() {
  274. var dsc = new goog.fx.DragScrollSupport(hContainerDiv);
  275. dsc.setHorizontalScrolling(false);
  276. // Set initial scroll state.
  277. var scrollLeft = 50;
  278. hContainerDiv.scrollLeft = scrollLeft;
  279. goog.testing.events.fireMouseMoveEvent(
  280. hContainerDiv, new goog.math.Coordinate(200 - 10, 20 + 50));
  281. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  282. assertEquals(
  283. 'Horizontal scrolling should be turned off', 0, clock.getTimeoutsMade());
  284. goog.testing.events.fireMouseMoveEvent(
  285. hContainerDiv, new goog.math.Coordinate(200 + 110, 20 + 50));
  286. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  287. assertEquals(
  288. 'Horizontal scrolling should be turned off', 0, clock.getTimeoutsMade());
  289. dsc.setHorizontalScrolling(true);
  290. scrollLeft = hContainerDiv.scrollLeft;
  291. goog.testing.events.fireMouseMoveEvent(
  292. hContainerDiv, new goog.math.Coordinate(200 - 10, 20 + 50));
  293. clock.tick(goog.fx.DragScrollSupport.TIMER_STEP_ + 1);
  294. assertTrue(
  295. 'Mousing left of the hContainer should trigger scrolling left.',
  296. scrollLeft > hContainerDiv.scrollLeft);
  297. dsc.dispose();
  298. }
  299. function testConstrainBoundsWithMargin() {
  300. var rect = goog.fx.DragScrollSupport.prototype.constrainBounds_.call(
  301. {margin_: 25}, new goog.math.Rect(0, 0, 100, 100));
  302. assertEquals(25, rect.left);
  303. assertEquals(25, rect.top);
  304. assertEquals(25, rect.left);
  305. assertEquals(50, rect.width);
  306. assertEquals(50, rect.height);
  307. }