OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, 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 EventTaskZoneTest; | |
6 | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'package:unittest/html_config.dart'; | |
9 import 'dart:async'; | |
10 import 'dart:html'; | |
11 | |
12 // Tests zone tasks with DOM events. | |
13 | |
14 class AbortedEventStreamSubscription implements StreamSubscription<Event> { | |
15 final Zone zone; | |
16 | |
17 AbortedEventStreamSubscription(this.zone); | |
18 | |
19 @override | |
20 Future asFuture([futureValue]) { | |
21 throw new UnsupportedError("asFuture"); | |
22 } | |
23 | |
24 @override | |
25 Future cancel() { | |
26 return null; | |
27 } | |
28 | |
29 @override | |
30 bool get isPaused => throw new UnsupportedError("pause"); | |
31 | |
32 @override | |
33 void onData(void handleData(Event data)) { | |
34 throw new UnsupportedError("cancel"); | |
35 } | |
36 | |
37 @override | |
38 void onDone(void handleDone()) { | |
39 throw new UnsupportedError("onDone"); | |
40 } | |
41 | |
42 @override | |
43 void onError(Function handleError) { | |
44 throw new UnsupportedError("onError"); | |
45 } | |
46 | |
47 @override | |
48 void pause([Future resumeSignal]) { | |
49 throw new UnsupportedError("pause"); | |
50 } | |
51 | |
52 @override | |
53 void resume() { | |
54 throw new UnsupportedError("resume"); | |
55 } | |
56 | |
57 static AbortedEventStreamSubscription _create( | |
58 EventSubscriptionSpecification spec, Zone zone) { | |
59 return new AbortedEventStreamSubscription(zone); | |
60 } | |
61 } | |
62 | |
63 eventTest(String name, Event eventFn(), void validate(Event event), | |
64 void validateSpec(EventSubscriptionSpecification spec), | |
65 {String type: 'foo', | |
66 bool abortCreation: false, | |
67 EventSubscriptionSpecification modifySpec( | |
68 EventSubscriptionSpecification spec), | |
69 bool abortEvent: false, | |
70 Event modifyEvent(Event event)}) { | |
71 test(name, () { | |
72 var lastSpec; | |
73 var lastTask; | |
74 var lastEvent; | |
75 | |
76 Object createTaskHandler(Zone self, ZoneDelegate parent, Zone zone, | |
77 TaskCreate create, TaskSpecification specification) { | |
78 if (specification is EventSubscriptionSpecification) { | |
79 if (abortCreation) { | |
80 create = AbortedEventStreamSubscription._create; | |
81 } | |
82 if (modifySpec != null) { | |
83 specification = modifySpec(specification); | |
84 } | |
85 lastSpec = specification; | |
86 return lastTask = parent.createTask(zone, create, specification); | |
87 } | |
88 return parent.createTask(zone, create, specification); | |
89 } | |
90 | |
91 void runTaskHandler(Zone self, ZoneDelegate parent, Zone zone, TaskRun run, | |
92 Object task, Object arg) { | |
93 if (identical(task, lastTask)) { | |
94 if (abortEvent) return; | |
95 if (modifyEvent != null) { | |
96 arg = modifyEvent(arg); | |
97 } | |
98 parent.runTask(zone, run, task, arg); | |
99 return; | |
100 } | |
101 parent.runTask(zone, run, task, arg); | |
102 } | |
103 | |
104 runZoned(() { | |
105 final el = new Element.tag('div'); | |
106 var fired = false; | |
107 var sub = el.on[type].listen((ev) { | |
108 lastEvent = ev; | |
109 fired = true; | |
110 }); | |
111 el.dispatchEvent(eventFn()); | |
112 | |
113 validateSpec(lastSpec); | |
114 validate(lastEvent); | |
115 | |
116 if (abortEvent || abortCreation) { | |
117 expect(fired, isFalse, reason: 'Expected event to be intercepted.'); | |
118 } else { | |
119 expect(fired, isTrue, reason: 'Expected event to be dispatched.'); | |
120 } | |
121 | |
122 sub.cancel(); | |
123 }, | |
124 zoneSpecification: new ZoneSpecification( | |
125 createTask: createTaskHandler, | |
126 runTask: runTaskHandler)); | |
127 }); | |
128 } | |
129 | |
130 Function checkSpec( | |
131 [String expectedType = 'foo', bool expectedUseCapture = false]) { | |
132 return (EventSubscriptionSpecification spec) { | |
133 expect(spec.eventType, expectedType); | |
134 expect(spec.useCapture, expectedUseCapture); | |
135 }; | |
136 } | |
137 | |
138 main() { | |
139 useHtmlConfiguration(); | |
140 | |
141 eventTest('Event', () => new Event('foo'), (ev) { | |
142 expect(ev.type, equals('foo')); | |
143 }, checkSpec('foo')); | |
144 | |
145 eventTest( | |
146 'WheelEvent', | |
147 () => new WheelEvent("mousewheel", | |
148 deltaX: 1, | |
149 deltaY: 0, | |
150 detail: 4, | |
151 screenX: 3, | |
152 screenY: 4, | |
153 clientX: 5, | |
154 clientY: 6, | |
155 ctrlKey: true, | |
156 altKey: true, | |
157 shiftKey: true, | |
158 metaKey: true), (ev) { | |
159 expect(ev.deltaX, 1); | |
160 expect(ev.deltaY, 0); | |
161 expect(ev.screen.x, 3); | |
162 expect(ev.screen.y, 4); | |
163 expect(ev.client.x, 5); | |
164 expect(ev.client.y, 6); | |
165 expect(ev.ctrlKey, isTrue); | |
166 expect(ev.altKey, isTrue); | |
167 expect(ev.shiftKey, isTrue); | |
168 expect(ev.metaKey, isTrue); | |
169 }, checkSpec('mousewheel'), type: 'mousewheel'); | |
170 | |
171 eventTest('Event - no-create', () => new Event('foo'), (ev) { | |
172 expect(ev, isNull); | |
173 }, checkSpec('foo'), abortCreation: true); | |
174 | |
175 eventTest( | |
176 'WheelEvent - no-create', | |
177 () => new WheelEvent("mousewheel", | |
178 deltaX: 1, | |
179 deltaY: 0, | |
180 detail: 4, | |
181 screenX: 3, | |
182 screenY: 4, | |
183 clientX: 5, | |
184 clientY: 6, | |
185 ctrlKey: true, | |
186 altKey: true, | |
187 shiftKey: true, | |
188 metaKey: true), (ev) { | |
189 expect(ev, isNull); | |
190 }, checkSpec('mousewheel'), type: 'mousewheel', abortCreation: true); | |
191 | |
192 eventTest('Event - no-run', () => new Event('foo'), (ev) { | |
193 expect(ev, isNull); | |
194 }, checkSpec('foo'), abortEvent: true); | |
195 | |
196 eventTest( | |
197 'WheelEvent - no-run', | |
198 () => new WheelEvent("mousewheel", | |
199 deltaX: 1, | |
200 deltaY: 0, | |
201 detail: 4, | |
202 screenX: 3, | |
203 screenY: 4, | |
204 clientX: 5, | |
205 clientY: 6, | |
206 ctrlKey: true, | |
207 altKey: true, | |
208 shiftKey: true, | |
209 metaKey: true), (ev) { | |
210 expect(ev, isNull); | |
211 }, checkSpec('mousewheel'), type: 'mousewheel', abortEvent: true); | |
212 | |
213 // Register for 'foo', but receive a 'bar' event, because the specification | |
214 // is rewritten. | |
215 eventTest( | |
216 'Event - replace eventType', | |
217 () => new Event('bar'), | |
218 (ev) { | |
219 expect(ev.type, equals('bar')); | |
220 }, | |
221 checkSpec('bar'), | |
222 type: 'foo', | |
223 modifySpec: (EventSubscriptionSpecification spec) { | |
224 return spec.replace(eventType: 'bar'); | |
225 }); | |
226 | |
227 // Intercept the 'foo' event and replace it with a 'bar' event. | |
228 eventTest( | |
229 'Event - intercept result', | |
230 () => new Event('foo'), | |
231 (ev) { | |
232 expect(ev.type, equals('bar')); | |
233 }, | |
234 checkSpec('foo'), | |
235 type: 'foo', | |
236 modifyEvent: (Event event) { | |
237 return new Event('bar'); | |
238 }); | |
239 } | |
OLD | NEW |