Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Side by Side Diff: tests/html/event_zone_task_test.dart

Issue 2022263002: Make Dom events run through zone tasks. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 zone.cancelTask(this);
27 return null;
28 }
29
30 @override
31 bool get isPaused => throw new UnsupportedError("pause");
32
33 @override
34 void onData(void handleData(Event data)) {
35 throw new UnsupportedError("cancel");
36 }
37
38 @override
39 void onDone(void handleDone()) {
40 throw new UnsupportedError("onDone");
41 }
42
43 @override
44 void onError(Function handleError) {
45 throw new UnsupportedError("onError");
46 }
47
48 @override
49 void pause([Future resumeSignal]) {
50 throw new UnsupportedError("pause");
51 }
52
53 @override
54 void resume() {
55 throw new UnsupportedError("resume");
56 }
57
58 static AbortedEventStreamSubscription _create(
59 EventSubscriptionSpecification spec, Zone zone) {
60 return new AbortedEventStreamSubscription(zone);
61 }
62 }
63
64 eventTest(String name, Event eventFn(), void validate(Event event),
65 void validateSpec(EventSubscriptionSpecification spec),
66 {String type: 'foo',
67 bool abortCreation: false,
68 EventSubscriptionSpecification modifySpec(
69 EventSubscriptionSpecification spec),
70 bool abortEvent: false,
71 Event modifyEvent(Event event)}) {
72 test(name, () {
73 var lastSpec;
74 var lastTask;
75 var lastEvent;
76 var canceled = false;
77
78 Object createTaskHandler(Zone self, ZoneDelegate parent, Zone zone,
79 TaskCreate create, TaskSpecification specification) {
80 if (specification is EventSubscriptionSpecification) {
81 if (abortCreation) {
82 create = AbortedEventStreamSubscription._create;
83 }
84 if (modifySpec != null) {
85 specification = modifySpec(specification);
86 }
87 lastSpec = specification;
88 return lastTask = parent.createTask(zone, create, specification);
89 }
90 return parent.createTask(zone, create, specification);
91 }
92
93 void runTaskHandler(Zone self, ZoneDelegate parent, Zone zone, TaskRun run,
94 Object task, Object arg) {
95 if (identical(task, lastTask)) {
96 if (abortEvent) return;
97 if (modifyEvent != null) {
98 arg = modifyEvent(arg);
99 }
100 parent.runTask(zone, run, task, arg);
101 return;
102 }
103 parent.runTask(zone, run, task, arg);
104 }
105
106 void cancelTaskHandler(
107 Zone self, ZoneDelegate parent, Zone zone, Object task) {
108 if (identical(task, lastTask)) {
109 canceled = true;
110 }
111 parent.cancelTask(zone, task);
112 }
113
114 runZoned(() {
115 final el = new Element.tag('div');
116 var fired = false;
117 var sub = el.on[type].listen((ev) {
118 lastEvent = ev;
119 fired = true;
120 });
121 el.dispatchEvent(eventFn());
122
123 validateSpec(lastSpec);
124 validate(lastEvent);
125
126 if (abortEvent || abortCreation) {
127 expect(fired, isFalse, reason: 'Expected event to be intercepted.');
128 } else {
129 expect(fired, isTrue, reason: 'Expected event to be dispatched.');
130 }
131
132 expect(canceled, isFalse);
133 sub.cancel();
134 expect(canceled, isTrue);
135 },
136 zoneSpecification: new ZoneSpecification(
137 createTask: createTaskHandler,
138 runTask: runTaskHandler,
139 cancelTask: cancelTaskHandler));
140 });
141 }
142
143 Function checkSpec(
144 [String expectedType = 'foo', bool expectedUseCapture = false]) {
145 return (EventSubscriptionSpecification spec) {
146 expect(spec.eventType, expectedType);
147 expect(spec.useCapture, expectedUseCapture);
148 };
149 }
150
151 main() {
152 useHtmlConfiguration();
153
154 eventTest('Event', () => new Event('foo'), (ev) {
155 expect(ev.type, equals('foo'));
156 }, checkSpec('foo'));
157
158 eventTest(
159 'WheelEvent',
160 () => new WheelEvent("mousewheel",
161 deltaX: 1,
162 deltaY: 0,
163 detail: 4,
164 screenX: 3,
165 screenY: 4,
166 clientX: 5,
167 clientY: 6,
168 ctrlKey: true,
169 altKey: true,
170 shiftKey: true,
171 metaKey: true), (ev) {
172 expect(ev.deltaX, 1);
173 expect(ev.deltaY, 0);
174 expect(ev.screen.x, 3);
175 expect(ev.screen.y, 4);
176 expect(ev.client.x, 5);
177 expect(ev.client.y, 6);
178 expect(ev.ctrlKey, isTrue);
179 expect(ev.altKey, isTrue);
180 expect(ev.shiftKey, isTrue);
181 expect(ev.metaKey, isTrue);
182 }, checkSpec('mousewheel'), type: 'mousewheel');
183
184 eventTest('Event - no-create', () => new Event('foo'), (ev) {
185 expect(ev, isNull);
186 }, checkSpec('foo'), abortCreation: true);
187
188 eventTest(
189 'WheelEvent - no-create',
190 () => new WheelEvent("mousewheel",
191 deltaX: 1,
192 deltaY: 0,
193 detail: 4,
194 screenX: 3,
195 screenY: 4,
196 clientX: 5,
197 clientY: 6,
198 ctrlKey: true,
199 altKey: true,
200 shiftKey: true,
201 metaKey: true), (ev) {
202 expect(ev, isNull);
203 }, checkSpec('mousewheel'), type: 'mousewheel', abortCreation: true);
204
205 eventTest('Event - no-run', () => new Event('foo'), (ev) {
206 expect(ev, isNull);
207 }, checkSpec('foo'), abortEvent: true);
208
209 eventTest(
210 'WheelEvent - no-run',
211 () => new WheelEvent("mousewheel",
212 deltaX: 1,
213 deltaY: 0,
214 detail: 4,
215 screenX: 3,
216 screenY: 4,
217 clientX: 5,
218 clientY: 6,
219 ctrlKey: true,
220 altKey: true,
221 shiftKey: true,
222 metaKey: true), (ev) {
223 expect(ev, isNull);
224 }, checkSpec('mousewheel'), type: 'mousewheel', abortEvent: true);
225
226 // Register for 'foo', but receive a 'bar' event, because the specification
227 // is rewritten.
228 eventTest(
229 'Event - replace eventType',
230 () => new Event('bar'),
231 (ev) {
232 expect(ev.type, equals('bar'));
233 },
234 checkSpec('bar'),
235 type: 'foo',
236 modifySpec: (EventSubscriptionSpecification spec) {
237 return spec.replace(eventType: 'bar');
238 });
239
240 // Intercept the 'foo' event and replace it with a 'bar' event.
241 eventTest(
242 'Event - intercept result',
243 () => new Event('foo'),
244 (ev) {
245 expect(ev.type, equals('bar'));
246 },
247 checkSpec('foo'),
248 type: 'foo',
249 modifyEvent: (Event event) {
250 return new Event('bar');
251 });
252 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698