| 1 |
// Copyright 2009 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.events.MouseWheelHandlerTest'); |
| 16 |
goog.setTestOnly('goog.events.MouseWheelHandlerTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.events.BrowserEvent'); |
| 21 |
goog.require('goog.events.MouseWheelEvent'); |
| 22 |
goog.require('goog.events.MouseWheelHandler'); |
| 23 |
goog.require('goog.functions'); |
| 24 |
goog.require('goog.string'); |
| 25 |
goog.require('goog.testing.PropertyReplacer'); |
| 26 |
goog.require('goog.testing.events'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
goog.require('goog.userAgent'); |
| 29 |
|
| 30 |
var log; |
| 31 |
var stubs = new goog.testing.PropertyReplacer(); |
| 32 |
|
| 33 |
var DEFAULT_TYPE = 'mousewheel'; |
| 34 |
var GECKO_TYPE = 'DOMMouseScroll'; |
| 35 |
|
| 36 |
var HORIZONTAL = 'h'; |
| 37 |
var VERTICAL = 'v'; |
| 38 |
|
| 39 |
var mouseWheelEvent; |
| 40 |
var mouseWheelEventRtl; |
| 41 |
var mouseWheelHandler; |
| 42 |
var mouseWheelHandlerRtl; |
| 43 |
|
| 44 |
function setUpPage() { |
| 45 |
log = goog.dom.getElement('log'); |
| 46 |
} |
| 47 |
|
| 48 |
function setUp() { |
| 49 |
stubs.remove(goog, 'userAgent'); |
| 50 |
} |
| 51 |
|
| 52 |
function tearDown() { |
| 53 |
stubs.reset(); |
| 54 |
goog.dispose(mouseWheelHandler); |
| 55 |
goog.dispose(mouseWheelHandlerRtl); |
| 56 |
mouseWheelHandlerRtl = null; |
| 57 |
mouseWheelHandler = null; |
| 58 |
mouseWheelEvent = null; |
| 59 |
mouseWheelEventRtl = null; |
| 60 |
} |
| 61 |
|
| 62 |
function tearDownPage() { |
| 63 |
// Create interactive demo. |
| 64 |
mouseWheelHandler = new goog.events.MouseWheelHandler(document.body); |
| 65 |
|
| 66 |
goog.events.listen( |
| 67 |
mouseWheelHandler, goog.events.MouseWheelHandler.EventType.MOUSEWHEEL, |
| 68 |
function(e) { |
| 69 |
log.innerHTML += goog.string.subs( |
| 70 |
'<br />(deltaX, deltaY): (%s, %s)', e.deltaX, e.deltaY); |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
function testIeStyleMouseWheel() { |
| 75 |
goog.userAgent = |
| 76 |
{OPERA: false, EDGE_OR_IE: true, GECKO: false, WEBKIT: false}; |
| 77 |
|
| 78 |
createHandlerAndListen(); |
| 79 |
|
| 80 |
// Non-gecko, non-webkit events get wheelDelta divided by -40 to get detail. |
| 81 |
handleEvent(createFakeMouseWheelEvent(DEFAULT_TYPE, 120)); |
| 82 |
assertMouseWheelEvent(-3, 0, -3); |
| 83 |
|
| 84 |
handleEvent(createFakeMouseWheelEvent(DEFAULT_TYPE, -120)); |
| 85 |
assertMouseWheelEvent(3, 0, 3); |
| 86 |
|
| 87 |
handleEvent(createFakeMouseWheelEvent(DEFAULT_TYPE, 1200)); |
| 88 |
assertMouseWheelEvent(-30, 0, -30); |
| 89 |
} |
| 90 |
|
| 91 |
function testNullBody() { |
| 92 |
goog.userAgent = {OPERA: false, IE: true, GECKO: false, WEBKIT: false}; |
| 93 |
var documentObjectWithNoBody = {}; |
| 94 |
goog.testing.events.mixinListenable(documentObjectWithNoBody); |
| 95 |
mouseWheelHandler = |
| 96 |
new goog.events.MouseWheelHandler(documentObjectWithNoBody); |
| 97 |
} |
| 98 |
|
| 99 |
function testGeckoStyleMouseWheel() { |
| 100 |
goog.userAgent = {OPERA: false, IE: false, GECKO: true, WEBKIT: false}; |
| 101 |
|
| 102 |
createHandlerAndListen(); |
| 103 |
|
| 104 |
handleEvent(createFakeMouseWheelEvent(GECKO_TYPE, null, 3)); |
| 105 |
assertMouseWheelEvent(3, 0, 3); |
| 106 |
|
| 107 |
handleEvent(createFakeMouseWheelEvent(GECKO_TYPE, null, -12)); |
| 108 |
assertMouseWheelEvent(-12, 0, -12); |
| 109 |
|
| 110 |
// Really big values should get truncated to +-3. |
| 111 |
handleEvent(createFakeMouseWheelEvent(GECKO_TYPE, null, 1200)); |
| 112 |
assertMouseWheelEvent(3, 0, 3); |
| 113 |
|
| 114 |
handleEvent(createFakeMouseWheelEvent(GECKO_TYPE, null, -1200)); |
| 115 |
assertMouseWheelEvent(-3, 0, -3); |
| 116 |
|
| 117 |
// Test scrolling with the additional axis property. |
| 118 |
handleEvent(createFakeMouseWheelEvent(GECKO_TYPE, null, 3, VERTICAL)); |
| 119 |
assertMouseWheelEvent(3, 0, 3); |
| 120 |
|
| 121 |
handleEvent(createFakeMouseWheelEvent(GECKO_TYPE, null, 3, HORIZONTAL)); |
| 122 |
assertMouseWheelEvent(3, 3, 0); |
| 123 |
|
| 124 |
handleEvent(createFakeMouseWheelEvent(GECKO_TYPE, null, -3, HORIZONTAL)); |
| 125 |
assertMouseWheelEvent(-3, -3, 0); |
| 126 |
} |
| 127 |
|
| 128 |
function testWebkitStyleMouseWheel_ieStyle() { |
| 129 |
goog.userAgent = |
| 130 |
{OPERA: false, IE: false, GECKO: false, WEBKIT: true, WINDOWS: true}; |
| 131 |
|
| 132 |
createHandlerAndListen(); |
| 133 |
|
| 134 |
// IE-style Webkit events get wheelDelta divided by -40 to get detail. |
| 135 |
handleEvent(createFakeWebkitMouseWheelEvent(-40, 0)); |
| 136 |
assertMouseWheelEvent(1, 1, 0); |
| 137 |
|
| 138 |
handleEvent(createFakeWebkitMouseWheelEvent(120, 0)); |
| 139 |
assertMouseWheelEvent(-3, -3, 0); |
| 140 |
|
| 141 |
handleEvent(createFakeWebkitMouseWheelEvent(0, 120)); |
| 142 |
assertMouseWheelEvent(-3, 0, -3); |
| 143 |
|
| 144 |
handleEvent(createFakeWebkitMouseWheelEvent(0, -40)); |
| 145 |
assertMouseWheelEvent(1, 0, 1); |
| 146 |
|
| 147 |
handleEvent(createFakeWebkitMouseWheelEvent(80, -40)); |
| 148 |
assertMouseWheelEvent(-2, -2, 1); |
| 149 |
} |
| 150 |
|
| 151 |
function testWebkitStyleMouseWheel_ieStyleOnLinux() { |
| 152 |
goog.userAgent = { |
| 153 |
OPERA: false, |
| 154 |
IE: false, |
| 155 |
GECKO: false, |
| 156 |
WEBKIT: true, |
| 157 |
WINDOWS: false, |
| 158 |
LINUX: true |
| 159 |
}; |
| 160 |
runWebKitContinuousAndDiscreteEventsTest(); |
| 161 |
} |
| 162 |
|
| 163 |
function testWebkitStyleMouseWheel_ieStyleOnMac() { |
| 164 |
goog.userAgent = { |
| 165 |
OPERA: false, |
| 166 |
IE: false, |
| 167 |
GECKO: false, |
| 168 |
WEBKIT: true, |
| 169 |
WINDOWS: false, |
| 170 |
MAC: true |
| 171 |
}; |
| 172 |
runWebKitContinuousAndDiscreteEventsTest(); |
| 173 |
} |
| 174 |
|
| 175 |
function runWebKitContinuousAndDiscreteEventsTest() { |
| 176 |
goog.userAgent.isVersionOrHigher = goog.functions.TRUE; |
| 177 |
|
| 178 |
createHandlerAndListen(); |
| 179 |
|
| 180 |
// IE-style wheel events. |
| 181 |
handleEvent(createFakeWebkitMouseWheelEvent(0, -40)); |
| 182 |
assertMouseWheelEvent(1, 0, 1); |
| 183 |
|
| 184 |
handleEvent(createFakeWebkitMouseWheelEvent(80, -40)); |
| 185 |
assertMouseWheelEvent(-2, -2, 1); |
| 186 |
|
| 187 |
// Even in Webkit versions that usually behave in IE style, sometimes wheel |
| 188 |
// events don't behave; this has been observed for instance with Macbook |
| 189 |
// and Chrome OS touchpads in Webkit 534.10+. |
| 190 |
handleEvent(createFakeWebkitMouseWheelEvent(-3, 5)); |
| 191 |
assertMouseWheelEvent(-5, 3, -5); |
| 192 |
|
| 193 |
handleEvent(createFakeWebkitMouseWheelEvent(4, -7)); |
| 194 |
assertMouseWheelEvent(7, -4, 7); |
| 195 |
} |
| 196 |
|
| 197 |
function testWebkitStyleMouseWheel_nonIeStyle() { |
| 198 |
goog.userAgent = |
| 199 |
{OPERA: false, IE: false, GECKO: false, WEBKIT: true, WINDOWS: false}; |
| 200 |
|
| 201 |
goog.userAgent.isVersionOrHigher = goog.functions.FALSE; |
| 202 |
|
| 203 |
createHandlerAndListen(); |
| 204 |
|
| 205 |
// non-IE-style Webkit events do not get wheelDelta scaled |
| 206 |
handleEvent(createFakeWebkitMouseWheelEvent(-40, 0)); |
| 207 |
assertMouseWheelEvent(1, 1, 0); |
| 208 |
|
| 209 |
handleEvent(createFakeWebkitMouseWheelEvent(120, 0)); |
| 210 |
assertMouseWheelEvent(-3, -3, 0); |
| 211 |
|
| 212 |
handleEvent(createFakeWebkitMouseWheelEvent(0, 120)); |
| 213 |
assertMouseWheelEvent(-3, 0, -3); |
| 214 |
|
| 215 |
handleEvent(createFakeWebkitMouseWheelEvent(0, -40)); |
| 216 |
assertMouseWheelEvent(1, 0, 1); |
| 217 |
|
| 218 |
handleEvent(createFakeWebkitMouseWheelEvent(80, -40)); |
| 219 |
assertMouseWheelEvent(-2, -2, 1); |
| 220 |
} |
| 221 |
|
| 222 |
function testMaxDeltaX() { |
| 223 |
goog.userAgent = |
| 224 |
{OPERA: false, IE: false, GECKO: false, WEBKIT: true, WINDOWS: true}; |
| 225 |
|
| 226 |
createHandlerAndListen(); |
| 227 |
|
| 228 |
// IE-style Webkit events get wheelDelta divided by -40 to get detail. |
| 229 |
handleEvent(createFakeWebkitMouseWheelEvent(-120, 0)); |
| 230 |
assertMouseWheelEvent(3, 3, 0); |
| 231 |
|
| 232 |
mouseWheelHandler.setMaxDeltaX(3); |
| 233 |
mouseWheelHandlerRtl.setMaxDeltaX(3); |
| 234 |
handleEvent(createFakeWebkitMouseWheelEvent(-120, 0)); |
| 235 |
assertMouseWheelEvent(3, 3, 0); |
| 236 |
|
| 237 |
mouseWheelHandler.setMaxDeltaX(2); |
| 238 |
mouseWheelHandlerRtl.setMaxDeltaX(2); |
| 239 |
handleEvent(createFakeWebkitMouseWheelEvent(-120, 0)); |
| 240 |
assertMouseWheelEvent(3, 2, 0); |
| 241 |
|
| 242 |
handleEvent(createFakeWebkitMouseWheelEvent(0, -120)); |
| 243 |
assertMouseWheelEvent(3, 0, 3); |
| 244 |
} |
| 245 |
|
| 246 |
function testMaxDeltaY() { |
| 247 |
goog.userAgent = |
| 248 |
{OPERA: false, IE: false, GECKO: false, WEBKIT: true, WINDOWS: true}; |
| 249 |
|
| 250 |
createHandlerAndListen(); |
| 251 |
|
| 252 |
// IE-style Webkit events get wheelDelta divided by -40 to get detail. |
| 253 |
handleEvent(createFakeWebkitMouseWheelEvent(0, -120)); |
| 254 |
assertMouseWheelEvent(3, 0, 3); |
| 255 |
|
| 256 |
mouseWheelHandler.setMaxDeltaY(3); |
| 257 |
mouseWheelHandlerRtl.setMaxDeltaY(3); |
| 258 |
handleEvent(createFakeWebkitMouseWheelEvent(0, -120)); |
| 259 |
assertMouseWheelEvent(3, 0, 3); |
| 260 |
|
| 261 |
mouseWheelHandler.setMaxDeltaY(2); |
| 262 |
mouseWheelHandlerRtl.setMaxDeltaY(2); |
| 263 |
handleEvent(createFakeWebkitMouseWheelEvent(0, -120)); |
| 264 |
assertMouseWheelEvent(3, 0, 2); |
| 265 |
|
| 266 |
handleEvent(createFakeWebkitMouseWheelEvent(-120, 0)); |
| 267 |
assertMouseWheelEvent(3, 3, 0); |
| 268 |
} |
| 269 |
|
| 270 |
// Be sure to call this after setting up goog.userAgent mock and not before. |
| 271 |
function createHandlerAndListen() { |
| 272 |
mouseWheelHandler = |
| 273 |
new goog.events.MouseWheelHandler(goog.dom.getElement('foo')); |
| 274 |
|
| 275 |
goog.events.listen( |
| 276 |
mouseWheelHandler, goog.events.MouseWheelHandler.EventType.MOUSEWHEEL, |
| 277 |
function(e) { mouseWheelEvent = e; }); |
| 278 |
|
| 279 |
mouseWheelHandlerRtl = |
| 280 |
new goog.events.MouseWheelHandler(goog.dom.getElement('fooRtl')); |
| 281 |
|
| 282 |
goog.events.listen( |
| 283 |
mouseWheelHandlerRtl, goog.events.MouseWheelHandler.EventType.MOUSEWHEEL, |
| 284 |
function(e) { mouseWheelEventRtl = e; }); |
| 285 |
} |
| 286 |
|
| 287 |
function handleEvent(event) { |
| 288 |
mouseWheelHandler.handleEvent(event); |
| 289 |
mouseWheelHandlerRtl.handleEvent(event); |
| 290 |
} |
| 291 |
|
| 292 |
function assertMouseWheelEvent(expectedDetail, expectedDeltaX, expectedDeltaY) { |
| 293 |
assertTrue('event should be non-null', !!mouseWheelEvent); |
| 294 |
assertTrue( |
| 295 |
'event should have correct JS type', |
| 296 |
mouseWheelEvent instanceof goog.events.MouseWheelEvent); |
| 297 |
assertEquals( |
| 298 |
'event should have correct detail property', expectedDetail, |
| 299 |
mouseWheelEvent.detail); |
| 300 |
assertEquals( |
| 301 |
'event should have correct deltaX property', expectedDeltaX, |
| 302 |
mouseWheelEvent.deltaX); |
| 303 |
assertEquals( |
| 304 |
'event should have correct deltaY property', expectedDeltaY, |
| 305 |
mouseWheelEvent.deltaY); |
| 306 |
|
| 307 |
// RTL |
| 308 |
assertTrue('event should be non-null', !!mouseWheelEventRtl); |
| 309 |
assertTrue( |
| 310 |
'event should have correct JS type', |
| 311 |
mouseWheelEventRtl instanceof goog.events.MouseWheelEvent); |
| 312 |
assertEquals( |
| 313 |
'event should have correct detail property', expectedDetail, |
| 314 |
mouseWheelEventRtl.detail); |
| 315 |
assertEquals( |
| 316 |
'event should have correct deltaX property', -expectedDeltaX, |
| 317 |
mouseWheelEventRtl.deltaX); |
| 318 |
assertEquals( |
| 319 |
'event should have correct deltaY property', expectedDeltaY, |
| 320 |
mouseWheelEventRtl.deltaY); |
| 321 |
} |
| 322 |
|
| 323 |
function createFakeMouseWheelEvent( |
| 324 |
type, opt_wheelDelta, opt_detail, opt_axis, opt_wheelDeltaX, |
| 325 |
opt_wheelDeltaY) { |
| 326 |
var event = { |
| 327 |
type: type, |
| 328 |
wheelDelta: goog.isDef(opt_wheelDelta) ? opt_wheelDelta : undefined, |
| 329 |
detail: goog.isDef(opt_detail) ? opt_detail : undefined, |
| 330 |
axis: opt_axis || undefined, |
| 331 |
wheelDeltaX: goog.isDef(opt_wheelDeltaX) ? opt_wheelDeltaX : undefined, |
| 332 |
wheelDeltaY: goog.isDef(opt_wheelDeltaY) ? opt_wheelDeltaY : undefined, |
| 333 |
|
| 334 |
// These two are constants defined on the event in FF3.1 and later. |
| 335 |
// It doesn't matter exactly what they are, and it doesn't affect |
| 336 |
// our simulations of other browsers. |
| 337 |
HORIZONTAL_AXIS: HORIZONTAL, |
| 338 |
VERTICAL_AXIS: VERTICAL |
| 339 |
}; |
| 340 |
return new goog.events.BrowserEvent(event); |
| 341 |
} |
| 342 |
|
| 343 |
function createFakeWebkitMouseWheelEvent(wheelDeltaX, wheelDeltaY) { |
| 344 |
return createFakeMouseWheelEvent( |
| 345 |
DEFAULT_TYPE, |
| 346 |
Math.abs(wheelDeltaX) > Math.abs(wheelDeltaY) ? wheelDeltaX : wheelDeltaY, |
| 347 |
undefined, undefined, wheelDeltaX, wheelDeltaY); |
| 348 |
} |