From 98e61b77d91a58fe13406ded2b94167b595701d5 Mon Sep 17 00:00:00 2001 From: yjbanov Date: Mon, 16 Jun 2014 17:20:16 -0700 Subject: [PATCH] feat(mock): Add timer queue checks in mock zone Closes #1157 --- lib/mock/zone.dart | 19 +++++++++++++++++++ test/mock/zone_spec.dart | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/mock/zone.dart b/lib/mock/zone.dart index ab8c3868c..153539a57 100644 --- a/lib/mock/zone.dart +++ b/lib/mock/zone.dart @@ -68,6 +68,25 @@ microLeap() { */ isAsyncQueueEmpty() => _asyncQueue.isEmpty; +/** + * Returns whether there are outstanding timers. + */ +isTimerQueueEmpty() => _timerQueue.isEmpty; + +/** + * Returns whether there are outstanding non-periodic timers. + */ +isNonPeriodicTimerQueueEmpty() => _timerQueue + .where((_TimerSpec spec) => !spec.periodic) + .isEmpty; + +/** + * Returns whether there are outstanding periodic timers. + */ +isPeriodicTimerQueueEmpty() => _timerQueue + .where((_TimerSpec spec) => spec.periodic) + .isEmpty; + /** * Simulates a clock tick by running any scheduled timers. Can only be used * in [async] tests.Clock tick will call [microLeap] to process the microtask diff --git a/test/mock/zone_spec.dart b/test/mock/zone_spec.dart index b1aaf9c83..00454cafd 100644 --- a/test/mock/zone_spec.dart +++ b/test/mock/zone_spec.dart @@ -332,6 +332,28 @@ void main() { (_) => dump("i never run")); })).toThrow('1 active timer(s) are still in the queue.'); }); + + it('should report no timers when there are none', async(() { + expect(isTimerQueueEmpty()).toBe(true); + expect(isNonPeriodicTimerQueueEmpty()).toBe(true); + expect(isPeriodicTimerQueueEmpty()).toBe(true); + })); + + it('should report remaining non-periodic timers', async(() { + new Future(() => null); + expect(isTimerQueueEmpty()).toBe(false); + expect(isNonPeriodicTimerQueueEmpty()).toBe(false); + expect(isPeriodicTimerQueueEmpty()).toBe(true); + clockTick(); + })); + + it('should report remaining periodic timers', async(() { + var t = new Timer.periodic(new Duration(seconds: 1), (_) => null); + expect(isTimerQueueEmpty()).toBe(false); + expect(isNonPeriodicTimerQueueEmpty()).toBe(true); + expect(isPeriodicTimerQueueEmpty()).toBe(false); + t.cancel(); + })); }); }); });