Skip to content

Commit 5653816

Browse files
committed
test [nfc]: Add checks-extensions for ScrollMetrics, ScrollPosition
1 parent 9070749 commit 5653816

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

test/flutter_checks.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ extension TextEditingControllerChecks on Subject<TextEditingController> {
142142
Subject<String?> get text => has((t) => t.text, 'text');
143143
}
144144

145+
extension ScrollMetricsChecks on Subject<ScrollMetrics> {
146+
Subject<double> get minScrollExtent => has((x) => x.minScrollExtent, 'minScrollExtent');
147+
Subject<double> get maxScrollExtent => has((x) => x.maxScrollExtent, 'maxScrollExtent');
148+
Subject<double> get pixels => has((x) => x.pixels, 'pixels');
149+
Subject<double> get extentBefore => has((x) => x.extentBefore, 'extentBefore');
150+
Subject<double> get extentAfter => has((x) => x.extentAfter, 'extentAfter');
151+
}
152+
153+
extension ScrollPositionChecks on Subject<ScrollPosition> {
154+
Subject<ScrollActivity?> get activity => has((x) => x.activity, 'activity');
155+
}
156+
145157
extension ScrollActivityChecks on Subject<ScrollActivity> {
146158
Subject<double> get velocity => has((x) => x.velocity, 'velocity');
147159
}

test/widgets/message_list_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,13 @@ void main() {
500500
final controller = findMessageListScrollController(tester)!;
501501
controller.jumpTo(-600);
502502
await tester.pump();
503-
check(controller.position.pixels).equals(-600);
503+
check(controller.position).pixels.equals(-600);
504504

505505
// Tap button.
506506
await tester.tap(find.byType(ScrollToBottomButton));
507507
// The list scrolls to the end…
508508
await tester.pumpAndSettle();
509-
check(controller.position.pixels).equals(0);
509+
check(controller.position).pixels.equals(0);
510510
// … and for good measure confirm the button disappeared.
511511
check(isButtonVisible(tester)).equals(false);
512512
});
@@ -520,7 +520,7 @@ void main() {
520520
// Scroll a long distance up, many screenfuls.
521521
controller.jumpTo(-distance);
522522
await tester.pump();
523-
check(controller.position.pixels).equals(-distance);
523+
check(controller.position).pixels.equals(-distance);
524524

525525
// Tap button.
526526
await tester.tap(find.byType(ScrollToBottomButton));

test/widgets/scrolling_test.dart

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void main() {
124124

125125
// Starts out scrolled all the way to the bottom,
126126
// even though it must have taken several rounds of layout to find that.
127-
check(controller.position.pixels)
128-
.equals(itemHeight * numItems * (numItems + 1)/2);
127+
check(controller.position)
128+
.pixels.equals(itemHeight * numItems * (numItems + 1)/2);
129129
check(tester.getRect(find.text('item ${numItems-1}', skipOffstage: false)))
130130
.bottom.equals(600);
131131
});
@@ -198,30 +198,30 @@ void main() {
198198
await prepare(tester, topHeight: 300, bottomHeight: 600);
199199
await tester.drag(findBottom, Offset(0, 300));
200200
await tester.pump();
201-
check(position.extentAfter).equals(300);
201+
check(position).extentAfter.equals(300);
202202

203203
// Start scrolling to end, from just a short distance up.
204204
position.scrollToEnd();
205205
await tester.pump();
206-
check(position.extentAfter).equals(300);
207-
check(position.activity).isA<ScrollToEndActivity>();
206+
check(position).extentAfter.equals(300);
207+
check(position).activity.isA<ScrollToEndActivity>();
208208

209209
// The scrolling moves at a stately pace; …
210210
await tester.pump(Duration(milliseconds: 100));
211-
check(position.extentAfter).equals(200);
211+
check(position).extentAfter.equals(200);
212212

213213
await tester.pump(Duration(milliseconds: 100));
214-
check(position.extentAfter).equals(100);
214+
check(position).extentAfter.equals(100);
215215

216216
// … then upon reaching the end, …
217217
await tester.pump(Duration(milliseconds: 100));
218-
check(position.extentAfter).equals(0);
218+
check(position).extentAfter.equals(0);
219219

220220
// … goes idle on the next frame, …
221221
await tester.pump(Duration(milliseconds: 1));
222-
check(position.activity).isA<IdleScrollActivity>();
222+
check(position).activity.isA<IdleScrollActivity>();
223223
// … without moving any farther.
224-
check(position.extentAfter).equals(0);
224+
check(position).extentAfter.equals(0);
225225
});
226226

227227
testWidgets('long -> bounded speed', (tester) async {
@@ -231,12 +231,12 @@ void main() {
231231
await prepare(tester, topHeight: distance + 1000, bottomHeight: 300);
232232
await tester.drag(findBottom, Offset(0, distance));
233233
await tester.pump();
234-
check(position.extentAfter).equals(distance);
234+
check(position).extentAfter.equals(distance);
235235

236236
// Start scrolling to end.
237237
position.scrollToEnd();
238238
await tester.pump();
239-
check(position.activity).isA<ScrollToEndActivity>();
239+
check(position).activity.isA<ScrollToEndActivity>();
240240

241241
// Let it scroll, plotting the trajectory.
242242
final log = <double>[];
@@ -249,12 +249,12 @@ void main() {
249249
(i) => distance - referenceSpeed * i));
250250

251251
// Having reached the end, …
252-
check(position.extentAfter).equals(0);
252+
check(position).extentAfter.equals(0);
253253
// … it goes idle on the next frame, …
254254
await tester.pump(Duration(milliseconds: 1));
255-
check(position.activity).isA<IdleScrollActivity>();
255+
check(position).activity.isA<IdleScrollActivity>();
256256
// … without moving any farther.
257-
check(position.extentAfter).equals(0);
257+
check(position).extentAfter.equals(0);
258258
});
259259

260260
testWidgets('starting from overscroll, just drift', (tester) async {
@@ -266,33 +266,33 @@ void main() {
266266
await tester.pump();
267267
final offset1 = position.pixels - position.maxScrollExtent;
268268
check(offset1).isGreaterThan(100 / 2);
269-
check(position.activity).isA<BallisticScrollActivity>();
269+
check(position).activity.isA<BallisticScrollActivity>();
270270

271271
// Start drifting back into range.
272272
await tester.pump(Duration(milliseconds: 10));
273273
final offset2 = position.pixels - position.maxScrollExtent;
274274
check(offset2)..isGreaterThan(0.0)..isLessThan(offset1);
275-
check(position.activity).isA<BallisticScrollActivity>()
275+
check(position).activity.isA<BallisticScrollActivity>()
276276
.velocity.isLessThan(0);
277277

278278
// Invoke `scrollToEnd`. The motion should stop…
279279
position.scrollToEnd();
280280
await tester.pump();
281281
check(position.pixels - position.maxScrollExtent).equals(offset2);
282-
check(position.activity).isA<BallisticScrollActivity>()
282+
check(position).activity.isA<BallisticScrollActivity>()
283283
.velocity.equals(0);
284284

285285
// … and resume drifting from there…
286286
await tester.pump(Duration(milliseconds: 10));
287287
final offset3 = position.pixels - position.maxScrollExtent;
288288
check(offset3)..isGreaterThan(0.0)..isLessThan(offset2);
289-
check(position.activity).isA<BallisticScrollActivity>()
289+
check(position).activity.isA<BallisticScrollActivity>()
290290
.velocity.isLessThan(0);
291291

292292
// … to eventually return to being in range.
293293
await tester.pump(Duration(seconds: 1));
294294
check(position.pixels - position.maxScrollExtent).equals(0);
295-
check(position.activity).isA<IdleScrollActivity>();
295+
check(position).activity.isA<IdleScrollActivity>();
296296

297297
debugDefaultTargetPlatformOverride = null;
298298
});
@@ -305,17 +305,17 @@ void main() {
305305

306306
position.jumpTo(398);
307307
await tester.pump();
308-
check(position.extentAfter).equals(2);
308+
check(position).extentAfter.equals(2);
309309

310310
position.scrollToEnd();
311311
await tester.pump();
312-
check(position.extentAfter).equals(2);
312+
check(position).extentAfter.equals(2);
313313

314314
// Reach the end in just 150ms, not 300ms.
315315
await tester.pump(Duration(milliseconds: 75));
316-
check(position.extentAfter).equals(1);
316+
check(position).extentAfter.equals(1);
317317
await tester.pump(Duration(milliseconds: 75));
318-
check(position.extentAfter).equals(0);
318+
check(position).extentAfter.equals(0);
319319
});
320320

321321
testWidgets('on overscroll, stop', (tester) async {
@@ -325,7 +325,7 @@ void main() {
325325
// Scroll up…
326326
position.jumpTo(400);
327327
await tester.pump();
328-
check(position.extentAfter).equals(600);
328+
check(position).extentAfter.equals(600);
329329

330330
// … then invoke `scrollToEnd`…
331331
position.scrollToEnd();
@@ -334,7 +334,7 @@ void main() {
334334
// … but have the bottom sliver turn out to be shorter than it was.
335335
await prepare(tester, topHeight: 400, bottomHeight: 600,
336336
reuseController: true);
337-
check(position.extentAfter).equals(200);
337+
check(position).extentAfter.equals(200);
338338

339339
// Let the scrolling animation proceed until it hits the end.
340340
int steps = 0;
@@ -348,10 +348,10 @@ void main() {
348348
check(position.pixels - position.maxScrollExtent).equals(0);
349349

350350
// … and the animation is done. Nothing further happens.
351-
check(position.activity).isA<IdleScrollActivity>();
351+
check(position).activity.isA<IdleScrollActivity>();
352352
await tester.pump(Duration(milliseconds: 11));
353353
check(position.pixels - position.maxScrollExtent).equals(0);
354-
check(position.activity).isA<IdleScrollActivity>();
354+
check(position).activity.isA<IdleScrollActivity>();
355355

356356
debugDefaultTargetPlatformOverride = null;
357357
});
@@ -362,7 +362,7 @@ void main() {
362362
// Scroll up…
363363
position.jumpTo(0);
364364
await tester.pump();
365-
check(position.extentAfter).equals(3000);
365+
check(position).extentAfter.equals(3000);
366366

367367
// … then invoke `scrollToEnd`…
368368
position.scrollToEnd();
@@ -371,7 +371,7 @@ void main() {
371371
// … but have the bottom sliver turn out to be longer than it was.
372372
await prepare(tester, topHeight: 1000, bottomHeight: 6000,
373373
reuseController: true);
374-
check(position.extentAfter).equals(6000);
374+
check(position).extentAfter.equals(6000);
375375

376376
// Let the scrolling animation go until it stops.
377377
int steps = 0;

0 commit comments

Comments
 (0)