OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Test the basic StreamController and StreamController.singleSubscription. | 5 // Test the basic StreamController and StreamController.singleSubscription. |
6 library stream_controller_test; | 6 library stream_controller_test; |
7 | 7 |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
10 import 'dart:async'; | 10 import 'dart:async'; |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
712 }); | 712 }); |
713 asyncEnd(); | 713 asyncEnd(); |
714 }); | 714 }); |
715 } | 715 } |
716 c.add(42); | 716 c.add(42); |
717 c.addError(87); | 717 c.addError(87); |
718 } | 718 } |
719 asyncEnd(); | 719 asyncEnd(); |
720 } | 720 } |
721 | 721 |
722 void testSettingCallbacks() { | |
723 const int initial = 0; | |
724 const int running = 1; | |
725 const int paused = 2; | |
726 const int canceled = 3; | |
727 | |
728 var controller = new StreamController(); | |
729 var stream = controller.stream; | |
730 var state = initial; | |
731 | |
732 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
| |
733 ..onPause = () { state = paused; } | |
734 ..onResume = () { state = running; } | |
735 ..onCancel = () { state = canceled; }; | |
736 | |
737 Expect.equals(initial, state); | |
738 var sub = stream.listen(null); | |
739 Expect.equals(running, state); | |
740 sub.pause(); | |
741 Expect.equals(paused, state); | |
742 Expect.isTrue(controller.isPaused); | |
743 sub.resume(); | |
744 Expect.equals(running, state); | |
745 Expect.isFalse(controller.isPaused); | |
746 | |
747 // Changing them later does make a difference. | |
748 controller..onListen = () { throw "Second listen?"; } | |
749 ..onPause = () { state = -paused; } | |
750 ..onResume = () { state = -running; } | |
751 ..onCancel = () { state = -canceled; }; | |
752 | |
753 Expect.equals(running, state); | |
754 sub.pause(); | |
755 Expect.equals(-paused, state); | |
756 Expect.isTrue(controller.isPaused); | |
757 sub.resume(); | |
758 Expect.equals(-running, state); | |
759 Expect.isFalse(controller.isPaused); | |
760 sub.cancel(); | |
761 Expect.equals(-canceled, state); | |
762 } | |
763 | |
764 void testSettingNullCallbacks() { | |
765 failCallback() => fail("Callback should not be called"); | |
766 var controller = new StreamController(onListen: failCallback, | |
767 onPause : failCallback, | |
768 onResume: failCallback, | |
769 onCancel: failCallback); | |
770 | |
771 var stream = controller.stream; | |
772 | |
773 Expect.isFalse(controller.hasListener); | |
774 Expect.isTrue(controller.isPaused); | |
775 | |
776 controller.onListen = null; | |
777 | |
778 var sub = stream.listen(null); | |
779 | |
780 Expect.isTrue(controller.hasListener); | |
781 Expect.isFalse(controller.isPaused); | |
782 | |
783 controller.onPause = null; | |
784 | |
785 sub.pause(); | |
786 | |
787 Expect.isTrue(controller.hasListener); | |
788 Expect.isTrue(controller.isPaused); | |
789 | |
790 controller.onResume = null; | |
791 | |
792 sub.resume(); | |
793 | |
794 Expect.isTrue(controller.hasListener); | |
795 Expect.isFalse(controller.isPaused); | |
796 | |
797 controller.onCancel = null; | |
798 | |
799 sub.cancel(); | |
800 | |
801 Expect.isFalse(controller.hasListener); | |
802 Expect.isFalse(controller.isPaused); | |
803 } | |
804 | |
805 void testBroadcastSettingCallbacks() { | |
806 const int initial = 0; | |
807 const int running = 1; | |
808 const int canceled = 2; | |
809 | |
810 var controller = new StreamController.broadcast(); | |
811 var stream = controller.stream; | |
812 var state = initial; | |
813 | |
814 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
| |
815 (e) => e is UnsupportedError); | |
816 Expect.throws(() { controller.onResume = (){}; }, | |
817 (e) => e is UnsupportedError); | |
818 | |
819 controller..onListen = () { state = running; } | |
820 ..onCancel = () { state = canceled; }; | |
821 | |
822 Expect.equals(initial, state); | |
823 var sub = stream.listen(null); | |
824 Expect.equals(running, state); | |
825 sub.cancel(); | |
826 Expect.equals(canceled, state); | |
827 | |
828 // Changing them later does make a difference. | |
829 controller..onListen = () { state = -running; } | |
830 ..onCancel = () { state = -canceled; }; | |
831 | |
832 var sub2 = stream.listen(null); | |
833 Expect.equals(-running, state); | |
834 sub2.cancel(); | |
835 Expect.equals(-canceled, state); | |
836 } | |
837 | |
838 void testBroadcastSettingNullCallbacks() { | |
839 failCallback() => fail("Callback should not be called"); | |
840 var controller = new StreamController.broadcast(onListen: failCallback, | |
841 onCancel: failCallback); | |
842 | |
843 var stream = controller.stream; | |
844 | |
845 Expect.isFalse(controller.hasListener); | |
846 | |
847 controller.onListen = null; | |
848 | |
849 var sub = stream.listen(null); | |
850 | |
851 Expect.isTrue(controller.hasListener); | |
852 | |
853 controller.onCancel = null; | |
854 | |
855 sub.cancel(); | |
856 | |
857 Expect.isFalse(controller.hasListener); | |
858 } | |
859 | |
722 main() { | 860 main() { |
723 asyncStart(); | 861 asyncStart(); |
724 testMultiController(); | 862 testMultiController(); |
725 testSingleController(); | 863 testSingleController(); |
726 testExtraMethods(); | 864 testExtraMethods(); |
727 testClosed(); | 865 testClosed(); |
728 testCloseFuture(); | 866 testCloseFuture(); |
729 testCloseFuture2(); | 867 testCloseFuture2(); |
730 testCloseFuture3(); | 868 testCloseFuture3(); |
731 testStreamEquals(); | 869 testStreamEquals(); |
732 testCancelThrow(); | 870 testCancelThrow(); |
733 testCancelThrow2(); | 871 testCancelThrow2(); |
734 testCancelThrow3(); | 872 testCancelThrow3(); |
735 testBroadcastListenAfterClose(); | 873 testBroadcastListenAfterClose(); |
736 testBroadcastListenAfterClosePaused(); | 874 testBroadcastListenAfterClosePaused(); |
737 testAsBroadcastListenAfterClose(); | 875 testAsBroadcastListenAfterClose(); |
738 testAsBroadcastListenAfterClosePaused(); | 876 testAsBroadcastListenAfterClosePaused(); |
739 testEventInListen(); | 877 testEventInListen(); |
740 testSyncControllerNotReentrant(); | 878 testSyncControllerNotReentrant(); |
879 testSettingCallbacks(); | |
880 testSettingNullCallbacks(); | |
881 testBroadcastSettingCallbacks(); | |
882 testBroadcastSettingNullCallbacks(); | |
741 asyncEnd(); | 883 asyncEnd(); |
742 } | 884 } |
OLD | NEW |