OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library timer_cancel_test; | 5 library timer_cancel_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 import '../../pkg/unittest/lib/unittest.dart'; | 9 import '../../pkg/unittest/lib/unittest.dart'; |
10 | 10 |
11 main() { | 11 main() { |
| 12 final ms = const Duration(milliseconds: 1); |
12 test("simple timer", () { | 13 test("simple timer", () { |
13 Timer cancelTimer; | 14 Timer cancelTimer; |
14 int repeatTimer; | 15 int repeatTimer; |
15 | 16 |
16 void unreachable(Timer timer) { | 17 void unreachable() { |
17 fail("should not be reached"); | 18 fail("should not be reached"); |
18 } | 19 } |
19 | 20 |
20 void handler(Timer timer) { | 21 void handler() { |
21 cancelTimer.cancel(); | 22 cancelTimer.cancel(); |
22 } | 23 } |
23 | 24 |
24 void repeatHandler(Timer timer) { | 25 void repeatHandler(Timer timer) { |
25 repeatTimer++; | 26 repeatTimer++; |
26 timer.cancel(); | 27 timer.cancel(); |
27 expect(repeatTimer, 1); | 28 expect(repeatTimer, 1); |
28 } | 29 } |
29 | 30 |
30 cancelTimer = new Timer(1000, expectAsync1(unreachable, count: 0)); | 31 cancelTimer = new Timer(ms * 1000, expectAsync0(unreachable, count: 0)); |
31 cancelTimer.cancel(); | 32 cancelTimer.cancel(); |
32 new Timer(1000, expectAsync1(handler)); | 33 new Timer(ms * 1000, expectAsync0(handler)); |
33 cancelTimer = new Timer(2000, expectAsync1(unreachable, count: 0)); | 34 cancelTimer = new Timer(ms * 2000, expectAsync0(unreachable, count: 0)); |
34 repeatTimer = 0; | 35 repeatTimer = 0; |
35 new Timer.repeating(1500, expectAsync1(repeatHandler)); | 36 new Timer.repeating(ms * 1500, expectAsync1(repeatHandler)); |
36 }); | 37 }); |
37 | 38 |
38 test("cancel timer with same time", () { | 39 test("cancel timer with same time", () { |
39 var t2; | 40 var t2; |
40 var t1 = new Timer(0, expectAsync1((t) => t2.cancel())); | 41 var t1 = Timer.run(expectAsync0(() => t2.cancel())); |
41 t2 = new Timer(0, expectAsync1((t) => t1.cancel(), count: 0)); | 42 t2 = Timer.run(expectAsync0(t1.cancel, count: 0)); |
42 }); | 43 }); |
43 } | 44 } |
OLD | NEW |