scroll_view_test.dart 52.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
  });

  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(
151
        padding: EdgeInsets.zero,
152 153 154 155 156 157 158 159 160 161 162
        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,
163
              ),
164 165 166 167 168 169 170 171 172
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
177
    expect(textField.focusNode!.hasFocus, isFalse);
178 179 180 181 182 183 184
  });

  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(
185
        padding: EdgeInsets.zero,
186 187 188 189 190 191 192 193 194 195 196
        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,
197
                ),
198 199 200 201 202 203 204 205 206 207 208
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
213
    expect(textField.focusNode!.hasFocus, isFalse);
214 215 216 217 218 219 220
  });

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

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
248
    expect(textField.focusNode!.hasFocus, isFalse);
249 250 251 252 253 254 255
  });

  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(
256
        padding: EdgeInsets.zero,
257 258 259 260 261 262 263 264 265 266 267
        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,
268
              ),
269 270 271 272 273 274 275 276 277
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
282
    expect(textField.focusNode!.hasFocus, isFalse);
283 284 285 286 287 288 289
  });

  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(
290
        padding: EdgeInsets.zero,
291 292 293
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
        itemCount: focusNodes.length,
294
        itemBuilder: (BuildContext context, int index) {
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
          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);
313
    expect(textField.focusNode!.hasFocus, isTrue);
314 315 316

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
317
    expect(textField.focusNode!.hasFocus, isFalse);
318 319 320 321 322 323 324
  });

  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(
325
        padding: EdgeInsets.zero,
326 327 328 329 330 331 332 333 334 335 336
        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,
337
              ),
338 339 340 341 342 343 344 345 346
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
351
    expect(textField.focusNode!.hasFocus, isFalse);
352 353 354 355 356 357 358
  });

  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(
359
        padding: EdgeInsets.zero,
360 361 362 363 364 365 366 367 368 369 370
        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,
371
              ),
372 373 374 375 376 377 378 379 380
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
385
    expect(textField.focusNode!.hasFocus, isFalse);
386 387 388 389 390 391 392
  });

  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(
393
        padding: EdgeInsets.zero,
394 395 396 397 398 399 400 401 402 403 404 405
        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,
406
                ),
407 408 409 410 411 412 413
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));
414 415 416 417

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
422
    expect(textField.focusNode!.hasFocus, isFalse);
423 424 425 426 427 428
  });

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

    await tester.pumpWidget(textFieldBoilerplate(
429 430 431 432 433 434 435
      child: ListView(
        padding: EdgeInsets.zero,
        children: focusNodes.map((FocusNode focusNode) {
          return Container(
            height: 50,
            color: Colors.green,
            child: TextField(
436 437 438 439
              focusNode: focusNode,
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
440 441 442 443 444 445
              ),
            ),
          );
        }).toList(),
      ),
    ));
446 447 448 449

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

    await tester.drag(finder, const Offset(0.0, -40.0));
453
    await tester.pumpAndSettle();
454
    expect(textField.focusNode!.hasFocus, isTrue);
455 456 457 458 459 460 461
  });

  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(
462
        padding: EdgeInsets.zero,
463 464 465 466 467 468 469 470 471 472
        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,
473
              ),
474 475 476 477 478 479 480 481 482
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
487
    expect(textField.focusNode!.hasFocus, isTrue);
488 489 490 491 492 493 494
  });

  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(
495
        padding: EdgeInsets.zero,
496 497 498 499 500 501 502 503 504 505
        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,
506
                ),
507 508 509 510 511 512 513 514 515 516 517
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
522
    expect(textField.focusNode!.hasFocus, isTrue);
523 524 525 526 527 528 529
  });

  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(
530
        padding: EdgeInsets.zero,
531 532 533 534 535 536 537 538 539 540 541
        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,
542
              ),
543 544 545 546 547 548 549 550 551
            ),
          );
        },
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
556
    expect(textField.focusNode!.hasFocus, isTrue);
557 558 559 560 561 562 563
  });

  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(
564
        padding: EdgeInsets.zero,
565 566 567 568 569 570 571 572 573 574
        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,
575
              ),
576 577 578 579 580 581 582 583 584
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
589
    expect(textField.focusNode!.hasFocus, isTrue);
590 591 592 593 594 595 596
  });

  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(
597
        padding: EdgeInsets.zero,
598 599
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
        itemCount: focusNodes.length,
600
        itemBuilder: (BuildContext context, int index) {
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
          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);
619
    expect(textField.focusNode!.hasFocus, isTrue);
620 621 622

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
623
    expect(textField.focusNode!.hasFocus, isTrue);
624 625 626 627 628 629 630
  });

  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(
631
        padding: EdgeInsets.zero,
632 633 634 635 636 637 638 639 640 641
        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,
642
              ),
643 644 645 646 647 648 649 650 651
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
656
    expect(textField.focusNode!.hasFocus, isTrue);
657 658 659 660 661 662 663
  });

  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(
664
        padding: EdgeInsets.zero,
665 666 667 668 669 670 671 672 673 674
        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,
675
              ),
676 677 678 679 680 681 682 683 684
            ),
          );
        }).toList(),
      ),
    ));

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

    await tester.drag(finder, const Offset(0.0, -40.0));
    await tester.pumpAndSettle();
689
    expect(textField.focusNode!.hasFocus, isTrue);
690 691 692 693 694 695 696
  });

  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(
697
        padding: EdgeInsets.zero,
698 699 700 701 702 703 704 705 706 707 708
        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,
709
                ),
710 711 712 713 714 715 716 717 718 719 720
              ),
            );
          },
          childCount: focusNodes.length,
        ),
      ),
    ));

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

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

728 729
  testWidgets('ListView restart ballistic activity out of range', (WidgetTester tester) async {
    Widget buildListView(int n) {
730
      return Directionality(
731
        textDirection: TextDirection.ltr,
732
        child: ListView(
733
          dragStartBehavior: DragStartBehavior.down,
734
          children: kStates.take(n).map<Widget>((String state) {
735
            return Container(
736 737
              height: 200.0,
              color: const Color(0xFF0000FF),
738
              child: Text(state),
739 740 741
            );
          }).toList(),
        ),
742 743 744
      );
    }

745 746 747
    await tester.pumpWidget(buildListView(30));
    await tester.fling(find.byType(ListView), const Offset(0.0, -4000.0), 4000.0);
    await tester.pumpWidget(buildListView(15));
748 749 750 751
    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));
752
    await tester.pumpAndSettle();
753

754
    final Viewport viewport = tester.widget(find.byType(Viewport));
755 756
    expect(viewport.offset.pixels, equals(2400.0));
  });
Adam Barth's avatar
Adam Barth committed
757 758

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

761
    await tester.pumpWidget(
762
      Directionality(
763
        textDirection: TextDirection.ltr,
764
        child: CustomScrollView(
765
          dragStartBehavior: DragStartBehavior.down,
766
          slivers: <Widget>[
767 768
            SliverList(
              delegate: SliverChildListDelegate(
769
                kStates.map<Widget>((String state) {
770
                  return GestureDetector(
771
                    dragStartBehavior: DragStartBehavior.down,
772 773 774
                    onTap: () {
                      log.add(state);
                    },
775
                    child: Container(
776 777
                      height: 200.0,
                      color: const Color(0xFF0000FF),
778
                      child: Text(state),
779 780 781 782 783 784
                    ),
                  );
                }).toList(),
              ),
            ),
          ],
Adam Barth's avatar
Adam Barth committed
785
        ),
786 787
      ),
    );
Adam Barth's avatar
Adam Barth committed
788 789 790 791 792 793 794

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

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

795
    await tester.drag(find.text('Alabama'), const Offset(0.0, -4000.0));
Adam Barth's avatar
Adam Barth committed
796 797 798
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
799
    expect(tester.getCenter(find.text('Massachusetts')), equals(const Offset(400.0, 100.0)));
Adam Barth's avatar
Adam Barth committed
800 801 802 803 804

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

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
  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,
825
                    ),
826 827 828 829 830 831 832 833 834 835 836 837
                  ),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    ));

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

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

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

849
    await tester.pumpWidget(
850
      Directionality(
851
        textDirection: TextDirection.ltr,
852
        child: NotificationListener<ScrollNotification>(
853 854 855 856
          onNotification: (ScrollNotification notification) {
            log.add(notification.runtimeType);
            return false;
          },
857
          child: ListView(
858 859
            controller: controller,
            children: kStates.map<Widget>((String state) {
860
              return SizedBox(
861
                height: 200.0,
862
                child: Text(state),
863 864 865 866
              );
            }).toList(),
          ),
        ),
867
      ),
868
    );
869 870 871

    expect(log, isEmpty);

872
    final TestGesture gesture = await tester.startGesture(const Offset(100.0, 100.0));
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
    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);
  });
902

903 904 905 906 907 908 909 910 911 912 913 914
  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 {
915
    const CustomScrollView view = CustomScrollView();
916
    expect(view.primary, isNull);
917 918
  });

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
  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 {
938
    final ListView view = ListView();
939
    expect(view.primary, isNull);
940 941
  });

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
  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);
963 964
  });

965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
  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());

983
  testWidgets('Horizontal CustomScrollViews are non-primary by default', (WidgetTester tester) async {
984 985 986 987 988 989 990 991 992
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: CustomScrollView(
        scrollDirection: Axis.horizontal,
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
993 994 995
  });

  testWidgets('Horizontal ListViews are non-primary by default', (WidgetTester tester) async {
996 997 998 999 1000 1001 1002 1003 1004
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: ListView(
        scrollDirection: Axis.horizontal,
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1005 1006 1007
  });

  testWidgets('Horizontal GridViews are non-primary by default', (WidgetTester tester) async {
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: GridView.count(
        scrollDirection: Axis.horizontal,
        controller: ScrollController(),
        crossAxisCount: 1,
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1018 1019 1020
  });

  testWidgets('CustomScrollViews with controllers are non-primary by default', (WidgetTester tester) async {
1021 1022 1023 1024 1025 1026 1027 1028
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: CustomScrollView(
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1029 1030 1031
  });

  testWidgets('ListViews with controllers are non-primary by default', (WidgetTester tester) async {
1032 1033 1034 1035 1036 1037 1038 1039
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: ListView(
        controller: ScrollController(),
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1040 1041 1042
  });

  testWidgets('GridViews with controllers are non-primary by default', (WidgetTester tester) async {
1043 1044 1045 1046 1047 1048 1049 1050 1051
    final ScrollController controller = ScrollController();
    await tester.pumpWidget(primaryScrollControllerBoilerplate(
      child: GridView.count(
        controller: ScrollController(),
        crossAxisCount: 1,
      ),
      controller: controller,
    ));
    expect(controller.hasClients, isFalse);
1052 1053
  });

1054
  testWidgets('CustomScrollView sets PrimaryScrollController when primary', (WidgetTester tester) async {
1055
    final ScrollController primaryScrollController = ScrollController();
1056
    await tester.pumpWidget(
1057
      Directionality(
1058
        textDirection: TextDirection.ltr,
1059
        child: PrimaryScrollController(
1060
          controller: primaryScrollController,
1061
          child: const CustomScrollView(primary: true),
1062 1063 1064
        ),
      ),
    );
1065
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
1066 1067 1068 1069
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('ListView sets PrimaryScrollController when primary', (WidgetTester tester) async {
1070
    final ScrollController primaryScrollController = ScrollController();
1071
    await tester.pumpWidget(
1072
      Directionality(
1073
        textDirection: TextDirection.ltr,
1074
        child: PrimaryScrollController(
1075
          controller: primaryScrollController,
1076
          child: ListView(primary: true),
1077 1078 1079
        ),
      ),
    );
1080
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
1081 1082 1083 1084
    expect(scrollable.controller, primaryScrollController);
  });

  testWidgets('GridView sets PrimaryScrollController when primary', (WidgetTester tester) async {
1085
    final ScrollController primaryScrollController = ScrollController();
1086
    await tester.pumpWidget(
1087
      Directionality(
1088
        textDirection: TextDirection.ltr,
1089
        child: PrimaryScrollController(
1090
          controller: primaryScrollController,
1091
          child: GridView.count(primary: true, crossAxisCount: 1),
1092 1093 1094
        ),
      ),
    );
1095
    final Scrollable scrollable = tester.widget(find.byType(Scrollable));
1096 1097
    expect(scrollable.controller, primaryScrollController);
  });
1098 1099

  testWidgets('Nested scrollables have a null PrimaryScrollController', (WidgetTester tester) async {
1100
    const Key innerKey = Key('inner');
1101
    final ScrollController primaryScrollController = ScrollController();
1102
    await tester.pumpWidget(
1103
      Directionality(
1104
        textDirection: TextDirection.ltr,
1105
        child: PrimaryScrollController(
1106
          controller: primaryScrollController,
1107
          child: ListView(
1108 1109
            primary: true,
            children: <Widget>[
1110
              Container(
1111
                constraints: const BoxConstraints(maxHeight: 200.0),
1112
                child: ListView(key: innerKey, primary: true),
1113 1114
              ),
            ],
1115
          ),
1116
        ),
1117
      ),
1118
    );
1119

1120
    final Scrollable innerScrollable = tester.widget(
1121 1122 1123 1124 1125 1126 1127
      find.descendant(
        of: find.byKey(innerKey),
        matching: find.byType(Scrollable),
      ),
    );
    expect(innerScrollable.controller, isNull);
  });
1128 1129

  testWidgets('Primary ListViews are always scrollable', (WidgetTester tester) async {
1130
    final ListView view = ListView(primary: true);
Dan Field's avatar
Dan Field committed
1131
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1132 1133 1134
  });

  testWidgets('Non-primary ListViews are not always scrollable', (WidgetTester tester) async {
1135
    final ListView view = ListView(primary: false);
Dan Field's avatar
Dan Field committed
1136
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1137 1138 1139
  });

  testWidgets('Defaulting-to-primary ListViews are always scrollable', (WidgetTester tester) async {
1140
    final ListView view = ListView();
Dan Field's avatar
Dan Field committed
1141
    expect(view.physics, isA<AlwaysScrollableScrollPhysics>());
1142 1143 1144
  });

  testWidgets('Defaulting-to-not-primary ListViews are not always scrollable', (WidgetTester tester) async {
1145
    final ListView view = ListView(scrollDirection: Axis.horizontal);
Dan Field's avatar
Dan Field committed
1146
    expect(view.physics, isNot(isA<AlwaysScrollableScrollPhysics>()));
1147 1148 1149 1150 1151
  });

  testWidgets('primary:true leads to scrolling', (WidgetTester tester) async {
    bool scrolled = false;
    await tester.pumpWidget(
1152
      Directionality(
1153
        textDirection: TextDirection.ltr,
1154
        child: NotificationListener<OverscrollNotification>(
1155 1156 1157 1158
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1159
          child: ListView(
1160 1161
            primary: true,
          ),
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
        ),
      ),
    );
    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(
1172
      Directionality(
1173
        textDirection: TextDirection.ltr,
1174
        child: NotificationListener<OverscrollNotification>(
1175 1176 1177 1178
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1179
          child: ListView(
1180 1181
            primary: false,
          ),
1182 1183 1184 1185 1186 1187 1188
        ),
      ),
    );
    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
1189
  testWidgets('physics:AlwaysScrollableScrollPhysics actually overrides primary:false default behavior', (WidgetTester tester) async {
1190 1191
    bool scrolled = false;
    await tester.pumpWidget(
1192
      Directionality(
1193
        textDirection: TextDirection.ltr,
1194
        child: NotificationListener<OverscrollNotification>(
1195 1196 1197 1198
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1199
          child: ListView(
1200 1201 1202
            primary: false,
            physics: const AlwaysScrollableScrollPhysics(),
          ),
1203 1204 1205 1206 1207 1208 1209
        ),
      ),
    );
    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
1210
  testWidgets('physics:ScrollPhysics actually overrides primary:true default behavior', (WidgetTester tester) async {
1211 1212
    bool scrolled = false;
    await tester.pumpWidget(
1213
      Directionality(
1214
        textDirection: TextDirection.ltr,
1215
        child: NotificationListener<OverscrollNotification>(
1216 1217 1218 1219
          onNotification: (OverscrollNotification message) {
            scrolled = true;
            return false;
          },
1220
          child: ListView(
1221 1222 1223
            primary: true,
            physics: const ScrollPhysics(),
          ),
1224 1225 1226 1227 1228 1229
        ),
      ),
    );
    await tester.dragFrom(const Offset(100.0, 100.0), const Offset(0.0, 100.0));
    expect(scrolled, isFalse);
  });
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

  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;
1244
              } else {
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
                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
1286
    expect(tester.takeException(), isA<Exception>());
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
    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
1322
    expect(tester.takeException(), isA<Exception>());
1323 1324
    expect(finder, findsOneWidget);
  });
1325

1326
   testWidgets('ListView asserts on both non-null itemExtent and prototypeItem', (WidgetTester tester) async {
1327 1328 1329 1330 1331 1332
    expect(() => ListView(
      itemExtent: 100,
      prototypeItem: const SizedBox(),
    ), throwsAssertionError);
  });

1333 1334 1335 1336 1337 1338
  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
1339
    ), throwsAssertionError);
1340 1341 1342 1343 1344 1345 1346 1347 1348
  });

  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
1349
    ), throwsAssertionError);
1350 1351 1352 1353 1354 1355 1356 1357 1358
  });

  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
1359
    ), throwsAssertionError);
1360
  });
1361

1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
  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);
  });

1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
  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(
1404
      tester.element(find.byType(CustomScrollView)),
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
    )!;
    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)),
    );
  });
1427

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
  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.'
      ),
    );
  });

1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
  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);
  });
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 1553 1554

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