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