scroll_view_test.dart 54.1 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 58 59 60 61 62 63 64 65 66 67 68 69
Widget primaryScrollControllerBoilerplate({ required Widget child, required ScrollController controller }) {
  return Directionality(
    textDirection: TextDirection.ltr,
    child: MediaQuery(
      data: const MediaQueryData(),
      child: PrimaryScrollController(
        controller: controller,
        child: child,
      ),
    ),
  );
}

70
void main() {
71
  testWidgets('ListView control test', (WidgetTester tester) async {
72
    final List<String> log = <String>[];
73

74
    await tester.pumpWidget(
75
      Directionality(
76
        textDirection: TextDirection.ltr,
77
        child: ListView(
78
          dragStartBehavior: DragStartBehavior.down,
79
          children: kStates.map<Widget>((String state) {
80
            return GestureDetector(
81 82 83
              onTap: () {
                log.add(state);
              },
84
              dragStartBehavior: DragStartBehavior.down,
85
              child: Container(
86 87
                height: 200.0,
                color: const Color(0xFF0000FF),
88
                child: Text(state),
89 90 91 92 93 94
              ),
            );
          }).toList(),
        ),
      ),
    );
95 96 97 98 99 100 101

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

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

102
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
103 104 105
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
106
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
107 108 109 110 111

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

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

    await tester.pumpWidget(textFieldBoilerplate(
117
      child: ListView(
118
        padding: EdgeInsets.zero,
119 120 121 122 123 124
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
125 126 127 128
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
129
              ),
130 131 132 133 134 135 136 137 138
            ),
          );
        }).toList(),
      ),
    ));

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

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

146 147 148 149 150 151 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  testWidgets('GridView.builder supports null items', (WidgetTester tester) async {
    await tester.pumpWidget(textFieldBoilerplate(
      child: GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 42,
        ),
        itemCount: 42,
        itemBuilder: (BuildContext context, int index) {
          if (index == 5) {
            return null;
          }

          return const Text('item');
        },
      ),
    ));

    expect(find.text('item'), findsNWidgets(5));
  });

  testWidgets('ListView.builder supports null items', (WidgetTester tester) async {
    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.builder(
        itemCount: 42,
        itemBuilder: (BuildContext context, int index) {
          if (index == 5) {
            return null;
          }

          return const Text('item');
        },
      ),
    ));

    expect(find.text('item'), findsNWidgets(5));
  });

  testWidgets('PageView supports null items in itemBuilder', (WidgetTester tester) async {
    await tester.pumpWidget(textFieldBoilerplate(
      child: PageView.builder(
        itemCount: 5,
        controller: PageController(viewportFraction: 1/5),
        itemBuilder: (BuildContext context, int index) {
          if (index == 2) {
            return null;
          }

          return const Text('item');
        },
      ),
    ));

    expect(find.text('item'), findsNWidgets(2));
  });

  testWidgets('ListView.separated supports null items in itemBuilder', (WidgetTester tester) async {
    await tester.pumpWidget(textFieldBoilerplate(
      child: ListView.separated(
        itemCount: 42,
        separatorBuilder: (BuildContext context, int index) {
          return const Text('separator');
        },
        itemBuilder: (BuildContext context, int index) {
          if (index == 5) {
            return null;
          }

          return const Text('item');
        },
      ),
    ));

    expect(find.text('item'), findsNWidgets(5));
    expect(find.text('separator'), findsNWidgets(5));
  });

222 223 224 225 226
  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(
227
        padding: EdgeInsets.zero,
228 229 230 231 232 233 234 235 236 237 238
        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,
239
              ),
240 241 242 243 244 245 246 247 248
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
253
    expect(textField.focusNode!.hasFocus, isFalse);
254 255 256 257 258 259 260
  });

  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(
261
        padding: EdgeInsets.zero,
262 263 264 265 266 267 268 269 270 271 272
        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,
273
                ),
274 275 276 277 278 279 280 281 282 283 284
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
289
    expect(textField.focusNode!.hasFocus, isFalse);
290 291 292 293 294 295 296
  });

  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(
297
        padding: EdgeInsets.zero,
298 299 300 301 302 303 304 305 306 307 308 309
        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,
310
              ),
311 312 313 314 315 316 317 318 319
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
324
    expect(textField.focusNode!.hasFocus, isFalse);
325 326 327 328 329 330 331
  });

  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(
332
        padding: EdgeInsets.zero,
333 334 335 336 337 338 339 340 341 342 343
        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,
344
              ),
345 346 347 348 349 350 351 352 353
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
358
    expect(textField.focusNode!.hasFocus, isFalse);
359 360 361 362 363 364 365
  });

  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(
366
        padding: EdgeInsets.zero,
367 368 369
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
370
        itemBuilder: (BuildContext context, int index) {
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
          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);
389
    expect(textField.focusNode!.hasFocus, isTrue);
390 391 392

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
393
    expect(textField.focusNode!.hasFocus, isFalse);
394 395 396 397 398 399 400
  });

  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(
401
        padding: EdgeInsets.zero,
402 403 404 405 406 407 408 409 410 411 412
        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,
413
              ),
414 415 416 417 418 419 420 421 422
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
427
    expect(textField.focusNode!.hasFocus, isFalse);
428 429 430 431 432 433 434
  });

  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(
435
        padding: EdgeInsets.zero,
436 437 438 439 440 441 442 443 444 445 446
        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,
447
              ),
448 449 450 451 452 453 454 455 456
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
461
    expect(textField.focusNode!.hasFocus, isFalse);
462 463 464 465 466 467 468
  });

  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(
469
        padding: EdgeInsets.zero,
470 471 472 473 474 475 476 477 478 479 480 481
        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,
482
                ),
483 484 485 486 487 488 489
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));
490 491 492 493

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
498
    expect(textField.focusNode!.hasFocus, isFalse);
499 500 501 502 503 504
  });

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

    await tester.pumpWidget(textFieldBoilerplate(
505 506 507 508 509 510 511
      child: ListView(
        padding: EdgeInsets.zero,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
512 513 514 515
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
516 517 518 519 520 521
              ),
            ),
          );
        }).toList(),
      ),
    ));
522 523 524 525

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

    await tester.drag(finder, const Offset(0.0, -40.0));
529
    await tester.pumpAndSettle();
530
    expect(textField.focusNode!.hasFocus, isTrue);
531 532 533 534 535 536 537
  });

  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(
538
        padding: EdgeInsets.zero,
539 540 541 542 543 544 545 546 547 548
        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,
549
              ),
550 551 552 553 554 555 556 557 558
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
563
    expect(textField.focusNode!.hasFocus, isTrue);
564 565 566 567 568 569 570
  });

  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(
571
        padding: EdgeInsets.zero,
572 573 574 575 576 577 578 579 580 581
        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,
582
                ),
583 584 585 586 587 588 589 590 591 592 593
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
598
    expect(textField.focusNode!.hasFocus, isTrue);
599 600 601 602 603 604 605
  });

  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(
606
        padding: EdgeInsets.zero,
607 608 609 610 611 612 613 614 615 616 617
        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,
618
              ),
619 620 621 622 623 624 625 626 627
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
632
    expect(textField.focusNode!.hasFocus, isTrue);
633 634 635 636 637 638 639
  });

  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(
640
        padding: EdgeInsets.zero,
641 642 643 644 645 646 647 648 649 650
        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,
651
              ),
652 653 654 655 656 657 658 659 660
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
665
    expect(textField.focusNode!.hasFocus, isTrue);
666 667 668 669 670 671 672
  });

  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(
673
        padding: EdgeInsets.zero,
674 675
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        itemCount: focusNodes.length,
676
        itemBuilder: (BuildContext context, int index) {
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
          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);
695
    expect(textField.focusNode!.hasFocus, isTrue);
696 697 698

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
699
    expect(textField.focusNode!.hasFocus, isTrue);
700 701 702 703 704 705 706
  });

  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(
707
        padding: EdgeInsets.zero,
708 709 710 711 712 713 714 715 716 717
        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,
718
              ),
719 720 721 722 723 724 725 726 727
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
732
    expect(textField.focusNode!.hasFocus, isTrue);
733 734 735 736 737 738 739
  });

  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(
740
        padding: EdgeInsets.zero,
741 742 743 744 745 746 747 748 749 750
        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,
751
              ),
752 753 754 755 756 757 758 759 760
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
765
    expect(textField.focusNode!.hasFocus, isTrue);
766 767 768 769 770 771 772
  });

  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(
773
        padding: EdgeInsets.zero,
774 775 776 777 778 779 780 781 782 783 784
        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,
785
                ),
786 787 788 789 790 791 792 793 794 795 796
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

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

804 805
  testWidgets('ListView restart ballistic activity out of range', (WidgetTester tester) async {
    Widget buildListView(int n) {
806
      return Directionality(
807
        textDirection: TextDirection.ltr,
808
        child: ListView(
809
          dragStartBehavior: DragStartBehavior.down,
810
          children: kStates.take(n).map<Widget>((String state) {
811
            return Container(
812 813
              height: 200.0,
              color: const Color(0xFF0000FF),
814
              child: Text(state),
815 816 817
            );
          }).toList(),
        ),
818 819 820
      );
    }

821 822 823
    await tester.pumpWidget(buildListView(30));
    await tester.fling(find.byType(ListView), const Offset(0.0, -4000.0), 4000.0);
    await tester.pumpWidget(buildListView(15));
824 825 826 827
    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));
828
    await tester.pumpAndSettle();
829

830
    final Viewport viewport = tester.widget(find.byType(Viewport));
831 832
    expect(viewport.offset.pixels, equals(2400.0));
  });
Adam Barth's avatar
Adam Barth committed
833 834

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

837
    await tester.pumpWidget(
838
      Directionality(
839
        textDirection: TextDirection.ltr,
840
        child: CustomScrollView(
841
          dragStartBehavior: DragStartBehavior.down,
842
          slivers: <Widget>[
843 844
            SliverList(
              delegate: SliverChildListDelegate(
845
                kStates.map<Widget>((String state) {
846
                  return GestureDetector(
847
                    dragStartBehavior: DragStartBehavior.down,
848 849 850
                    onTap: () {
                      log.add(state);
                    },
851
                    child: Container(
852 853
                      height: 200.0,
                      color: const Color(0xFF0000FF),
854
                      child: Text(state),
855 856 857 858 859 860
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
Adam Barth's avatar
Adam Barth committed
861
        ),
862 863
      ),
    );
Adam Barth's avatar
Adam Barth committed
864 865 866 867 868 869 870

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

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

871
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
Adam Barth's avatar
Adam Barth committed
872 873 874
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
875
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
Adam Barth's avatar
Adam Barth committed
876 877 878 879 880

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

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
  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,
901
                    ),
902 903 904 905 906 907 908 909 910 911 912 913
                  ),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    ));

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

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

921 922
  testWidgets('Can jumpTo during drag', (WidgetTester tester) async {
    final List<Type> log = <Type>[];
923
    final ScrollController controller = ScrollController();
924

925
    await tester.pumpWidget(
926
      Directionality(
927
        textDirection: TextDirection.ltr,
928
        child: NotificationListener<ScrollNotification>(
929 930 931 932
          onNotification: (ScrollNotification notification) {
            log.add(notification.runtimeType);
            return false;
          },
933
          child: ListView(
934 935
            controller: controller,
            children: kStates.map<Widget>((String state) {
936
              return SizedBox(
937
                height: 200.0,
938
                child: Text(state),
939 940 941 942
              );
            }).toList(),
          ),
        ),
943
      ),
944
    );
945 946 947

    expect(log, isEmpty);

948
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
    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);
  });
978

979 980 981 982 983 984 985 986 987 988 989 990
  test('PrimaryScrollController.automaticallyInheritOnPlatforms defaults to all mobile platforms', (){
     final PrimaryScrollController primaryScrollController = PrimaryScrollController(
        controller: ScrollController(),
       child: const SizedBox(),
     );
     expect(
       primaryScrollController.automaticallyInheritForPlatforms,
       TargetPlatformVariant.mobile().values,
     );
  });

  testWidgets('Vertical CustomScrollViews are not primary by default', (WidgetTester tester) async {
991
    const CustomScrollView view = CustomScrollView();
992
    expect(view.primary, isNull);
993 994
  });

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  testWidgets('Vertical CustomScrollViews use PrimaryScrollController by default on mobile', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: const CustomScrollView(),
      controller: controller,
    ));
    expect(controller.hasClients, isTrue);
  }, variant: TargetPlatformVariant.mobile());

  testWidgets("Vertical CustomScrollViews don't use PrimaryScrollController by default on desktop", (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
        child: const CustomScrollView(),
        controller: controller,
    ));
    expect(controller.hasClients, isFalse);
  }, variant: TargetPlatformVariant.desktop());

  testWidgets('Vertical ListViews are not primary by default', (WidgetTester tester) async {
1014
    final ListView view = ListView();
1015
    expect(view.primary, isNull);
1016 1017
  });

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
  testWidgets('Vertical ListViews use PrimaryScrollController by default on mobile', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: ListView(),
      controller: controller,
    ));
    expect(controller.hasClients, isTrue);
  }, variant: TargetPlatformVariant.mobile());

  testWidgets("Vertical ListViews don't use PrimaryScrollController by default on desktop", (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: ListView(),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
  }, variant: TargetPlatformVariant.desktop());

  testWidgets('Vertical GridViews are not primary by default', (WidgetTester tester) async {
    final GridView view = GridView.count(crossAxisCount: 1);
    expect(view.primary, isNull);
1039 1040
  });

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  testWidgets('Vertical GridViews use PrimaryScrollController by default on mobile', (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: GridView.count(crossAxisCount: 1),
      controller: controller,
    ));
    expect(controller.hasClients, isTrue);
  }, variant: TargetPlatformVariant.mobile());

  testWidgets("Vertical GridViews don't use PrimaryScrollController by default on desktop", (WidgetTester tester) async {
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: GridView.count(crossAxisCount: 1),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
  }, variant: TargetPlatformVariant.desktop());

1059
  testWidgets('Horizontal CustomScrollViews are non-primary by default', (WidgetTester tester) async {
1060 1061 1062 1063 1064 1065 1066 1067 1068
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: CustomScrollView(
        scrollDirection: Axis.horizontal,
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1069 1070 1071
  });

  testWidgets('Horizontal ListViews are non-primary by default', (WidgetTester tester) async {
1072 1073 1074 1075 1076 1077 1078 1079 1080
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: ListView(
        scrollDirection: Axis.horizontal,
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1081 1082 1083
  });

  testWidgets('Horizontal GridViews are non-primary by default', (WidgetTester tester) async {
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: GridView.count(
        scrollDirection: Axis.horizontal,
        controller: ScrollController(),
        crossAxisCount: 1,
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1094 1095 1096
  });

  testWidgets('CustomScrollViews with controllers are non-primary by default', (WidgetTester tester) async {
1097 1098 1099 1100 1101 1102 1103 1104
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: CustomScrollView(
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1105 1106 1107
  });

  testWidgets('ListViews with controllers are non-primary by default', (WidgetTester tester) async {
1108 1109 1110 1111 1112 1113 1114 1115
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: ListView(
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1116 1117 1118
  });

  testWidgets('GridViews with controllers are non-primary by default', (WidgetTester tester) async {
1119 1120 1121 1122 1123 1124 1125 1126 1127
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: GridView.count(
        controller: ScrollController(),
        crossAxisCount: 1,
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1128 1129
  });

1130
  testWidgets('CustomScrollView sets PrimaryScrollController when primary', (WidgetTester tester) async {
1131
    final ScrollController primaryScrollController = ScrollController();
1132
    await tester.pumpWidget(
1133
      Directionality(
1134
        textDirection: TextDirection.ltr,
1135
        child: PrimaryScrollController(
1136
          controller: primaryScrollController,
1137
          child: const CustomScrollView(primary: true),
1138 1139 1140
        ),
      ),
    );
1141
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
1142 1143 1144 1145
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('ListView sets PrimaryScrollController when primary', (WidgetTester tester) async {
1146
    final ScrollController primaryScrollController = ScrollController();
1147
    await tester.pumpWidget(
1148
      Directionality(
1149
        textDirection: TextDirection.ltr,
1150
        child: PrimaryScrollController(
1151
          controller: primaryScrollController,
1152
          child: ListView(primary: true),
1153 1154 1155
        ),
      ),
    );
1156
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
1157 1158 1159 1160
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('GridView sets PrimaryScrollController when primary', (WidgetTester tester) async {
1161
    final ScrollController primaryScrollController = ScrollController();
1162
    await tester.pumpWidget(
1163
      Directionality(
1164
        textDirection: TextDirection.ltr,
1165
        child: PrimaryScrollController(
1166
          controller: primaryScrollController,
1167
          child: GridView.count(primary: true, crossAxisCount: 1),
1168 1169 1170
        ),
      ),
    );
1171
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
1172 1173
    expect(scrollable.controller, primaryScrollController);
  });
1174 1175

  testWidgets('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
1176
    const Key innerKey = Key('inner');
1177
    final ScrollController primaryScrollController = ScrollController();
1178
    await tester.pumpWidget(
1179
      Directionality(
1180
        textDirection: TextDirection.ltr,
1181
        child: PrimaryScrollController(
1182
          controller: primaryScrollController,
1183
          child: ListView(
1184 1185
            primary: true,
            children: <Widget>[
1186
              Container(
1187
                constraints: const BoxConstraints(maxHeight: 200.0),
1188
                child: ListView(key: innerKey, primary: true),
1189 1190
              ),
            ],
1191
          ),
1192
        ),
1193
      ),
1194
    );
1195

1196
    final Scrollable innerScrollable = tester.widget(
1197 1198 1199 1200 1201 1202 1203
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
1204 1205

  testWidgets('Primary ListViews are always scrollable', (WidgetTester tester) async {
1206
    final ListView view = ListView(primary: true);
Dan Field's avatar
Dan Field committed
1207
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1208 1209 1210
  });

  testWidgets('Non-primary ListViews are not always scrollable', (WidgetTester tester) async {
1211
    final ListView view = ListView(primary: false);
Dan Field's avatar
Dan Field committed
1212
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1213 1214 1215
  });

  testWidgets('Defaulting-to-primary ListViews are always scrollable', (WidgetTester tester) async {
1216
    final ListView view = ListView();
Dan Field's avatar
Dan Field committed
1217
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1218 1219 1220
  });

  testWidgets('Defaulting-to-not-primary ListViews are not always scrollable', (WidgetTester tester) async {
1221
    final ListView view = ListView(scrollDirection: Axis.horizontal);
Dan Field's avatar
Dan Field committed
1222
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1223 1224 1225 1226 1227
  });

  testWidgets('primary:true leads to scrolling', (WidgetTester tester) async {
    bool scrolled = false;
    await tester.pumpWidget(
1228
      Directionality(
1229
        textDirection: TextDirection.ltr,
1230
        child: NotificationListener<OverscrollNotification>(
1231 1232 1233 1234
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1235
          child: ListView(
1236 1237
            primary: true,
          ),
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
        ),
      ),
    );
    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(
1248
      Directionality(
1249
        textDirection: TextDirection.ltr,
1250
        child: NotificationListener<OverscrollNotification>(
1251 1252 1253 1254
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1255
          child: ListView(
1256 1257
            primary: false,
          ),
1258 1259 1260 1261 1262 1263 1264
        ),
      ),
    );
    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
1265
  testWidgets('physics:AlwaysScrollableScrollPhysics actually overrides primary:false default behavior', (WidgetTester tester) async {
1266 1267
    bool scrolled = false;
    await tester.pumpWidget(
1268
      Directionality(
1269
        textDirection: TextDirection.ltr,
1270
        child: NotificationListener<OverscrollNotification>(
1271 1272 1273 1274
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1275
          child: ListView(
1276 1277 1278
            primary: false,
            physics: const AlwaysScrollableScrollPhysics(),
          ),
1279 1280 1281 1282 1283 1284 1285
        ),
      ),
    );
    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
1286
  testWidgets('physics:ScrollPhysics actually overrides primary:true default behavior', (WidgetTester tester) async {
1287 1288
    bool scrolled = false;
    await tester.pumpWidget(
1289
      Directionality(
1290
        textDirection: TextDirection.ltr,
1291
        child: NotificationListener<OverscrollNotification>(
1292 1293 1294 1295
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1296
          child: ListView(
1297 1298 1299
            primary: true,
            physics: const ScrollPhysics(),
          ),
1300 1301 1302 1303 1304 1305
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isFalse);
  });
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

  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;
1320
              } else {
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 1358 1359 1360 1361
                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
1362
    expect(tester.takeException(), isA<Exception>());
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
    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
1398
    expect(tester.takeException(), isA<Exception>());
1399 1400
    expect(finder, findsOneWidget);
  });
1401

1402
   testWidgets('ListView asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
1403 1404 1405 1406 1407 1408
    expect(() => ListView(
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

1409 1410 1411 1412 1413 1414
  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
1415
    ), throwsAssertionError);
1416 1417 1418 1419 1420 1421 1422 1423 1424
  });

  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
1425
    ), throwsAssertionError);
1426 1427 1428 1429 1430 1431 1432 1433 1434
  });

  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
1435
    ), throwsAssertionError);
1436
  });
1437

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
  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);
  });

1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
  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(
1480
      tester.element(find.byType(CustomScrollView)),
1481
    );
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    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)),
    );
  });
1503

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
  testWidgets('Fallback ScrollActions handle too many positions with error message', (WidgetTester tester)  async {
    Widget getScrollView() {
      return SizedBox(
        width: 400.0,
        child: CustomScrollView(
          primary: true,
          slivers: List<Widget>.generate(
            20,
            (int index) {
              return SliverToBoxAdapter(
                child: Focus(
                  child: SizedBox(key: ValueKey<String>('Box $index'), height: 50.0),
                ),
              );
            },
          ),
        ),
      );
    }

    await tester.pumpWidget(
      MaterialApp(
        home: Row(
          children: <Widget>[
            getScrollView(),
            getScrollView(),
          ],
        ),
      ),
    );
    await tester.pumpAndSettle();
    expect(
      tester.getRect(
        find.byKey(const ValueKey<String>('Box 0'), skipOffstage: false).first
      ),
      equals(const Rect.fromLTRB(0.0, 0.0, 400.0, 50.0)),
    );
    await tester.sendKeyEvent(LogicalKeyboardKey.pageDown);
    final AssertionError exception = tester.takeException() as AssertionError;
    expect(exception, isAssertionError);
    expect(
      exception.message,
      contains(
        'A ScrollAction was invoked with the PrimaryScrollController, but '
        'more than one ScrollPosition is attached.'
      ),
    );
  });

1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
  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);
  });
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630

  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);
  });
1631
}