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

import 'package:flutter_test/flutter_test.dart';
6
import 'package:flutter/widgets.dart';
7
import 'package:flutter/gestures.dart' show DragStartBehavior;
8
import 'package:flutter/material.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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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;
}

Widget textFieldBoilerplate({ Widget child }) {
  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
              child: Container(
72 73
                height: 200.0,
                color: const Color(0xFF0000FF),
74
                child: Text(state),
75
              ),
76
              dragStartBehavior: DragStartBehavior.down,
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  testWidgets('ListView dismiss keyboard onDrag test', (WidgetTester tester) async {
    final List<FocusNode> focusNodes = List<FocusNode>.generate(50, (int i) => FocusNode());

    await tester.pumpWidget(textFieldBoilerplate(
        child: ListView(
      padding: const EdgeInsets.all(0),
      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,
              )),
        );
      }).toList(),
    )));

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

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

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

    await tester.pumpWidget(textFieldBoilerplate(
        child: ListView(
      padding: const EdgeInsets.all(0),
      keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.manual,
      children: focusNodes.map((FocusNode focusNode) {
        return Container(
          height: 50,
          color: Colors.green,
          child: TextField(
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              )),
        );
      }).toList(),
    )));

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

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

162 163
  testWidgets('ListView restart ballistic activity out of range', (WidgetTester tester) async {
    Widget buildListView(int n) {
164
      return Directionality(
165
        textDirection: TextDirection.ltr,
166
        child: ListView(
167
          dragStartBehavior: DragStartBehavior.down,
168
          children: kStates.take(n).map<Widget>((String state) {
169
            return Container(
170 171
              height: 200.0,
              color: const Color(0xFF0000FF),
172
              child: Text(state),
173 174 175
            );
          }).toList(),
        ),
176 177 178
      );
    }

179 180 181
    await tester.pumpWidget(buildListView(30));
    await tester.fling(find.byType(ListView), const Offset(0.0, -4000.0), 4000.0);
    await tester.pumpWidget(buildListView(15));
182 183 184 185
    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));
186
    await tester.pumpAndSettle(const Duration(milliseconds: 100));
187

188
    final Viewport viewport = tester.widget(find.byType(Viewport));
189 190
    expect(viewport.offset.pixels, equals(2400.0));
  });
Adam Barth's avatar
Adam Barth committed
191 192

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

195
    await tester.pumpWidget(
196
      Directionality(
197
        textDirection: TextDirection.ltr,
198
        child: CustomScrollView(
199
          dragStartBehavior: DragStartBehavior.down,
200
          slivers: <Widget>[
201 202
            SliverList(
              delegate: SliverChildListDelegate(
203
                kStates.map<Widget>((String state) {
204
                  return GestureDetector(
205
                    dragStartBehavior: DragStartBehavior.down,
206 207 208
                    onTap: () {
                      log.add(state);
                    },
209
                    child: Container(
210 211
                      height: 200.0,
                      color: const Color(0xFF0000FF),
212
                      child: Text(state),
213 214 215 216 217 218
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
Adam Barth's avatar
Adam Barth committed
219
        ),
220 221
      ),
    );
Adam Barth's avatar
Adam Barth committed
222 223 224 225 226 227 228

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

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

229
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
Adam Barth's avatar
Adam Barth committed
230 231 232
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
233
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
Adam Barth's avatar
Adam Barth committed
234 235 236 237 238

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

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

244
    await tester.pumpWidget(
245
      Directionality(
246
        textDirection: TextDirection.ltr,
247
        child: NotificationListener<ScrollNotification>(
248 249 250 251
          onNotification: (ScrollNotification notification) {
            log.add(notification.runtimeType);
            return false;
          },
252
          child: ListView(
253 254
            controller: controller,
            children: kStates.map<Widget>((String state) {
255
              return Container(
256
                height: 200.0,
257
                child: Text(state),
258 259 260 261
              );
            }).toList(),
          ),
        ),
262
      ),
263
    );
264 265 266

    expect(log, isEmpty);

267
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    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);
  });
297

298
  testWidgets('Vertical CustomScrollViews are primary by default', (WidgetTester tester) async {
299
    const CustomScrollView view = CustomScrollView(scrollDirection: Axis.vertical);
300 301 302 303
    expect(view.primary, isTrue);
  });

  testWidgets('Vertical ListViews are primary by default', (WidgetTester tester) async {
304
    final ListView view = ListView(scrollDirection: Axis.vertical);
305 306 307 308
    expect(view.primary, isTrue);
  });

  testWidgets('Vertical GridViews are primary by default', (WidgetTester tester) async {
309
    final GridView view = GridView.count(
310 311 312 313 314 315 316
      scrollDirection: Axis.vertical,
      crossAxisCount: 1,
    );
    expect(view.primary, isTrue);
  });

  testWidgets('Horizontal CustomScrollViews are non-primary by default', (WidgetTester tester) async {
317
    const CustomScrollView view = CustomScrollView(scrollDirection: Axis.horizontal);
318 319 320 321
    expect(view.primary, isFalse);
  });

  testWidgets('Horizontal ListViews are non-primary by default', (WidgetTester tester) async {
322
    final ListView view = ListView(scrollDirection: Axis.horizontal);
323 324 325 326
    expect(view.primary, isFalse);
  });

  testWidgets('Horizontal GridViews are non-primary by default', (WidgetTester tester) async {
327
    final GridView view = GridView.count(
328 329 330 331 332 333 334
      scrollDirection: Axis.horizontal,
      crossAxisCount: 1,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('CustomScrollViews with controllers are non-primary by default', (WidgetTester tester) async {
335 336
    final CustomScrollView view = CustomScrollView(
      controller: ScrollController(),
337 338 339 340 341 342
      scrollDirection: Axis.vertical,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('ListViews with controllers are non-primary by default', (WidgetTester tester) async {
343 344
    final ListView view = ListView(
      controller: ScrollController(),
345 346 347 348 349 350
      scrollDirection: Axis.vertical,
    );
    expect(view.primary, isFalse);
  });

  testWidgets('GridViews with controllers are non-primary by default', (WidgetTester tester) async {
351 352
    final GridView view = GridView.count(
      controller: ScrollController(),
353 354 355 356 357 358
      scrollDirection: Axis.vertical,
      crossAxisCount: 1,
    );
    expect(view.primary, isFalse);
  });

359
  testWidgets('CustomScrollView sets PrimaryScrollController when primary', (WidgetTester tester) async {
360
    final ScrollController primaryScrollController = ScrollController();
361
    await tester.pumpWidget(
362
      Directionality(
363
        textDirection: TextDirection.ltr,
364
        child: PrimaryScrollController(
365
          controller: primaryScrollController,
366
          child: const CustomScrollView(primary: true),
367 368 369
        ),
      ),
    );
370
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
371 372 373 374
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('ListView sets PrimaryScrollController when primary', (WidgetTester tester) async {
375
    final ScrollController primaryScrollController = ScrollController();
376
    await tester.pumpWidget(
377
      Directionality(
378
        textDirection: TextDirection.ltr,
379
        child: PrimaryScrollController(
380
          controller: primaryScrollController,
381
          child: ListView(primary: true),
382 383 384
        ),
      ),
    );
385
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
386 387 388 389
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('GridView sets PrimaryScrollController when primary', (WidgetTester tester) async {
390
    final ScrollController primaryScrollController = ScrollController();
391
    await tester.pumpWidget(
392
      Directionality(
393
        textDirection: TextDirection.ltr,
394
        child: PrimaryScrollController(
395
          controller: primaryScrollController,
396
          child: GridView.count(primary: true, crossAxisCount: 1),
397 398 399
        ),
      ),
    );
400
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
401 402
    expect(scrollable.controller, primaryScrollController);
  });
403 404

  testWidgets('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
405
    const Key innerKey = Key('inner');
406
    final ScrollController primaryScrollController = ScrollController();
407
    await tester.pumpWidget(
408
      Directionality(
409
        textDirection: TextDirection.ltr,
410
        child: PrimaryScrollController(
411
          controller: primaryScrollController,
412
          child: ListView(
413 414
            primary: true,
            children: <Widget>[
415
              Container(
416
                constraints: const BoxConstraints(maxHeight: 200.0),
417
                child: ListView(key: innerKey, primary: true),
418 419
              ),
            ],
420
          ),
421
        ),
422
      ),
423
    );
424

425
    final Scrollable innerScrollable = tester.widget(
426 427 428 429 430 431 432
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
433 434

  testWidgets('Primary ListViews are always scrollable', (WidgetTester tester) async {
435
    final ListView view = ListView(primary: true);
Dan Field's avatar
Dan Field committed
436
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
437 438 439
  });

  testWidgets('Non-primary ListViews are not always scrollable', (WidgetTester tester) async {
440
    final ListView view = ListView(primary: false);
Dan Field's avatar
Dan Field committed
441
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
442 443 444
  });

  testWidgets('Defaulting-to-primary ListViews are always scrollable', (WidgetTester tester) async {
445
    final ListView view = ListView(scrollDirection: Axis.vertical);
Dan Field's avatar
Dan Field committed
446
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
447 448 449
  });

  testWidgets('Defaulting-to-not-primary ListViews are not always scrollable', (WidgetTester tester) async {
450
    final ListView view = ListView(scrollDirection: Axis.horizontal);
Dan Field's avatar
Dan Field committed
451
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
452 453 454 455 456
  });

  testWidgets('primary:true leads to scrolling', (WidgetTester tester) async {
    bool scrolled = false;
    await tester.pumpWidget(
457
      Directionality(
458
        textDirection: TextDirection.ltr,
459
        child: NotificationListener<OverscrollNotification>(
460 461 462 463
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
464
          child: ListView(
465
            primary: true,
466
            children: const <Widget>[],
467
          ),
468 469 470 471 472 473 474 475 476 477
        ),
      ),
    );
    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(
478
      Directionality(
479
        textDirection: TextDirection.ltr,
480
        child: NotificationListener<OverscrollNotification>(
481 482 483 484
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
485
          child: ListView(
486
            primary: false,
487
            children: const <Widget>[],
488
          ),
489 490 491 492 493 494 495
        ),
      ),
    );
    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
496
  testWidgets('physics:AlwaysScrollableScrollPhysics actually overrides primary:false default behavior', (WidgetTester tester) async {
497 498
    bool scrolled = false;
    await tester.pumpWidget(
499
      Directionality(
500
        textDirection: TextDirection.ltr,
501
        child: NotificationListener<OverscrollNotification>(
502 503 504 505
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
506
          child: ListView(
507 508
            primary: false,
            physics: const AlwaysScrollableScrollPhysics(),
509
            children: const <Widget>[],
510
          ),
511 512 513 514 515 516 517
        ),
      ),
    );
    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
518
  testWidgets('physics:ScrollPhysics actually overrides primary:true default behavior', (WidgetTester tester) async {
519 520
    bool scrolled = false;
    await tester.pumpWidget(
521
      Directionality(
522
        textDirection: TextDirection.ltr,
523
        child: NotificationListener<OverscrollNotification>(
524 525 526 527
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
528
          child: ListView(
529 530
            primary: true,
            physics: const ScrollPhysics(),
531
            children: const <Widget>[],
532
          ),
533 534 535 536 537 538
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isFalse);
  });
539 540 541 542 543 544 545 546 547 548 549 550 551 552

  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;
553
              } else {
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
                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);

    // A separatorBuilder that returns null throws a FlutterError
    await tester.pumpWidget(buildFrame(null));
Dan Field's avatar
Dan Field committed
569
    expect(tester.takeException(), isFlutterError);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    expect(find.byType(ErrorWidget), findsOneWidget);
  });

  testWidgets('itemBuilder can return null', (WidgetTester tester) async {
    const List<String> listOfValues = <String>['ALPHA', 'BETA', 'GAMMA', 'DELTA'];
    const Key key = Key('list');
    const int RENDER_NULL_AT = 2; // only render the first 2 values

    Widget buildFrame() {
      return MaterialApp(
        home: Material(
          child: ListView.builder(
            key: key,
            itemBuilder: (BuildContext context, int index) {
              if (index == RENDER_NULL_AT) {
                return null;
              }
              return Text(listOfValues[index]);
            },
            itemCount: listOfValues.length,
          ),
        ),
      );
    }

    // The length of a list is itemCount or the index of the first itemBuilder
    // that returns null, whichever is smaller
    await tester.pumpWidget(buildFrame());
    expect(tester.takeException(), isNull);
    expect(find.byType(ErrorWidget), findsNothing);
    expect(find.byType(Text), findsNWidgets(RENDER_NULL_AT));
  });

  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
630
    expect(tester.takeException(), isA<Exception>());
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    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
666
    expect(tester.takeException(), isA<Exception>());
667 668
    expect(finder, findsOneWidget);
  });
669 670 671 672 673 674 675

  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
676
    ), throwsAssertionError);
677 678 679 680 681 682 683 684 685
  });

  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
686
    ), throwsAssertionError);
687 688 689 690 691 692 693 694 695
  });

  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
696
    ), throwsAssertionError);
697
  });
698
}