OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library tests.html.events_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:html'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 import 'package:unittest/html_config.dart'; |
| 11 |
| 12 main() { |
| 13 useHtmlConfiguration(); |
| 14 |
| 15 test('TimeStamp', () { |
| 16 Event event = new Event('test'); |
| 17 |
| 18 int timeStamp = event.timeStamp; |
| 19 expect(timeStamp, greaterThan(0)); |
| 20 }); |
| 21 |
| 22 test('Event canBubble and cancelable', () { |
| 23 // Try every combination of canBubble and cancelable |
| 24 for (var i = 0; i < 4; i++) { |
| 25 var bubble = (i & 1) != 0; |
| 26 var cancel = (i & 2) != 0; |
| 27 var e = new Event('input', canBubble: bubble, cancelable: cancel); |
| 28 expect(e.bubbles, bubble, reason: 'canBubble was set to $bubble'); |
| 29 expect(e.cancelable, cancel, reason: 'cancelable was set to $cancel'); |
| 30 } |
| 31 }); |
| 32 |
| 33 // The next test is not asynchronous because [on['test'].dispatch(event)] fire
s the event |
| 34 // and event listener synchronously. |
| 35 test('EventTarget', () { |
| 36 Element element = new Element.tag('test'); |
| 37 element.id = 'eventtarget'; |
| 38 window.document.body.append(element); |
| 39 |
| 40 int invocationCounter = 0; |
| 41 void handler(Event e) { |
| 42 expect(e.type, equals('test')); |
| 43 Element target = e.target; |
| 44 expect(element, equals(target)); |
| 45 invocationCounter++; |
| 46 } |
| 47 |
| 48 Event event = new Event('test'); |
| 49 |
| 50 invocationCounter = 0; |
| 51 element.dispatchEvent(event); |
| 52 expect(invocationCounter, isZero); |
| 53 |
| 54 var provider = new EventStreamProvider<Event>('test'); |
| 55 |
| 56 var sub = provider.forTarget(element).listen(handler); |
| 57 invocationCounter = 0; |
| 58 element.dispatchEvent(event); |
| 59 expect(invocationCounter, 1); |
| 60 |
| 61 sub.cancel(); |
| 62 invocationCounter = 0; |
| 63 element.dispatchEvent(event); |
| 64 expect(invocationCounter, isZero); |
| 65 |
| 66 provider.forTarget(element).listen(handler); |
| 67 invocationCounter = 0; |
| 68 element.dispatchEvent(event); |
| 69 expect(invocationCounter, 1); |
| 70 |
| 71 provider.forTarget(element).listen(handler); |
| 72 invocationCounter = 0; |
| 73 element.dispatchEvent(event); |
| 74 |
| 75 // NOTE: when run in a custom zone, the handler is wrapped |
| 76 // The logic for html events which ensures identical handlers are added only |
| 77 // once is therefor muted by the wrapped handlers. |
| 78 // Hence, we get different behavior depending on the current zone. |
| 79 if(Zone.current == Zone.ROOT) { |
| 80 expect(invocationCounter, 1); |
| 81 } else { |
| 82 expect(invocationCounter, 2); |
| 83 } |
| 84 }); |
| 85 |
| 86 test('InitMouseEvent', () { |
| 87 DivElement div = new Element.tag('div'); |
| 88 MouseEvent event = new MouseEvent('zebra', relatedTarget: div); |
| 89 }); |
| 90 |
| 91 test('DOM event callbacks are associated with the correct zone', () { |
| 92 var callbacks = []; |
| 93 |
| 94 final element = new Element.tag('test'); |
| 95 element.id = 'eventtarget'; |
| 96 document.body.append(element); |
| 97 |
| 98 // runZoned executes the function synchronously, but we don't want to |
| 99 // rely on this. We therefore wrap it into an expectAsync. |
| 100 runZoned(expectAsync(() { |
| 101 Zone zone = Zone.current; |
| 102 expect(zone, isNot(equals(Zone.ROOT))); |
| 103 |
| 104 var sub; |
| 105 |
| 106 void handler(Event e) { |
| 107 expect(Zone.current, equals(zone)); |
| 108 |
| 109 scheduleMicrotask(expectAsync(() { |
| 110 expect(Zone.current, equals(zone)); |
| 111 sub.cancel(); |
| 112 })); |
| 113 } |
| 114 |
| 115 sub = element.on['test'].listen(expectAsync(handler)); |
| 116 })); |
| 117 element.dispatchEvent(new Event('test')); |
| 118 }); |
| 119 } |
OLD | NEW |