OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <!-- TODO(arv): Check in Closue unit tests and make this run as part of the | |
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> | |
15 <body> | |
16 <script> | |
17 | |
18 const EventTarget = cr.EventTarget; | |
19 const Event = cr.Event; | |
20 | |
21 function testFunctionListener() { | |
22 var fi = 0; | |
23 function f(e) { | |
24 fi++; | |
25 } | |
26 | |
27 var gi = 0; | |
28 function g(e) { | |
29 gi++; | |
30 } | |
31 | |
32 var et = new EventTarget; | |
33 et.addEventListener('f', f); | |
34 et.addEventListener('g', g); | |
35 | |
36 // Adding again should not cause it to be called twice | |
37 et.addEventListener('f', f); | |
38 et.dispatchEvent(new Event('f')); | |
39 assertEquals('Should have been called once', 1, fi); | |
40 assertEquals(0, gi); | |
41 | |
42 et.removeEventListener('f', f); | |
43 et.dispatchEvent(new Event('f')); | |
44 assertEquals('Should not have been called again', 1, fi); | |
45 | |
46 et.dispatchEvent(new Event('g')); | |
47 assertEquals('Should have been called once', 1, gi); | |
48 } | |
49 | |
50 function testHandleEvent() { | |
51 var fi = 0; | |
52 var f = { | |
53 handleEvent: function(e) { | |
54 fi++; | |
55 } | |
56 }; | |
57 | |
58 var gi = 0; | |
59 var g = { | |
60 handleEvent: function(e) { | |
61 gi++; | |
62 } | |
63 }; | |
64 | |
65 var et = new EventTarget; | |
66 et.addEventListener('f', f); | |
67 et.addEventListener('g', g); | |
68 | |
69 // Adding again should not cause it to be called twice | |
70 et.addEventListener('f', f); | |
71 et.dispatchEvent(new Event('f')); | |
72 assertEquals('Should have been called once', 1, fi); | |
73 assertEquals(0, gi); | |
74 | |
75 et.removeEventListener('f', f); | |
76 et.dispatchEvent(new Event('f')); | |
77 assertEquals('Should not have been called again', 1, fi); | |
78 | |
79 et.dispatchEvent(new Event('g')); | |
80 assertEquals('Should have been called once', 1, gi); | |
81 } | |
82 | |
83 function testPreventDefault() { | |
84 var i = 0; | |
85 function prevent(e) { | |
86 i++; | |
87 e.preventDefault(); | |
88 } | |
89 | |
90 var j = 0; | |
91 function pass(e) { | |
92 j++; | |
93 } | |
94 | |
95 var et = new EventTarget; | |
96 et.addEventListener('test', pass); | |
97 | |
98 assertTrue(et.dispatchEvent(new Event('test'))); | |
99 assertEquals(1, j); | |
100 | |
101 et.addEventListener('test', prevent); | |
102 | |
103 console.log('NOW'); | |
104 assertFalse(et.dispatchEvent(new Event('test'))); | |
105 assertEquals(2, j); | |
106 assertEquals(1, i); | |
107 } | |
108 | |
109 | |
110 function testReturnFalse() { | |
111 var i = 0; | |
112 function prevent(e) { | |
113 i++; | |
114 return false; | |
115 } | |
116 | |
117 var j = 0; | |
118 function pass(e) { | |
119 j++; | |
120 } | |
121 | |
122 var et = new EventTarget; | |
123 et.addEventListener('test', pass); | |
124 | |
125 assertTrue(et.dispatchEvent(new Event('test'))); | |
126 assertEquals(1, j); | |
127 | |
128 et.addEventListener('test', prevent); | |
129 | |
130 assertFalse(et.dispatchEvent(new Event('test'))); | |
131 assertEquals(2, j); | |
132 assertEquals(1, i); | |
133 } | |
134 | |
135 </script> | |
136 </body> | |
137 </html> | |
OLD | NEW |