LEFT | RIGHT |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 | 2 |
3 <!-- | 3 <!-- |
4 | 4 |
5 Author: nicksantos@google.com (Nick Santos) | 5 Author: nicksantos@google.com (Nick Santos) |
6 --> | 6 --> |
7 | 7 |
8 <html> | 8 <html> |
9 <!-- | 9 <!-- |
10 Copyright 2009 The Closure Library Authors. All Rights Reserved. | 10 Copyright 2009 The Closure Library Authors. All Rights Reserved. |
11 | 11 |
12 Use of this source code is governed by the Apache License, Version 2.0. | 12 Use of this source code is governed by the Apache License, Version 2.0. |
13 See the COPYING file for details. | 13 See the COPYING file for details. |
14 --> | 14 --> |
15 <head> | 15 <head> |
16 <meta http-equiv="X-UA-Compatible" content="IE=edge"> | 16 <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
17 <title>JsUnit tests for goog.events.BrowserEvent</title> | 17 <title>JsUnit tests for goog.events.BrowserEvent</title> |
18 <script type='text/javascript' src='../base.js'></script> | 18 <script type='text/javascript' src='../base.js'></script> |
19 <script type='text/javascript'> | 19 <script type='text/javascript'> |
20 | 20 |
21 goog.require('goog.events.BrowserEvent'); | 21 goog.require('goog.events.BrowserEvent'); |
22 goog.require('goog.math.Coordinate'); | |
23 goog.require('goog.testing.PropertyReplacer'); | 22 goog.require('goog.testing.PropertyReplacer'); |
24 goog.require('goog.testing.jsunit'); | 23 goog.require('goog.testing.jsunit'); |
25 goog.require('goog.userAgent'); | 24 goog.require('goog.userAgent'); |
26 | 25 |
27 | 26 |
28 </script> | 27 </script> |
29 </head> | 28 </head> |
30 <body> | 29 <body> |
31 <script type='text/javascript'> | 30 <script type='text/javascript'> |
32 | 31 |
(...skipping 18 matching lines...) Expand all Loading... |
51 function() { | 50 function() { |
52 throw Error('https://bugzilla.mozilla.org/show_bug.cgi?id=497780'); | 51 throw Error('https://bugzilla.mozilla.org/show_bug.cgi?id=497780'); |
53 }); | 52 }); |
54 assertThrows(function() { return event.relatedTarget.nodeName; }); | 53 assertThrows(function() { return event.relatedTarget.nodeName; }); |
55 | 54 |
56 var bEvent = new goog.events.BrowserEvent(event); | 55 var bEvent = new goog.events.BrowserEvent(event); |
57 assertEquals(event, bEvent.event_); | 56 assertEquals(event, bEvent.event_); |
58 assertNull(bEvent.relatedTarget); | 57 assertNull(bEvent.relatedTarget); |
59 } | 58 } |
60 | 59 |
| 60 function testPreventDefault() { |
| 61 var event = {}; |
| 62 event.defaultPrevented = false; |
| 63 var bEvent = new goog.events.BrowserEvent(event); |
| 64 assertFalse(bEvent.defaultPrevented); |
| 65 bEvent.preventDefault(); |
| 66 assertTrue(bEvent.defaultPrevented); |
| 67 } |
| 68 |
| 69 function testDefaultPrevented() { |
| 70 var event = {}; |
| 71 event.defaultPrevented = true; |
| 72 var bEvent = new goog.events.BrowserEvent(event); |
| 73 assertTrue(bEvent.defaultPrevented); |
| 74 } |
| 75 |
61 function testIsButtonIe() { | 76 function testIsButtonIe() { |
62 stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', false); | 77 stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', false); |
63 assertIsButton( | 78 assertIsButton( |
64 createMouseEvent('mousedown', 1), | 79 createMouseEvent('mousedown', 1), |
65 Button.LEFT, | 80 Button.LEFT, |
66 true); | 81 true); |
67 assertIsButton( | 82 assertIsButton( |
68 createMouseEvent('click', 0), | 83 createMouseEvent('click', 0), |
69 Button.LEFT, | 84 Button.LEFT, |
70 true); | 85 true); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 createMouseEvent('mousedown', 0), | 131 createMouseEvent('mousedown', 0), |
117 Button.LEFT, | 132 Button.LEFT, |
118 true); | 133 true); |
119 assertIsButton( | 134 assertIsButton( |
120 createMouseEvent('mousedown', 2, true), | 135 createMouseEvent('mousedown', 2, true), |
121 Button.RIGHT, | 136 Button.RIGHT, |
122 false); | 137 false); |
123 } | 138 } |
124 | 139 |
125 function testTouchEventHandling() { | 140 function testTouchEventHandling() { |
126 var clientCoords = new goog.math.Coordinate(5, 5); | 141 var clientCoords = [5, 5]; |
127 var screenCoords = new goog.math.Coordinate(10, 10); | 142 var screenCoords = [10, 10]; |
128 var target = document.body; | 143 var target = document.body; |
129 var touchStart = createTargetTouchEvent('touchstart', target, clientCoords, | 144 var touchStart = createTargetTouchEvent('touchstart', target, clientCoords, |
130 screenCoords); | 145 screenCoords); |
131 var touchMove = createTargetTouchEvent('touchmove', target, clientCoords, | 146 var touchMove = createTargetTouchEvent('touchmove', target, clientCoords, |
132 screenCoords); | 147 screenCoords); |
133 var touchEnd = createChangedTouchEvent('touchend', target, clientCoords, | 148 var touchEnd = createChangedTouchEvent('touchend', target, clientCoords, |
134 screenCoords); | 149 screenCoords); |
135 var touchCancel = createChangedTouchEvent('touchcancel', target, | 150 var touchCancel = createChangedTouchEvent('touchcancel', target, |
136 clientCoords, screenCoords); | 151 clientCoords, screenCoords); |
137 | 152 |
138 assertEquals(clientCoords.x, touchStart.clientX); | 153 assertEquals(clientCoords[0], touchStart.clientX); |
139 assertEquals(clientCoords.y, touchStart.clientY); | 154 assertEquals(clientCoords[1], touchStart.clientY); |
140 assertEquals(target, touchStart.target); | 155 assertEquals(target, touchStart.target); |
141 | 156 |
142 assertEquals(screenCoords.x, touchMove.screenX); | 157 assertEquals(screenCoords[0], touchMove.screenX); |
143 assertEquals(screenCoords.y, touchMove.screenY); | 158 assertEquals(screenCoords[1], touchMove.screenY); |
144 | 159 |
145 assertEquals(clientCoords.x, touchEnd.clientX); | 160 assertEquals(clientCoords[0], touchEnd.clientX); |
146 assertEquals(clientCoords.y, touchEnd.clientY); | 161 assertEquals(clientCoords[1], touchEnd.clientY); |
147 | 162 |
148 assertEquals(screenCoords.x, touchCancel.screenX); | 163 assertEquals(screenCoords[0], touchCancel.screenX); |
149 assertEquals(screenCoords.y, touchCancel.screenY); | 164 assertEquals(screenCoords[1], touchCancel.screenY); |
150 assertEquals(target, touchCancel.target); | 165 assertEquals(target, touchCancel.target); |
151 } | 166 } |
152 | 167 |
153 function createMouseEvent(type, button, opt_ctrlKey) { | 168 function createMouseEvent(type, button, opt_ctrlKey) { |
154 return new goog.events.BrowserEvent({ | 169 return new goog.events.BrowserEvent({ |
155 type: type, | 170 type: type, |
156 button: button, | 171 button: button, |
157 ctrlKey: !!opt_ctrlKey | 172 ctrlKey: !!opt_ctrlKey |
158 }); | 173 }); |
159 } | 174 } |
160 | 175 |
161 function createTargetTouchEvent(type, target, clientCoords, screenCoords) { | 176 function createTargetTouchEvent(type, target, clientCoords, screenCoords) { |
162 return new goog.events.BrowserEvent({ | 177 return new goog.events.BrowserEvent({ |
163 type: type, | 178 type: type, |
164 targetTouches: [{ | 179 targetTouches: [{ |
165 target: target, | 180 target: target, |
166 clientX: clientCoords.x, | 181 clientX: clientCoords[0], |
167 clientY: clientCoords.y, | 182 clientY: clientCoords[1], |
168 screenX: screenCoords.x, | 183 screenX: screenCoords[0], |
169 screenY: screenCoords.y | 184 screenY: screenCoords[1] |
170 }] | 185 }] |
171 }); | 186 }); |
172 } | 187 } |
173 | 188 |
174 function createChangedTouchEvent(type, target, clientCoords, screenCoords) { | 189 function createChangedTouchEvent(type, target, clientCoords, screenCoords) { |
175 return new goog.events.BrowserEvent({ | 190 return new goog.events.BrowserEvent({ |
176 type: type, | 191 type: type, |
177 changedTouches: [{ | 192 changedTouches: [{ |
178 target: target, | 193 target: target, |
179 clientX: clientCoords.x, | 194 clientX: clientCoords[0], |
180 clientY: clientCoords.y, | 195 clientY: clientCoords[1], |
181 screenX: screenCoords.x, | 196 screenX: screenCoords[0], |
182 screenY: screenCoords.y | 197 screenY: screenCoords[1] |
183 }] | 198 }] |
184 }); | 199 }); |
185 } | 200 } |
186 | 201 |
187 function assertIsButton(event, button, isActionButton) { | 202 function assertIsButton(event, button, isActionButton) { |
188 for (var key in Button) { | 203 for (var key in Button) { |
189 assertEquals( | 204 assertEquals( |
190 'Testing isButton(' + key + ') against ' + | 205 'Testing isButton(' + key + ') against ' + |
191 button + ' and type ' + event.type, | 206 button + ' and type ' + event.type, |
192 Button[key] == button, event.isButton(Button[key])); | 207 Button[key] == button, event.isButton(Button[key])); |
193 } | 208 } |
194 | 209 |
195 assertEquals(isActionButton, event.isMouseActionButton()); | 210 assertEquals(isActionButton, event.isMouseActionButton()); |
196 } | 211 } |
197 | 212 |
198 </script> | 213 </script> |
199 </body> | 214 </body> |
200 </html> | 215 </html> |
LEFT | RIGHT |