ensure_visible_test.dart 22.7 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 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.

import 'dart:math' as math;

import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/rendering.dart';
9 10 11 12 13 14 15 16 17 18 19 20
import 'package:flutter/widgets.dart';

Finder findKey(int i) => find.byKey(new ValueKey<int>(i));

Widget buildSingleChildScrollView(Axis scrollDirection, { bool reverse: false }) {
  return new Center(
    child: new SizedBox(
      width: 600.0,
      height: 400.0,
      child: new SingleChildScrollView(
        scrollDirection: scrollDirection,
        reverse: reverse,
21
        child: new ListBody(
22 23
          mainAxis: scrollDirection,
          children: <Widget>[
24 25 26 27 28 29 30
            new Container(key: const ValueKey<int>(0), width: 200.0, height: 200.0),
            new Container(key: const ValueKey<int>(1), width: 200.0, height: 200.0),
            new Container(key: const ValueKey<int>(2), width: 200.0, height: 200.0),
            new Container(key: const ValueKey<int>(3), width: 200.0, height: 200.0),
            new Container(key: const ValueKey<int>(4), width: 200.0, height: 200.0),
            new Container(key: const ValueKey<int>(5), width: 200.0, height: 200.0),
            new Container(key: const ValueKey<int>(6), width: 200.0, height: 200.0),
31 32 33 34 35 36 37
          ],
        ),
      ),
    ),
  );
}

38 39 40 41 42 43 44 45 46 47
Widget buildListView(Axis scrollDirection, { bool reverse: false, bool shrinkWrap: false }) {
  return new Center(
    child: new SizedBox(
      width: 600.0,
      height: 400.0,
      child: new ListView(
        scrollDirection: scrollDirection,
        reverse: reverse,
        shrinkWrap: shrinkWrap,
        children: <Widget>[
48 49 50 51 52 53 54
          new Container(key: const ValueKey<int>(0), width: 200.0, height: 200.0),
          new Container(key: const ValueKey<int>(1), width: 200.0, height: 200.0),
          new Container(key: const ValueKey<int>(2), width: 200.0, height: 200.0),
          new Container(key: const ValueKey<int>(3), width: 200.0, height: 200.0),
          new Container(key: const ValueKey<int>(4), width: 200.0, height: 200.0),
          new Container(key: const ValueKey<int>(5), width: 200.0, height: 200.0),
          new Container(key: const ValueKey<int>(6), width: 200.0, height: 200.0),
55 56 57 58 59
        ],
      ),
    ),
  );
}
60

61
void main() {
62

63 64 65
  group('SingleChildScollView', () {
    testWidgets('SingleChildScollView ensureVisible Axis.vertical', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
66

67
      await tester.pumpWidget(buildSingleChildScrollView(Axis.vertical));
68

Adam Barth's avatar
Adam Barth committed
69
      Scrollable.ensureVisible(findContext(3));
70
      await tester.pump();
71
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
72

Adam Barth's avatar
Adam Barth committed
73
      Scrollable.ensureVisible(findContext(6));
74
      await tester.pump();
75
      expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));
76

Adam Barth's avatar
Adam Barth committed
77
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
78
      await tester.pump();
79
      expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));
80

Adam Barth's avatar
Adam Barth committed
81
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
82
      await tester.pump();
83
      expect(tester.getTopLeft(findKey(0)).dy, equals(100.0));
84

Adam Barth's avatar
Adam Barth committed
85
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
86 87
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
88
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
89 90 91 92 93 94 95
    });

    testWidgets('SingleChildScollView ensureVisible Axis.horizontal', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));

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

Adam Barth's avatar
Adam Barth committed
96
      Scrollable.ensureVisible(findContext(3));
97
      await tester.pump();
98
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
99

Adam Barth's avatar
Adam Barth committed
100
      Scrollable.ensureVisible(findContext(6));
101
      await tester.pump();
102
      expect(tester.getTopLeft(findKey(6)).dx, equals(500.0));
103

Adam Barth's avatar
Adam Barth committed
104
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
105
      await tester.pump();
106
      expect(tester.getBottomRight(findKey(4)).dx, equals(700.0));
107

Adam Barth's avatar
Adam Barth committed
108
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
109
      await tester.pump();
110
      expect(tester.getTopLeft(findKey(0)).dx, equals(100.0));
111

Adam Barth's avatar
Adam Barth committed
112
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
113 114
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
115
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
116 117 118 119 120 121 122
    });

    testWidgets('SingleChildScollView ensureVisible Axis.vertical reverse', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));

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

Adam Barth's avatar
Adam Barth committed
123
      Scrollable.ensureVisible(findContext(3));
124
      await tester.pump();
125
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
126

Adam Barth's avatar
Adam Barth committed
127
      Scrollable.ensureVisible(findContext(0));
128
      await tester.pump();
129
      expect(tester.getBottomRight(findKey(0)).dy, equals(300.0));
130

Adam Barth's avatar
Adam Barth committed
131
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
132
      await tester.pump();
133
      expect(tester.getTopLeft(findKey(2)).dy, equals(100.0));
134

Adam Barth's avatar
Adam Barth committed
135
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
136
      await tester.pump();
137
      expect(tester.getBottomRight(findKey(6)).dy, equals(500.0));
138

Adam Barth's avatar
Adam Barth committed
139
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
140 141
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
142
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
143 144 145 146 147 148 149
    });

    testWidgets('SingleChildScollView ensureVisible Axis.horizontal reverse', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));

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

Adam Barth's avatar
Adam Barth committed
150
      Scrollable.ensureVisible(findContext(3));
151
      await tester.pump();
152
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
153

Adam Barth's avatar
Adam Barth committed
154
      Scrollable.ensureVisible(findContext(0));
155
      await tester.pump();
156
      expect(tester.getBottomRight(findKey(0)).dx, equals(300.0));
157

Adam Barth's avatar
Adam Barth committed
158
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
159
      await tester.pump();
160
      expect(tester.getTopLeft(findKey(2)).dx, equals(100.0));
161

Adam Barth's avatar
Adam Barth committed
162
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
163
      await tester.pump();
164
      expect(tester.getBottomRight(findKey(6)).dx, equals(700.0));
165

Adam Barth's avatar
Adam Barth committed
166
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
167 168
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
169
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
170 171 172 173 174 175 176 177 178 179 180
    });

    testWidgets('SingleChildScollView ensureVisible rotated child', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));

      await tester.pumpWidget(
        new Center(
          child: new SizedBox(
            width: 600.0,
            height: 400.0,
            child: new SingleChildScrollView(
181
              child: new ListBody(
182 183 184 185 186 187 188 189 190 191
                children: <Widget>[
                  new Container(height: 200.0),
                  new Container(height: 200.0),
                  new Container(height: 200.0),
                  new Container(
                    height: 200.0,
                    child: new Center(
                      child: new Transform(
                        transform: new Matrix4.rotationZ(math.PI),
                        child: new Container(
192
                          key: const ValueKey<int>(0),
193 194
                          width: 100.0,
                          height: 100.0,
195
                          color: const Color(0xFFFFFFFF),
196 197 198 199 200 201 202 203 204 205 206 207 208
                        ),
                      ),
                    ),
                  ),
                  new Container(height: 200.0),
                  new Container(height: 200.0),
                  new Container(height: 200.0),
                ],
              ),
            ),
          ),
        )
      );
209

Adam Barth's avatar
Adam Barth committed
210
      Scrollable.ensureVisible(findContext(0));
211
      await tester.pump();
212
      expect(tester.getBottomRight(findKey(0)).dy, closeTo(100.0, 0.1));
213

Adam Barth's avatar
Adam Barth committed
214
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
215
      await tester.pump();
216
      expect(tester.getTopLeft(findKey(0)).dy, closeTo(500.0, 0.1));
217
    });
218 219
  });

220 221 222 223
  group('ListView', () {
    testWidgets('ListView ensureVisible Axis.vertical', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
224
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
225 226 227 228 229 230
        await tester.pump();
      }

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

      await prepare(480.0);
Adam Barth's avatar
Adam Barth committed
231
      Scrollable.ensureVisible(findContext(3));
232
      await tester.pump();
233
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
234 235

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
236
      Scrollable.ensureVisible(findContext(6));
237
      await tester.pump();
238
      expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));
239 240

      await prepare(735.0);
Adam Barth's avatar
Adam Barth committed
241
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
242
      await tester.pump();
243
      expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));
244 245

      await prepare(123.0);
Adam Barth's avatar
Adam Barth committed
246
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
247
      await tester.pump();
248
      expect(tester.getTopLeft(findKey(0)).dy, equals(100.0));
249 250

      await prepare(523.0);
Adam Barth's avatar
Adam Barth committed
251
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
252 253
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
254
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
255 256 257 258 259
    });

    testWidgets('ListView ensureVisible Axis.horizontal', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
260
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
261 262 263 264 265 266
        await tester.pump();
      }

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

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
267
      Scrollable.ensureVisible(findContext(3));
268
      await tester.pump();
269
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
270 271

      await prepare(843.0);
Adam Barth's avatar
Adam Barth committed
272
      Scrollable.ensureVisible(findContext(6));
273
      await tester.pump();
274
      expect(tester.getTopLeft(findKey(6)).dx, equals(500.0));
275 276

      await prepare(415.0);
Adam Barth's avatar
Adam Barth committed
277
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
278
      await tester.pump();
279
      expect(tester.getBottomRight(findKey(4)).dx, equals(700.0));
280 281

      await prepare(46.0);
Adam Barth's avatar
Adam Barth committed
282
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
283
      await tester.pump();
284
      expect(tester.getTopLeft(findKey(0)).dx, equals(100.0));
285 286

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
287
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
288 289
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
290
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
291 292 293 294 295
    });

    testWidgets('ListView ensureVisible Axis.vertical reverse', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
296
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
297 298 299 300 301 302
        await tester.pump();
      }

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

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
303
      Scrollable.ensureVisible(findContext(3));
304
      await tester.pump();
305
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
306 307

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
308
      Scrollable.ensureVisible(findContext(0));
309
      await tester.pump();
310
      expect(tester.getBottomRight(findKey(0)).dy, equals(500.0));
311 312

      await prepare(230.0);
Adam Barth's avatar
Adam Barth committed
313
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
314
      await tester.pump();
315
      expect(tester.getTopLeft(findKey(2)).dy, equals(100.0));
316 317

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
318
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
319
      await tester.pump();
320
      expect(tester.getBottomRight(findKey(6)).dy, equals(300.0));
321 322

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
323
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
324 325
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
326
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
327 328 329 330 331
    });

    testWidgets('ListView ensureVisible Axis.horizontal reverse', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
332
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
333 334 335 336 337 338
        await tester.pump();
      }

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

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
339
      Scrollable.ensureVisible(findContext(3));
340
      await tester.pump();
341
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
342 343

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
344
      Scrollable.ensureVisible(findContext(0));
345
      await tester.pump();
346
      expect(tester.getBottomRight(findKey(0)).dx, equals(700.0));
347 348

      await prepare(230.0);
Adam Barth's avatar
Adam Barth committed
349
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
350
      await tester.pump();
351
      expect(tester.getTopLeft(findKey(2)).dx, equals(100.0));
352 353

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
354
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
355
      await tester.pump();
356
      expect(tester.getBottomRight(findKey(6)).dx, equals(300.0));
357 358

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
359
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
360 361
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
362
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
363 364 365 366 367 368
    });

    // TODO(abarth): Unskip this test. See https://github.com/flutter/flutter/issues/7919
    testWidgets('ListView ensureVisible negative child', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
369
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
370 371 372 373
        await tester.pump();
      }

      double getOffset() {
Adam Barth's avatar
Adam Barth committed
374
        return tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels;
375 376 377 378 379 380 381 382 383 384
      }

      Widget buildSliver(int i) {
        return new SliverToBoxAdapter(
          key: new ValueKey<int>(i),
          child: new Container(width: 200.0, height: 200.0),
        );
      }

      await tester.pumpWidget(new Center(
385 386 387
        child: new SizedBox(
          width: 600.0,
          height: 400.0,
Adam Barth's avatar
Adam Barth committed
388
          child: new Scrollable(
389
            viewportBuilder: (BuildContext context, ViewportOffset offset) {
Adam Barth's avatar
Adam Barth committed
390
              return new Viewport(
391
                offset: offset,
392
                center: const ValueKey<int>(4),
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
                slivers: <Widget>[
                  buildSliver(0),
                  buildSliver(1),
                  buildSliver(2),
                  buildSliver(3),
                  buildSliver(4),
                  buildSliver(5),
                  buildSliver(6),
                ],
              );
            },
          ),
        ),
      ));

      await prepare(-125.0);
Adam Barth's avatar
Adam Barth committed
409
      Scrollable.ensureVisible(findContext(3));
410 411 412 413
      await tester.pump();
      expect(getOffset(), equals(-200.0));

      await prepare(-225.0);
Adam Barth's avatar
Adam Barth committed
414
      Scrollable.ensureVisible(findContext(2));
415 416 417 418 419 420 421
      await tester.pump();
      expect(getOffset(), equals(-400.0));
    }, skip: true);

    testWidgets('ListView ensureVisible rotated child', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
422
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
423 424 425 426 427 428 429 430 431
        await tester.pump();
      }

      await tester.pumpWidget(
        new Center(
          child: new SizedBox(
            width: 600.0,
            height: 400.0,
            child: new ListView(
432 433 434 435 436 437 438 439 440 441
              children: <Widget>[
                new Container(height: 200.0),
                new Container(height: 200.0),
                new Container(height: 200.0),
                new Container(
                  height: 200.0,
                  child: new Center(
                    child: new Transform(
                      transform: new Matrix4.rotationZ(math.PI),
                      child: new Container(
442
                        key: const ValueKey<int>(0),
443 444
                        width: 100.0,
                        height: 100.0,
445
                        color: const Color(0xFFFFFFFF),
446 447 448 449 450 451 452 453 454 455
                      ),
                    ),
                  ),
                ),
                new Container(height: 200.0),
                new Container(height: 200.0),
                new Container(height: 200.0),
              ],
            ),
          ),
456 457 458 459
        )
      );

      await prepare(321.0);
Adam Barth's avatar
Adam Barth committed
460
      Scrollable.ensureVisible(findContext(0));
461
      await tester.pump();
462
      expect(tester.getBottomRight(findKey(0)).dy, closeTo(100.0, 0.1));
463

Adam Barth's avatar
Adam Barth committed
464
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
465
      await tester.pump();
466
      expect(tester.getTopLeft(findKey(0)).dy, closeTo(500.0, 0.1));
467
    });
468 469
  });

470 471 472 473
  group('ListView shrinkWrap', () {
    testWidgets('ListView ensureVisible Axis.vertical', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
474
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
475 476 477 478 479 480
        await tester.pump();
      }

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

      await prepare(480.0);
Adam Barth's avatar
Adam Barth committed
481
      Scrollable.ensureVisible(findContext(3));
482
      await tester.pump();
483
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
484 485

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
486
      Scrollable.ensureVisible(findContext(6));
487
      await tester.pump();
488
      expect(tester.getTopLeft(findKey(6)).dy, equals(300.0));
489 490

      await prepare(735.0);
Adam Barth's avatar
Adam Barth committed
491
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
492
      await tester.pump();
493
      expect(tester.getBottomRight(findKey(4)).dy, equals(500.0));
494 495

      await prepare(123.0);
Adam Barth's avatar
Adam Barth committed
496
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
497
      await tester.pump();
498
      expect(tester.getTopLeft(findKey(0)).dy, equals(100.0));
499 500

      await prepare(523.0);
Adam Barth's avatar
Adam Barth committed
501
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
502 503
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
504
      expect(tester.getTopLeft(findKey(3)).dy, equals(100.0));
505 506 507 508 509
    });

    testWidgets('ListView ensureVisible Axis.horizontal', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
510
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
511 512 513 514 515 516
        await tester.pump();
      }

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

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
517
      Scrollable.ensureVisible(findContext(3));
518
      await tester.pump();
519
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
520 521

      await prepare(843.0);
Adam Barth's avatar
Adam Barth committed
522
      Scrollable.ensureVisible(findContext(6));
523
      await tester.pump();
524
      expect(tester.getTopLeft(findKey(6)).dx, equals(500.0));
525 526

      await prepare(415.0);
Adam Barth's avatar
Adam Barth committed
527
      Scrollable.ensureVisible(findContext(4), alignment: 1.0);
528
      await tester.pump();
529
      expect(tester.getBottomRight(findKey(4)).dx, equals(700.0));
530 531

      await prepare(46.0);
Adam Barth's avatar
Adam Barth committed
532
      Scrollable.ensureVisible(findContext(0), alignment: 1.0);
533
      await tester.pump();
534
      expect(tester.getTopLeft(findKey(0)).dx, equals(100.0));
535 536

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
537
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
538 539
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
540
      expect(tester.getTopLeft(findKey(3)).dx, equals(100.0));
541 542 543 544 545
    });

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

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

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
553
      Scrollable.ensureVisible(findContext(3));
554
      await tester.pump();
555
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
556 557

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
558
      Scrollable.ensureVisible(findContext(0));
559
      await tester.pump();
560
      expect(tester.getBottomRight(findKey(0)).dy, equals(500.0));
561 562

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

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

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
573
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
574 575
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
576
      expect(tester.getBottomRight(findKey(3)).dy, equals(500.0));
577 578 579 580 581
    });

    testWidgets('ListView ensureVisible Axis.horizontal reverse', (WidgetTester tester) async {
      BuildContext findContext(int i) => tester.element(findKey(i));
      Future<Null> prepare(double offset) async {
Adam Barth's avatar
Adam Barth committed
582
        tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(offset);
583 584 585 586 587 588
        await tester.pump();
      }

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

      await prepare(211.0);
Adam Barth's avatar
Adam Barth committed
589
      Scrollable.ensureVisible(findContext(3));
590
      await tester.pump();
591
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
592 593

      await prepare(23.0);
Adam Barth's avatar
Adam Barth committed
594
      Scrollable.ensureVisible(findContext(0));
595
      await tester.pump();
596
      expect(tester.getBottomRight(findKey(0)).dx, equals(700.0));
597 598

      await prepare(230.0);
Adam Barth's avatar
Adam Barth committed
599
      Scrollable.ensureVisible(findContext(2), alignment: 1.0);
600
      await tester.pump();
601
      expect(tester.getTopLeft(findKey(2)).dx, equals(100.0));
602 603

      await prepare(1083.0);
Adam Barth's avatar
Adam Barth committed
604
      Scrollable.ensureVisible(findContext(6), alignment: 1.0);
605
      await tester.pump();
606
      expect(tester.getBottomRight(findKey(6)).dx, equals(300.0));
607 608

      await prepare(345.0);
Adam Barth's avatar
Adam Barth committed
609
      Scrollable.ensureVisible(findContext(3), duration: const Duration(seconds: 1));
610 611
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 1020));
612
      expect(tester.getBottomRight(findKey(3)).dx, equals(700.0));
613 614
    });
  });
615
}