diff --git a/sdk/lib/core/stopwatch.dart b/sdk/lib/core/stopwatch.dart index 7d8ce334cd1..b446069f70c 100644 --- a/sdk/lib/core/stopwatch.dart +++ b/sdk/lib/core/stopwatch.dart @@ -63,6 +63,11 @@ abstract class Stopwatch { * Returns the frequency of the elapsed counter in Hz. */ int get frequency; + + /** + * Returns wether the [StopWatch] is currently running. + */ + bool get isRunning; } class _StopwatchImpl implements Stopwatch { @@ -77,14 +82,12 @@ class _StopwatchImpl implements Stopwatch { _StopwatchImpl() : _start = null, _stop = null {} void start() { + if (isRunning) return; if (_start == null) { // This stopwatch has never been started. _start = _now(); } else { - if (_stop == null) { - return; - } - // Restarting this stopwatch. Prepend the elapsed time to the current + // Restart this stopwatch. Prepend the elapsed time to the current // start time. _start = _now() - (_stop - _start); _stop = null; @@ -92,9 +95,7 @@ class _StopwatchImpl implements Stopwatch { } void stop() { - if (_start == null || _stop != null) { - return; - } + if (!isRunning) return; _stop = _now(); } @@ -127,6 +128,8 @@ class _StopwatchImpl implements Stopwatch { int get frequency => _frequency(); + bool get isRunning => _start != null && _stop == null; + external static int _frequency(); external static int _now(); } diff --git a/tests/corelib/stopwatch_test.dart b/tests/corelib/stopwatch_test.dart index b688ab3f377..2b9c1a6ae14 100644 --- a/tests/corelib/stopwatch_test.dart +++ b/tests/corelib/stopwatch_test.dart @@ -8,7 +8,9 @@ library stopwatch_test; class StopwatchTest { static bool checkTicking(Stopwatch sw) { + Expect.isFalse(sw.isRunning); sw.start(); + Expect.isTrue(sw.isRunning); for (int i = 0; i < 10000; i++) { int.parse(i.toString()); if (sw.elapsedTicks > 0) { @@ -20,10 +22,12 @@ class StopwatchTest { static bool checkStopping(Stopwatch sw) { sw.stop(); + Expect.isFalse(sw.isRunning); int v1 = sw.elapsedTicks; Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time. Stopwatch sw2 = new Stopwatch(); // Used for verification. sw2.start(); + Expect.isTrue(sw2.isRunning); int sw2LastElapsed = 0; for (int i = 0; i < 100000; i++) { int.parse(i.toString()); @@ -46,7 +50,9 @@ class StopwatchTest { static checkRestart() { Stopwatch sw = new Stopwatch(); + Expect.isFalse(sw.isRunning); sw.start(); + Expect.isTrue(sw.isRunning); for (int i = 0; i < 100000; i++) { int.parse(i.toString()); if (sw.elapsedTicks > 0) { @@ -54,8 +60,10 @@ class StopwatchTest { } } sw.stop(); + Expect.isFalse(sw.isRunning); int initial = sw.elapsedTicks; sw.start(); + Expect.isTrue(sw.isRunning); for (int i = 0; i < 100000; i++) { int.parse(i.toString()); if (sw.elapsedTicks > initial) { @@ -63,12 +71,15 @@ class StopwatchTest { } } sw.stop(); + Expect.isFalse(sw.isRunning); Expect.isTrue(sw.elapsedTicks > initial); } static checkReset() { Stopwatch sw = new Stopwatch(); + Expect.isFalse(sw.isRunning); sw.start(); + Expect.isTrue(sw.isRunning); for (int i = 0; i < 100000; i++) { int.parse(i.toString()); if (sw.elapsedTicks > 0) { @@ -76,9 +87,12 @@ class StopwatchTest { } } sw.stop(); + Expect.isFalse(sw.isRunning); sw.reset(); + Expect.isFalse(sw.isRunning); Expect.equals(0, sw.elapsedTicks); sw.start(); + Expect.isTrue(sw.isRunning); for (int i = 0; i < 100000; i++) { int.parse(i.toString()); if (sw.elapsedTicks > 0) { @@ -86,6 +100,7 @@ class StopwatchTest { } } sw.reset(); + Expect.isTrue(sw.isRunning); for (int i = 0; i < 100000; i++) { int.parse(i.toString()); if (sw.elapsedTicks > 0) { @@ -93,6 +108,7 @@ class StopwatchTest { } } sw.stop(); + Expect.isFalse(sw.isRunning); Expect.isTrue(sw.elapsedTicks > 0); }