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

Side by Side Diff: test/restartable_timer_test.dart

Issue 1417373004: Add a ResetTimer class. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes Created 5 years, 1 month 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
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, 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 import 'dart:async';
6
7 import 'package:async/async.dart';
8 import 'package:fake_async/fake_async.dart';
9 import 'package:test/test.dart';
10
11 main() {
12 test("runs the callback once the duration has elapsed", () {
13 new FakeAsync().run((async) {
14 var fired = false;
15 var timer = new RestartableTimer(new Duration(seconds: 5), () {
16 fired = true;
17 });
18
19 async.elapse(new Duration(seconds: 4));
20 expect(fired, isFalse);
21
22 async.elapse(new Duration(seconds: 1));
23 expect(fired, isTrue);
24 });
25 });
26
27 test("doesn't run the callback if the timer is canceled", () {
28 new FakeAsync().run((async) {
29 var fired = false;
30 var timer = new RestartableTimer(new Duration(seconds: 5), () {
31 fired = true;
32 });
33
34 async.elapse(new Duration(seconds: 4));
35 expect(fired, isFalse);
36 timer.cancel();
37
38 async.elapse(new Duration(seconds: 4));
39 expect(fired, isFalse);
40 });
41 });
42
43 test("resets the duration if the timer is reset before it fires", () {
44 new FakeAsync().run((async) {
45 var fired = false;
46 var timer = new RestartableTimer(new Duration(seconds: 5), () {
47 fired = true;
48 });
49
50 async.elapse(new Duration(seconds: 4));
51 expect(fired, isFalse);
52 timer.reset();
53
54 async.elapse(new Duration(seconds: 4));
55 expect(fired, isFalse);
56
57 async.elapse(new Duration(seconds: 1));
58 expect(fired, isTrue);
59 });
60 });
61
62 test("re-runs the callback if the timer is reset after firing", () {
63 new FakeAsync().run((async) {
64 var fired = 0;
65 var timer = new RestartableTimer(new Duration(seconds: 5), () {
66 fired++;
67 });
68
69 async.elapse(new Duration(seconds: 5));
70 expect(fired, equals(1));
71 timer.reset();
72
73 async.elapse(new Duration(seconds: 5));
74 expect(fired, equals(2));
75 timer.reset();
76
77 async.elapse(new Duration(seconds: 5));
78 expect(fired, equals(3));
79 });
80 });
81
82 test("runs the callback if the timer is reset after being canceled", () {
83 new FakeAsync().run((async) {
84 var fired = false;
85 var timer = new RestartableTimer(new Duration(seconds: 5), () {
86 fired = true;
87 });
88
89 async.elapse(new Duration(seconds: 4));
90 expect(fired, isFalse);
91 timer.cancel();
92
93 async.elapse(new Duration(seconds: 4));
94 expect(fired, isFalse);
95 timer.reset();
96
97 async.elapse(new Duration(seconds: 5));
98 expect(fired, isTrue);
99 });
100 });
101
102 test("only runs the callback once if the timer isn't reset", () {
103 new FakeAsync().run((async) {
104 var timer = new RestartableTimer(
105 new Duration(seconds: 5),
106 expectAsync(() {}, count: 1));
107 async.elapse(new Duration(seconds: 10));
108 });
109 });
110 }
OLDNEW
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698