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