Chromium Code Reviews| Index: tests/lib/async/stream_controller_test.dart |
| diff --git a/tests/lib/async/stream_controller_test.dart b/tests/lib/async/stream_controller_test.dart |
| index e4ddecf1a3400c0c13250744af14db178305dc51..c03dc4a721cb69ddac63c08521cd9749c2a3c352 100644 |
| --- a/tests/lib/async/stream_controller_test.dart |
| +++ b/tests/lib/async/stream_controller_test.dart |
| @@ -719,6 +719,144 @@ void testSyncControllerNotReentrant() { |
| asyncEnd(); |
| } |
| +void testSettingCallbacks() { |
| + const int initial = 0; |
| + const int running = 1; |
| + const int paused = 2; |
| + const int canceled = 3; |
| + |
| + var controller = new StreamController(); |
| + var stream = controller.stream; |
| + var state = initial; |
| + |
| + controller..onListen = () { state = running; } |
|
nweiz
2015/07/17 20:40:16
Nit: Either make these bodies "=>"-style or make t
Lasse Reichstein Nielsen
2015/08/05 09:08:45
No.
I will not make them => style. For onCancel th
|
| + ..onPause = () { state = paused; } |
| + ..onResume = () { state = running; } |
| + ..onCancel = () { state = canceled; }; |
| + |
| + Expect.equals(initial, state); |
| + var sub = stream.listen(null); |
| + Expect.equals(running, state); |
| + sub.pause(); |
| + Expect.equals(paused, state); |
| + Expect.isTrue(controller.isPaused); |
| + sub.resume(); |
| + Expect.equals(running, state); |
| + Expect.isFalse(controller.isPaused); |
| + |
| + // Changing them later does make a difference. |
| + controller..onListen = () { throw "Second listen?"; } |
| + ..onPause = () { state = -paused; } |
| + ..onResume = () { state = -running; } |
| + ..onCancel = () { state = -canceled; }; |
| + |
| + Expect.equals(running, state); |
| + sub.pause(); |
| + Expect.equals(-paused, state); |
| + Expect.isTrue(controller.isPaused); |
| + sub.resume(); |
| + Expect.equals(-running, state); |
| + Expect.isFalse(controller.isPaused); |
| + sub.cancel(); |
| + Expect.equals(-canceled, state); |
| +} |
| + |
| +void testSettingNullCallbacks() { |
| + failCallback() => fail("Callback should not be called"); |
| + var controller = new StreamController(onListen: failCallback, |
| + onPause : failCallback, |
| + onResume: failCallback, |
| + onCancel: failCallback); |
| + |
| + var stream = controller.stream; |
| + |
| + Expect.isFalse(controller.hasListener); |
| + Expect.isTrue(controller.isPaused); |
| + |
| + controller.onListen = null; |
| + |
| + var sub = stream.listen(null); |
| + |
| + Expect.isTrue(controller.hasListener); |
| + Expect.isFalse(controller.isPaused); |
| + |
| + controller.onPause = null; |
| + |
| + sub.pause(); |
| + |
| + Expect.isTrue(controller.hasListener); |
| + Expect.isTrue(controller.isPaused); |
| + |
| + controller.onResume = null; |
| + |
| + sub.resume(); |
| + |
| + Expect.isTrue(controller.hasListener); |
| + Expect.isFalse(controller.isPaused); |
| + |
| + controller.onCancel = null; |
| + |
| + sub.cancel(); |
| + |
| + Expect.isFalse(controller.hasListener); |
| + Expect.isFalse(controller.isPaused); |
| +} |
| + |
| +void testBroadcastSettingCallbacks() { |
| + const int initial = 0; |
| + const int running = 1; |
| + const int canceled = 2; |
| + |
| + var controller = new StreamController.broadcast(); |
| + var stream = controller.stream; |
| + var state = initial; |
| + |
| + Expect.throws(() { controller.onPause = (){}; }, |
|
nweiz
2015/07/17 20:40:16
Nit: "() {}" (https://www.dartlang.org/articles/st
Lasse Reichstein Nielsen
2015/08/05 09:08:45
While I don't find it more readable (the space is
|
| + (e) => e is UnsupportedError); |
| + Expect.throws(() { controller.onResume = (){}; }, |
| + (e) => e is UnsupportedError); |
| + |
| + controller..onListen = () { state = running; } |
| + ..onCancel = () { state = canceled; }; |
| + |
| + Expect.equals(initial, state); |
| + var sub = stream.listen(null); |
| + Expect.equals(running, state); |
| + sub.cancel(); |
| + Expect.equals(canceled, state); |
| + |
| + // Changing them later does make a difference. |
| + controller..onListen = () { state = -running; } |
| + ..onCancel = () { state = -canceled; }; |
| + |
| + var sub2 = stream.listen(null); |
| + Expect.equals(-running, state); |
| + sub2.cancel(); |
| + Expect.equals(-canceled, state); |
| +} |
| + |
| +void testBroadcastSettingNullCallbacks() { |
| + failCallback() => fail("Callback should not be called"); |
| + var controller = new StreamController.broadcast(onListen: failCallback, |
| + onCancel: failCallback); |
| + |
| + var stream = controller.stream; |
| + |
| + Expect.isFalse(controller.hasListener); |
| + |
| + controller.onListen = null; |
| + |
| + var sub = stream.listen(null); |
| + |
| + Expect.isTrue(controller.hasListener); |
| + |
| + controller.onCancel = null; |
| + |
| + sub.cancel(); |
| + |
| + Expect.isFalse(controller.hasListener); |
| +} |
| + |
| main() { |
| asyncStart(); |
| testMultiController(); |
| @@ -738,5 +876,9 @@ main() { |
| testAsBroadcastListenAfterClosePaused(); |
| testEventInListen(); |
| testSyncControllerNotReentrant(); |
| + testSettingCallbacks(); |
| + testSettingNullCallbacks(); |
| + testBroadcastSettingCallbacks(); |
| + testBroadcastSettingNullCallbacks(); |
| asyncEnd(); |
| } |