Skip to content

Commit 48a77b5

Browse files
committed
pause resume
1 parent ff04d76 commit 48a77b5

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

test/listenable_stream_test.dart

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/foundation.dart';
24
import 'package:flutter_test/flutter_test.dart';
35
import 'package:listenable_stream/listenable_stream.dart';
@@ -58,6 +60,28 @@ void main() {
5860
changeNotifier.notifyListeners();
5961
changeNotifier.notifyListeners();
6062
});
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+
});
6185
});
6286

6387
group('ValueListenableToStream', () {
@@ -151,5 +175,84 @@ void main() {
151175
valueNotifier.value = 5;
152176
}
153177
});
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+
});
154257
});
155258
}

0 commit comments

Comments
 (0)