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

5
import 'package:flutter/gestures.dart' show DragStartBehavior;
6
import 'package:flutter/material.dart';
7 8
import 'package:flutter/services.dart' show LogicalKeyboardKey;
import 'package:flutter_test/flutter_test.dart';
9

10
import 'states.dart';
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<MaterialLocalizations> load(Locale locale) => DefaultMaterialLocalizations.load(locale);

  @override
  bool shouldReload(MaterialLocalizationsDelegate old) => false;
}

class WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocalizations> {
  @override
  bool isSupported(Locale locale) => true;

  @override
  Future<WidgetsLocalizations> load(Locale locale) => DefaultWidgetsLocalizations.load(locale);

  @override
  bool shouldReload(WidgetsLocalizationsDelegate old) => false;
}

34
Widget textFieldBoilerplate({ required Widget child }) {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  return MaterialApp(
    home: Localizations(
      locale: const Locale('en', 'US'),
      delegates: <LocalizationsDelegate<dynamic>>[
        WidgetsLocalizationsDelegate(),
        MaterialLocalizationsDelegate(),
      ],
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: const MediaQueryData(size: Size(800.0, 600.0)),
          child: Center(
            child: Material(
              child: child,
            ),
          ),
        ),
      ),
    ),
  );
}

57
void main() {
58
  testWidgets('ListView control test', (WidgetTester tester) async {
59
    final List<String> log = <String>[];
60

61
    await tester.pumpWidget(
62
      Directionality(
63
        textDirection: TextDirection.ltr,
64
        child: ListView(
65
          dragStartBehavior: DragStartBehavior.down,
66
          children: kStates.map<Widget>((String state) {
67
            return GestureDetector(
68 69 70
              onTap: () {
                log.add(state);
              },
71
              dragStartBehavior: DragStartBehavior.down,
72
              child: Container(
73 74
                height: 200.0,
                color: const Color(0xFF0000FF),
75
                child: Text(state),
76 77 78 79 80 81
              ),
            );
          }).toList(),
        ),
      ),
    );
82 83 84 85 86 87 88

    await tester.tap(find.text('Alabama'));
    expect(log, equals(<String>['Alabama']));
    log.clear();

    expect(find.text('Nevada'), findsNothing);

89
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
90 91 92
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
93
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
94 95 96 97 98

    await tester.tap(find.text('Massachusetts'));
    expect(log, equals(<String>['Massachusetts']));
    log.clear();
  });
99

100 101 102 103
  testWidgets('ListView dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
104
      child: ListView(
105
        padding: EdgeInsets.zero,
106 107 108 109 110 111
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
112 113 114 115
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
116
              ),
117 118 119 120 121 122 123 124 125
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
130
    expect(textField.focusNode!.hasFocus, isFalse);
131 132 133 134 135 136 137
  });

  testWidgets('ListView.builder dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.builder(
138
        padding: EdgeInsets.zero,
139 140 141 142 143 144 145 146 147 148 149
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
150
              ),
151 152 153 154 155 156 157 158 159
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
164
    expect(textField.focusNode!.hasFocus, isFalse);
165 166 167 168 169 170 171
  });

  testWidgets('ListView.custom dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.custom(
172
        padding: EdgeInsets.zero,
173 174 175 176 177 178 179 180 181 182 183
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
184
                ),
185 186 187 188 189 190 191 192 193 194 195
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
200
    expect(textField.focusNode!.hasFocus, isFalse);
201 202 203 204 205 206 207
  });

  testWidgets('ListView.separated dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.separated(
208
        padding: EdgeInsets.zero,
209 210 211 212 213 214 215 216 217 218 219 220
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
        separatorBuilder: (BuildContext context, int index) => const Divider(),
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
221
              ),
222 223 224 225 226 227 228 229 230
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
235
    expect(textField.focusNode!.hasFocus, isFalse);
236 237 238 239 240 241 242
  });

  testWidgets('GridView dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView(
243
        padding: EdgeInsets.zero,
244 245 246 247 248 249 250 251 252 253 254
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
255
              ),
256 257 258 259 260 261 262 263 264
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
269
    expect(textField.focusNode!.hasFocus, isFalse);
270 271 272 273 274 275 276
  });

  testWidgets('GridView.builder dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.builder(
277
        padding: EdgeInsets.zero,
278 279 280
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
281
        itemBuilder: (BuildContext context, int index) {
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
304
    expect(textField.focusNode!.hasFocus, isFalse);
305 306 307 308 309 310 311
  });

  testWidgets('GridView.count dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.count(
312
        padding: EdgeInsets.zero,
313 314 315 316 317 318 319 320 321 322 323
        crossAxisCount: 2,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
324
              ),
325 326 327 328 329 330 331 332 333
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
338
    expect(textField.focusNode!.hasFocus, isFalse);
339 340 341 342 343 344 345
  });

  testWidgets('GridView.extent dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.extent(
346
        padding: EdgeInsets.zero,
347 348 349 350 351 352 353 354 355 356 357
        maxCrossAxisExtent: 300,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
358
              ),
359 360 361 362 363 364 365 366 367
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
372
    expect(textField.focusNode!.hasFocus, isFalse);
373 374 375 376 377 378 379
  });

  testWidgets('GridView.custom dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.custom(
380
        padding: EdgeInsets.zero,
381 382 383 384 385 386 387 388 389 390 391 392
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
393
                ),
394 395 396 397 398 399 400
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));
401 402 403 404

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
409
    expect(textField.focusNode!.hasFocus, isFalse);
410 411 412 413 414 415
  });

  testWidgets('ListView dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
416 417 418 419 420 421 422
      child: ListView(
        padding: EdgeInsets.zero,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
423 424 425 426
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
427 428 429 430 431 432
              ),
            ),
          );
        }).toList(),
      ),
    ));
433 434 435 436

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

    await tester.drag(finder, const Offset(0.0, -40.0));
440
    await tester.pumpAndSettle();
441
    expect(textField.focusNode!.hasFocus, isTrue);
442 443 444 445 446 447 448
  });

  testWidgets('ListView.builder dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.builder(
449
        padding: EdgeInsets.zero,
450 451 452 453 454 455 456 457 458 459
        itemCount: focusNodes.length,
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
460
              ),
461 462 463 464 465 466 467 468 469
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
474
    expect(textField.focusNode!.hasFocus, isTrue);
475 476 477 478 479 480 481
  });

  testWidgets('ListView.custom dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.custom(
482
        padding: EdgeInsets.zero,
483 484 485 486 487 488 489 490 491 492
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
493
                ),
494 495 496 497 498 499 500 501 502 503 504
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
509
    expect(textField.focusNode!.hasFocus, isTrue);
510 511 512 513 514 515 516
  });

  testWidgets('ListView.separated dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.separated(
517
        padding: EdgeInsets.zero,
518 519 520 521 522 523 524 525 526 527 528
        itemCount: focusNodes.length,
        separatorBuilder: (BuildContext context, int index) => const Divider(),
        itemBuilder: (BuildContext context,int index) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
529
              ),
530 531 532 533 534 535 536 537 538
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
543
    expect(textField.focusNode!.hasFocus, isTrue);
544 545 546 547 548 549 550
  });

  testWidgets('GridView dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView(
551
        padding: EdgeInsets.zero,
552 553 554 555 556 557 558 559 560 561
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
562
              ),
563 564 565 566 567 568 569 570 571
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
576
    expect(textField.focusNode!.hasFocus, isTrue);
577 578 579 580 581 582 583
  });

  testWidgets('GridView.builder dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.builder(
584
        padding: EdgeInsets.zero,
585 586
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        itemCount: focusNodes.length,
587
        itemBuilder: (BuildContext context, int index) {
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNodes[index],
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
610
    expect(textField.focusNode!.hasFocus, isTrue);
611 612 613 614 615 616 617
  });

  testWidgets('GridView.count dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.count(
618
        padding: EdgeInsets.zero,
619 620 621 622 623 624 625 626 627 628
        crossAxisCount: 2,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
629
              ),
630 631 632 633 634 635 636 637 638
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
643
    expect(textField.focusNode!.hasFocus, isTrue);
644 645 646 647 648 649 650
  });

  testWidgets('GridView.extent dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.extent(
651
        padding: EdgeInsets.zero,
652 653 654 655 656 657 658 659 660 661
        maxCrossAxisExtent: 300,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
662
              ),
663 664 665 666 667 668 669 670 671
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
676
    expect(textField.focusNode!.hasFocus, isTrue);
677 678 679 680 681 682 683
  });

  testWidgets('GridView.custom dismiss keyboard manual test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.custom(
684
        padding: EdgeInsets.zero,
685 686 687 688 689 690 691 692 693 694 695
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        childrenDelegate: SliverChildBuilderDelegate(
          (BuildContext context,int index) {
            return Container(
              height: 50,
              color: Colors.green,
              child: TextField(
                focusNode: focusNodes[index],
                style: const TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
696
                ),
697 698 699 700 701 702 703 704 705 706 707
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

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

715 716
  testWidgets('ListView restart ballistic activity out of range', (WidgetTester tester) async {
    Widget buildListView(int n) {
717
      return Directionality(
718
        textDirection: TextDirection.ltr,
719
        child: ListView(
720
          dragStartBehavior: DragStartBehavior.down,
721
          children: kStates.take(n).map<Widget>((String state) {
722
            return Container(
723 724
              height: 200.0,
              color: const Color(0xFF0000FF),
725
              child: Text(state),
726 727 728
            );
          }).toList(),
        ),
729 730 731
      );
    }

732 733 734
    await tester.pumpWidget(buildListView(30));
    await tester.fling(find.byType(ListView), const Offset(0.0, -4000.0), 4000.0);
    await tester.pumpWidget(buildListView(15));
735 736 737 738
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
    await tester.pump(const Duration(milliseconds: 10));
739
    await tester.pumpAndSettle();
740

741
    final Viewport viewport = tester.widget(find.byType(Viewport));
742 743
    expect(viewport.offset.pixels, equals(2400.0));
  });
Adam Barth's avatar
Adam Barth committed
744 745

  testWidgets('CustomScrollView control test', (WidgetTester tester) async {
746
    final List<String> log = <String>[];
Adam Barth's avatar
Adam Barth committed
747

748
    await tester.pumpWidget(
749
      Directionality(
750
        textDirection: TextDirection.ltr,
751
        child: CustomScrollView(
752
          dragStartBehavior: DragStartBehavior.down,
753
          slivers: <Widget>[
754 755
            SliverList(
              delegate: SliverChildListDelegate(
756
                kStates.map<Widget>((String state) {
757
                  return GestureDetector(
758
                    dragStartBehavior: DragStartBehavior.down,
759 760 761
                    onTap: () {
                      log.add(state);
                    },
762
                    child: Container(
763 764
                      height: 200.0,
                      color: const Color(0xFF0000FF),
765
                      child: Text(state),
766 767 768 769 770 771
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
Adam Barth's avatar
Adam Barth committed
772
        ),
773 774
      ),
    );
Adam Barth's avatar
Adam Barth committed
775 776 777 778 779 780 781

    await tester.tap(find.text('Alabama'));
    expect(log, equals(<String>['Alabama']));
    log.clear();

    expect(find.text('Nevada'), findsNothing);

782
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
Adam Barth's avatar
Adam Barth committed
783 784 785
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
786
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
Adam Barth's avatar
Adam Barth committed
787 788 789 790 791

    await tester.tap(find.text('Massachusetts'));
    expect(log, equals(<String>['Massachusetts']));
    log.clear();
  });
792

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  testWidgets('CustomScrollView dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
      child: CustomScrollView(
        dragStartBehavior: DragStartBehavior.down,
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildListDelegate(
              focusNodes.map((FocusNode focusNode) {
                return Container(
                  height: 50,
                  color: Colors.green,
                  child: TextField(
                    focusNode: focusNode,
                    style: const TextStyle(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
812
                    ),
813 814 815 816 817 818 819 820 821 822 823 824
                  ),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    ));

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

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

832 833
  testWidgets('Can jumpTo during drag', (WidgetTester tester) async {
    final List<Type> log = <Type>[];
834
    final ScrollController controller = ScrollController();
835

836
    await tester.pumpWidget(
837
      Directionality(
838
        textDirection: TextDirection.ltr,
839
        child: NotificationListener<ScrollNotification>(
840 841 842 843
          onNotification: (ScrollNotification notification) {
            log.add(notification.runtimeType);
            return false;
          },
844
          child: ListView(
845 846
            controller: controller,
            children: kStates.map<Widget>((String state) {
847
              return SizedBox(
848
                height: 200.0,
849
                child: Text(state),
850 851 852 853
              );
            }).toList(),
          ),
        ),
854
      ),
855
    );
856 857 858

    expect(log, isEmpty);

859
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    await gesture.moveBy(const Offset(0.0, -100.0));

    expect(log, equals(<Type>[
      ScrollStartNotification,
      UserScrollNotification,
      ScrollUpdateNotification,
    ]));
    log.clear();

    await tester.pump();

    controller.jumpTo(550.0);

    expect(controller.offset, equals(550.0));
    expect(log, equals(<Type>[
      ScrollEndNotification,
      UserScrollNotification,
      ScrollStartNotification,
      ScrollUpdateNotification,
      ScrollEndNotification,
    ]));
    log.clear();

    await tester.pump();
    await gesture.moveBy(const Offset(0.0, -100.0));

    expect(controller.offset, equals(550.0));
    expect(log, isEmpty);
  });
889

890
  testWidgets('Vertical CustomScrollViews are primary by default', (WidgetTester tester) async {
891
    const CustomScrollView view = CustomScrollView();
892 893 894 895
    expect(view.primary, isTrue);
  });

  testWidgets('Vertical ListViews are primary by default', (WidgetTester tester) async {
896
    final ListView view = ListView();
897 898 899 900
    expect(view.primary, isTrue);
  });

  testWidgets('Vertical GridViews are primary by default', (WidgetTester tester) async {
901
    final GridView view = GridView.count(
902 903 904 905 906 907
      crossAxisCount: 1,
    );
    expect(view.primary, isTrue);
  });

  testWidgets('Horizontal CustomScrollViews are non-primary by default', (WidgetTester tester) async {
908
    const CustomScrollView view = CustomScrollView(scrollDirection: Axis.horizontal);
909 910 911 912
    expect(view.primary, isFalse);
  });

  testWidgets('Horizontal ListViews are non-primary by default', (WidgetTester tester) async {
913
    final ListView view = ListView(scrollDirection: Axis.horizontal);
914 915 916 917
    expect(view.primary, isFalse);
  });

  testWidgets('Horizontal GridViews are non-primary by default', (WidgetTester tester) async {
918
    final GridView view = GridView.count(
919 920 921 922 923 924 925
      scrollDirection: Axis.horizontal,
      crossAxisCount: 1,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('CustomScrollViews with controllers are non-primary by default', (WidgetTester tester) async {
926 927
    final CustomScrollView view = CustomScrollView(
      controller: ScrollController(),
928 929 930 931 932
    );
    expect(view.primary, isFalse);
  });

  testWidgets('ListViews with controllers are non-primary by default', (WidgetTester tester) async {
933 934
    final ListView view = ListView(
      controller: ScrollController(),
935 936 937 938 939
    );
    expect(view.primary, isFalse);
  });

  testWidgets('GridViews with controllers are non-primary by default', (WidgetTester tester) async {
940 941
    final GridView view = GridView.count(
      controller: ScrollController(),
942 943 944 945 946
      crossAxisCount: 1,
    );
    expect(view.primary, isFalse);
  });

947
  testWidgets('CustomScrollView sets PrimaryScrollController when primary', (WidgetTester tester) async {
948
    final ScrollController primaryScrollController = ScrollController();
949
    await tester.pumpWidget(
950
      Directionality(
951
        textDirection: TextDirection.ltr,
952
        child: PrimaryScrollController(
953
          controller: primaryScrollController,
954
          child: const CustomScrollView(primary: true),
955 956 957
        ),
      ),
    );
958
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
959 960 961 962
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('ListView sets PrimaryScrollController when primary', (WidgetTester tester) async {
963
    final ScrollController primaryScrollController = ScrollController();
964
    await tester.pumpWidget(
965
      Directionality(
966
        textDirection: TextDirection.ltr,
967
        child: PrimaryScrollController(
968
          controller: primaryScrollController,
969
          child: ListView(primary: true),
970 971 972
        ),
      ),
    );
973
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
974 975 976 977
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('GridView sets PrimaryScrollController when primary', (WidgetTester tester) async {
978
    final ScrollController primaryScrollController = ScrollController();
979
    await tester.pumpWidget(
980
      Directionality(
981
        textDirection: TextDirection.ltr,
982
        child: PrimaryScrollController(
983
          controller: primaryScrollController,
984
          child: GridView.count(primary: true, crossAxisCount: 1),
985 986 987
        ),
      ),
    );
988
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
989 990
    expect(scrollable.controller, primaryScrollController);
  });
991 992

  testWidgets('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
993
    const Key innerKey = Key('inner');
994
    final ScrollController primaryScrollController = ScrollController();
995
    await tester.pumpWidget(
996
      Directionality(
997
        textDirection: TextDirection.ltr,
998
        child: PrimaryScrollController(
999
          controller: primaryScrollController,
1000
          child: ListView(
1001 1002
            primary: true,
            children: <Widget>[
1003
              Container(
1004
                constraints: const BoxConstraints(maxHeight: 200.0),
1005
                child: ListView(key: innerKey, primary: true),
1006 1007
              ),
            ],
1008
          ),
1009
        ),
1010
      ),
1011
    );
1012

1013
    final Scrollable innerScrollable = tester.widget(
1014 1015 1016 1017 1018 1019 1020
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
1021 1022

  testWidgets('Primary ListViews are always scrollable', (WidgetTester tester) async {
1023
    final ListView view = ListView(primary: true);
Dan Field's avatar
Dan Field committed
1024
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1025 1026 1027
  });

  testWidgets('Non-primary ListViews are not always scrollable', (WidgetTester tester) async {
1028
    final ListView view = ListView(primary: false);
Dan Field's avatar
Dan Field committed
1029
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1030 1031 1032
  });

  testWidgets('Defaulting-to-primary ListViews are always scrollable', (WidgetTester tester) async {
1033
    final ListView view = ListView();
Dan Field's avatar
Dan Field committed
1034
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1035 1036 1037
  });

  testWidgets('Defaulting-to-not-primary ListViews are not always scrollable', (WidgetTester tester) async {
1038
    final ListView view = ListView(scrollDirection: Axis.horizontal);
Dan Field's avatar
Dan Field committed
1039
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1040 1041 1042 1043 1044
  });

  testWidgets('primary:true leads to scrolling', (WidgetTester tester) async {
    bool scrolled = false;
    await tester.pumpWidget(
1045
      Directionality(
1046
        textDirection: TextDirection.ltr,
1047
        child: NotificationListener<OverscrollNotification>(
1048 1049 1050 1051
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1052
          child: ListView(
1053 1054
            primary: true,
          ),
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isTrue);
  });

  testWidgets('primary:false leads to no scrolling', (WidgetTester tester) async {
    bool scrolled = false;
    await tester.pumpWidget(
1065
      Directionality(
1066
        textDirection: TextDirection.ltr,
1067
        child: NotificationListener<OverscrollNotification>(
1068 1069 1070 1071
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1072
          child: ListView(
1073 1074
            primary: false,
          ),
1075 1076 1077 1078 1079 1080 1081
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isFalse);
  });

Ian Hickson's avatar
Ian Hickson committed
1082
  testWidgets('physics:AlwaysScrollableScrollPhysics actually overrides primary:false default behavior', (WidgetTester tester) async {
1083 1084
    bool scrolled = false;
    await tester.pumpWidget(
1085
      Directionality(
1086
        textDirection: TextDirection.ltr,
1087
        child: NotificationListener<OverscrollNotification>(
1088 1089 1090 1091
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1092
          child: ListView(
1093 1094 1095
            primary: false,
            physics: const AlwaysScrollableScrollPhysics(),
          ),
1096 1097 1098 1099 1100 1101 1102
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isTrue);
  });

Ian Hickson's avatar
Ian Hickson committed
1103
  testWidgets('physics:ScrollPhysics actually overrides primary:true default behavior', (WidgetTester tester) async {
1104 1105
    bool scrolled = false;
    await tester.pumpWidget(
1106
      Directionality(
1107
        textDirection: TextDirection.ltr,
1108
        child: NotificationListener<OverscrollNotification>(
1109 1110 1111 1112
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1113
          child: ListView(
1114 1115 1116
            primary: true,
            physics: const ScrollPhysics(),
          ),
1117 1118 1119 1120 1121 1122
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isFalse);
  });
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

  testWidgets('separatorBuilder must return something', (WidgetTester tester) async {
    const List<String> listOfValues = <String>['ALPHA', 'BETA', 'GAMMA', 'DELTA'];

    Widget buildFrame(Widget firstSeparator) {
      return MaterialApp(
        home: Material(
          child: ListView.separated(
            itemBuilder: (BuildContext context, int index) {
              return Text(listOfValues[index]);
            },
            separatorBuilder: (BuildContext context, int index) {
              if (index == 0) {
                return firstSeparator;
1137
              } else {
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
                return const Divider();
              }
            },
            itemCount: listOfValues.length,
          ),
        ),
      );
    }

    // A separatorBuilder that always returns a Divider is fine
    await tester.pumpWidget(buildFrame(const Divider()));
    expect(tester.takeException(), isNull);
  });

  testWidgets('when itemBuilder throws, creates Error Widget', (WidgetTester tester) async {
    const List<String> listOfValues = <String>['ALPHA', 'BETA', 'GAMMA', 'DELTA'];

    Widget buildFrame(bool throwOnFirstItem) {
      return MaterialApp(
        home: Material(
          child: ListView.builder(
            itemBuilder: (BuildContext context, int index) {
              if (index == 0 && throwOnFirstItem) {
                throw Exception('itemBuilder fail');
              }
              return Text(listOfValues[index]);
            },
            itemCount: listOfValues.length,
          ),
        ),
      );
    }

    // When itemBuilder doesn't throw, no ErrorWidget
    await tester.pumpWidget(buildFrame(false));
    expect(tester.takeException(), isNull);
    final Finder finder = find.byType(ErrorWidget);
    expect(find.byType(ErrorWidget), findsNothing);

    // When it does throw, one error widget is rendered in the item's place
    await tester.pumpWidget(buildFrame(true));
Dan Field's avatar
Dan Field committed
1179
    expect(tester.takeException(), isA<Exception>());
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
    expect(finder, findsOneWidget);
  });

  testWidgets('when separatorBuilder throws, creates ErrorWidget', (WidgetTester tester) async {
    const List<String> listOfValues = <String>['ALPHA', 'BETA', 'GAMMA', 'DELTA'];
    const Key key = Key('list');

    Widget buildFrame(bool throwOnFirstSeparator) {
      return MaterialApp(
        home: Material(
          child: ListView.separated(
            key: key,
            itemBuilder: (BuildContext context, int index) {
              return Text(listOfValues[index]);
            },
            separatorBuilder: (BuildContext context, int index) {
              if (index == 0 && throwOnFirstSeparator) {
                throw Exception('separatorBuilder fail');
              }
              return const Divider();
            },
            itemCount: listOfValues.length,
          ),
        ),
      );
    }

    // When separatorBuilder doesn't throw, no ErrorWidget
    await tester.pumpWidget(buildFrame(false));
    expect(tester.takeException(), isNull);
    final Finder finder = find.byType(ErrorWidget);
    expect(find.byType(ErrorWidget), findsNothing);

    // When it does throw, one error widget is rendered in the separator's place
    await tester.pumpWidget(buildFrame(true));
Dan Field's avatar
Dan Field committed
1215
    expect(tester.takeException(), isA<Exception>());
1216 1217
    expect(finder, findsOneWidget);
  });
1218

1219
   testWidgets('ListView asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
1220 1221 1222 1223 1224 1225
    expect(() => ListView(
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

1226 1227 1228 1229 1230 1231
  testWidgets('ListView.builder asserts on negative childCount', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemCount: -1,
Dan Field's avatar
Dan Field committed
1232
    ), throwsAssertionError);
1233 1234 1235 1236 1237 1238 1239 1240 1241
  });

  testWidgets('ListView.builder asserts on negative semanticChildCount', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemCount: 1,
      semanticChildCount: -1,
Dan Field's avatar
Dan Field committed
1242
    ), throwsAssertionError);
1243 1244 1245 1246 1247 1248 1249 1250 1251
  });

  testWidgets('ListView.builder asserts on nonsensical childCount/semanticChildCount', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemCount: 1,
      semanticChildCount: 4,
Dan Field's avatar
Dan Field committed
1252
    ), throwsAssertionError);
1253
  });
1254

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
  testWidgets('ListView.builder asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
    expect(() => ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return const SizedBox();
      },
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

  testWidgets('ListView.custom asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
    expect(() => ListView.custom(
      childrenDelegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return const SizedBox();
        },
      ),
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
  testWidgets('PrimaryScrollController provides fallback ScrollActions', (WidgetTester tester)  async {
    await tester.pumpWidget(
      MaterialApp(
        home: CustomScrollView(
          primary: true,
          slivers: List<Widget>.generate(
            20,
            (int index) {
              return SliverToBoxAdapter(
                child: Focus(
                  autofocus: index == 0,
                  child: SizedBox(key: ValueKey<String>('Box $index'), height: 50.0),
                ),
              );
            },
          ),
        ),
      ),
    );
    final ScrollController controller = PrimaryScrollController.of(
1297
      tester.element(find.byType(CustomScrollView)),
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    )!;
    await tester.pumpAndSettle();
    expect(controller.position.pixels, equals(0.0));
    expect(
      tester.getRect(find.byKey(const ValueKey<String>('Box 0'), skipOffstage: false)),
      equals(const Rect.fromLTRB(0.0, 0.0, 800.0, 50.0)),
    );
    await tester.sendKeyEvent(LogicalKeyboardKey.pageDown);
    await tester.pumpAndSettle();
    expect(controller.position.pixels, equals(400.0));
    expect(
      tester.getRect(find.byKey(const ValueKey<String>('Box 0'), skipOffstage: false)),
      equals(const Rect.fromLTRB(0.0, -400.0, 800.0, -350.0)),
    );
    await tester.sendKeyEvent(LogicalKeyboardKey.pageUp);
    await tester.pumpAndSettle();
    expect(controller.position.pixels, equals(0.0));
    expect(
      tester.getRect(find.byKey(const ValueKey<String>('Box 0'), skipOffstage: false)),
      equals(const Rect.fromLTRB(0.0, 0.0, 800.0, 50.0)),
    );
  });
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357

  testWidgets('if itemExtent is non-null, children have same extent in the scroll direction', (WidgetTester tester) async {
    final List<int> numbers = <int>[0,1,2];

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    return SizedBox(
                        key: ValueKey<int>(numbers[index]),
                        // children with different heights
                        height: 20 + numbers[index] * 10,
                        child: ReorderableDragStartListener(
                          index: index,
                          child: Text(numbers[index].toString()),
                        )
                    );
                  },
                  itemCount: numbers.length,
                  itemExtent: 30,
                );
              },
            ),
          ),
        )
    );

    final double item0Height = tester.getSize(find.text('0').hitTestable()).height;
    final double item1Height = tester.getSize(find.text('1').hitTestable()).height;
    final double item2Height = tester.getSize(find.text('2').hitTestable()).height;

    expect(item0Height, 30.0);
    expect(item1Height, 30.0);
    expect(item2Height, 30.0);
  });
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398

  testWidgets('if prototypeItem is non-null, children have same extent in the scroll direction', (WidgetTester tester) async {
    final List<int> numbers = <int>[0,1,2];

    await tester.pumpWidget(
        MaterialApp(
          home: Scaffold(
            body: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
                return ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                    return SizedBox(
                        key: ValueKey<int>(numbers[index]),
                        // children with different heights
                        height: 20 + numbers[index] * 10,
                        child: ReorderableDragStartListener(
                          index: index,
                          child: Text(numbers[index].toString()),
                        )
                    );
                  },
                  itemCount: numbers.length,
                  prototypeItem: const SizedBox(
                      height: 30,
                      child: Text('3'),
                  ),
                );
              },
            ),
          ),
        )
    );

    final double item0Height = tester.getSize(find.text('0').hitTestable()).height;
    final double item1Height = tester.getSize(find.text('1').hitTestable()).height;
    final double item2Height = tester.getSize(find.text('2').hitTestable()).height;

    expect(item0Height, 30.0);
    expect(item1Height, 30.0);
    expect(item2Height, 30.0);
  });
1399
}