| 1 |
// Copyright 2006 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.EventHandlerTest'); |
| 16 |
goog.setTestOnly('goog.events.EventHandlerTest'); |
| 17 |
|
| 18 |
goog.require('goog.events'); |
| 19 |
goog.require('goog.events.EventHandler'); |
| 20 |
goog.require('goog.events.EventTarget'); |
| 21 |
goog.require('goog.testing.PropertyReplacer'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.testing.recordFunction'); |
| 24 |
|
| 25 |
var a, b, c, d, eh; |
| 26 |
var propertyReplacer; |
| 27 |
|
| 28 |
function setUpPage() { |
| 29 |
a = document.getElementById('a'); |
| 30 |
b = document.getElementById('b'); |
| 31 |
c = document.getElementById('c'); |
| 32 |
d = document.getElementById('d'); |
| 33 |
propertyReplacer = new goog.testing.PropertyReplacer(); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
function tearDown() { |
| 38 |
goog.dispose(eh); |
| 39 |
propertyReplacer.reset(); |
| 40 |
} |
| 41 |
|
| 42 |
function testEventHandlerClearsListeners() { |
| 43 |
function tmp() {} |
| 44 |
|
| 45 |
goog.events.listen(a, 'click', tmp); |
| 46 |
|
| 47 |
assertEquals(1, goog.events.getListeners(a, 'click', false).length); |
| 48 |
|
| 49 |
eh = new goog.events.EventHandler(); |
| 50 |
eh.listen(a, 'click'); |
| 51 |
eh.listen(a, 'keypress'); |
| 52 |
eh.listen(b, 'mouseover'); |
| 53 |
eh.listen(c, 'mousedown'); |
| 54 |
eh.listen(d, 'click'); |
| 55 |
eh.listen(d, 'mousedown'); |
| 56 |
|
| 57 |
assertEquals(2, goog.events.getListeners(a, 'click', false).length); |
| 58 |
assertEquals(1, goog.events.getListeners(a, 'keypress', false).length); |
| 59 |
assertEquals(1, goog.events.getListeners(b, 'mouseover', false).length); |
| 60 |
assertEquals(1, goog.events.getListeners(c, 'mousedown', false).length); |
| 61 |
assertEquals(1, goog.events.getListeners(d, 'click', false).length); |
| 62 |
assertEquals(1, goog.events.getListeners(d, 'mousedown', false).length); |
| 63 |
|
| 64 |
eh.unlisten(d, 'mousedown'); |
| 65 |
|
| 66 |
assertEquals(2, goog.events.getListeners(a, 'click', false).length); |
| 67 |
assertEquals(1, goog.events.getListeners(a, 'keypress', false).length); |
| 68 |
assertEquals(1, goog.events.getListeners(b, 'mouseover', false).length); |
| 69 |
assertEquals(1, goog.events.getListeners(c, 'mousedown', false).length); |
| 70 |
assertEquals(1, goog.events.getListeners(d, 'click', false).length); |
| 71 |
assertEquals(0, goog.events.getListeners(d, 'mousedown', false).length); |
| 72 |
|
| 73 |
eh.dispose(); |
| 74 |
|
| 75 |
assertEquals(1, goog.events.getListeners(a, 'click', false).length); |
| 76 |
assertEquals(0, goog.events.getListeners(a, 'keypress', false).length); |
| 77 |
assertEquals(0, goog.events.getListeners(b, 'mouseover', false).length); |
| 78 |
assertEquals(0, goog.events.getListeners(c, 'mousedown', false).length); |
| 79 |
assertEquals(0, goog.events.getListeners(d, 'click', false).length); |
| 80 |
assertEquals(0, goog.events.getListeners(d, 'mousedown', false).length); |
| 81 |
|
| 82 |
goog.events.unlisten(a, 'click', tmp); |
| 83 |
assertEquals(0, goog.events.getListeners(a, 'click', false).length); |
| 84 |
} |
| 85 |
|
| 86 |
function testListenArray() { |
| 87 |
eh = new goog.events.EventHandler(); |
| 88 |
|
| 89 |
eh.listen(a, ['click', 'mousedown', 'mouseup']); |
| 90 |
|
| 91 |
assertEquals(1, goog.events.getListeners(a, 'click', false).length); |
| 92 |
assertEquals(1, goog.events.getListeners(a, 'mousedown', false).length); |
| 93 |
assertEquals(1, goog.events.getListeners(a, 'mouseup', false).length); |
| 94 |
|
| 95 |
eh.unlisten(a, ['click', 'mousedown', 'mouseup']); |
| 96 |
|
| 97 |
assertEquals(0, goog.events.getListeners(a, 'click', false).length); |
| 98 |
assertEquals(0, goog.events.getListeners(a, 'mousedown', false).length); |
| 99 |
assertEquals(0, goog.events.getListeners(a, 'mouseup', false).length); |
| 100 |
|
| 101 |
eh.listen(a, ['click', 'mousedown', 'mouseup']); |
| 102 |
|
| 103 |
assertEquals(1, goog.events.getListeners(a, 'click', false).length); |
| 104 |
assertEquals(1, goog.events.getListeners(a, 'mousedown', false).length); |
| 105 |
assertEquals(1, goog.events.getListeners(a, 'mouseup', false).length); |
| 106 |
|
| 107 |
eh.removeAll(); |
| 108 |
|
| 109 |
assertEquals(0, goog.events.getListeners(a, 'click', false).length); |
| 110 |
assertEquals(0, goog.events.getListeners(a, 'mousedown', false).length); |
| 111 |
assertEquals(0, goog.events.getListeners(a, 'mouseup', false).length); |
| 112 |
} |
| 113 |
|
| 114 |
function testListenOnceRemovesListenerWhenFired() { |
| 115 |
var target = new goog.events.EventTarget(); |
| 116 |
eh = new goog.events.EventHandler(); |
| 117 |
var handler = goog.testing.recordFunction(); |
| 118 |
eh.listenOnce(target, 'click', handler); |
| 119 |
|
| 120 |
target.dispatchEvent('click'); |
| 121 |
assertEquals( |
| 122 |
'One event should have been dispatched', 1, handler.getCallCount()); |
| 123 |
|
| 124 |
target.dispatchEvent('click'); |
| 125 |
assertEquals( |
| 126 |
'No event should have been dispatched', 1, handler.getCallCount()); |
| 127 |
} |
| 128 |
|
| 129 |
function testListenOnceListenerIsCleanedUp() { |
| 130 |
var target = new goog.events.EventTarget(); |
| 131 |
eh = new goog.events.EventHandler(); |
| 132 |
var handler = goog.testing.recordFunction(); |
| 133 |
eh.listenOnce(target, 'click', handler); |
| 134 |
|
| 135 |
eh.removeAll(); |
| 136 |
|
| 137 |
target.dispatchEvent('click'); |
| 138 |
assertEquals(0, handler.getCallCount()); |
| 139 |
} |
| 140 |
|
| 141 |
function testClearListenersWithListenOnceListenerRemoved() { |
| 142 |
var target = new goog.events.EventTarget(); |
| 143 |
eh = new goog.events.EventHandler(); |
| 144 |
|
| 145 |
var handler = goog.testing.recordFunction(); |
| 146 |
eh.listenOnce(target, 'click', handler); |
| 147 |
|
| 148 |
assertNotNull(goog.events.getListener(target, 'click', handler, false, eh)); |
| 149 |
|
| 150 |
target.dispatchEvent('click'); |
| 151 |
assertEquals( |
| 152 |
'One event should have been dispatched', 1, handler.getCallCount()); |
| 153 |
|
| 154 |
assertNull(goog.events.getListener(target, 'click', handler, false, eh)); |
| 155 |
|
| 156 |
eh.removeAll(); |
| 157 |
|
| 158 |
target.dispatchEvent('click'); |
| 159 |
assertEquals( |
| 160 |
'No event should have been dispatched', 1, handler.getCallCount()); |
| 161 |
} |
| 162 |
|
| 163 |
function testListenOnceArray() { |
| 164 |
var target = new goog.events.EventTarget(); |
| 165 |
|
| 166 |
eh = new goog.events.EventHandler(); |
| 167 |
var handler = goog.testing.recordFunction(); |
| 168 |
eh.listenOnce(target, ['click', 'mousedown', 'mouseup'], handler); |
| 169 |
|
| 170 |
target.dispatchEvent('click'); |
| 171 |
assertEquals( |
| 172 |
'1 event should have been dispatched', 1, handler.getCallCount()); |
| 173 |
assertEquals( |
| 174 |
'Should be a click event', 'click', |
| 175 |
handler.getLastCall().getArgument(0).type); |
| 176 |
|
| 177 |
target.dispatchEvent('click'); |
| 178 |
assertEquals('No event should be dispatched', 1, handler.getCallCount()); |
| 179 |
|
| 180 |
target.dispatchEvent('mouseup'); |
| 181 |
assertEquals( |
| 182 |
'1 event should have been dispatched', 2, handler.getCallCount()); |
| 183 |
assertEquals( |
| 184 |
'Should be a mouseup event', 'mouseup', |
| 185 |
handler.getLastCall().getArgument(0).type); |
| 186 |
|
| 187 |
target.dispatchEvent('mouseup'); |
| 188 |
assertEquals('No event should be dispatched', 2, handler.getCallCount()); |
| 189 |
|
| 190 |
target.dispatchEvent('mousedown'); |
| 191 |
assertEquals( |
| 192 |
'1 event should have been dispatched', 3, handler.getCallCount()); |
| 193 |
assertEquals( |
| 194 |
'Should be a mousedown event', 'mousedown', |
| 195 |
handler.getLastCall().getArgument(0).type); |
| 196 |
|
| 197 |
target.dispatchEvent('mousedown'); |
| 198 |
assertEquals('No event should be dispatched', 3, handler.getCallCount()); |
| 199 |
} |
| 200 |
|
| 201 |
function testListenUnlistenWithObjectHandler() { |
| 202 |
var target = new goog.events.EventTarget(); |
| 203 |
eh = new goog.events.EventHandler(); |
| 204 |
var handlerObj = {handleEvent: goog.testing.recordFunction()}; |
| 205 |
eh.listen(target, 'click', handlerObj); |
| 206 |
|
| 207 |
target.dispatchEvent('click'); |
| 208 |
assertEquals( |
| 209 |
'One event should have been dispatched', 1, |
| 210 |
handlerObj.handleEvent.getCallCount()); |
| 211 |
|
| 212 |
target.dispatchEvent('click'); |
| 213 |
assertEquals( |
| 214 |
'One event should have been dispatched', 2, |
| 215 |
handlerObj.handleEvent.getCallCount()); |
| 216 |
|
| 217 |
eh.unlisten(target, 'click', handlerObj); |
| 218 |
target.dispatchEvent('click'); |
| 219 |
assertEquals( |
| 220 |
'No event should have been dispatched', 2, |
| 221 |
handlerObj.handleEvent.getCallCount()); |
| 222 |
} |
| 223 |
|
| 224 |
function testListenOnceWithObjectHandler() { |
| 225 |
var target = new goog.events.EventTarget(); |
| 226 |
eh = new goog.events.EventHandler(); |
| 227 |
var handlerObj = {handleEvent: goog.testing.recordFunction()}; |
| 228 |
eh.listenOnce(target, 'click', handlerObj); |
| 229 |
|
| 230 |
target.dispatchEvent('click'); |
| 231 |
assertEquals( |
| 232 |
'One event should have been dispatched', 1, |
| 233 |
handlerObj.handleEvent.getCallCount()); |
| 234 |
|
| 235 |
target.dispatchEvent('click'); |
| 236 |
assertEquals( |
| 237 |
'No event should have been dispatched', 1, |
| 238 |
handlerObj.handleEvent.getCallCount()); |
| 239 |
} |
| 240 |
|
| 241 |
function testGetListenerCount() { |
| 242 |
eh = new goog.events.EventHandler(); |
| 243 |
assertEquals('0 listeners registered initially', 0, eh.getListenerCount()); |
| 244 |
var target = new goog.events.EventTarget(); |
| 245 |
eh.listen(target, 'click', goog.nullFunction, false); |
| 246 |
eh.listen(target, 'click', goog.nullFunction, true); |
| 247 |
assertEquals('2 listeners registered', 2, eh.getListenerCount()); |
| 248 |
eh.unlisten(target, 'click', goog.nullFunction, true); |
| 249 |
assertEquals('1 listener removed, 1 left', 1, eh.getListenerCount()); |
| 250 |
eh.removeAll(); |
| 251 |
assertEquals('all listeners removed', 0, eh.getListenerCount()); |
| 252 |
} |
| 253 |
|
| 254 |
function testRemoveAllCheckSForOwnKeys() { |
| 255 |
propertyReplacer.set(Object.prototype, 'customMethod', function() { |
| 256 |
throw new Error('NOOOOOOOOOOO'); |
| 257 |
}); |
| 258 |
eh = new goog.events.EventHandler(); |
| 259 |
// If removeAll doesn't check keys using hasOwnProperty it will also try |
| 260 |
// unlisten using customMethod function as key and will throw error. |
| 261 |
eh.removeAll(); |
| 262 |
} |