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

import 'dart:math' as math;

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

11 12
import 'two_dimensional_utils.dart';

13
Finder findKey(int i) => find.byKey(ValueKey<int>(i), skipOffstage: false);
14

15
Widget buildSingleChildScrollView(Axis scrollDirection, { bool reverse = false }) {
16
  return Directionality(
17
    textDirection: TextDirection.ltr,
18 19
    child: Center(
      child: SizedBox(
20 21
        width: 600.0,
        height: 400.0,
22
        child: SingleChildScrollView(
23 24
          scrollDirection: scrollDirection,
          reverse: reverse,
25
          child: ListBody(
26
            mainAxis: scrollDirection,
27 28 29 30 31 32 33 34
            children: const <Widget>[
              SizedBox(key: ValueKey<int>(0), width: 200.0, height: 200.0),
              SizedBox(key: ValueKey<int>(1), width: 200.0, height: 200.0),
              SizedBox(key: ValueKey<int>(2), width: 200.0, height: 200.0),
              SizedBox(key: ValueKey<int>(3), width: 200.0, height: 200.0),
              SizedBox(key: ValueKey<int>(4), width: 200.0, height: 200.0),
              SizedBox(key: ValueKey<int>(5), width: 200.0, height: 200.0),
              SizedBox(key: ValueKey<int>(6), width: 200.0, height: 200.0),
35 36 37 38 39 40 41 42
            ],
          ),
        ),
      ),
    ),
  );
}

43
Widget buildListView(Axis scrollDirection, { bool reverse = false, bool shrinkWrap = false }) {
44
  return Directionality(
45
    textDirection: TextDirection.ltr,
46 47
    child: Center(
      child: SizedBox(
48 49
        width: 600.0,
        height: 400.0,
50
        child: ListView(
51 52
          scrollDirection: scrollDirection,
          reverse: reverse,
53
          addSemanticIndexes: false,
54
          shrinkWrap: shrinkWrap,
55 56 57 58 59 60 61 62
          children: const <Widget>[
            SizedBox(key: ValueKey<int>(0), width: 200.0, height: 200.0),
            SizedBox(key: ValueKey<int>(1), width: 200.0, height: 200.0),
            SizedBox(key: ValueKey<int>(2), width: 200.0, height: 200.0),
            SizedBox(key: ValueKey<int>(3), width: 200.0, height: 200.0),
            SizedBox(key: ValueKey<int>(4), width: 200.0, height: 200.0),
            SizedBox(key: ValueKey<int>(5), width: 200.0, height: 200.0),
            SizedBox(key: ValueKey<int>(6), width: 200.0, height: 200.0),
63 64 65 66 67 68 69
          ],
        ),
      ),
    ),
  );
}

70
void main() {
71

Josh Soref's avatar
Josh Soref committed
72
  group('SingleChildScrollView', () {
73
    testWidgets('SingleChildScrollView ensureVisible Axis.vertical', (WidgetTester tester) async {
74
      BuildContext findContext(int i) => tester.element(findKey(i));
75

76
      await tester.pumpWidget(buildSingleChildScrollView(Axis.vertical));
77

Adam Barth's avatar
Adam Barth committed
78
      Scrollable.ensureVisible(findContext(3));
79
      await tester.pump();
80
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
81

Adam Barth's avatar
Adam Barth committed
82
      Scrollable.ensureVisible(findContext(6));
83
      await tester.pump();
84
      expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));
85

Adam Barth's avatar
Adam Barth committed
86
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
87
      await tester.pump();
88
      expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));
89

Adam Barth's avatar
Adam Barth committed
90
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
91
      await tester.pump();
92
      expect(tester.getTopLeft(findKey(0)).dy, equals(100.0));
93

Adam Barth's avatar
Adam Barth committed
94
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
95 96
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
97
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
98 99
    });

100
    testWidgets('SingleChildScrollView ensureVisible Axis.horizontal', (WidgetTester tester) async {
101 102 103 104
      BuildContext findContext(int i) => tester.element(findKey(i));

      await tester.pumpWidget(buildSingleChildScrollView(Axis.horizontal));

Adam Barth's avatar
Adam Barth committed
105
      Scrollable.ensureVisible(findContext(3));
106
      await tester.pump();
107
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
108

Adam Barth's avatar
Adam Barth committed
109
      Scrollable.ensureVisible(findContext(6));
110
      await tester.pump();
111
      expect(tester.getTopLeft(findKey(6)).dx, equals(500.0));
112

Adam Barth's avatar
Adam Barth committed
113
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
114
      await tester.pump();
115
      expect(tester.getBottomRight(findKey(4)).dx, equals(700.0));
116

Adam Barth's avatar
Adam Barth committed
117
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
118
      await tester.pump();
119
      expect(tester.getTopLeft(findKey(0)).dx, equals(100.0));
120

Adam Barth's avatar
Adam Barth committed
121
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
122 123
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
124
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
125 126
    });

127
    testWidgets('SingleChildScrollView ensureVisible Axis.vertical reverse', (WidgetTester tester) async {
128 129 130 131
      BuildContext findContext(int i) => tester.element(findKey(i));

      await tester.pumpWidget(buildSingleChildScrollView(Axis.vertical, reverse: true));

Adam Barth's avatar
Adam Barth committed
132
      Scrollable.ensureVisible(findContext(3));
133
      await tester.pump();
134
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
135

Adam Barth's avatar
Adam Barth committed
136
      Scrollable.ensureVisible(findContext(0));
137
      await tester.pump();
138
      expect(tester.getBottomRight(findKey(0)).dy, equals(300.0));
139

Adam Barth's avatar
Adam Barth committed
140
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
141
      await tester.pump();
142
      expect(tester.getTopLeft(findKey(2)).dy, equals(100.0));
143

Adam Barth's avatar
Adam Barth committed
144
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
145
      await tester.pump();
146
      expect(tester.getBottomRight(findKey(6)).dy, equals(500.0));
147

Adam Barth's avatar
Adam Barth committed
148
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
149 150
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
151
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

      // Regression test for https://github.com/flutter/flutter/issues/128749
      // Reset to zero position.
      tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(0.0);
      await tester.pump();
      // 4 is not currently visible as the SingleChildScrollView is contained
      // within a centered SizedBox.
      expect(tester.getBottomLeft(findKey(4)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(6)).dy, equals(500.0));
      Scrollable.ensureVisible(
        findContext(6),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(5),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // 5 and 6 are already visible beyond the top edge, so no change.
      expect(tester.getBottomLeft(findKey(4)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(6)).dy, equals(500.0));
      Scrollable.ensureVisible(
        findContext(4),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // Since it is reversed, 4 should have come into view at the top
      // edge of the scrollable, matching the alignment expectation.
      expect(tester.getBottomLeft(findKey(4)).dy, equals(300.0));
      expect(tester.getBottomLeft(findKey(6)).dy, equals(700.0));

      // Bring 6 back into view at the trailing edge, checking the other
      // alignment.
      Scrollable.ensureVisible(
        findContext(6),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      expect(tester.getBottomLeft(findKey(4)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(6)).dy, equals(500.0));
193 194
    });

195
    testWidgets('SingleChildScrollView ensureVisible Axis.horizontal reverse', (WidgetTester tester) async {
196 197 198 199
      BuildContext findContext(int i) => tester.element(findKey(i));

      await tester.pumpWidget(buildSingleChildScrollView(Axis.horizontal, reverse: true));

Adam Barth's avatar
Adam Barth committed
200
      Scrollable.ensureVisible(findContext(3));
201
      await tester.pump();
202
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
203

Adam Barth's avatar
Adam Barth committed
204
      Scrollable.ensureVisible(findContext(0));
205
      await tester.pump();
206
      expect(tester.getBottomRight(findKey(0)).dx, equals(300.0));
207

Adam Barth's avatar
Adam Barth committed
208
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
209
      await tester.pump();
210
      expect(tester.getTopLeft(findKey(2)).dx, equals(100.0));
211

Adam Barth's avatar
Adam Barth committed
212
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
213
      await tester.pump();
214
      expect(tester.getBottomRight(findKey(6)).dx, equals(700.0));
215

Adam Barth's avatar
Adam Barth committed
216
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
217 218
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
219
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

      // Regression test for https://github.com/flutter/flutter/issues/128749
      // Reset to zero position.
      tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(0.0);
      await tester.pump();
      // 4 is not currently visible as the SingleChildScrollView is contained
      // within a centered SizedBox.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(6)).dx, equals(500.0));
      Scrollable.ensureVisible(
        findContext(6),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(5),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(4),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // 4, 5 and 6 are already visible beyond the left edge, so no change.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(6)).dx, equals(500.0));
      Scrollable.ensureVisible(
        findContext(3),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // Since it is reversed, 3 should have come into view at the leading
      // edge of the scrollable, matching the alignment expectation.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(100.0));
      expect(tester.getBottomLeft(findKey(6)).dx, equals(700.0));

      // Bring 6 back into view at the trailing edge, checking the other
      // alignment.
      Scrollable.ensureVisible(
        findContext(6),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(6)).dx, equals(500.0));
266 267
    });

268
    testWidgets('SingleChildScrollView ensureVisible rotated child', (WidgetTester tester) async {
269 270 271
      BuildContext findContext(int i) => tester.element(findKey(i));

      await tester.pumpWidget(
272 273
        Center(
          child: SizedBox(
274 275
            width: 600.0,
            height: 400.0,
276 277
            child: SingleChildScrollView(
              child: ListBody(
278
                children: <Widget>[
279 280 281 282
                  const SizedBox(height: 200.0),
                  const SizedBox(height: 200.0),
                  const SizedBox(height: 200.0),
                  SizedBox(
283
                    height: 200.0,
284 285 286 287
                    child: Center(
                      child: Transform(
                        transform: Matrix4.rotationZ(math.pi),
                        child: Container(
288
                          key: const ValueKey<int>(0),
289 290
                          width: 100.0,
                          height: 100.0,
291
                          color: const Color(0xFFFFFFFF),
292 293 294 295
                        ),
                      ),
                    ),
                  ),
296 297 298
                  const SizedBox(height: 200.0),
                  const SizedBox(height: 200.0),
                  const SizedBox(height: 200.0),
299 300 301 302
                ],
              ),
            ),
          ),
303
        ),
304
      );
305

Adam Barth's avatar
Adam Barth committed
306
      Scrollable.ensureVisible(findContext(0));
307
      await tester.pump();
308
      expect(tester.getBottomRight(findKey(0)).dy, moreOrLessEquals(100.0, epsilon: 0.1));
309

Adam Barth's avatar
Adam Barth committed
310
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
311
      await tester.pump();
312
      expect(tester.getTopLeft(findKey(0)).dy, moreOrLessEquals(500.0, epsilon: 0.1));
313
    });
314

315
    testWidgets('Nested SingleChildScrollView ensureVisible behavior test', (WidgetTester tester) async {
316 317 318 319 320 321
      // Regressing test for https://github.com/flutter/flutter/issues/65100
      Finder findKey(String coordinate) => find.byKey(ValueKey<String>(coordinate));
      BuildContext findContext(String coordinate) => tester.element(findKey(coordinate));
      final List<Row> rows = List<Row>.generate(
        7,
        (int y) => Row(
322
          children: List<SizedBox>.generate(
323
            7,
324
            (int x) => SizedBox(key: ValueKey<String>('$x, $y'), width: 200.0, height: 200.0),
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
          ),
        ),
      );

      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Center(
            child: SizedBox(
              width: 600.0,
              height: 400.0,
              child: SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: SingleChildScrollView(
                  child: Column(
                    children: rows,
                  ),
                ),
              ),
            ),
          ),
        ),
      );

      //      Items: 7 * 7 Container(width: 200.0, height: 200.0)
      //      viewport: Size(width: 600.0, height: 400.0)
      //
      //               0                       600
      //                 +----------------------+
      //                 |0,0    |1,0    |2,0   |
      //                 |       |       |      |
      //                 +----------------------+
      //                 |0,1    |1,1    |2,1   |
      //                 |       |       |      |
      //             400 +----------------------+

      Scrollable.ensureVisible(findContext('0, 0'));
      await tester.pump();
      expect(tester.getTopLeft(findKey('0, 0')), const Offset(100.0, 100.0));

      Scrollable.ensureVisible(findContext('3, 0'));
      await tester.pump();
      expect(tester.getTopLeft(findKey('3, 0')), const Offset(100.0, 100.0));

      Scrollable.ensureVisible(findContext('3, 0'), alignment: 0.5);
      await tester.pump();
      expect(tester.getTopLeft(findKey('3, 0')), const Offset(300.0, 100.0));

      Scrollable.ensureVisible(findContext('6, 0'));
      await tester.pump();
      expect(tester.getTopLeft(findKey('6, 0')), const Offset(500.0, 100.0));

      Scrollable.ensureVisible(findContext('0, 2'));
      await tester.pump();
      expect(tester.getTopLeft(findKey('0, 2')), const Offset(100.0, 100.0));

      Scrollable.ensureVisible(findContext('3, 2'));
      await tester.pump();
      expect(tester.getTopLeft(findKey('3, 2')), const Offset(100.0, 100.0));

      // It should be at the center of the screen.
      Scrollable.ensureVisible(findContext('3, 2'), alignment: 0.5);
      await tester.pump();
      expect(tester.getTopLeft(findKey('3, 2')), const Offset(300.0, 200.0));
    });
390 391
  });

392
  group('ListView', () {
393
    testWidgets('ListView ensureVisible Axis.vertical', (WidgetTester tester) async {
394
      BuildContext findContext(int i) => tester.element(findKey(i));
395
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
396
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
397 398 399 400 401 402
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.vertical));

      await prepare(480.0);
Adam Barth's avatar
Adam Barth committed
403
      Scrollable.ensureVisible(findContext(3));
404
      await tester.pump();
405
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
406 407

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
408
      Scrollable.ensureVisible(findContext(6));
409
      await tester.pump();
410
      expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));
411 412

      await prepare(735.0);
Adam Barth's avatar
Adam Barth committed
413
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
414
      await tester.pump();
415
      expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));
416 417

      await prepare(123.0);
Adam Barth's avatar
Adam Barth committed
418
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
419
      await tester.pump();
420
      expect(tester.getTopLeft(findKey(0)).dy, equals(100.0));
421 422

      await prepare(523.0);
Adam Barth's avatar
Adam Barth committed
423
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
424 425
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
426
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
427 428
    });

429
    testWidgets('ListView ensureVisible Axis.horizontal', (WidgetTester tester) async {
430
      BuildContext findContext(int i) => tester.element(findKey(i));
431
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
432
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
433 434 435 436 437 438
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.horizontal));

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
439
      Scrollable.ensureVisible(findContext(3));
440
      await tester.pump();
441
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
442 443

      await prepare(843.0);
Adam Barth's avatar
Adam Barth committed
444
      Scrollable.ensureVisible(findContext(6));
445
      await tester.pump();
446
      expect(tester.getTopLeft(findKey(6)).dx, equals(500.0));
447 448

      await prepare(415.0);
Adam Barth's avatar
Adam Barth committed
449
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
450
      await tester.pump();
451
      expect(tester.getBottomRight(findKey(4)).dx, equals(700.0));
452 453

      await prepare(46.0);
Adam Barth's avatar
Adam Barth committed
454
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
455
      await tester.pump();
456
      expect(tester.getTopLeft(findKey(0)).dx, equals(100.0));
457 458

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
459
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
460 461
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
462
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
463 464
    });

465
    testWidgets('ListView ensureVisible Axis.vertical reverse', (WidgetTester tester) async {
466
      BuildContext findContext(int i) => tester.element(findKey(i));
467
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
468
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
469 470 471 472 473 474
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.vertical, reverse: true));

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
475
      Scrollable.ensureVisible(findContext(3));
476
      await tester.pump();
477
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
478 479

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
480
      Scrollable.ensureVisible(findContext(0));
481
      await tester.pump();
482
      expect(tester.getBottomRight(findKey(0)).dy, equals(500.0));
483 484

      await prepare(230.0);
Adam Barth's avatar
Adam Barth committed
485
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
486
      await tester.pump();
487
      expect(tester.getTopLeft(findKey(2)).dy, equals(100.0));
488 489

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
490
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
491
      await tester.pump();
492
      expect(tester.getBottomRight(findKey(6)).dy, equals(300.0));
493 494

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
495
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
496 497
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
498
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

      // Regression test for https://github.com/flutter/flutter/issues/128749
      // Reset to zero position.
      await prepare(0.0);
      // 2 is not currently visible as the ListView is contained
      // within a centered SizedBox.
      expect(tester.getBottomLeft(findKey(2)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(500.0));
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(1),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // 0 and 1 are already visible beyond the top edge, so no change.
      expect(tester.getBottomLeft(findKey(2)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(500.0));
      Scrollable.ensureVisible(
        findContext(2),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // Since it is reversed, 2 should have come into view at the top
      // edge of the scrollable, matching the alignment expectation.
      expect(tester.getBottomLeft(findKey(2)).dy, equals(300.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(700.0));

      // Bring 0 back into view at the trailing edge, checking the other
      // alignment.
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      expect(tester.getBottomLeft(findKey(2)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(500.0));
539 540
    });

541
    testWidgets('ListView ensureVisible Axis.horizontal reverse', (WidgetTester tester) async {
542
      BuildContext findContext(int i) => tester.element(findKey(i));
543
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
544
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
545 546 547 548 549 550
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.horizontal, reverse: true));

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
551
      Scrollable.ensureVisible(findContext(3));
552
      await tester.pump();
553
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
554 555

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
556
      Scrollable.ensureVisible(findContext(0));
557
      await tester.pump();
558
      expect(tester.getBottomRight(findKey(0)).dx, equals(700.0));
559 560

      await prepare(230.0);
Adam Barth's avatar
Adam Barth committed
561
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
562
      await tester.pump();
563
      expect(tester.getTopLeft(findKey(2)).dx, equals(100.0));
564 565

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
566
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
567
      await tester.pump();
568
      expect(tester.getBottomRight(findKey(6)).dx, equals(300.0));
569 570

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
571
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
572 573
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
574
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
575 576 577 578 579 580 581 582 583 584 585 586 587 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

      // Regression test for https://github.com/flutter/flutter/issues/128749
      // Reset to zero position.
      await prepare(0.0);
      // 3 is not currently visible as the ListView is contained
      // within a centered SizedBox.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(500.0));
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(1),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(2),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // 0, 1 and 2 are already visible beyond the left edge, so no change.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(500.0));
      Scrollable.ensureVisible(
        findContext(3),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // Since it is reversed, 3 should have come into view at the leading
      // edge of the scrollable, matching the alignment expectation.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(700.0));

      // Bring 0 back into view at the trailing edge, checking the other
      // alignment.
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(500.0));
620 621
    });

622
    testWidgets('ListView ensureVisible negative child', (WidgetTester tester) async {
623
      BuildContext findContext(int i) => tester.element(findKey(i));
624
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
625
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
626 627 628 629
        await tester.pump();
      }

      double getOffset() {
Adam Barth's avatar
Adam Barth committed
630
        return tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels;
631 632 633
      }

      Widget buildSliver(int i) {
634 635
        return SliverToBoxAdapter(
          key: ValueKey<int>(i),
636
          child: const SizedBox(width: 200.0, height: 200.0),
637 638 639
        );
      }

640
      await tester.pumpWidget(
641
        Directionality(
642
          textDirection: TextDirection.ltr,
643 644
          child: Center(
            child: SizedBox(
645 646
              width: 600.0,
              height: 400.0,
647
              child: Scrollable(
648
                viewportBuilder: (BuildContext context, ViewportOffset offset) {
649
                  return Viewport(
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
                    offset: offset,
                    center: const ValueKey<int>(4),
                    slivers: <Widget>[
                      buildSliver(0),
                      buildSliver(1),
                      buildSliver(2),
                      buildSliver(3),
                      buildSliver(4),
                      buildSliver(5),
                      buildSliver(6),
                    ],
                  );
                },
              ),
            ),
665 666
          ),
        ),
667
      );
668 669

      await prepare(-125.0);
Adam Barth's avatar
Adam Barth committed
670
      Scrollable.ensureVisible(findContext(3));
671 672 673 674
      await tester.pump();
      expect(getOffset(), equals(-200.0));

      await prepare(-225.0);
Adam Barth's avatar
Adam Barth committed
675
      Scrollable.ensureVisible(findContext(2));
676 677
      await tester.pump();
      expect(getOffset(), equals(-400.0));
678
    });
679

680
    testWidgets('ListView ensureVisible rotated child', (WidgetTester tester) async {
681
      BuildContext findContext(int i) => tester.element(findKey(i));
682
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
683
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
684 685 686
        await tester.pump();
      }

687
      await tester.pumpWidget(Directionality(
688
        textDirection: TextDirection.ltr,
689 690
        child: Center(
          child: SizedBox(
691 692
            width: 600.0,
            height: 400.0,
693
            child: ListView(
694
              children: <Widget>[
695 696 697 698
                const SizedBox(height: 200.0),
                const SizedBox(height: 200.0),
                const SizedBox(height: 200.0),
                SizedBox(
699
                  height: 200.0,
700 701 702 703
                  child: Center(
                    child: Transform(
                      transform: Matrix4.rotationZ(math.pi),
                      child: Container(
704
                        key: const ValueKey<int>(0),
705 706
                        width: 100.0,
                        height: 100.0,
707
                        color: const Color(0xFFFFFFFF),
708 709 710 711
                      ),
                    ),
                  ),
                ),
712 713 714
                const SizedBox(height: 200.0),
                const SizedBox(height: 200.0),
                const SizedBox(height: 200.0),
715 716 717
              ],
            ),
          ),
718
        ),
719
      ));
720 721

      await prepare(321.0);
Adam Barth's avatar
Adam Barth committed
722
      Scrollable.ensureVisible(findContext(0));
723
      await tester.pump();
724
      expect(tester.getBottomRight(findKey(0)).dy, moreOrLessEquals(100.0, epsilon: 0.1));
725

Adam Barth's avatar
Adam Barth committed
726
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
727
      await tester.pump();
728
      expect(tester.getTopLeft(findKey(0)).dy, moreOrLessEquals(500.0, epsilon: 0.1));
729
    });
730 731
  });

732
  group('ListView shrinkWrap', () {
733
    testWidgets('ListView ensureVisible Axis.vertical', (WidgetTester tester) async {
734
      BuildContext findContext(int i) => tester.element(findKey(i));
735
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
736
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
737 738 739 740 741 742
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.vertical, shrinkWrap: true));

      await prepare(480.0);
Adam Barth's avatar
Adam Barth committed
743
      Scrollable.ensureVisible(findContext(3));
744
      await tester.pump();
745
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
746 747

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
748
      Scrollable.ensureVisible(findContext(6));
749
      await tester.pump();
750
      expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));
751 752

      await prepare(735.0);
Adam Barth's avatar
Adam Barth committed
753
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
754
      await tester.pump();
755
      expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));
756 757

      await prepare(123.0);
Adam Barth's avatar
Adam Barth committed
758
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
759
      await tester.pump();
760
      expect(tester.getTopLeft(findKey(0)).dy, equals(100.0));
761 762

      await prepare(523.0);
Adam Barth's avatar
Adam Barth committed
763
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
764 765
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
766
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
767 768
    });

769
    testWidgets('ListView ensureVisible Axis.horizontal', (WidgetTester tester) async {
770
      BuildContext findContext(int i) => tester.element(findKey(i));
771
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
772
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
773 774 775 776 777 778
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.horizontal, shrinkWrap: true));

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
779
      Scrollable.ensureVisible(findContext(3));
780
      await tester.pump();
781
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
782 783

      await prepare(843.0);
Adam Barth's avatar
Adam Barth committed
784
      Scrollable.ensureVisible(findContext(6));
785
      await tester.pump();
786
      expect(tester.getTopLeft(findKey(6)).dx, equals(500.0));
787 788

      await prepare(415.0);
Adam Barth's avatar
Adam Barth committed
789
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
790
      await tester.pump();
791
      expect(tester.getBottomRight(findKey(4)).dx, equals(700.0));
792 793

      await prepare(46.0);
Adam Barth's avatar
Adam Barth committed
794
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
795
      await tester.pump();
796
      expect(tester.getTopLeft(findKey(0)).dx, equals(100.0));
797 798

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
799
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
800 801
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
802
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
803 804
    });

805
    testWidgets('ListView ensureVisible Axis.vertical reverse', (WidgetTester tester) async {
806
      BuildContext findContext(int i) => tester.element(findKey(i));
807
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
808
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
809 810 811 812 813 814
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.vertical, reverse: true, shrinkWrap: true));

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
815
      Scrollable.ensureVisible(findContext(3));
816
      await tester.pump();
817
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
818 819

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
820
      Scrollable.ensureVisible(findContext(0));
821
      await tester.pump();
822
      expect(tester.getBottomRight(findKey(0)).dy, equals(500.0));
823 824

      await prepare(230.0);
Adam Barth's avatar
Adam Barth committed
825
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
826
      await tester.pump();
827
      expect(tester.getTopLeft(findKey(2)).dy, equals(100.0));
828 829

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
830
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
831
      await tester.pump();
832
      expect(tester.getBottomRight(findKey(6)).dy, equals(300.0));
833 834

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
835
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
836 837
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
838
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
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

      // Regression test for https://github.com/flutter/flutter/issues/128749
      // Reset to zero position.
      await prepare(0.0);
      // 2 is not currently visible as the ListView is contained
      // within a centered SizedBox.
      expect(tester.getBottomLeft(findKey(2)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(500.0));
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(1),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // 0 and 1 are already visible beyond the top edge, so no change.
      expect(tester.getBottomLeft(findKey(2)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(500.0));
      Scrollable.ensureVisible(
        findContext(2),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // Since it is reversed, 2 should have come into view at the top
      // edge of the scrollable, matching the alignment expectation.
      expect(tester.getBottomLeft(findKey(2)).dy, equals(300.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(700.0));

      // Bring 0 back into view at the trailing edge, checking the other
      // alignment.
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      expect(tester.getBottomLeft(findKey(2)).dy, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dy, equals(500.0));
879 880
    });

881
    testWidgets('ListView ensureVisible Axis.horizontal reverse', (WidgetTester tester) async {
882
      BuildContext findContext(int i) => tester.element(findKey(i));
883
      Future<void> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
884
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
885 886 887 888 889 890
        await tester.pump();
      }

      await tester.pumpWidget(buildListView(Axis.horizontal, reverse: true, shrinkWrap: true));

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
891
      Scrollable.ensureVisible(findContext(3));
892
      await tester.pump();
893
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
894 895

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
896
      Scrollable.ensureVisible(findContext(0));
897
      await tester.pump();
898
      expect(tester.getBottomRight(findKey(0)).dx, equals(700.0));
899 900

      await prepare(230.0);
Adam Barth's avatar
Adam Barth committed
901
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
902
      await tester.pump();
903
      expect(tester.getTopLeft(findKey(2)).dx, equals(100.0));
904 905

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
906
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
907
      await tester.pump();
908
      expect(tester.getBottomRight(findKey(6)).dx, equals(300.0));
909 910

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
911
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
912 913
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
914
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

      // Regression test for https://github.com/flutter/flutter/issues/128749
      // Reset to zero position.
      await prepare(0.0);
      // 3 is not currently visible as the ListView is contained
      // within a centered SizedBox.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(500.0));
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(1),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      Scrollable.ensureVisible(
        findContext(2),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // 0, 1 and 2 are already visible beyond the left edge, so no change.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(500.0));
      Scrollable.ensureVisible(
        findContext(3),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtStart,
      );
      await tester.pump();
      // Since it is reversed, 3 should have come into view at the leading
      // edge of the scrollable, matching the alignment expectation.
      expect(tester.getBottomLeft(findKey(3)).dx, equals(100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(700.0));

      // Bring 0 back into view at the trailing edge, checking the other
      // alignment.
      Scrollable.ensureVisible(
        findContext(0),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      expect(tester.getBottomLeft(findKey(3)).dx, equals(-100.0));
      expect(tester.getBottomLeft(findKey(0)).dx, equals(500.0));
960 961
    });
  });
962 963

  group('Scrollable with center', () {
964
    testWidgets('ensureVisible', (WidgetTester tester) async {
965
      BuildContext findContext(int i) => tester.element(findKey(i));
966
      Future<void> prepare(double offset) async {
967 968 969 970
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
        await tester.pump();
      }

971
      await tester.pumpWidget(
972
        Directionality(
973
          textDirection: TextDirection.ltr,
974 975
          child: Center(
            child: SizedBox(
976 977
              width: 600.0,
              height: 400.0,
978
              child: Scrollable(
979
                viewportBuilder: (BuildContext context, ViewportOffset offset) {
980
                  return Viewport(
981 982
                    offset: offset,
                    center: const ValueKey<String>('center'),
983 984 985 986 987 988 989
                    slivers: const <Widget>[
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(-6), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(-5), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(-4), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(-3), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(-2), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(-1), width: 200.0, height: 200.0)),
990
                      SliverToBoxAdapter(key: ValueKey<String>('center'), child: SizedBox(key: ValueKey<int>(0), width: 200.0, height: 200.0)),
991 992 993 994 995 996
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(1), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(2), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(3), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(4), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(5), width: 200.0, height: 200.0)),
                      SliverToBoxAdapter(child: SizedBox(key: ValueKey<int>(6), width: 200.0, height: 200.0)),
997 998 999 1000 1001
                    ],
                  );
                },
              ),
            ),
1002 1003
          ),
        ),
1004
      );
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

      await prepare(480.0);
      Scrollable.ensureVisible(findContext(3));
      await tester.pump();
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));

      await prepare(1083.0);
      Scrollable.ensureVisible(findContext(6));
      await tester.pump();
      expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));

      await prepare(735.0);
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
      await tester.pump();
      expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));

      await prepare(123.0);
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
      await tester.pump();
      expect(tester.getBottomRight(findKey(0)).dy, equals(500.0));

      await prepare(523.0);
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));


      await prepare(-480.0);
      Scrollable.ensureVisible(findContext(-3));
      await tester.pump();
      expect(tester.getTopLeft(findKey(-3)).dy, equals(100.0));

      await prepare(-1083.0);
      Scrollable.ensureVisible(findContext(-6));
      await tester.pump();
      expect(tester.getTopLeft(findKey(-6)).dy, equals(100.0));

      await prepare(-735.0);
      Scrollable.ensureVisible(findContext(-4), alignment: 1.0);
      await tester.pump();
      expect(tester.getBottomRight(findKey(-4)).dy, equals(500.0));

      await prepare(-523.0);
      Scrollable.ensureVisible(findContext(-3), duration: const Duration(seconds: 1));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
      expect(tester.getTopLeft(findKey(-3)).dy, equals(100.0));
    });
  });
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 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

  group('TwoDimensionalViewport ensureVisible', () {
    Finder findKey(ChildVicinity vicinity) {
      return find.byKey(ValueKey<ChildVicinity>(vicinity));
    }

    BuildContext findContext(WidgetTester tester, ChildVicinity vicinity) {
      return tester.element(findKey(vicinity));
    }

    testWidgets('Axis.vertical', (WidgetTester tester) async {
      await tester.pumpWidget(simpleBuilderTest(useCacheExtent: true));

      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 0, yIndex: 0)),
      );
      await tester.pump();
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 0))).dy,
        equals(0.0),
      );
      // (0, 3) is in the cache extent, and will be brought into view next
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 3))).dy,
        equals(600.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 0, yIndex: 3)),
      );
      await tester.pump();
      // Now in view at top edge of viewport
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 3))).dy,
        equals(0.0),
      );

      // If already visible, no change
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 0, yIndex: 3)),
      );
      await tester.pump();
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 3))).dy,
        equals(0.0),
      );
    });

    testWidgets('Axis.horizontal', (WidgetTester tester) async {
      await tester.pumpWidget(simpleBuilderTest(useCacheExtent: true));

      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 1, yIndex: 0)),
      );
      await tester.pump();
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 1, yIndex: 0))).dx,
        equals(0.0),
      );
      // (5, 0) is now in the cache extent, and will be brought into view next
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 5, yIndex: 0))).dx,
        equals(800.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 5, yIndex: 0)),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      // Now in view at trailing edge of viewport
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 5, yIndex: 0))).dx,
        equals(600.0),
      );

      // If already in position, no change
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 5, yIndex: 0)),
        alignmentPolicy: ScrollPositionAlignmentPolicy.keepVisibleAtEnd,
      );
      await tester.pump();
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 5, yIndex: 0))).dx,
        equals(600.0),
      );
    });

    testWidgets('both axes', (WidgetTester tester) async {
      await tester.pumpWidget(simpleBuilderTest(useCacheExtent: true));

      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 1, yIndex: 1)),
      );
      await tester.pump();
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 1, yIndex: 1))),
        const Rect.fromLTRB(0.0, 0.0, 200.0, 200.0),
      );
      // (5, 4) is in the cache extent, and will be brought into view next
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 5, yIndex: 4))),
        const Rect.fromLTRB(800.0, 600.0, 1000.0, 800.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 5, yIndex: 4)),
        alignment: 1.0, // Same as ScrollAlignmentPolicy.keepVisibleAtEnd
      );
      await tester.pump();
      // Now in view at bottom trailing corner of viewport
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 5, yIndex: 4))),
        const Rect.fromLTRB(600.0, 400.0, 800.0, 600.0),
      );

      // If already visible, no change
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 5, yIndex: 4)),
        alignment: 1.0,
      );
      await tester.pump();
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 5, yIndex: 4))),
        const Rect.fromLTRB(600.0, 400.0, 800.0, 600.0),
      );
    });

    testWidgets('Axis.vertical reverse', (WidgetTester tester) async {
      await tester.pumpWidget(simpleBuilderTest(
        verticalDetails: const ScrollableDetails.vertical(reverse: true),
        useCacheExtent: true,
      ));

      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 0))).dy,
        equals(400.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 0, yIndex: 0)),
      );
      await tester.pump();
      // Already visible so no change.
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 0))).dy,
        equals(400.0),
      );
      // (0, 3) is in the cache extent, and will be brought into view next
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 3))).dy,
        equals(-200.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 0, yIndex: 3)),
      );
      await tester.pump();
      // Now in view at bottom edge of viewport since we are reversed
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 3))).dy,
        equals(400.0),
      );

      // If already visible, no change
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 0, yIndex: 3)),
      );
      await tester.pump();
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 3))).dy,
        equals(400.0),
      );
    });

    testWidgets('Axis.horizontal reverse', (WidgetTester tester) async {
      await tester.pumpWidget(simpleBuilderTest(
        horizontalDetails: const ScrollableDetails.horizontal(reverse: true),
        useCacheExtent: true,
      ));

      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 0))).dx,
        equals(600.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 0, yIndex: 0)),
      );
      await tester.pump();
      // Already visible so no change.
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 0, yIndex: 0))).dx,
        equals(600.0),
      );
      // (4, 0) is in the cache extent, and will be brought into view next
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 4, yIndex: 0))).dx,
        equals(-200.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 4, yIndex: 0)),
      );
      await tester.pump();
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 4, yIndex: 0))).dx,
        equals(200.0),
      );

      // If already visible, no change
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 4, yIndex: 0)),
      );
      await tester.pump();
      expect(
        tester.getTopLeft(findKey(const ChildVicinity(xIndex: 4, yIndex: 0))).dx,
        equals(200.0),
      );
    });

    testWidgets('both axes reverse', (WidgetTester tester) async {
      await tester.pumpWidget(simpleBuilderTest(
        verticalDetails: const ScrollableDetails.vertical(reverse: true),
        horizontalDetails: const ScrollableDetails.horizontal(reverse: true),
        useCacheExtent: true,
      ));

      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 1, yIndex: 1)),
      );
      await tester.pump();
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 1, yIndex: 1))),
        const Rect.fromLTRB(600.0, 400.0, 800.0, 600.0),
      );
      // (5, 4) is in the cache extent, and will be brought into view next
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 5, yIndex: 4))),
        const Rect.fromLTRB(-200.0, -200.0, 0.0, 0.0),
      );
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 5, yIndex: 4)),
        alignment: 1.0, // Same as ScrollAlignmentPolicy.keepVisibleAtEnd
      );
      await tester.pump();
      // Now in view at trailing corner of viewport
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 5, yIndex: 4))),
        const Rect.fromLTRB(0.0, 0.0, 200.0, 200.0),
      );

      // If already visible, no change
      Scrollable.ensureVisible(findContext(
        tester,
        const ChildVicinity(xIndex: 5, yIndex: 4)),
        alignment: 1.0,
      );
      await tester.pump();
      expect(
        tester.getRect(findKey(const ChildVicinity(xIndex: 5, yIndex: 4))),
        const Rect.fromLTRB(0.0, 0.0, 200.0, 200.0),
      );
    });
  });
1330
}