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 window.requestAnimationFrame. | |
13 | |
14 class MockAnimationFrameTask implements AnimationFrameTask { | |
15 static int taskId = 499; | |
16 | |
17 final int id; | |
18 final Zone zone; | |
19 bool _isCanceled = false; | |
20 Function _callback; | |
21 | |
22 MockAnimationFrameTask( | |
23 this.id, this.zone, this._callback); | |
24 | |
25 void cancel(Window window) { | |
26 _isCanceled = true; | |
27 } | |
28 | |
29 trigger(num stamp) { | |
30 zone.runTask(run, this, stamp); | |
31 } | |
32 | |
33 static create(AnimationFrameRequestSpecification spec, Zone zone) { | |
34 var callback = zone.registerUnaryCallback(spec.callback); | |
35 return new MockAnimationFrameTask( | |
36 taskId++, zone, callback); | |
37 } | |
38 | |
39 static run(MockAnimationFrameTask task, num arg) { | |
40 AnimationFrameTask.removeMapping(task.id); | |
41 task._callback(arg); | |
42 } | |
43 } | |
44 | |
45 animationFrameTest() { | |
46 test("animationFrameTest - no intercept", () async { | |
47 AnimationFrameTask lastTask; | |
48 bool sawRequest = false; | |
49 int id; | |
50 num providedArg; | |
51 | |
52 Object createTaskHandler(Zone self, ZoneDelegate parent, Zone zone, | |
53 TaskCreate create, TaskSpecification specification) { | |
54 if (specification is AnimationFrameRequestSpecification) { | |
55 sawRequest = true; | |
56 lastTask = parent.createTask(zone, create, specification); | |
57 id = lastTask.id; | |
58 return lastTask; | |
59 } | |
60 return parent.createTask(zone, create, specification); | |
61 } | |
62 | |
63 void runTaskHandler(Zone self, ZoneDelegate parent, Zone zone, TaskRun run, | |
64 Object task, Object arg) { | |
65 if (identical(task, lastTask)) { | |
66 providedArg = arg; | |
67 } | |
68 parent.runTask(zone, run, task, arg); | |
69 } | |
70 | |
71 var completer = new Completer(); | |
72 var publicId; | |
73 runZoned(() { | |
74 publicId = window.requestAnimationFrame((num stamp) { | |
75 completer.complete(stamp); | |
76 }); | |
77 }, | |
78 zoneSpecification: new ZoneSpecification( | |
79 createTask: createTaskHandler, runTask: runTaskHandler)); | |
80 | |
81 var referenceCompleter = new Completer(); | |
82 window.requestAnimationFrame((num stamp) { | |
83 referenceCompleter.complete(stamp); | |
84 }); | |
85 | |
86 var callbackStamp = await completer.future; | |
87 var referenceStamp = await referenceCompleter.future; | |
88 | |
89 expect(callbackStamp, equals(referenceStamp)); | |
90 expect(providedArg, equals(callbackStamp)); | |
91 expect(sawRequest, isTrue); | |
92 expect(publicId, isNotNull); | |
93 expect(publicId, equals(id)); | |
94 }); | |
95 } | |
96 | |
97 interceptedAnimationFrameTest() { | |
98 test("animationFrameTest - intercepted", () { | |
99 List<MockAnimationFrameTask> tasks = []; | |
100 List<num> loggedRuns = []; | |
101 int executedTaskId; | |
102 int executedStamp; | |
103 | |
104 Object createTaskHandler(Zone self, ZoneDelegate parent, Zone zone, | |
105 TaskCreate create, TaskSpecification specification) { | |
106 if (specification is AnimationFrameRequestSpecification) { | |
107 var task = parent.createTask( | |
108 zone, MockAnimationFrameTask.create, specification); | |
109 tasks.add(task); | |
110 return task; | |
111 } | |
112 return parent.createTask(zone, create, specification); | |
113 } | |
114 | |
115 void runTaskHandler(Zone self, ZoneDelegate parent, Zone zone, TaskRun run, | |
116 Object task, Object arg) { | |
117 if (tasks.contains(task)) { | |
118 loggedRuns.add(arg); | |
119 } | |
120 parent.runTask(zone, run, task, arg); | |
121 } | |
122 | |
123 var id0, id1, id2; | |
124 | |
125 runZoned(() { | |
126 id0 = window.requestAnimationFrame((num stamp) { | |
127 executedTaskId = id0; | |
128 executedStamp = stamp; | |
129 }); | |
130 id1 = window.requestAnimationFrame((num stamp) { | |
131 executedTaskId = id1; | |
132 executedStamp = stamp; | |
133 }); | |
134 id2 = window.requestAnimationFrame((num stamp) { | |
135 executedTaskId = id2; | |
136 executedStamp = stamp; | |
137 }); | |
138 }, | |
139 zoneSpecification: new ZoneSpecification( | |
140 createTask: createTaskHandler, runTask: runTaskHandler)); | |
141 | |
142 expect(tasks.length, 3); | |
143 expect(executedTaskId, isNull); | |
144 expect(executedStamp, isNull); | |
145 expect(loggedRuns.isEmpty, isTrue); | |
146 | |
147 tasks[0].trigger(123.1); | |
148 expect(executedTaskId, id0); | |
149 expect(executedStamp, 123.1); | |
150 | |
151 tasks[1].trigger(123.2); | |
152 expect(executedTaskId, id1); | |
153 expect(executedStamp, 123.2); | |
154 | |
155 expect(loggedRuns, equals([123.1, 123.2])); | |
156 | |
157 window.cancelAnimationFrame(id2); | |
158 expect(tasks[2]._isCanceled, isTrue); | |
159 // Cancel it a second time. Should not crash. | |
160 window.cancelAnimationFrame(id2); | |
161 expect(tasks[2]._isCanceled, isTrue); | |
162 }); | |
163 } | |
164 | |
165 main() { | |
166 useHtmlConfiguration(); | |
167 | |
168 animationFrameTest(); | |
169 interceptedAnimationFrameTest(); | |
170 } | |
OLD | NEW |