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

Unified Diff: tests/lib/async/stream_controller_test.dart

Issue 1242023007: Add setters for callbacks on StreamController. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comments. Created 5 years, 5 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
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();
}
« sdk/lib/async/stream_controller.dart ('K') | « sdk/lib/async/stream_controller.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698