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

Unified Diff: test/reset_timer_test.dart

Issue 1417373004: Add a ResetTimer class. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Created 5 years, 2 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 side-by-side diff with in-line comments
Download patch
« lib/src/reset_timer.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/reset_timer_test.dart
diff --git a/test/reset_timer_test.dart b/test/reset_timer_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6d0b81169885c8a9e96b34f03645c7e45c739a6f
--- /dev/null
+++ b/test/reset_timer_test.dart
@@ -0,0 +1,91 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:async';
+
+import 'package:async/async.dart';
+import 'package:fake_async/fake_async.dart';
+import 'package:test/test.dart';
+
+main() {
+ test("runs the callback once the duration has elapsed", () {
+ new FakeAsync().run((async) {
+ var fired = false;
+ var timer = new ResetTimer(new Duration(seconds: 5), () => fired = true);
Lasse Reichstein Nielsen 2015/10/27 09:45:14 Use braces, not =>, for functions where the return
nweiz 2015/10/27 21:08:12 Done.
+
+ async.elapse(new Duration(seconds: 4));
+ expect(fired, isFalse);
+
+ async.elapse(new Duration(seconds: 1));
+ expect(fired, isTrue);
+ });
+ });
+
+ test("doesn't run the callback if the timer is canceled", () {
+ new FakeAsync().run((async) {
+ var fired = false;
+ var timer = new ResetTimer(new Duration(seconds: 5), () => fired = true);
+
+ async.elapse(new Duration(seconds: 4));
+ expect(fired, isFalse);
+ timer.cancel();
+
+ async.elapse(new Duration(seconds: 4));
Lasse Reichstein Nielsen 2015/10/27 09:45:14 Interesting - this looks like it's synchronous, so
nweiz 2015/10/27 21:08:12 I believe it just creates an entire simulated even
+ expect(fired, isFalse);
+ });
+ });
+
+ test("resets the duration if the timer is reset before it fires", () {
+ new FakeAsync().run((async) {
+ var fired = false;
+ var timer = new ResetTimer(new Duration(seconds: 5), () => fired = true);
+
+ async.elapse(new Duration(seconds: 4));
+ expect(fired, isFalse);
+ timer.reset();
+
+ async.elapse(new Duration(seconds: 4));
+ expect(fired, isFalse);
+
+ async.elapse(new Duration(seconds: 1));
+ expect(fired, isTrue);
+ });
+ });
+
+ test("re-runs the callback if the timer is reset after firing", () {
+ new FakeAsync().run((async) {
+ var fired = 0;
+ var timer = new ResetTimer(new Duration(seconds: 5), () => fired++);
+
+ async.elapse(new Duration(seconds: 5));
+ expect(fired, equals(1));
+ timer.reset();
+
+ async.elapse(new Duration(seconds: 5));
+ expect(fired, equals(2));
+ timer.reset();
+
+ async.elapse(new Duration(seconds: 5));
+ expect(fired, equals(3));
+ });
+ });
+
+ test("runs the callback if the timer is reset after being canceled", () {
+ new FakeAsync().run((async) {
+ var fired = false;
+ var timer = new ResetTimer(new Duration(seconds: 5), () => fired = true);
+
+ async.elapse(new Duration(seconds: 4));
+ expect(fired, isFalse);
+ timer.cancel();
+
+ async.elapse(new Duration(seconds: 4));
+ expect(fired, isFalse);
+ timer.reset();
+
+ async.elapse(new Duration(seconds: 5));
+ expect(fired, isTrue);
+ });
+ });
Lasse Reichstein Nielsen 2015/10/27 09:45:14 Check that the timer is not periodic: Wait at leas
nweiz 2015/10/27 21:08:12 Done.
+}
« lib/src/reset_timer.dart ('K') | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698