OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library ElementTest; | 5 library ElementTest; |
6 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; |
7 import '../../pkg/unittest/lib/html_individual_config.dart'; | 7 import '../../pkg/unittest/lib/html_individual_config.dart'; |
| 8 import 'dart:async'; |
8 import 'dart:html'; | 9 import 'dart:html'; |
9 import 'dart:svg' as svg; | 10 import 'dart:svg' as svg; |
10 | 11 |
11 expectLargeRect(ClientRect rect) { | 12 expectLargeRect(ClientRect rect) { |
12 expect(rect.top, 0); | 13 expect(rect.top, 0); |
13 expect(rect.left, 0); | 14 expect(rect.left, 0); |
14 expect(rect.width, greaterThan(100)); | 15 expect(rect.width, greaterThan(100)); |
15 expect(rect.height, greaterThan(100)); | 16 expect(rect.height, greaterThan(100)); |
16 expect(rect.bottom, rect.top + rect.height); | 17 expect(rect.bottom, rect.top + rect.height); |
17 expect(rect.right, rect.left + rect.width); | 18 expect(rect.right, rect.left + rect.width); |
18 } | 19 } |
19 | 20 |
20 void testEventHelper(EventListenerList listenerList, String type, | |
21 [Function registerOnEventListener = null]) { | |
22 testMultipleEventHelper(listenerList, [type], registerOnEventListener); | |
23 } | |
24 // Allows testing where we polyfill different browsers firing different events. | |
25 void testMultipleEventHelper(EventListenerList listenerList, List<String> types, | |
26 [Function registerOnEventListener = null]) { | |
27 bool firedWhenAddedToListenerList = false; | |
28 bool firedOnEvent = false; | |
29 listenerList.add((e) { | |
30 firedWhenAddedToListenerList = true; | |
31 }); | |
32 if (registerOnEventListener != null) { | |
33 registerOnEventListener((e) { | |
34 firedOnEvent = true; | |
35 }); | |
36 } | |
37 for (var type in types) { | |
38 final event = new Event(type); | |
39 listenerList.dispatch(event); | |
40 } | |
41 | |
42 expect(firedWhenAddedToListenerList, isTrue); | |
43 if (registerOnEventListener != null) { | |
44 expect(firedOnEvent, isTrue); | |
45 } | |
46 } | |
47 | |
48 main() { | 21 main() { |
49 useHtmlIndividualConfiguration(); | 22 useHtmlIndividualConfiguration(); |
50 | 23 |
51 var isHRElement = predicate((x) => x is HRElement, 'is a HRElement'); | 24 var isHRElement = predicate((x) => x is HRElement, 'is a HRElement'); |
52 var isBRElement = predicate((x) => x is BRElement, 'is a BRElement'); | 25 var isBRElement = predicate((x) => x is BRElement, 'is a BRElement'); |
53 var isInputElement = | 26 var isInputElement = |
54 predicate((x) => x is InputElement, 'is an InputElement'); | 27 predicate((x) => x is InputElement, 'is an InputElement'); |
55 var isImageElement = | 28 var isImageElement = |
56 predicate((x) => x is ImageElement, 'is an ImageElement'); | 29 predicate((x) => x is ImageElement, 'is an ImageElement'); |
57 var isSpanElement = predicate((x) => x is SpanElement, 'is a SpanElement'); | 30 var isSpanElement = predicate((x) => x is SpanElement, 'is a SpanElement'); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 var node = new Element.html('<th>foobar'); | 198 var node = new Element.html('<th>foobar'); |
226 expect(node, predicate((x) => x is TableCellElement, | 199 expect(node, predicate((x) => x is TableCellElement, |
227 'is a TableCellElement')); | 200 'is a TableCellElement')); |
228 expect(node.tagName, 'TH'); | 201 expect(node.tagName, 'TH'); |
229 expect(node.parent, isNull); | 202 expect(node.parent, isNull); |
230 expect(node.innerHtml, 'foobar'); | 203 expect(node.innerHtml, 'foobar'); |
231 }); | 204 }); |
232 }); | 205 }); |
233 | 206 |
234 group('eventListening', () { | 207 group('eventListening', () { |
235 test('eventListeners', () { | 208 test('streams', () { |
236 final element = new Element.tag('div'); | 209 final target = new Element.tag('div'); |
237 final on = element.on; | |
238 | 210 |
239 testEventHelper(on.abort, 'abort', | 211 void testEvent(Stream stream, String type) { |
240 (listener) => Testing.addEventListener( | 212 var firedOnEvent = false; |
241 element, 'abort', listener, true)); | 213 stream.listen((e) { |
242 testEventHelper(on.beforeCopy, 'beforecopy', | 214 firedOnEvent = true; |
243 (listener) => Testing.addEventListener( | 215 }); |
244 element, 'beforecopy', listener, true)); | 216 expect(firedOnEvent, isFalse); |
245 testEventHelper(on.beforeCut, 'beforecut', | 217 var event = new Event(type); |
246 (listener) => Testing.addEventListener( | 218 target.dispatchEvent(event); |
247 element, 'beforecut', listener, true)); | 219 |
248 testEventHelper(on.beforePaste, 'beforepaste', | 220 expect(firedOnEvent, isTrue); |
249 (listener) => Testing.addEventListener( | 221 } |
250 element, 'beforepaste', listener, true)); | 222 |
251 testEventHelper(on.blur, 'blur', | 223 testEvent(target.onAbort, 'abort'); |
252 (listener) => Testing.addEventListener( | 224 testEvent(target.onBeforeCopy, 'beforecopy'); |
253 element, 'blur', listener, true)); | 225 testEvent(target.onBeforeCut, 'beforecut'); |
254 testEventHelper(on.change, 'change', | 226 testEvent(target.onBeforePaste, 'beforepaste'); |
255 (listener) => Testing.addEventListener( | 227 testEvent(target.onBlur, 'blur'); |
256 element, 'change', listener, true)); | 228 testEvent(target.onChange, 'change'); |
257 testEventHelper(on.contextMenu, 'contextmenu', | 229 testEvent(target.onContextMenu, 'contextmenu'); |
258 (listener) => Testing.addEventListener( | 230 testEvent(target.onCopy, 'copy'); |
259 element, 'contextmenu', listener, true)); | 231 testEvent(target.onCut, 'cut'); |
260 testEventHelper(on.copy, 'copy', | 232 testEvent(target.onDoubleClick, 'dblclick'); |
261 (listener) => Testing.addEventListener( | 233 testEvent(target.onDrag, 'drag'); |
262 element, 'copy', listener, true)); | 234 testEvent(target.onDragEnd, 'dragend'); |
263 testEventHelper(on.cut, 'cut', | 235 testEvent(target.onDragEnter, 'dragenter'); |
264 (listener) => Testing.addEventListener( | 236 testEvent(target.onDragLeave, 'dragleave'); |
265 element, 'cut', listener, true)); | 237 testEvent(target.onDragOver, 'dragover'); |
266 testEventHelper(on.doubleClick, 'dblclick', | 238 testEvent(target.onDragStart, 'dragstart'); |
267 (listener) => Testing.addEventListener( | 239 testEvent(target.onDrop, 'drop'); |
268 element, 'dblclick', listener, true)); | 240 testEvent(target.onError, 'error'); |
269 testEventHelper(on.drag, 'drag', | 241 testEvent(target.onFocus, 'focus'); |
270 (listener) => Testing.addEventListener( | 242 testEvent(target.onFullscreenChange, 'webkitfullscreenchange'); |
271 element, 'drag', listener, true)); | 243 testEvent(target.onInput, 'input'); |
272 testEventHelper(on.dragEnd, 'dragend', | 244 testEvent(target.onInvalid, 'invalid'); |
273 (listener) => Testing.addEventListener( | 245 testEvent(target.onKeyDown, 'keydown'); |
274 element, 'dragend', listener, true)); | 246 testEvent(target.onKeyPress, 'keypress'); |
275 testEventHelper(on.dragEnter, 'dragenter', | 247 testEvent(target.onKeyUp, 'keyup'); |
276 (listener) => Testing.addEventListener( | 248 testEvent(target.onLoad, 'load'); |
277 element, 'dragenter', listener, true)); | 249 testEvent(target.onMouseDown, 'mousedown'); |
278 testEventHelper(on.dragLeave, 'dragleave', | 250 testEvent(target.onMouseMove, 'mousemove'); |
279 (listener) => Testing.addEventListener( | 251 testEvent(target.onMouseOut, 'mouseout'); |
280 element, 'dragleave', listener, true)); | 252 testEvent(target.onMouseOver, 'mouseover'); |
281 testEventHelper(on.dragOver, 'dragover', | 253 testEvent(target.onMouseUp, 'mouseup'); |
282 (listener) => Testing.addEventListener( | 254 testEvent(target.onPaste, 'paste'); |
283 element, 'dragover', listener, true)); | 255 testEvent(target.onReset, 'reset'); |
284 testEventHelper(on.dragStart, 'dragstart', | 256 testEvent(target.onScroll, 'scroll'); |
285 (listener) => Testing.addEventListener( | 257 testEvent(target.onSearch, 'search'); |
286 element, 'dragstart', listener, true)); | 258 testEvent(target.onSelect, 'select'); |
287 testEventHelper(on.drop, 'drop', | 259 testEvent(target.onSelectStart, 'selectstart'); |
288 (listener) => Testing.addEventListener( | 260 testEvent(target.onSubmit, 'submit'); |
289 element, 'drop', listener, true)); | 261 testEvent(target.onTouchCancel, 'touchcancel'); |
290 testEventHelper(on.error, 'error', | 262 testEvent(target.onTouchEnd, 'touchend'); |
291 (listener) => Testing.addEventListener( | 263 testEvent(target.onTouchLeave, 'touchleave'); |
292 element, 'error', listener, true)); | 264 testEvent(target.onTouchMove, 'touchmove'); |
293 testEventHelper(on.focus, 'focus', | 265 testEvent(target.onTouchStart, 'touchstart'); |
294 (listener) => Testing.addEventListener( | 266 testEvent(target.onTransitionEnd, 'webkitTransitionEnd'); |
295 element, 'focus', listener, true)); | |
296 testEventHelper(on.input, 'input', | |
297 (listener) => Testing.addEventListener( | |
298 element, 'input', listener, true)); | |
299 testEventHelper(on.invalid, 'invalid', | |
300 (listener) => Testing.addEventListener( | |
301 element, 'invalid', listener, true)); | |
302 testEventHelper(on.keyDown, 'keydown', | |
303 (listener) => Testing.addEventListener( | |
304 element, 'keydown', listener, true)); | |
305 testEventHelper(on.keyPress, 'keypress', | |
306 (listener) => Testing.addEventListener( | |
307 element, 'keypress', listener, true)); | |
308 testEventHelper(on.keyUp, 'keyup', | |
309 (listener) => Testing.addEventListener( | |
310 element, 'keyup', listener, true)); | |
311 testEventHelper(on.load, 'load', | |
312 (listener) => Testing.addEventListener( | |
313 element, 'load', listener, true)); | |
314 testEventHelper(on.mouseDown, 'mousedown', | |
315 (listener) => Testing.addEventListener( | |
316 element, 'mousedown', listener, true)); | |
317 testEventHelper(on.mouseMove, 'mousemove', | |
318 (listener) => Testing.addEventListener( | |
319 element, 'mousemove', listener, true)); | |
320 testEventHelper(on.mouseOut, 'mouseout', | |
321 (listener) => Testing.addEventListener( | |
322 element, 'mouseout', listener, true)); | |
323 testEventHelper(on.mouseOver, 'mouseover', | |
324 (listener) => Testing.addEventListener( | |
325 element, 'mouseover', listener, true)); | |
326 testEventHelper(on.mouseUp, 'mouseup', | |
327 (listener) => Testing.addEventListener( | |
328 element, 'mouseup', listener, true)); | |
329 // Browsers have different events that they use, so fire all variants. | |
330 testMultipleEventHelper(on.mouseWheel, | |
331 ['mousewheel', 'wheel', 'DOMMouseScroll'], | |
332 (listener) => Testing.addEventListener( | |
333 element, 'mousewheel', listener, true)); | |
334 testEventHelper(on.paste, 'paste', | |
335 (listener) => Testing.addEventListener( | |
336 element, 'paste', listener, true)); | |
337 testEventHelper(on.reset, 'reset', | |
338 (listener) => Testing.addEventListener( | |
339 element, 'reset', listener, true)); | |
340 testEventHelper(on.scroll, 'scroll', | |
341 (listener) => Testing.addEventListener( | |
342 element, 'scroll', listener, true)); | |
343 testEventHelper(on.search, 'search', | |
344 (listener) => Testing.addEventListener( | |
345 element, 'search', listener, true)); | |
346 testEventHelper(on.select, 'select', | |
347 (listener) => Testing.addEventListener( | |
348 element, 'select', listener, true)); | |
349 testEventHelper(on.selectStart, 'selectstart', | |
350 (listener) => Testing.addEventListener( | |
351 element, 'selectstart', listener, true)); | |
352 testEventHelper(on.submit, 'submit', | |
353 (listener) => Testing.addEventListener( | |
354 element, 'submit', listener, true)); | |
355 testEventHelper(on.touchCancel, 'touchcancel', | |
356 (listener) => Testing.addEventListener( | |
357 element, 'touchcancel', listener, true)); | |
358 testEventHelper(on.touchEnd, 'touchend', | |
359 (listener) => Testing.addEventListener( | |
360 element, 'touchend', listener, true)); | |
361 testEventHelper(on.touchLeave, 'touchleave'); | |
362 testEventHelper(on.touchMove, 'touchmove', | |
363 (listener) => Testing.addEventListener( | |
364 element, 'touchmove', listener, true)); | |
365 testEventHelper(on.touchStart, 'touchstart', | |
366 (listener) => Testing.addEventListener( | |
367 element, 'touchstart', listener, true)); | |
368 testEventHelper(on.transitionEnd, 'webkitTransitionEnd'); | |
369 testEventHelper(on.fullscreenChange, 'webkitfullscreenchange', | |
370 (listener) => Testing.addEventListener(element, | |
371 'webkitfullscreenchange', listener, true)); | |
372 }); | 267 }); |
373 }); | 268 }); |
374 | 269 |
375 group('click', () { | 270 group('click', () { |
376 test('clickEvent', () { | 271 test('clickEvent', () { |
377 var e = new DivElement(); | 272 var e = new DivElement(); |
378 var firedEvent = false; | 273 var firedEvent = false; |
379 e.on.click.add((event) { | 274 e.onClick.listen((event) { |
380 firedEvent = true; | 275 firedEvent = true; |
381 }); | 276 }); |
382 expect(firedEvent, false); | 277 expect(firedEvent, false); |
383 e.click(); | 278 e.click(); |
384 expect(firedEvent, true); | 279 expect(firedEvent, true); |
385 }); | 280 }); |
386 }); | 281 }); |
387 | 282 |
388 group('attributes', () { | 283 group('attributes', () { |
389 test('coercion', () { | 284 test('coercion', () { |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 }); | 630 }); |
736 | 631 |
737 test('getRange', () { | 632 test('getRange', () { |
738 var range = makeElList().getRange(1, 2); | 633 var range = makeElList().getRange(1, 2); |
739 expect(range, isElementList); | 634 expect(range, isElementList); |
740 expect(range[0], isImageElement); | 635 expect(range[0], isImageElement); |
741 expect(range[1], isInputElement); | 636 expect(range[1], isInputElement); |
742 }); | 637 }); |
743 }); | 638 }); |
744 } | 639 } |
OLD | NEW |