| <!DOCTYPE html> |
| <html> |
| <body> |
| <script> |
| |
| /* @const */ var EventTarget; |
| |
| function setUp(){ |
| EventTarget = cr.EventTarget; |
| } |
| |
| function testFunctionListener() { |
| var fi = 0; |
| function f(e) { |
| fi++; |
| } |
| |
| var gi = 0; |
| function g(e) { |
| gi++; |
| } |
| |
| var et = new EventTarget; |
| et.addEventListener('f', f); |
| et.addEventListener('g', g); |
| |
| // Adding again should not cause it to be called twice |
| et.addEventListener('f', f); |
| et.dispatchEvent(new Event('f')); |
| assertEquals(1, fi, 'Should have been called once'); |
| assertEquals(0, gi); |
| |
| et.removeEventListener('f', f); |
| et.dispatchEvent(new Event('f')); |
| assertEquals(1, fi, 'Should not have been called again'); |
| |
| et.dispatchEvent(new Event('g')); |
| assertEquals(1, gi, 'Should have been called once'); |
| } |
| |
| function testHandleEvent() { |
| var fi = 0; |
| var f = { |
| handleEvent: function(e) { |
| fi++; |
| } |
| }; |
| |
| var gi = 0; |
| var g = { |
| handleEvent: function(e) { |
| gi++; |
| } |
| }; |
| |
| var et = new EventTarget; |
| et.addEventListener('f', f); |
| et.addEventListener('g', g); |
| |
| // Adding again should not cause it to be called twice |
| et.addEventListener('f', f); |
| et.dispatchEvent(new Event('f')); |
| assertEquals(1, fi, 'Should have been called once'); |
| assertEquals(0, gi); |
| |
| et.removeEventListener('f', f); |
| et.dispatchEvent(new Event('f')); |
| assertEquals(1, fi, 'Should not have been called again'); |
| |
| et.dispatchEvent(new Event('g')); |
| assertEquals(1, gi, 'Should have been called once'); |
| } |
| |
| function testPreventDefault() { |
| var i = 0; |
| function prevent(e) { |
| i++; |
| e.preventDefault(); |
| } |
| |
| var j = 0; |
| function pass(e) { |
| j++; |
| } |
| |
| var et = new EventTarget; |
| et.addEventListener('test', pass); |
| |
| assertTrue(et.dispatchEvent(new Event('test', {cancelable: true}))); |
| assertEquals(1, j); |
| |
| et.addEventListener('test', prevent); |
| |
| console.log('NOW'); |
| assertFalse(et.dispatchEvent(new Event('test', {cancelable: true}))); |
| assertEquals(2, j); |
| assertEquals(1, i); |
| } |
| |
| |
| function testReturnFalse() { |
| var i = 0; |
| function prevent(e) { |
| i++; |
| return false; |
| } |
| |
| var j = 0; |
| function pass(e) { |
| j++; |
| } |
| |
| var et = new EventTarget; |
| et.addEventListener('test', pass); |
| |
| assertTrue(et.dispatchEvent(new Event('test', {cancelable: true}))); |
| assertEquals(1, j); |
| |
| et.addEventListener('test', prevent); |
| |
| assertFalse(et.dispatchEvent(new Event('test', {cancelable: true}))); |
| assertEquals(2, j); |
| assertEquals(1, i); |
| } |
| |
| </script> |
| </body> |
| </html> |