single_child_scroll_view_test.dart 26.9 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:ui';

7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter/widgets.dart';
10

11 12
import 'semantics_tester.dart';

13
class TestScrollPosition extends ScrollPositionWithSingleContext {
14 15
  TestScrollPosition({
    ScrollPhysics physics,
16
    ScrollContext state,
17
    double initialPixels = 0.0,
18 19 20
    ScrollPosition oldPosition,
  }) : super(
    physics: physics,
21
    context: state,
22 23 24 25 26 27 28
    initialPixels: initialPixels,
    oldPosition: oldPosition,
  );
}

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

39 40
void main() {
  testWidgets('SingleChildScrollView control test', (WidgetTester tester) async {
41 42
    await tester.pumpWidget(SingleChildScrollView(
      child: Container(
43
        height: 2000.0,
44
        color: const Color(0xFF00FF00),
45 46 47
      ),
    ));

48
    final RenderBox box = tester.renderObject(find.byType(Container));
49
    expect(box.localToGlobal(Offset.zero), equals(Offset.zero));
50

51
    await tester.drag(find.byType(SingleChildScrollView), const Offset(-200.0, -200.0));
52

53
    expect(box.localToGlobal(Offset.zero), equals(const Offset(0.0, -200.0)));
54
  });
55 56

  testWidgets('Changing controllers changes scroll position', (WidgetTester tester) async {
57
    final TestScrollController controller = TestScrollController();
58

59 60
    await tester.pumpWidget(SingleChildScrollView(
      child: Container(
61
        height: 2000.0,
62
        color: const Color(0xFF00FF00),
63 64 65
      ),
    ));

66
    await tester.pumpWidget(SingleChildScrollView(
67
      controller: controller,
68
      child: Container(
69
        height: 2000.0,
70
        color: const Color(0xFF00FF00),
71 72 73
      ),
    ));

74
    final ScrollableState scrollable = tester.state(find.byType(Scrollable));
75
    expect(scrollable.position, isInstanceOf<TestScrollPosition>());
76 77
  });

78
  testWidgets('Sets PrimaryScrollController when primary', (WidgetTester tester) async {
79 80
    final ScrollController primaryScrollController = ScrollController();
    await tester.pumpWidget(PrimaryScrollController(
81
      controller: primaryScrollController,
82
      child: SingleChildScrollView(
83
        primary: true,
84
        child: Container(
85
          height: 2000.0,
86
          color: const Color(0xFF00FF00),
87 88 89 90
        ),
      ),
    ));

91
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
92 93 94 95
    expect(scrollable.controller, primaryScrollController);
  });


96
  testWidgets('Changing scroll controller inside dirty layout builder does not assert', (WidgetTester tester) async {
97
    final ScrollController controller = ScrollController();
98

99 100
    await tester.pumpWidget(Center(
      child: SizedBox(
101
        width: 750.0,
102
        child: LayoutBuilder(
103
          builder: (BuildContext context, BoxConstraints constraints) {
104 105
            return SingleChildScrollView(
              child: Container(
106
                height: 2000.0,
107
                color: const Color(0xFF00FF00),
108 109 110 111 112 113 114
              ),
            );
          },
        ),
      ),
    ));

115 116
    await tester.pumpWidget(Center(
      child: SizedBox(
117
        width: 700.0,
118
        child: LayoutBuilder(
119
          builder: (BuildContext context, BoxConstraints constraints) {
120
            return SingleChildScrollView(
121
              controller: controller,
122
              child: Container(
123
                height: 2000.0,
124
                color: const Color(0xFF00FF00),
125 126 127 128 129 130 131
              ),
            );
          },
        ),
      ),
    ));
  });
132 133

  testWidgets('Vertical SingleChildScrollViews are primary by default', (WidgetTester tester) async {
134
    const SingleChildScrollView view = SingleChildScrollView(scrollDirection: Axis.vertical);
135 136 137 138
    expect(view.primary, isTrue);
  });

  testWidgets('Horizontal SingleChildScrollViews are non-primary by default', (WidgetTester tester) async {
139
    const SingleChildScrollView view = SingleChildScrollView(scrollDirection: Axis.horizontal);
140 141 142 143
    expect(view.primary, isFalse);
  });

  testWidgets('SingleChildScrollViews with controllers are non-primary by default', (WidgetTester tester) async {
144 145
    final SingleChildScrollView view = SingleChildScrollView(
      controller: ScrollController(),
146 147 148 149 150 151
      scrollDirection: Axis.vertical,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
152
    const Key innerKey = Key('inner');
153
    final ScrollController primaryScrollController = ScrollController();
154
    await tester.pumpWidget(
155
      Directionality(
156
        textDirection: TextDirection.ltr,
157
        child: PrimaryScrollController(
158
          controller: primaryScrollController,
159
          child: SingleChildScrollView(
160
            primary: true,
161
            child: Container(
162
              constraints: const BoxConstraints(maxHeight: 200.0),
163
              child: ListView(key: innerKey, primary: true),
164 165
            ),
          ),
166 167
        ),
      ),
168
    );
169

170
    final Scrollable innerScrollable = tester.widget(
171 172 173 174 175 176 177
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
178 179

  testWidgets('SingleChildScrollView semantics', (WidgetTester tester) async {
180 181
    final SemanticsTester semantics = SemanticsTester(tester);
    final ScrollController controller = ScrollController();
182 183

    await tester.pumpWidget(
184
      Directionality(
185
        textDirection: TextDirection.ltr,
186
        child: SingleChildScrollView(
187
          controller: controller,
188 189 190
          child: Column(
            children: List<Widget>.generate(30, (int i) {
              return Container(
191
                height: 200.0,
192
                child: Text('Tile $i'),
193 194 195 196 197 198 199 200
              );
            }),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
201
      TestSemantics(
202
        children: <TestSemantics>[
203
          TestSemantics(
204 205 206
            flags: <SemanticsFlag>[
              SemanticsFlag.hasImplicitScrolling,
            ],
207 208 209 210
            actions: <SemanticsAction>[
              SemanticsAction.scrollUp,
            ],
            children: <TestSemantics>[
211
              TestSemantics(
212 213 214
                label: r'Tile 0',
                textDirection: TextDirection.ltr,
              ),
215
              TestSemantics(
216 217 218
                label: r'Tile 1',
                textDirection: TextDirection.ltr,
              ),
219
              TestSemantics(
220 221 222
                label: r'Tile 2',
                textDirection: TextDirection.ltr,
              ),
223
              TestSemantics(
224 225 226 227 228 229
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,
                ],
                label: r'Tile 3',
                textDirection: TextDirection.ltr,
              ),
230
              TestSemantics(
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,],
                label: r'Tile 4',
                textDirection: TextDirection.ltr,
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true, ignoreTransform: true, ignoreId: true,
    ));

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

    expect(semantics, hasSemantics(
247
      TestSemantics(
248
        children: <TestSemantics>[
249
          TestSemantics(
250 251 252
            flags: <SemanticsFlag>[
              SemanticsFlag.hasImplicitScrolling,
            ],
253 254 255 256 257
            actions: <SemanticsAction>[
              SemanticsAction.scrollUp,
              SemanticsAction.scrollDown,
            ],
            children: <TestSemantics>[
258
              TestSemantics(
259 260 261 262 263 264
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,
                ],
                label: r'Tile 13',
                textDirection: TextDirection.ltr,
              ),
265
              TestSemantics(
266 267 268 269 270 271
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,
                ],
                label: r'Tile 14',
                textDirection: TextDirection.ltr,
              ),
272
              TestSemantics(
273 274 275
                label: r'Tile 15',
                textDirection: TextDirection.ltr,
              ),
276
              TestSemantics(
277 278 279
                label: r'Tile 16',
                textDirection: TextDirection.ltr,
              ),
280
              TestSemantics(
281 282 283
                label: r'Tile 17',
                textDirection: TextDirection.ltr,
              ),
284
              TestSemantics(
285 286 287 288 289 290
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,
                ],
                label: r'Tile 18',
                textDirection: TextDirection.ltr,
              ),
291
              TestSemantics(
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,
                ],
                label: r'Tile 19',
                textDirection: TextDirection.ltr,
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true, ignoreTransform: true, ignoreId: true,
    ));

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

    expect(semantics, hasSemantics(
309
      TestSemantics(
310
        children: <TestSemantics>[
311
          TestSemantics(
312 313 314
            flags: <SemanticsFlag>[
              SemanticsFlag.hasImplicitScrolling,
            ],
315 316 317 318
            actions: <SemanticsAction>[
              SemanticsAction.scrollDown,
            ],
            children: <TestSemantics>[
319
              TestSemantics(
320 321 322 323 324 325
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,
                ],
                label: r'Tile 25',
                textDirection: TextDirection.ltr,
              ),
326
              TestSemantics(
327 328 329 330 331 332
                flags: <SemanticsFlag>[
                  SemanticsFlag.isHidden,
                ],
                label: r'Tile 26',
                textDirection: TextDirection.ltr,
              ),
333
              TestSemantics(
334 335 336
                label: r'Tile 27',
                textDirection: TextDirection.ltr,
              ),
337
              TestSemantics(
338 339 340
                label: r'Tile 28',
                textDirection: TextDirection.ltr,
              ),
341
              TestSemantics(
342 343 344 345 346 347 348 349 350 351 352 353
                label: r'Tile 29',
                textDirection: TextDirection.ltr,
              ),
            ],
          ),
        ],
      ),
      ignoreRect: true, ignoreTransform: true, ignoreId: true,
    ));

    semantics.dispose();
  });
354 355 356 357

  testWidgets('SingleChildScrollView getOffsetToReveal - down', (WidgetTester tester) async {
    List<Widget> children;
    await tester.pumpWidget(
358
      Directionality(
359
        textDirection: TextDirection.ltr,
360
        child: Center(
361 362 363
          child: Container(
            height: 200.0,
            width: 300.0,
364 365 366 367 368
            child: SingleChildScrollView(
              controller: ScrollController(initialScrollOffset: 300.0),
              child: Column(
                children: children = List<Widget>.generate(20, (int i) {
                  return Container(
369 370
                    height: 100.0,
                    width: 300.0,
371
                    child: Text('Tile $i'),
372 373 374 375 376 377 378 379 380 381 382 383 384 385
                  );
                }),
              ),
            ),
          ),
        ),
      ),
    );

    final RenderAbstractViewport viewport = tester.allRenderObjects.firstWhere((RenderObject r) => r is RenderAbstractViewport);

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
386
    expect(revealed.rect, Rect.fromLTWH(0.0, 0.0, 300.0, 100.0));
387 388 389

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
390
    expect(revealed.rect, Rect.fromLTWH(0.0, 100.0, 300.0, 100.0));
391

392
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
393
    expect(revealed.offset, 540.0);
394
    expect(revealed.rect, Rect.fromLTWH(40.0, 0.0, 10.0, 10.0));
395

396
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
397
    expect(revealed.offset, 350.0);
398
    expect(revealed.rect, Rect.fromLTWH(40.0, 190.0, 10.0, 10.0));
399 400 401
  });

  testWidgets('SingleChildScrollView getOffsetToReveal - up', (WidgetTester tester) async {
402 403
    final List<Widget> children = List<Widget>.generate(20, (int i) {
      return Container(
404 405
        height: 100.0,
        width: 300.0,
406
        child: Text('Tile $i'),
407 408 409
      );
    });
    await tester.pumpWidget(
410
      Directionality(
411
        textDirection: TextDirection.ltr,
412
        child: Center(
413 414 415
          child: Container(
            height: 200.0,
            width: 300.0,
416 417
            child: SingleChildScrollView(
              controller: ScrollController(initialScrollOffset: 300.0),
418
              reverse: true,
419
              child: Column(
420 421 422 423 424 425 426 427 428 429 430 431 432
                children: children.reversed.toList(),
              ),
            ),
          ),
        ),
      ),
    );

    final RenderAbstractViewport viewport = tester.allRenderObjects.firstWhere((RenderObject r) => r is RenderAbstractViewport);

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
433
    expect(revealed.rect, Rect.fromLTWH(0.0, 100.0, 300.0, 100.0));
434 435 436

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
437
    expect(revealed.rect, Rect.fromLTWH(0.0, 0.0, 300.0, 100.0));
438

439
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
440
    expect(revealed.offset, 550.0);
441
    expect(revealed.rect, Rect.fromLTWH(40.0, 190.0, 10.0, 10.0));
442

443
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
444
    expect(revealed.offset, 360.0);
445
    expect(revealed.rect, Rect.fromLTWH(40.0, 0.0, 10.0, 10.0));
446 447 448 449 450 451
  });

  testWidgets('SingleChildScrollView getOffsetToReveal - right', (WidgetTester tester) async {
    List<Widget> children;

    await tester.pumpWidget(
452
      Directionality(
453
        textDirection: TextDirection.ltr,
454
        child: Center(
455 456 457
          child: Container(
            height: 300.0,
            width: 200.0,
458
            child: SingleChildScrollView(
459
              scrollDirection: Axis.horizontal,
460 461 462 463
              controller: ScrollController(initialScrollOffset: 300.0),
              child: Row(
                children: children = List<Widget>.generate(20, (int i) {
                  return Container(
464 465
                    height: 300.0,
                    width: 100.0,
466
                    child: Text('Tile $i'),
467 468 469 470 471 472 473 474 475 476 477 478 479 480
                  );
                }),
              ),
            ),
          ),
        ),
      ),
    );

    final RenderAbstractViewport viewport = tester.allRenderObjects.firstWhere((RenderObject r) => r is RenderAbstractViewport);

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
481
    expect(revealed.rect, Rect.fromLTWH(0.0, 0.0, 100.0, 300.0));
482 483 484

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
485
    expect(revealed.rect, Rect.fromLTWH(100.0, 0.0, 100.0, 300.0));
486

487
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
488
    expect(revealed.offset, 540.0);
489
    expect(revealed.rect, Rect.fromLTWH(0.0, 40.0, 10.0, 10.0));
490

491
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
492
    expect(revealed.offset, 350.0);
493
    expect(revealed.rect, Rect.fromLTWH(190.0, 40.0, 10.0, 10.0));
494 495 496
  });

  testWidgets('SingleChildScrollView getOffsetToReveal - left', (WidgetTester tester) async {
497 498
    final List<Widget> children = List<Widget>.generate(20, (int i) {
      return Container(
499 500
        height: 300.0,
        width: 100.0,
501
        child: Text('Tile $i'),
502 503 504 505
      );
    });

    await tester.pumpWidget(
506
      Directionality(
507
        textDirection: TextDirection.ltr,
508
        child: Center(
509 510 511
          child: Container(
            height: 300.0,
            width: 200.0,
512
            child: SingleChildScrollView(
513 514
              scrollDirection: Axis.horizontal,
              reverse: true,
515 516
              controller: ScrollController(initialScrollOffset: 300.0),
              child: Row(
517 518 519 520 521 522 523 524 525 526 527 528 529
                children: children.reversed.toList(),
              ),
            ),
          ),
        ),
      ),
    );

    final RenderAbstractViewport viewport = tester.allRenderObjects.firstWhere((RenderObject r) => r is RenderAbstractViewport);

    final RenderObject target = tester.renderObject(find.byWidget(children[5]));
    RevealedOffset revealed = viewport.getOffsetToReveal(target, 0.0);
    expect(revealed.offset, 500.0);
530
    expect(revealed.rect, Rect.fromLTWH(100.0, 0.0, 100.0, 300.0));
531 532 533

    revealed = viewport.getOffsetToReveal(target, 1.0);
    expect(revealed.offset, 400.0);
534
    expect(revealed.rect, Rect.fromLTWH(0.0, 0.0, 100.0, 300.0));
535

536
    revealed = viewport.getOffsetToReveal(target, 0.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
537
    expect(revealed.offset, 550.0);
538
    expect(revealed.rect, Rect.fromLTWH(190.0, 40.0, 10.0, 10.0));
539

540
    revealed = viewport.getOffsetToReveal(target, 1.0, rect: Rect.fromLTWH(40.0, 40.0, 10.0, 10.0));
541
    expect(revealed.offset, 360.0);
542
    expect(revealed.rect, Rect.fromLTWH(0.0, 40.0, 10.0, 10.0));
543 544 545
  });

  testWidgets('Nested SingleChildScrollView showOnScreen', (WidgetTester tester) async {
546
    final List<List<Widget>> children = List<List<Widget>>(10);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    ScrollController controllerX;
    ScrollController controllerY;

    /// 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(
569
      Directionality(
570
        textDirection: TextDirection.ltr,
571
        child: Center(
572 573 574
          child: Container(
            height: 200.0,
            width: 200.0,
575 576 577 578
            child: SingleChildScrollView(
              controller: controllerY = ScrollController(initialScrollOffset: 400.0),
              child: SingleChildScrollView(
                controller: controllerX = ScrollController(initialScrollOffset: 400.0),
579
                scrollDirection: Axis.horizontal,
580 581 582 583 584
                child: Column(
                  children: List<Widget>.generate(10, (int y) {
                    return Row(
                      children: children[y] = List<Widget>.generate(10, (int x) {
                        return Container(
585 586 587
                          height: 100.0,
                          width: 100.0,
                        );
588
                      }),
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
                    );
                  }),
                ),
              ),
            ),
          ),
        ),
      ),
    );

    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', () {
    List<Widget> children;

709
    Future<void> buildNestedScroller({ WidgetTester tester, ScrollController inner, ScrollController outer }) {
710
      return tester.pumpWidget(
711
        Directionality(
712
          textDirection: TextDirection.ltr,
713
          child: Center(
714 715 716
            child: Container(
              height: 200.0,
              width: 300.0,
717
              child: SingleChildScrollView(
718
                controller: outer,
719
                child: Column(
720
                  children: <Widget>[
721
                    Container(
722 723
                      height: 200.0,
                    ),
724
                    Container(
725 726
                      height: 200.0,
                      width: 300.0,
727
                      child: SingleChildScrollView(
728
                        controller: inner,
729 730 731
                        child: Column(
                          children: children = List<Widget>.generate(10, (int i) {
                            return Container(
732 733
                              height: 100.0,
                              width: 300.0,
734
                              child: Text('$i'),
735 736 737 738 739
                            );
                          }),
                        ),
                      ),
                    ),
740
                    Container(
741
                      height: 200.0,
742
                    ),
743 744 745 746 747 748 749 750 751 752
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    }

    testWidgets('in view in inner, but not in outer', (WidgetTester tester) async {
753 754
      final ScrollController inner = ScrollController();
      final ScrollController outer = ScrollController();
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
      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);
    });

    testWidgets('not in view of neither inner nor outer', (WidgetTester tester) async {
770 771
      final ScrollController inner = ScrollController();
      final ScrollController outer = ScrollController();
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
      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);
    });

    testWidgets('in view in inner and outer', (WidgetTester tester) async {
787 788
      final ScrollController inner = ScrollController(initialScrollOffset: 200.0);
      final ScrollController outer = ScrollController(initialScrollOffset: 200.0);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
      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);
    });

    testWidgets('inner shown in outer, but item not visible', (WidgetTester tester) async {
804 805
      final ScrollController inner = ScrollController(initialScrollOffset: 200.0);
      final ScrollController outer = ScrollController(initialScrollOffset: 200.0);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
      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);
    });

    testWidgets('inner half shown in outer, item only visible in inner', (WidgetTester tester) async {
821 822
      final ScrollController inner = ScrollController();
      final ScrollController outer = ScrollController(initialScrollOffset: 100.0);
823 824 825 826 827 828 829 830 831 832 833 834 835 836
      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);
    });
  });
837
}