OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <!-- TODO(arv): Check in Closue unit tests and make this run as part of the | 4 <script src="webui_resource_test.js"></script> |
5 tests --> | |
6 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.j s"></script> | |
7 <script src="../cr.js"></script> | |
8 <script src="event_target.js"></script> | |
9 <script> | |
10 | |
11 goog.require('goog.testing.jsunit'); | |
12 | |
13 </script> | |
14 </head> | 5 </head> |
15 <body> | 6 <body> |
16 <script> | 7 <script> |
17 | 8 |
18 const EventTarget = cr.EventTarget; | 9 var EventTarget; |
Dan Beam
2013/06/20 17:57:46
nit: /** @const */
kevers
2013/06/20 18:41:37
Done.
| |
19 const Event = cr.Event; | 10 var Event; |
11 | |
12 function setUp(){ | |
13 EventTarget = cr.EventTarget; | |
14 Event = cr.Event; | |
15 } | |
20 | 16 |
21 function testFunctionListener() { | 17 function testFunctionListener() { |
22 var fi = 0; | 18 var fi = 0; |
23 function f(e) { | 19 function f(e) { |
24 fi++; | 20 fi++; |
25 } | 21 } |
26 | 22 |
27 var gi = 0; | 23 var gi = 0; |
28 function g(e) { | 24 function g(e) { |
29 gi++; | 25 gi++; |
30 } | 26 } |
31 | 27 |
32 var et = new EventTarget; | 28 var et = new EventTarget; |
33 et.addEventListener('f', f); | 29 et.addEventListener('f', f); |
34 et.addEventListener('g', g); | 30 et.addEventListener('g', g); |
35 | 31 |
36 // Adding again should not cause it to be called twice | 32 // Adding again should not cause it to be called twice |
37 et.addEventListener('f', f); | 33 et.addEventListener('f', f); |
38 et.dispatchEvent(new Event('f')); | 34 et.dispatchEvent(new Event('f')); |
39 assertEquals('Should have been called once', 1, fi); | 35 assertEquals(1, fi, 'Should have been called once'); |
40 assertEquals(0, gi); | 36 assertEquals(0, gi); |
41 | 37 |
42 et.removeEventListener('f', f); | 38 et.removeEventListener('f', f); |
43 et.dispatchEvent(new Event('f')); | 39 et.dispatchEvent(new Event('f')); |
44 assertEquals('Should not have been called again', 1, fi); | 40 assertEquals(1, fi, 'Should not have been called again'); |
45 | 41 |
46 et.dispatchEvent(new Event('g')); | 42 et.dispatchEvent(new Event('g')); |
47 assertEquals('Should have been called once', 1, gi); | 43 assertEquals(1, gi, 'Should have been called once'); |
48 } | 44 } |
49 | 45 |
50 function testHandleEvent() { | 46 function testHandleEvent() { |
51 var fi = 0; | 47 var fi = 0; |
52 var f = { | 48 var f = { |
53 handleEvent: function(e) { | 49 handleEvent: function(e) { |
54 fi++; | 50 fi++; |
55 } | 51 } |
56 }; | 52 }; |
57 | 53 |
58 var gi = 0; | 54 var gi = 0; |
59 var g = { | 55 var g = { |
60 handleEvent: function(e) { | 56 handleEvent: function(e) { |
61 gi++; | 57 gi++; |
62 } | 58 } |
63 }; | 59 }; |
64 | 60 |
65 var et = new EventTarget; | 61 var et = new EventTarget; |
66 et.addEventListener('f', f); | 62 et.addEventListener('f', f); |
67 et.addEventListener('g', g); | 63 et.addEventListener('g', g); |
68 | 64 |
69 // Adding again should not cause it to be called twice | 65 // Adding again should not cause it to be called twice |
70 et.addEventListener('f', f); | 66 et.addEventListener('f', f); |
71 et.dispatchEvent(new Event('f')); | 67 et.dispatchEvent(new Event('f')); |
72 assertEquals('Should have been called once', 1, fi); | 68 assertEquals(1, fi, 'Should have been called once'); |
73 assertEquals(0, gi); | 69 assertEquals(0, gi); |
74 | 70 |
75 et.removeEventListener('f', f); | 71 et.removeEventListener('f', f); |
76 et.dispatchEvent(new Event('f')); | 72 et.dispatchEvent(new Event('f')); |
77 assertEquals('Should not have been called again', 1, fi); | 73 assertEquals(1, fi, 'Should not have been called again'); |
78 | 74 |
79 et.dispatchEvent(new Event('g')); | 75 et.dispatchEvent(new Event('g')); |
80 assertEquals('Should have been called once', 1, gi); | 76 assertEquals(1, gi, 'Should have been called once'); |
81 } | 77 } |
82 | 78 |
83 function testPreventDefault() { | 79 function testPreventDefault() { |
84 var i = 0; | 80 var i = 0; |
85 function prevent(e) { | 81 function prevent(e) { |
86 i++; | 82 i++; |
87 e.preventDefault(); | 83 e.preventDefault(); |
88 } | 84 } |
89 | 85 |
90 var j = 0; | 86 var j = 0; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 et.addEventListener('test', prevent); | 124 et.addEventListener('test', prevent); |
129 | 125 |
130 assertFalse(et.dispatchEvent(new Event('test'))); | 126 assertFalse(et.dispatchEvent(new Event('test'))); |
131 assertEquals(2, j); | 127 assertEquals(2, j); |
132 assertEquals(1, i); | 128 assertEquals(1, i); |
133 } | 129 } |
134 | 130 |
135 </script> | 131 </script> |
136 </body> | 132 </body> |
137 </html> | 133 </html> |
OLD | NEW |