| 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.BrowserEventTest'); |
| 16 |
goog.setTestOnly('goog.events.BrowserEventTest'); |
| 17 |
|
| 18 |
goog.require('goog.events.BrowserEvent'); |
| 19 |
goog.require('goog.events.BrowserFeature'); |
| 20 |
goog.require('goog.math.Coordinate'); |
| 21 |
goog.require('goog.testing.PropertyReplacer'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.userAgent'); |
| 24 |
|
| 25 |
var stubs = new goog.testing.PropertyReplacer(); |
| 26 |
var Button = goog.events.BrowserEvent.MouseButton; |
| 27 |
var PointerType = goog.events.BrowserEvent.PointerType; |
| 28 |
|
| 29 |
function setUp() { |
| 30 |
stubs.reset(); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=497780 |
| 36 |
*/ |
| 37 |
function testInvalidNodeBug() { |
| 38 |
if (!goog.userAgent.GECKO) return; |
| 39 |
|
| 40 |
var event = {}; |
| 41 |
event.relatedTarget = {}; |
| 42 |
event.relatedTarget.__defineGetter__('nodeName', function() { |
| 43 |
throw new Error('https://bugzilla.mozilla.org/show_bug.cgi?id=497780'); |
| 44 |
}); |
| 45 |
assertThrows(function() { return event.relatedTarget.nodeName; }); |
| 46 |
|
| 47 |
var bEvent = new goog.events.BrowserEvent(event); |
| 48 |
assertEquals(event, bEvent.event_); |
| 49 |
assertNull(bEvent.relatedTarget); |
| 50 |
} |
| 51 |
|
| 52 |
function testPreventDefault() { |
| 53 |
var event = {}; |
| 54 |
event.defaultPrevented = false; |
| 55 |
var bEvent = new goog.events.BrowserEvent(event); |
| 56 |
assertFalse(bEvent.defaultPrevented); |
| 57 |
bEvent.preventDefault(); |
| 58 |
assertTrue(bEvent.defaultPrevented); |
| 59 |
} |
| 60 |
|
| 61 |
function testDefaultPrevented() { |
| 62 |
var event = {}; |
| 63 |
event.defaultPrevented = true; |
| 64 |
var bEvent = new goog.events.BrowserEvent(event); |
| 65 |
assertTrue(bEvent.defaultPrevented); |
| 66 |
} |
| 67 |
|
| 68 |
function testIsButtonIe() { |
| 69 |
stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', false); |
| 70 |
assertIsButton(createMouseEvent('mousedown', 1), Button.LEFT, true); |
| 71 |
assertIsButton(createMouseEvent('click', 0), Button.LEFT, true); |
| 72 |
assertIsButton(createMouseEvent('mousedown', 2), Button.RIGHT, false); |
| 73 |
assertIsButton(createMouseEvent('mousedown', 4), Button.MIDDLE, false); |
| 74 |
} |
| 75 |
|
| 76 |
function testIsButtonWebkitMac() { |
| 77 |
stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', true); |
| 78 |
stubs.set(goog.userAgent, 'WEBKIT', true); |
| 79 |
stubs.set(goog.userAgent, 'MAC', true); |
| 80 |
assertIsButton(createMouseEvent('mousedown', 0), Button.LEFT, true); |
| 81 |
assertIsButton(createMouseEvent('mousedown', 0, true), Button.LEFT, false); |
| 82 |
assertIsButton(createMouseEvent('mousedown', 2), Button.RIGHT, false); |
| 83 |
assertIsButton(createMouseEvent('mousedown', 2, true), Button.RIGHT, false); |
| 84 |
assertIsButton(createMouseEvent('mousedown', 1), Button.MIDDLE, false); |
| 85 |
assertIsButton(createMouseEvent('mousedown', 1, true), Button.MIDDLE, false); |
| 86 |
} |
| 87 |
|
| 88 |
function testIsButtonGecko() { |
| 89 |
stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', true); |
| 90 |
stubs.set(goog.userAgent, 'GECKO', true); |
| 91 |
stubs.set(goog.userAgent, 'MAC', true); |
| 92 |
assertIsButton(createMouseEvent('mousedown', 0), Button.LEFT, true); |
| 93 |
assertIsButton(createMouseEvent('mousedown', 2, true), Button.RIGHT, false); |
| 94 |
} |
| 95 |
|
| 96 |
function testTouchEventHandling() { |
| 97 |
var clientCoords = new goog.math.Coordinate(5, 5); |
| 98 |
var screenCoords = new goog.math.Coordinate(10, 10); |
| 99 |
var target = document.body; |
| 100 |
var touchStart = |
| 101 |
createTouchEvent('touchstart', target, clientCoords, screenCoords); |
| 102 |
var touchMove = |
| 103 |
createTouchEvent('touchmove', target, clientCoords, screenCoords); |
| 104 |
var touchEnd = |
| 105 |
createTouchEvent('touchend', target, clientCoords, screenCoords); |
| 106 |
var touchCancel = |
| 107 |
createTouchEvent('touchcancel', target, clientCoords, screenCoords); |
| 108 |
|
| 109 |
assertEquals(clientCoords.x, touchStart.clientX); |
| 110 |
assertEquals(clientCoords.y, touchStart.clientY); |
| 111 |
assertEquals(target, touchStart.target); |
| 112 |
|
| 113 |
assertEquals(screenCoords.x, touchMove.screenX); |
| 114 |
assertEquals(screenCoords.y, touchMove.screenY); |
| 115 |
|
| 116 |
assertEquals(clientCoords.x, touchEnd.clientX); |
| 117 |
assertEquals(clientCoords.y, touchEnd.clientY); |
| 118 |
|
| 119 |
assertEquals(screenCoords.x, touchCancel.screenX); |
| 120 |
assertEquals(screenCoords.y, touchCancel.screenY); |
| 121 |
assertEquals(target, touchCancel.target); |
| 122 |
} |
| 123 |
|
| 124 |
function testPointerEvent() { |
| 125 |
var event = createPointerEvent('pointerdown', 123, PointerType.MOUSE); |
| 126 |
assertEquals(123, event.pointerId); |
| 127 |
assertEquals(PointerType.MOUSE, event.pointerType); |
| 128 |
} |
| 129 |
|
| 130 |
function testMSPointerEvent() { |
| 131 |
var event = createPointerEvent('MSPointerDown', 123, 4 /* mouse */); |
| 132 |
assertEquals(123, event.pointerId); |
| 133 |
assertEquals(PointerType.MOUSE, event.pointerType); |
| 134 |
} |
| 135 |
|
| 136 |
function testUnsupportedPointerEvent() { |
| 137 |
var event = createMouseEvent('mousedown', 1); |
| 138 |
assertEquals(0, event.pointerId); |
| 139 |
assertEquals('', event.pointerType); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @param {string} type |
| 144 |
* @param {number} button |
| 145 |
* @param {boolean=} opt_ctrlKey |
| 146 |
* @return {!goog.events.BrowserEvent} |
| 147 |
*/ |
| 148 |
function createMouseEvent(type, button, opt_ctrlKey) { |
| 149 |
return new goog.events.BrowserEvent( |
| 150 |
{type: type, button: button, ctrlKey: !!opt_ctrlKey}); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @param {string} type |
| 155 |
* @param {!Element} target |
| 156 |
* @param {!goog.math.Coordinate} clientCoords |
| 157 |
* @param {!goog.math.Coordinate} screenCoords |
| 158 |
* @return {!goog.events.BrowserEvent} |
| 159 |
*/ |
| 160 |
function createTouchEvent(type, target, clientCoords, screenCoords) { |
| 161 |
return new goog.events.BrowserEvent({ |
| 162 |
type: type, |
| 163 |
target: target, |
| 164 |
changedTouches: [{ |
| 165 |
clientX: clientCoords.x, |
| 166 |
clientY: clientCoords.y, |
| 167 |
screenX: screenCoords.x, |
| 168 |
screenY: screenCoords.y |
| 169 |
}] |
| 170 |
}); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param {string} type |
| 175 |
* @param {number} pointerId |
| 176 |
* @param {string} pointerType |
| 177 |
* @return {!goog.events.BrowserEvent} |
| 178 |
*/ |
| 179 |
function createPointerEvent(type, pointerId, pointerType) { |
| 180 |
return new goog.events.BrowserEvent( |
| 181 |
{type: type, pointerId: pointerId, pointerType: pointerType}); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* @param {!goog.events.BrowserEvent} event |
| 186 |
* @param {goog.events.BrowserEvent.MouseButton} button |
| 187 |
* @param {boolean} isActionButton |
| 188 |
* @return {!goog.events.BrowserEvent} |
| 189 |
*/ |
| 190 |
function assertIsButton(event, button, isActionButton) { |
| 191 |
for (var key in Button) { |
| 192 |
assertEquals( |
| 193 |
'Testing isButton(' + key + ') against ' + button + ' and type ' + |
| 194 |
event.type, |
| 195 |
Button[key] == button, event.isButton(Button[key])); |
| 196 |
} |
| 197 |
|
| 198 |
assertEquals(isActionButton, event.isMouseActionButton()); |
| 199 |
} |