|
| 1 | +import 'dart:async'; |
| 2 | + |
1 | 3 | import 'package:flutter/foundation.dart'; |
2 | 4 | import 'package:flutter_test/flutter_test.dart'; |
3 | 5 | import 'package:listenable_stream/listenable_stream.dart'; |
@@ -37,6 +39,50 @@ void main() { |
37 | 39 | final stream = ChangeNotifier().toStream(); |
38 | 40 | _isSingleSubscriptionStream(stream); |
39 | 41 | }); |
| 42 | + |
| 43 | + test('Cancel', () async { |
| 44 | + final changeNotifier = ChangeNotifier(); |
| 45 | + final stream = changeNotifier.toStream(); |
| 46 | + |
| 47 | + final subscription = stream.listen( |
| 48 | + expectAsync1( |
| 49 | + (e) => expect(e, changeNotifier), |
| 50 | + count: 3, |
| 51 | + ), |
| 52 | + ); |
| 53 | + |
| 54 | + changeNotifier.notifyListeners(); |
| 55 | + changeNotifier.notifyListeners(); |
| 56 | + changeNotifier.notifyListeners(); |
| 57 | + |
| 58 | + await pumpEventQueue(); |
| 59 | + await subscription.cancel(); |
| 60 | + |
| 61 | + changeNotifier.notifyListeners(); |
| 62 | + changeNotifier.notifyListeners(); |
| 63 | + }); |
| 64 | + |
| 65 | + test('Pause resume', () async { |
| 66 | + final changeNotifier = ChangeNotifier(); |
| 67 | + final stream = changeNotifier.toStream(); |
| 68 | + |
| 69 | + final subscription = stream.listen( |
| 70 | + expectAsync1( |
| 71 | + (v) => expect(v, changeNotifier), |
| 72 | + count: 1, |
| 73 | + ), |
| 74 | + )..pause(); |
| 75 | + |
| 76 | + // no effect |
| 77 | + changeNotifier.notifyListeners(); |
| 78 | + changeNotifier.notifyListeners(); |
| 79 | + changeNotifier.notifyListeners(); |
| 80 | + |
| 81 | + await Future<void>.delayed(const Duration(milliseconds: 50)); |
| 82 | + subscription.resume(); |
| 83 | + |
| 84 | + changeNotifier.notifyListeners(); |
| 85 | + }); |
40 | 86 | }); |
41 | 87 |
|
42 | 88 | group('ValueListenableToStream', () { |
@@ -82,5 +128,132 @@ void main() { |
82 | 128 | _isSingleSubscriptionStream(stream); |
83 | 129 | } |
84 | 130 | }); |
| 131 | + |
| 132 | + test('Cancel', () async { |
| 133 | + { |
| 134 | + final valueNotifier = ValueNotifier(0); |
| 135 | + final stream = valueNotifier.toValueStream(); |
| 136 | + |
| 137 | + var i = 1; |
| 138 | + final subscription = stream.listen( |
| 139 | + expectAsync1( |
| 140 | + (e) => expect(e, i++), |
| 141 | + count: 3, |
| 142 | + ), |
| 143 | + ); |
| 144 | + |
| 145 | + valueNotifier.value = 1; |
| 146 | + valueNotifier.value = 2; |
| 147 | + valueNotifier.value = 3; |
| 148 | + |
| 149 | + await pumpEventQueue(); |
| 150 | + await subscription.cancel(); |
| 151 | + |
| 152 | + valueNotifier.value = 4; |
| 153 | + valueNotifier.value = 5; |
| 154 | + } |
| 155 | + |
| 156 | + { |
| 157 | + final valueNotifier = ValueNotifier(0); |
| 158 | + final stream = valueNotifier.toValueStream(replayValue: true); |
| 159 | + |
| 160 | + var i = 0; |
| 161 | + final subscription = stream.listen( |
| 162 | + expectAsync1( |
| 163 | + (e) => expect(e, i++), |
| 164 | + count: 4, |
| 165 | + ), |
| 166 | + ); |
| 167 | + |
| 168 | + valueNotifier.value = 1; |
| 169 | + valueNotifier.value = 2; |
| 170 | + valueNotifier.value = 3; |
| 171 | + |
| 172 | + await pumpEventQueue(); |
| 173 | + await subscription.cancel(); |
| 174 | + |
| 175 | + valueNotifier.value = 4; |
| 176 | + valueNotifier.value = 5; |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + group('Pause resume', () { |
| 181 | + test('not replay', () async { |
| 182 | + final valueNotifier = ValueNotifier(0); |
| 183 | + final stream = valueNotifier.toValueStream(); |
| 184 | + final expected = 4; |
| 185 | + |
| 186 | + final subscription = stream.listen( |
| 187 | + expectAsync1( |
| 188 | + (v) => expect(v, expected), |
| 189 | + count: 1, |
| 190 | + ), |
| 191 | + )..pause(); |
| 192 | + |
| 193 | + // no effect |
| 194 | + valueNotifier.value = 1; |
| 195 | + valueNotifier.value = 2; |
| 196 | + valueNotifier.value = 3; |
| 197 | + |
| 198 | + await Future<void>.delayed(const Duration(milliseconds: 50)); |
| 199 | + subscription.resume(); |
| 200 | + |
| 201 | + valueNotifier.value = expected; |
| 202 | + }); |
| 203 | + |
| 204 | + test('replay + pause immediately', () async { |
| 205 | + final valueNotifier = ValueNotifier(0); |
| 206 | + final stream = valueNotifier.toValueStream(replayValue: true); |
| 207 | + final expected = [0, 4, 5]; |
| 208 | + |
| 209 | + var i = 0; |
| 210 | + final subscription = stream.listen( |
| 211 | + expectAsync1( |
| 212 | + (v) => expect(v, expected[i++]), |
| 213 | + count: expected.length, |
| 214 | + max: expected.length, |
| 215 | + ), |
| 216 | + )..pause(); |
| 217 | + |
| 218 | + // no effect |
| 219 | + valueNotifier.value = 1; |
| 220 | + valueNotifier.value = 2; |
| 221 | + valueNotifier.value = 3; |
| 222 | + |
| 223 | + subscription.resume(); |
| 224 | + |
| 225 | + await pumpEventQueue(); |
| 226 | + valueNotifier.value = 4; |
| 227 | + valueNotifier.value = 5; |
| 228 | + }); |
| 229 | + |
| 230 | + test('replay + pause after events queue.', () async { |
| 231 | + final valueNotifier = ValueNotifier(0); |
| 232 | + final stream = valueNotifier.toValueStream(replayValue: true); |
| 233 | + final expected = [0, 4, 5]; |
| 234 | + |
| 235 | + var i = 0; |
| 236 | + final subscription = stream.listen( |
| 237 | + expectAsync1( |
| 238 | + (v) => expect(v, expected[i++]), |
| 239 | + count: expected.length, |
| 240 | + max: expected.length, |
| 241 | + ), |
| 242 | + ); |
| 243 | + |
| 244 | + await pumpEventQueue(); |
| 245 | + subscription.pause(); |
| 246 | + |
| 247 | + // no effect |
| 248 | + valueNotifier.value = 1; |
| 249 | + valueNotifier.value = 2; |
| 250 | + valueNotifier.value = 3; |
| 251 | + |
| 252 | + subscription.resume(); |
| 253 | + |
| 254 | + valueNotifier.value = 4; |
| 255 | + valueNotifier.value = 5; |
| 256 | + }); |
| 257 | + }); |
85 | 258 | }); |
86 | 259 | } |
0 commit comments