|
| 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';
|
@@ -58,6 +60,28 @@ void main() {
|
58 | 60 | changeNotifier.notifyListeners();
|
59 | 61 | changeNotifier.notifyListeners();
|
60 | 62 | });
|
| 63 | + |
| 64 | + test('Pause resume', () async { |
| 65 | + final changeNotifier = ChangeNotifier(); |
| 66 | + final stream = changeNotifier.toStream(); |
| 67 | + |
| 68 | + final subscription = stream.listen( |
| 69 | + expectAsync1( |
| 70 | + (v) => expect(v, changeNotifier), |
| 71 | + count: 1, |
| 72 | + ), |
| 73 | + )..pause(); |
| 74 | + |
| 75 | + // no effect |
| 76 | + changeNotifier.notifyListeners(); |
| 77 | + changeNotifier.notifyListeners(); |
| 78 | + changeNotifier.notifyListeners(); |
| 79 | + |
| 80 | + await Future<void>.delayed(const Duration(milliseconds: 50)); |
| 81 | + subscription.resume(); |
| 82 | + |
| 83 | + changeNotifier.notifyListeners(); |
| 84 | + }); |
61 | 85 | });
|
62 | 86 |
|
63 | 87 | group('ValueListenableToStream', () {
|
@@ -151,5 +175,84 @@ void main() {
|
151 | 175 | valueNotifier.value = 5;
|
152 | 176 | }
|
153 | 177 | });
|
| 178 | + |
| 179 | + group('Pause resume', () { |
| 180 | + test('not replay', () async { |
| 181 | + final valueNotifier = ValueNotifier(0); |
| 182 | + final stream = valueNotifier.toValueStream(); |
| 183 | + final expected = 4; |
| 184 | + |
| 185 | + final subscription = stream.listen( |
| 186 | + expectAsync1( |
| 187 | + (v) => expect(v, expected), |
| 188 | + count: 1, |
| 189 | + ), |
| 190 | + )..pause(); |
| 191 | + |
| 192 | + // no effect |
| 193 | + valueNotifier.value = 1; |
| 194 | + valueNotifier.value = 2; |
| 195 | + valueNotifier.value = 3; |
| 196 | + |
| 197 | + await Future<void>.delayed(const Duration(milliseconds: 50)); |
| 198 | + subscription.resume(); |
| 199 | + |
| 200 | + valueNotifier.value = expected; |
| 201 | + }); |
| 202 | + |
| 203 | + test('replay + pause immediately', () async { |
| 204 | + final valueNotifier = ValueNotifier(0); |
| 205 | + final stream = valueNotifier.toValueStream(replayValue: true); |
| 206 | + final expected = [0, 4, 5]; |
| 207 | + |
| 208 | + var i = 0; |
| 209 | + final subscription = stream.listen( |
| 210 | + expectAsync1( |
| 211 | + (v) => expect(v, expected[i++]), |
| 212 | + count: expected.length, |
| 213 | + max: expected.length, |
| 214 | + ), |
| 215 | + )..pause(); |
| 216 | + |
| 217 | + // no effect |
| 218 | + valueNotifier.value = 1; |
| 219 | + valueNotifier.value = 2; |
| 220 | + valueNotifier.value = 3; |
| 221 | + |
| 222 | + subscription.resume(); |
| 223 | + |
| 224 | + await pumpEventQueue(); |
| 225 | + valueNotifier.value = 4; |
| 226 | + valueNotifier.value = 5; |
| 227 | + }); |
| 228 | + |
| 229 | + test('replay + pause after events queue.', () async { |
| 230 | + final valueNotifier = ValueNotifier(0); |
| 231 | + final stream = valueNotifier.toValueStream(replayValue: true); |
| 232 | + final expected = [0, 4, 5]; |
| 233 | + |
| 234 | + var i = 0; |
| 235 | + final subscription = stream.listen( |
| 236 | + expectAsync1( |
| 237 | + (v) => expect(v, expected[i++]), |
| 238 | + count: expected.length, |
| 239 | + max: expected.length, |
| 240 | + ), |
| 241 | + ); |
| 242 | + |
| 243 | + await pumpEventQueue(); |
| 244 | + subscription.pause(); |
| 245 | + |
| 246 | + // no effect |
| 247 | + valueNotifier.value = 1; |
| 248 | + valueNotifier.value = 2; |
| 249 | + valueNotifier.value = 3; |
| 250 | + |
| 251 | + subscription.resume(); |
| 252 | + |
| 253 | + valueNotifier.value = 4; |
| 254 | + valueNotifier.value = 5; |
| 255 | + }); |
| 256 | + }); |
154 | 257 | });
|
155 | 258 | }
|
0 commit comments