single_child_scroll_view_test.dart 37.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter/material.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9

10
import '../rendering/rendering_tester.dart' show TestClipPaintingContext;
11 12
import 'semantics_tester.dart';

13
class TestScrollPosition extends ScrollPositionWithSingleContext {
14
  TestScrollPosition({
15
    required super.physics,
16
    required ScrollContext state,
17 18
    double super.initialPixels,
    super.oldPosition,
19
  }) : super(
20
    context: state,
21 22 23 24 25
  );
}

class TestScrollController extends ScrollController {
  @override
26
  ScrollPosition createScrollPosition(ScrollPhysics physics, ScrollContext context, ScrollPosition? oldPosition) {
27
    return TestScrollPosition(
28
      physics: physics,
29
      state: context,
30 31 32 33 34 35
      initialPixels: initialScrollOffset,
      oldPosition: oldPosition,
    );
  }
}

36 37 38 39 40 41 42 43 44 45 46 47 48
Widget primaryScrollControllerBoilerplate({ required Widget child, required ScrollController controller }) {
  return Directionality(
    textDirection: TextDirection.ltr,
    child: MediaQuery(
      data: const MediaQueryData(),
      child: PrimaryScrollController(
        controller: controller,
        child: child,
      ),
    ),
  );
}

49
void main() {
50 51 52 53 54 55
  testWidgets('SingleChildScrollView overflow and clipRect test', (WidgetTester tester) async {
    // the test widowSize is Size(800.0, 600.0)
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SingleChildScrollView(
56
          child: Container(height: 600.0),
57 58
        ),
      ),
59 60 61
    );

    // 1st, check that the render object has received the default clip behavior.
62
    final dynamic renderObject = tester.allRenderObjects.where((RenderObject o) => o.runtimeType.toString() == '_RenderSingleChildViewport').first;
63
    expect(renderObject.clipBehavior, equals(Clip.hardEdge)); // ignore: avoid_dynamic_calls
64 65 66

    // 2nd, height == widow.height test: check that the painting context does not call pushClipRect .
    TestClipPaintingContext context = TestClipPaintingContext();
67
    renderObject.paint(context, Offset.zero); // ignore: avoid_dynamic_calls
68 69 70 71 72 73 74
    expect(context.clipBehavior, equals(Clip.none));

    // 3rd, height overflow test: check that the painting context call pushClipRect.
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SingleChildScrollView(
75 76 77
          child: Container(height: 600.1),
        ),
      ),
78
    );
79
    renderObject.paint(context, Offset.zero); // ignore: avoid_dynamic_calls
80 81 82 83 84 85 86 87 88 89
    expect(context.clipBehavior, equals(Clip.hardEdge));

    // 4th, width == widow.width test: check that the painting context do not call pushClipRect.
    context = TestClipPaintingContext();
    expect(context.clipBehavior, equals(Clip.none)); // initial value
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
90 91 92
          child: Container(width: 800.0),
        ),
      ),
93
    );
94
    renderObject.paint(context, Offset.zero); // ignore: avoid_dynamic_calls
95 96 97 98 99 100 101 102
    expect(context.clipBehavior, equals(Clip.none));

    // 5th, width overflow test: check that the painting context call pushClipRect.
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
103 104 105
          child: Container(width: 800.1),
        ),
      ),
106
    );
107
    renderObject.paint(context, Offset.zero); // ignore: avoid_dynamic_calls
108 109 110
    expect(context.clipBehavior, equals(Clip.hardEdge));
  });

111 112 113 114
  testWidgets('SingleChildScrollView respects clipBehavior', (WidgetTester tester) async {
    await tester.pumpWidget(SingleChildScrollView(child: Container(height: 2000.0)));

    // 1st, check that the render object has received the default clip behavior.
115
    final dynamic renderObject = tester.allRenderObjects.where((RenderObject o) => o.runtimeType.toString() == '_RenderSingleChildViewport').first;
116
    expect(renderObject.clipBehavior, equals(Clip.hardEdge)); // ignore: avoid_dynamic_calls
117 118 119

    // 2nd, check that the painting context has received the default clip behavior.
    final TestClipPaintingContext context = TestClipPaintingContext();
120
    renderObject.paint(context, Offset.zero); // ignore: avoid_dynamic_calls
121 122
    expect(context.clipBehavior, equals(Clip.hardEdge));

123 124 125 126 127 128 129 130 131
    // 3rd, check that the underlying Scrollable has the same clipBehavior
    // Regression test for https://github.com/flutter/flutter/issues/133330
    Finder scrollable = find.byWidgetPredicate((Widget widget) => widget is Scrollable);
    expect(
      (tester.widget(scrollable) as Scrollable).clipBehavior,
      Clip.hardEdge,
    );

    // 4th, pump a new widget to check that the render object can update its clip behavior.
132
    await tester.pumpWidget(SingleChildScrollView(clipBehavior: Clip.antiAlias, child: Container(height: 2000.0)));
133
    expect(renderObject.clipBehavior, equals(Clip.antiAlias)); // ignore: avoid_dynamic_calls
134

135
    // 5th, check that a non-default clip behavior can be sent to the painting context.
136
    renderObject.paint(context, Offset.zero); // ignore: avoid_dynamic_calls
137
    expect(context.clipBehavior, equals(Clip.antiAlias));
138 139 140 141 142 143 144 145

    // 6th, check that the underlying Scrollable has the same clipBehavior
    // Regression test for https://github.com/flutter/flutter/issues/133330
    scrollable = find.byWidgetPredicate((Widget widget) => widget is Scrollable);
    expect(
      (tester.widget(scrollable) as Scrollable).clipBehavior,
      Clip.antiAlias,
    );
146 147
  });

148
  testWidgetsWithLeakTracking('SingleChildScrollView control test', (WidgetTester tester) async {
149 150
    await tester.pumpWidget(SingleChildScrollView(
      child: Container(
151
        height: 2000.0,
152
        color: const Color(0xFF00FF00),
153 154 155
      ),
    ));

156
    final RenderBox box = tester.renderObject(find.byType(Container));
157
    expect(box.localToGlobal(Offset.zero), equals(Offset.zero));
158

159
    await tester.drag(find.byType(SingleChildScrollView), const Offset(-200.0, -200.0));
160

161
    expect(box.localToGlobal(Offset.zero), equals(const Offset(0.0, -200.0)));
162
  });
163

164
  testWidgetsWithLeakTracking('Changing controllers changes scroll position', (WidgetTester tester) async {
165
    final TestScrollController controller = TestScrollController();
166
    addTearDown(controller.dispose);
167

168 169
    await tester.pumpWidget(SingleChildScrollView(
      child: Container(
170
        height: 2000.0,
171
        color: const Color(0xFF00FF00),
172 173 174
      ),
    ));

175
    await tester.pumpWidget(SingleChildScrollView(
176
      controller: controller,
177
      child: Container(
178
        height: 2000.0,
179
        color: const Color(0xFF00FF00),
180 181 182
      ),
    ));

183
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
Dan Field's avatar
Dan Field committed
184
    expect(scrollable.position, isA<TestScrollPosition>());
185 186
  });

187
  testWidgetsWithLeakTracking('Sets PrimaryScrollController when primary', (WidgetTester tester) async {
188
    final ScrollController primaryScrollController = ScrollController();
189
    addTearDown(primaryScrollController.dispose);
190
    await tester.pumpWidget(PrimaryScrollController(
191
      controller: primaryScrollController,
192
      child: SingleChildScrollView(
193
        primary: true,
194
        child: Container(
195
          height: 2000.0,
196
          color: const Color(0xFF00FF00),
197 198 199 200
        ),
      ),
    ));

201
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
202 203 204 205
    expect(scrollable.controller, primaryScrollController);
  });


206
  testWidgetsWithLeakTracking('Changing scroll controller inside dirty layout builder does not assert', (WidgetTester tester) async {
207
    final ScrollController controller = ScrollController();
208
    addTearDown(controller.dispose);
209

210 211
    await tester.pumpWidget(Center(
      child: SizedBox(
212
        width: 750.0,
213
        child: LayoutBuilder(
214
          builder: (BuildContext context, BoxConstraints constraints) {
215 216
            return SingleChildScrollView(
              child: Container(
217
                height: 2000.0,
218
                color: const Color(0xFF00FF00),
219 220 221 222 223 224 225
              ),
            );
          },
        ),
      ),
    ));

226 227
    await tester.pumpWidget(Center(
      child: SizedBox(
228
        width: 700.0,
229
        child: LayoutBuilder(
230
          builder: (BuildContext context, BoxConstraints constraints) {
231
            return SingleChildScrollView(
232
              controller: controller,
233
              child: Container(
234
                height: 2000.0,
235
                color: const Color(0xFF00FF00),
236 237 238 239 240 241 242
              ),
            );
          },
        ),
      ),
    ));
  });
243

244
  testWidgetsWithLeakTracking('Vertical SingleChildScrollViews are not primary by default', (WidgetTester tester) async {
245
    const SingleChildScrollView view = SingleChildScrollView();
246
    expect(view.primary, isNull);
247 248
  });

249
  testWidgetsWithLeakTracking('Horizontal SingleChildScrollViews are not primary by default', (WidgetTester tester) async {
250
    const SingleChildScrollView view = SingleChildScrollView(scrollDirection: Axis.horizontal);
251
    expect(view.primary, isNull);
252 253
  });

254 255 256
  testWidgetsWithLeakTracking('SingleChildScrollViews with controllers are not primary by default', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    addTearDown(controller.dispose);
257
    final SingleChildScrollView view = SingleChildScrollView(
258
      controller: controller,
259
    );
260
    expect(view.primary, isNull);
261 262
  });

263
  testWidgetsWithLeakTracking('Vertical SingleChildScrollViews use PrimaryScrollController by default on mobile', (WidgetTester tester) async {
264
    final ScrollController controller = ScrollController();
265
    addTearDown(controller.dispose);
266 267 268 269 270 271 272
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: const SingleChildScrollView(),
      controller: controller,
    ));
    expect(controller.hasClients, isTrue);
  }, variant: TargetPlatformVariant.mobile());

273
  testWidgetsWithLeakTracking("Vertical SingleChildScrollViews don't use PrimaryScrollController by default on desktop", (WidgetTester tester) async {
274
    final ScrollController controller = ScrollController();
275
    addTearDown(controller.dispose);
276 277 278 279 280 281 282
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: const SingleChildScrollView(),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
  }, variant: TargetPlatformVariant.desktop());

283
  testWidgetsWithLeakTracking('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
284
    const Key innerKey = Key('inner');
285
    final ScrollController primaryScrollController = ScrollController();
286
    addTearDown(primaryScrollController.dispose);
287
    await tester.pumpWidget(
288
      Directionality(
289
        textDirection: TextDirection.ltr,
290
        child: PrimaryScrollController(
291
          controller: primaryScrollController,
292
          child: SingleChildScrollView(
293
            primary: true,
294
            child: Container(
295
              constraints: const BoxConstraints(maxHeight: 200.0),
296
              child: ListView(key: innerKey, primary: true),
297 298
            ),
          ),
299 300
        ),
      ),
301
    );
302

303
    final Scrollable innerScrollable = tester.widget(
304 305 306 307 308 309 310
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
311

312
  testWidgetsWithLeakTracking('SingleChildScrollView semantics', (WidgetTester tester) async {
313 314
    final SemanticsTester semantics = SemanticsTester(tester);
    final ScrollController controller = ScrollController();
315
    addTearDown(controller.dispose);
316 317

    await tester.pumpWidget(
318
      Directionality(
319
        textDirection: TextDirection.ltr,
320
        child: SingleChildScrollView(
321
          controller: controller,
322 323
          child: Column(
            children: List<Widget>.generate(30, (int i) {
324
              return SizedBox(
325
                height: 200.0,
326
                child: Text('Tile $i'),
327 328 329 330 331 332 333
              );
            }),
          ),
        ),
      ),
    );

334 335 336 337 338 339 340 341 342 343 344 345 346
    List<TestSemantics> generateSemanticsChildren({int startHidden = -1, int endHidden = 30}) {
      final List<TestSemantics> children = <TestSemantics>[];
      for (int index = 0; index < 30; index += 1) {
        final bool isHidden = index <= startHidden || index >= endHidden;
        children.add(TestSemantics(
          label: 'Tile $index',
          textDirection: TextDirection.ltr,
          flags: isHidden ? const <SemanticsFlag>[SemanticsFlag.isHidden] : 0,
        ));
      }
      return children;
    }

347
    expect(semantics, hasSemantics(
348
      TestSemantics(
349
        children: <TestSemantics>[
350
          TestSemantics(
351 352 353
            flags: <SemanticsFlag>[
              SemanticsFlag.hasImplicitScrolling,
            ],
354 355 356
            actions: <SemanticsAction>[
              SemanticsAction.scrollUp,
            ],
357
            children: generateSemanticsChildren(endHidden: 3),
358 359 360 361 362 363 364 365 366 367
          ),
        ],
      ),
      ignoreRect: true, ignoreTransform: true, ignoreId: true,
    ));

    controller.jumpTo(3000.0);
    await tester.pumpAndSettle();

    expect(semantics, hasSemantics(
368
      TestSemantics(
369
        children: <TestSemantics>[
370
          TestSemantics(
371 372 373
            flags: <SemanticsFlag>[
              SemanticsFlag.hasImplicitScrolling,
            ],
374 375 376 377
            actions: <SemanticsAction>[
              SemanticsAction.scrollUp,
              SemanticsAction.scrollDown,
            ],
378
            children: generateSemanticsChildren(startHidden: 14, endHidden: 18),
379 380 381 382 383 384 385 386 387 388
          ),
        ],
      ),
      ignoreRect: true, ignoreTransform: true, ignoreId: true,
    ));

    controller.jumpTo(6000.0);
    await tester.pumpAndSettle();

    expect(semantics, hasSemantics(
389
      TestSemantics(
390
        children: <TestSemantics>[
391
          TestSemantics(
392 393 394
            flags: <SemanticsFlag>[
              SemanticsFlag.hasImplicitScrolling,
            ],
395 396 397
            actions: <SemanticsAction>[
              SemanticsAction.scrollDown,
            ],
398
            children: generateSemanticsChildren(startHidden: 26),
399 400 401 402 403 404 405 406
          ),
        ],
      ),
      ignoreRect: true, ignoreTransform: true, ignoreId: true,
    ));

    semantics.dispose();
  });
407

408
  testWidgetsWithLeakTracking('SingleChildScrollView semantics clips cover entire child vertical', (WidgetTester tester) async {
409
    final ScrollController controller = ScrollController();
410
    addTearDown(controller.dispose);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    final UniqueKey scrollView = UniqueKey();
    final UniqueKey childBox = UniqueKey();
    const double length = 10000;
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SingleChildScrollView(
          key: scrollView,
          controller: controller,
          child: SizedBox(key: childBox, height: length),
        ),
      ),
    );
    final RenderObject scrollRenderObject = tester.renderObject(find.byKey(scrollView));
    RenderAbstractViewport? viewport;
    void findsRenderViewPort(RenderObject child) {
      if (viewport != null) {
        return;
      }
      if (child is RenderAbstractViewport) {
        viewport = child;
        return;
      }
      child.visitChildren(findsRenderViewPort);
    }
    scrollRenderObject.visitChildren(findsRenderViewPort);
    expect(viewport, isNotNull);
    final RenderObject childRenderObject = tester.renderObject(find.byKey(childBox));
    Rect semanticsClip = viewport!.describeSemanticsClip(childRenderObject)!;
    expect(semanticsClip.size.height, length);

    controller.jumpTo(2000);
    await tester.pump();
    semanticsClip = viewport!.describeSemanticsClip(childRenderObject)!;
    expect(semanticsClip.size.height, length);
  });

448
  testWidgetsWithLeakTracking('SingleChildScrollView semantics clips cover entire child', (WidgetTester tester) async {
449
    final ScrollController controller = ScrollController();
450
    addTearDown(controller.dispose);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    final UniqueKey scrollView = UniqueKey();
    final UniqueKey childBox = UniqueKey();
    const double length = 10000;
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: SingleChildScrollView(
          key: scrollView,
          scrollDirection: Axis.horizontal,
          controller: controller,
          child: SizedBox(key: childBox, width: length),
        ),
      ),
    );
    final RenderObject scrollRenderObject = tester.renderObject(find.byKey(scrollView));
    RenderAbstractViewport? viewport;
    void findsRenderViewPort(RenderObject child) {
      if (viewport != null) {
        return;
      }
      if (child is RenderAbstractViewport) {
        viewport = child;
        return;
      }
      child.visitChildren(findsRenderViewPort);
    }
    scrollRenderObject.visitChildren(findsRenderViewPort);
    expect(viewport, isNotNull);
    final RenderObject childRenderObject = tester.renderObject(find.byKey(childBox));
    Rect semanticsClip = viewport!.describeSemanticsClip(childRenderObject)!;
    expect(semanticsClip.size.width, length);

    controller.jumpTo(2000);
    await tester.pump();
    semanticsClip = viewport!.describeSemanticsClip(childRenderObject)!;
    expect(semanticsClip.size.width, length);
  });

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
  testWidgetsWithLeakTracking('SingleChildScrollView getOffsetToReveal - will not assert on axis mismatch', (WidgetTester tester) async {
    final ScrollController controller = ScrollController(initialScrollOffset: 300.0);
    addTearDown(controller.dispose);
    List<Widget> children;
    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: Center(
          child: SizedBox(
            height: 200.0,
            width: 300.0,
            child: SingleChildScrollView(
              controller: controller,
              child: Column(
                children: children = List<Widget>.generate(20, (int i) {
                  return SizedBox(
                    height: 100.0,
                    width: 300.0,
                    child: Text('Tile $i'),
                  );
                }),
              ),
            ),
          ),
        ),
      ),
    );

    final RenderAbstractViewport viewport = tester.allRenderObjects.whereType<RenderAbstractViewport>().first;

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    viewport.getOffsetToReveal(target, 0.0, axis: Axis.horizontal);
  });

523 524 525
  testWidgetsWithLeakTracking('SingleChildScrollView getOffsetToReveal - down', (WidgetTester tester) async {
    final ScrollController controller = ScrollController(initialScrollOffset: 300.0);
    addTearDown(controller.dispose);
526 527
    List<Widget> children;
    await tester.pumpWidget(
528
      Directionality(
529
        textDirection: TextDirection.ltr,
530
        child: Center(
531
          child: SizedBox(
532 533
            height: 200.0,
            width: 300.0,
534
            child: SingleChildScrollView(
535
              controller: controller,
536 537
              child: Column(
                children: children = List<Widget>.generate(20, (int i) {
538
                  return SizedBox(
539 540
                    height: 100.0,
                    width: 300.0,
541
                    child: Text('Tile $i'),
542 543 544 545 546 547 548 549 550
                  );
                }),
              ),
            ),
          ),
        ),
      ),
    );

551
    final RenderAbstractViewport viewport = tester.allRenderObjects.whereType<RenderAbstractViewport>().first;
552 553 554 555

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
Dan Field's avatar
Dan Field committed
556
    expect(revealed.rect, const Rect.fromLTWH(0.0, 0.0, 300.0, 100.0));
557 558 559

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
Dan Field's avatar
Dan Field committed
560
    expect(revealed.rect, const Rect.fromLTWH(0.0, 100.0, 300.0, 100.0));
561

Dan Field's avatar
Dan Field committed
562
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
563
    expect(revealed.offset, 540.0);
Dan Field's avatar
Dan Field committed
564
    expect(revealed.rect, const Rect.fromLTWH(40.0, 0.0, 10.0, 10.0));
565

Dan Field's avatar
Dan Field committed
566
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
567
    expect(revealed.offset, 350.0);
Dan Field's avatar
Dan Field committed
568
    expect(revealed.rect, const Rect.fromLTWH(40.0, 190.0, 10.0, 10.0));
569 570
  });

571 572 573
  testWidgetsWithLeakTracking('SingleChildScrollView getOffsetToReveal - up', (WidgetTester tester) async {
    final ScrollController controller = ScrollController(initialScrollOffset: 300.0);
    addTearDown(controller.dispose);
574
    final List<Widget> children = List<Widget>.generate(20, (int i) {
575
      return SizedBox(
576 577
        height: 100.0,
        width: 300.0,
578
        child: Text('Tile $i'),
579 580 581
      );
    });
    await tester.pumpWidget(
582
      Directionality(
583
        textDirection: TextDirection.ltr,
584
        child: Center(
585
          child: SizedBox(
586 587
            height: 200.0,
            width: 300.0,
588
            child: SingleChildScrollView(
589
              controller: controller,
590
              reverse: true,
591
              child: Column(
592 593 594 595 596 597 598 599
                children: children.reversed.toList(),
              ),
            ),
          ),
        ),
      ),
    );

600
    final RenderAbstractViewport viewport = tester.allRenderObjects.whereType<RenderAbstractViewport>().first;
601 602 603 604

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
Dan Field's avatar
Dan Field committed
605
    expect(revealed.rect, const Rect.fromLTWH(0.0, 100.0, 300.0, 100.0));
606 607 608

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
Dan Field's avatar
Dan Field committed
609
    expect(revealed.rect, const Rect.fromLTWH(0.0, 0.0, 300.0, 100.0));
610

Dan Field's avatar
Dan Field committed
611
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
612
    expect(revealed.offset, 550.0);
Dan Field's avatar
Dan Field committed
613
    expect(revealed.rect, const Rect.fromLTWH(40.0, 190.0, 10.0, 10.0));
614

Dan Field's avatar
Dan Field committed
615
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
616
    expect(revealed.offset, 360.0);
Dan Field's avatar
Dan Field committed
617
    expect(revealed.rect, const Rect.fromLTWH(40.0, 0.0, 10.0, 10.0));
618 619
  });

620 621 622
  testWidgetsWithLeakTracking('SingleChildScrollView getOffsetToReveal - right', (WidgetTester tester) async {
    final ScrollController controller = ScrollController(initialScrollOffset: 300.0);
    addTearDown(controller.dispose);
623 624 625
    List<Widget> children;

    await tester.pumpWidget(
626
      Directionality(
627
        textDirection: TextDirection.ltr,
628
        child: Center(
629
          child: SizedBox(
630 631
            height: 300.0,
            width: 200.0,
632
            child: SingleChildScrollView(
633
              scrollDirection: Axis.horizontal,
634
              controller: controller,
635 636
              child: Row(
                children: children = List<Widget>.generate(20, (int i) {
637
                  return SizedBox(
638 639
                    height: 300.0,
                    width: 100.0,
640
                    child: Text('Tile $i'),
641 642 643 644 645 646 647 648 649
                  );
                }),
              ),
            ),
          ),
        ),
      ),
    );

650
    final RenderAbstractViewport viewport = tester.allRenderObjects.whereType<RenderAbstractViewport>().first;
651 652 653 654

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
Dan Field's avatar
Dan Field committed
655
    expect(revealed.rect, const Rect.fromLTWH(0.0, 0.0, 100.0, 300.0));
656 657 658

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
Dan Field's avatar
Dan Field committed
659
    expect(revealed.rect, const Rect.fromLTWH(100.0, 0.0, 100.0, 300.0));
660

Dan Field's avatar
Dan Field committed
661
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
662
    expect(revealed.offset, 540.0);
Dan Field's avatar
Dan Field committed
663
    expect(revealed.rect, const Rect.fromLTWH(0.0, 40.0, 10.0, 10.0));
664

Dan Field's avatar
Dan Field committed
665
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
666
    expect(revealed.offset, 350.0);
Dan Field's avatar
Dan Field committed
667
    expect(revealed.rect, const Rect.fromLTWH(190.0, 40.0, 10.0, 10.0));
668 669
  });

670 671 672
  testWidgetsWithLeakTracking('SingleChildScrollView getOffsetToReveal - left', (WidgetTester tester) async {
    final ScrollController controller = ScrollController(initialScrollOffset: 300.0);
    addTearDown(controller.dispose);
673
    final List<Widget> children = List<Widget>.generate(20, (int i) {
674
      return SizedBox(
675 676
        height: 300.0,
        width: 100.0,
677
        child: Text('Tile $i'),
678 679 680 681
      );
    });

    await tester.pumpWidget(
682
      Directionality(
683
        textDirection: TextDirection.ltr,
684
        child: Center(
685
          child: SizedBox(
686 687
            height: 300.0,
            width: 200.0,
688
            child: SingleChildScrollView(
689 690
              scrollDirection: Axis.horizontal,
              reverse: true,
691
              controller: controller,
692
              child: Row(
693 694 695 696 697 698 699 700
                children: children.reversed.toList(),
              ),
            ),
          ),
        ),
      ),
    );

701
    final RenderAbstractViewport viewport = tester.allRenderObjects.whereType<RenderAbstractViewport>().first;
702 703 704 705

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
Dan Field's avatar
Dan Field committed
706
    expect(revealed.rect, const Rect.fromLTWH(100.0, 0.0, 100.0, 300.0));
707 708 709

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
Dan Field's avatar
Dan Field committed
710
    expect(revealed.rect, const Rect.fromLTWH(0.0, 0.0, 100.0, 300.0));
711

Dan Field's avatar
Dan Field committed
712
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
713
    expect(revealed.offset, 550.0);
Dan Field's avatar
Dan Field committed
714
    expect(revealed.rect, const Rect.fromLTWH(190.0, 40.0, 10.0, 10.0));
715

Dan Field's avatar
Dan Field committed
716
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: const Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
717
    expect(revealed.offset, 360.0);
Dan Field's avatar
Dan Field committed
718
    expect(revealed.rect, const Rect.fromLTWH(0.0, 40.0, 10.0, 10.0));
719 720
  });

721
  testWidgetsWithLeakTracking('Nested SingleChildScrollView showOnScreen', (WidgetTester tester) async {
722 723 724 725 726 727 728 729 730
    final List<List<Widget>> children = List<List<Widget>>.generate(10, (int x) {
      return List<Widget>.generate(10, (int y) {
        return SizedBox(
          key: UniqueKey(),
          height: 100.0,
          width: 100.0,
        );
      });
    });
731 732 733 734
    late ScrollController controllerX;
    addTearDown(() => controllerX.dispose());
    late ScrollController controllerY;
    addTearDown(() => controllerY.dispose());
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

    /// Builds a gird:
    ///
    ///       <- x ->
    ///   0 1 2 3 4 5 6 7 8 9
    /// 0 c c c c c c c c c c
    /// 1 c c c c c c c c c c
    /// 2 c c c c c c c c c c
    /// 3 c c c c c c c c c c  y
    /// 4 c c c c v v c c c c
    /// 5 c c c c v v c c c c
    /// 6 c c c c c c c c c c
    /// 7 c c c c c c c c c c
    /// 8 c c c c c c c c c c
    /// 9 c c c c c c c c c c
    ///
    /// Each c is a 100x100 container, v are containers visible in initial
    /// viewport.

    await tester.pumpWidget(
755
      Directionality(
756
        textDirection: TextDirection.ltr,
757
        child: Center(
758
          child: SizedBox(
759 760
            height: 200.0,
            width: 200.0,
761 762 763 764
            child: SingleChildScrollView(
              controller: controllerY = ScrollController(initialScrollOffset: 400.0),
              child: SingleChildScrollView(
                controller: controllerX = ScrollController(initialScrollOffset: 400.0),
765
                scrollDirection: Axis.horizontal,
766
                child: Column(
767
                  children: children.map((List<Widget> widgets) {
768
                    return Row(
769
                      children: widgets,
770
                    );
771
                  }).toList(),
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
                ),
              ),
            ),
          ),
        ),
      ),
    );

    expect(controllerX.offset, 400.0);
    expect(controllerY.offset, 400.0);

    // Already in viewport
    tester.renderObject(find.byWidget(children[4][4])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 400.0);
    expect(controllerY.offset, 400.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Above viewport
    tester.renderObject(find.byWidget(children[3][4])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 400.0);
    expect(controllerY.offset, 300.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Below viewport
    tester.renderObject(find.byWidget(children[6][4])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 400.0);
    expect(controllerY.offset, 500.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Left of viewport
    tester.renderObject(find.byWidget(children[4][3])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 300.0);
    expect(controllerY.offset, 400.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Right of viewport
    tester.renderObject(find.byWidget(children[4][6])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 500.0);
    expect(controllerY.offset, 400.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Above and left of viewport
    tester.renderObject(find.byWidget(children[3][3])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 300.0);
    expect(controllerY.offset, 300.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Below and left of viewport
    tester.renderObject(find.byWidget(children[6][3])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 300.0);
    expect(controllerY.offset, 500.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Above and right of viewport
    tester.renderObject(find.byWidget(children[3][6])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 500.0);
    expect(controllerY.offset, 300.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Below and right of viewport
    tester.renderObject(find.byWidget(children[6][6])).showOnScreen();
    await tester.pumpAndSettle();
    expect(controllerX.offset, 500.0);
    expect(controllerY.offset, 500.0);

    controllerX.jumpTo(400.0);
    controllerY.jumpTo(400.0);
    await tester.pumpAndSettle();

    // Below and right of viewport with animations
    tester.renderObject(find.byWidget(children[6][6])).showOnScreen(duration: const Duration(seconds: 2));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    expect(tester.hasRunningAnimations, isTrue);
    expect(controllerX.offset, greaterThan(400.0));
    expect(controllerX.offset, lessThan(500.0));
    expect(controllerY.offset, greaterThan(400.0));
    expect(controllerY.offset, lessThan(500.0));
    await tester.pumpAndSettle();
    expect(controllerX.offset, 500.0);
    expect(controllerY.offset, 500.0);
  });

  group('Nested SingleChildScrollView (same orientation) showOnScreen', () {
888
    late List<Widget> children;
889

890
    Future<void> buildNestedScroller({ required WidgetTester tester, ScrollController? inner, ScrollController? outer }) {
891
      return tester.pumpWidget(
892
        Directionality(
893
          textDirection: TextDirection.ltr,
894
          child: Center(
895
            child: SizedBox(
896 897
              height: 200.0,
              width: 300.0,
898
              child: SingleChildScrollView(
899
                controller: outer,
900
                child: Column(
901
                  children: <Widget>[
902
                    const SizedBox(
903 904
                      height: 200.0,
                    ),
905
                    SizedBox(
906 907
                      height: 200.0,
                      width: 300.0,
908
                      child: SingleChildScrollView(
909
                        controller: inner,
910 911
                        child: Column(
                          children: children = List<Widget>.generate(10, (int i) {
912
                            return SizedBox(
913 914
                              height: 100.0,
                              width: 300.0,
915
                              child: Text('$i'),
916 917 918 919 920
                            );
                          }),
                        ),
                      ),
                    ),
921
                    const SizedBox(
922
                      height: 200.0,
923
                    ),
924 925 926 927 928 929 930 931 932
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    }

933
    testWidgetsWithLeakTracking('in view in inner, but not in outer', (WidgetTester tester) async {
934
      final ScrollController inner = ScrollController();
935
      addTearDown(inner.dispose);
936
      final ScrollController outer = ScrollController();
937
      addTearDown(outer.dispose);
938 939 940 941 942 943 944 945 946 947 948 949 950 951
      await buildNestedScroller(
        tester: tester,
        inner: inner,
        outer: outer,
      );
      expect(outer.offset, 0.0);
      expect(inner.offset, 0.0);

      tester.renderObject(find.byWidget(children[0])).showOnScreen();
      await tester.pumpAndSettle();
      expect(inner.offset, 0.0);
      expect(outer.offset, 100.0);
    });

952
    testWidgetsWithLeakTracking('not in view of neither inner nor outer', (WidgetTester tester) async {
953
      final ScrollController inner = ScrollController();
954
      addTearDown(inner.dispose);
955
      final ScrollController outer = ScrollController();
956
      addTearDown(outer.dispose);
957 958 959 960 961 962 963 964 965 966 967 968 969 970
      await buildNestedScroller(
        tester: tester,
        inner: inner,
        outer: outer,
      );
      expect(outer.offset, 0.0);
      expect(inner.offset, 0.0);

      tester.renderObject(find.byWidget(children[5])).showOnScreen();
      await tester.pumpAndSettle();
      expect(inner.offset, 400.0);
      expect(outer.offset, 200.0);
    });

971
    testWidgetsWithLeakTracking('in view in inner and outer', (WidgetTester tester) async {
972
      final ScrollController inner = ScrollController(initialScrollOffset: 200.0);
973
      addTearDown(inner.dispose);
974
      final ScrollController outer = ScrollController(initialScrollOffset: 200.0);
975
      addTearDown(outer.dispose);
976 977 978 979 980 981 982 983 984 985 986 987 988 989
      await buildNestedScroller(
        tester: tester,
        inner: inner,
        outer: outer,
      );
      expect(outer.offset, 200.0);
      expect(inner.offset, 200.0);

      tester.renderObject(find.byWidget(children[2])).showOnScreen();
      await tester.pumpAndSettle();
      expect(outer.offset, 200.0);
      expect(inner.offset, 200.0);
    });

990
    testWidgetsWithLeakTracking('inner shown in outer, but item not visible', (WidgetTester tester) async {
991
      final ScrollController inner = ScrollController(initialScrollOffset: 200.0);
992
      addTearDown(inner.dispose);
993
      final ScrollController outer = ScrollController(initialScrollOffset: 200.0);
994
      addTearDown(outer.dispose);
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
      await buildNestedScroller(
        tester: tester,
        inner: inner,
        outer: outer,
      );
      expect(outer.offset, 200.0);
      expect(inner.offset, 200.0);

      tester.renderObject(find.byWidget(children[5])).showOnScreen();
      await tester.pumpAndSettle();
      expect(outer.offset, 200.0);
      expect(inner.offset, 400.0);
    });

1009
    testWidgetsWithLeakTracking('inner half shown in outer, item only visible in inner', (WidgetTester tester) async {
1010
      final ScrollController inner = ScrollController();
1011
      addTearDown(inner.dispose);
1012
      final ScrollController outer = ScrollController(initialScrollOffset: 100.0);
1013
      addTearDown(outer.dispose);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
      await buildNestedScroller(
        tester: tester,
        inner: inner,
        outer: outer,
      );
      expect(outer.offset, 100.0);
      expect(inner.offset, 0.0);

      tester.renderObject(find.byWidget(children[1])).showOnScreen();
      await tester.pumpAndSettle();
      expect(outer.offset, 200.0);
      expect(inner.offset, 0.0);
    });
  });
1028

1029
  testWidgetsWithLeakTracking('keyboardDismissBehavior tests', (WidgetTester tester) async {
1030
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());
1031 1032 1033 1034 1035
    addTearDown(() {
      for (final FocusNode node in focusNodes) {
        node.dispose();
      }
    });
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

    Future<void> boilerplate(ScrollViewKeyboardDismissBehavior behavior) {
      return tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: SingleChildScrollView(
              padding: EdgeInsets.zero,
              keyboardDismissBehavior: behavior,
              child: Column(
                children: focusNodes.map((FocusNode focusNode) {
1046
                  return SizedBox(
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
                    height: 50,
                    child: TextField(focusNode: focusNode),
                  );
                }).toList(),
              ),
            ),
          ),
        ),
      );
    }

    // ScrollViewKeyboardDismissBehavior.onDrag dismiss keyboard on drag
    await boilerplate(ScrollViewKeyboardDismissBehavior.onDrag);

    Finder finder = find.byType(TextField).first;
    TextField textField = tester.widget(finder);
    await tester.showKeyboard(finder);
    expect(textField.focusNode!.hasFocus, isTrue);

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
    expect(textField.focusNode!.hasFocus, isFalse);

    // ScrollViewKeyboardDismissBehavior.manual does no dismiss the keyboard
    await boilerplate(ScrollViewKeyboardDismissBehavior.manual);

    finder = find.byType(TextField).first;
    textField = tester.widget(finder);
    await tester.showKeyboard(finder);
    expect(textField.focusNode!.hasFocus, isTrue);

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
    expect(textField.focusNode!.hasFocus, isTrue);
  });
1082
}