- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / webui / event_target_test.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script>
5
6 /* @const */ var EventTarget;
7
8 function setUp(){
9   EventTarget = cr.EventTarget;
10 }
11
12 function testFunctionListener() {
13   var fi = 0;
14   function f(e) {
15     fi++;
16   }
17
18   var gi = 0;
19   function g(e) {
20     gi++;
21   }
22
23   var et = new EventTarget;
24   et.addEventListener('f', f);
25   et.addEventListener('g', g);
26
27   // Adding again should not cause it to be called twice
28   et.addEventListener('f', f);
29   et.dispatchEvent(new Event('f'));
30   assertEquals(1, fi, 'Should have been called once');
31   assertEquals(0, gi);
32
33   et.removeEventListener('f', f);
34   et.dispatchEvent(new Event('f'));
35   assertEquals(1, fi, 'Should not have been called again');
36
37   et.dispatchEvent(new Event('g'));
38   assertEquals(1, gi, 'Should have been called once');
39 }
40
41 function testHandleEvent() {
42   var fi = 0;
43   var f = {
44     handleEvent: function(e) {
45       fi++;
46     }
47   };
48
49   var gi = 0;
50   var g = {
51     handleEvent: function(e) {
52       gi++;
53     }
54   };
55
56   var et = new EventTarget;
57   et.addEventListener('f', f);
58   et.addEventListener('g', g);
59
60   // Adding again should not cause it to be called twice
61   et.addEventListener('f', f);
62   et.dispatchEvent(new Event('f'));
63   assertEquals(1, fi, 'Should have been called once');
64   assertEquals(0, gi);
65
66   et.removeEventListener('f', f);
67   et.dispatchEvent(new Event('f'));
68   assertEquals(1, fi, 'Should not have been called again');
69
70   et.dispatchEvent(new Event('g'));
71   assertEquals(1, gi, 'Should have been called once');
72 }
73
74 function testPreventDefault() {
75   var i = 0;
76   function prevent(e) {
77     i++;
78     e.preventDefault();
79   }
80
81   var j = 0;
82   function pass(e) {
83     j++;
84   }
85
86   var et = new EventTarget;
87   et.addEventListener('test', pass);
88
89   assertTrue(et.dispatchEvent(new Event('test', {cancelable: true})));
90   assertEquals(1, j);
91
92   et.addEventListener('test', prevent);
93
94   console.log('NOW');
95   assertFalse(et.dispatchEvent(new Event('test', {cancelable: true})));
96   assertEquals(2, j);
97   assertEquals(1, i);
98 }
99
100
101 function testReturnFalse() {
102   var i = 0;
103   function prevent(e) {
104     i++;
105     return false;
106   }
107
108   var j = 0;
109   function pass(e) {
110     j++;
111   }
112
113   var et = new EventTarget;
114   et.addEventListener('test', pass);
115
116   assertTrue(et.dispatchEvent(new Event('test', {cancelable: true})));
117   assertEquals(1, j);
118
119   et.addEventListener('test', prevent);
120
121   assertFalse(et.dispatchEvent(new Event('test', {cancelable: true})));
122   assertEquals(2, j);
123   assertEquals(1, i);
124 }
125
126 </script>
127 </body>
128 </html>