grid_view_test.dart 15.9 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 6
// @dart = 2.8

7 8
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
9
import 'package:flutter/gestures.dart' show DragStartBehavior;
10

Adam Barth's avatar
Adam Barth committed
11
import '../rendering/mock_canvas.dart';
12 13 14
import 'states.dart';

void main() {
15
  testWidgets('Empty GridView', (WidgetTester tester) async {
16
    await tester.pumpWidget(
17
      Directionality(
18
        textDirection: TextDirection.ltr,
19
        child: GridView.count(
20
          dragStartBehavior: DragStartBehavior.down,
21 22 23 24 25
          crossAxisCount: 4,
          children: const <Widget>[],
        ),
      ),
    );
26 27
  });

28
  testWidgets('GridView.count control test', (WidgetTester tester) async {
29
    final List<String> log = <String>[];
30

31
    await tester.pumpWidget(
32
      Directionality(
33
        textDirection: TextDirection.ltr,
34
        child: GridView.count(
35
          dragStartBehavior: DragStartBehavior.down,
36
          crossAxisCount: 4,
37
          children: kStates.map<Widget>((String state) {
38
            return GestureDetector(
39
              dragStartBehavior: DragStartBehavior.down,
40 41 42
              onTap: () {
                log.add(state);
              },
43
              child: Container(
44
                color: const Color(0xFF0000FF),
45
                child: Text(state),
46 47 48 49 50 51
              ),
            );
          }).toList(),
        ),
      ),
    );
52 53 54 55 56 57 58 59 60 61 62 63

    expect(tester.getSize(find.text('Arkansas')), equals(const Size(200.0, 200.0)));

    for (int i = 0; i < 8; ++i) {
      await tester.tap(find.text(kStates[i]));
      expect(log, equals(<String>[kStates[i]]));
      log.clear();
    }

    expect(find.text(kStates[12]), findsNothing);
    expect(find.text('Nevada'), findsNothing);

64
    await tester.drag(find.text('Arkansas'), const Offset(0.0, -200.0));
65 66 67 68 69 70 71 72 73 74 75
    await tester.pump();

    for (int i = 0; i < 4; ++i)
      expect(find.text(kStates[i]), findsNothing);

    for (int i = 4; i < 12; ++i) {
      await tester.tap(find.text(kStates[i]));
      expect(log, equals(<String>[kStates[i]]));
      log.clear();
    }

76
    await tester.drag(find.text('Delaware'), const Offset(0.0, -4000.0));
77 78 79 80 81 82
    await tester.pump();

    expect(find.text('Alabama'), findsNothing);
    expect(find.text('Pennsylvania'), findsNothing);

    expect(tester.getCenter(find.text('Tennessee')),
83
        equals(const Offset(300.0, 100.0)));
84 85 86 87 88

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

89
    await tester.drag(find.text('Tennessee'), const Offset(0.0, 200.0));
90 91 92 93 94 95 96 97 98 99 100
    await tester.pump();

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

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

101
  testWidgets('GridView.extent control test', (WidgetTester tester) async {
102
    final List<String> log = <String>[];
103

104
    await tester.pumpWidget(
105
      Directionality(
106
        textDirection: TextDirection.ltr,
107
        child: GridView.extent(
108
          dragStartBehavior: DragStartBehavior.down,
109
          maxCrossAxisExtent: 200.0,
110
          children: kStates.map<Widget>((String state) {
111
            return GestureDetector(
112
              dragStartBehavior: DragStartBehavior.down,
113 114 115
              onTap: () {
                log.add(state);
              },
116
              child: Container(
117
                color: const Color(0xFF0000FF),
118
                child: Text(state),
119 120 121 122 123 124
              ),
            );
          }).toList(),
        ),
      ),
    );
125 126 127 128 129 130 131 132 133 134 135

    expect(tester.getSize(find.text('Arkansas')), equals(const Size(200.0, 200.0)));

    for (int i = 0; i < 8; ++i) {
      await tester.tap(find.text(kStates[i]));
      expect(log, equals(<String>[kStates[i]]));
      log.clear();
    }

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

136
    await tester.drag(find.text('Arkansas'), const Offset(0.0, -4000.0));
137 138 139 140 141
    await tester.pump();

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

    expect(tester.getCenter(find.text('Tennessee')),
142
        equals(const Offset(300.0, 100.0)));
143 144 145 146 147 148

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

149
  testWidgets('GridView large scroll jump', (WidgetTester tester) async {
150
    final List<int> log = <int>[];
151

152
    await tester.pumpWidget(
153
      Directionality(
154
        textDirection: TextDirection.ltr,
155
        child: GridView.extent(
156 157 158
          scrollDirection: Axis.horizontal,
          maxCrossAxisExtent: 200.0,
          childAspectRatio: 0.75,
159 160
          children: List<Widget>.generate(80, (int i) {
            return Builder(
161 162
              builder: (BuildContext context) {
                log.add(i);
163 164
                return Container(
                  child: Text('$i'),
165 166 167 168 169
                );
              }
            );
          }),
        ),
170
      ),
171
    );
172 173 174 175 176 177 178

    expect(tester.getSize(find.text('4')), equals(const Size(200.0 / 0.75, 200.0)));

    expect(log, equals(<int>[
      0, 1, 2, // col 0
      3, 4, 5, // col 1
      6, 7, 8, // col 2
179
      9, 10, 11, // col 3 (in cached area)
180 181 182
    ]));
    log.clear();

183 184 185 186 187 188 189
    for (int i = 0; i < 9; i++) {
      expect(find.text('$i'), findsOneWidget);
    }
    for (int i = 9; i < 80; i++) {
      expect(find.text('$i'), findsNothing);
    }

190 191
    final ScrollableState state = tester.state(find.byType(Scrollable));
    final ScrollPosition position = state.position;
192 193 194 195 196 197
    position.jumpTo(3025.0);

    expect(log, isEmpty);
    await tester.pump();

    expect(log, equals(<int>[
198
      30, 31, 32, // col 10 (in cached area)
199 200 201 202
      33, 34, 35, // col 11
      36, 37, 38, // col 12
      39, 40, 41, // col 13
      42, 43, 44, // col 14
203
      45, 46, 47, // col 15 (in cached area)
204 205 206
    ]));
    log.clear();

207 208 209 210 211 212 213 214 215 216
    for (int i = 0; i < 33; i++) {
      expect(find.text('$i'), findsNothing);
    }
    for (int i = 33; i < 45; i++) {
      expect(find.text('$i'), findsOneWidget);
    }
    for (int i = 45; i < 80; i++) {
      expect(find.text('$i'), findsNothing);
    }

217 218 219 220 221 222
    position.jumpTo(975.0);

    expect(log, isEmpty);
    await tester.pump();

    expect(log, equals(<int>[
223
      6, 7, 8, // col2 (in cached area)
224 225 226 227
      9, 10, 11, // col 3
      12, 13, 14, // col 4
      15, 16, 17, // col 5
      18, 19, 20, // col 6
228
      21, 22, 23, // col 7 (in cached area)
229 230
    ]));
    log.clear();
231 232 233 234 235 236 237 238 239 240

    for (int i = 0; i < 9; i++) {
      expect(find.text('$i'), findsNothing);
    }
    for (int i = 9; i < 21; i++) {
      expect(find.text('$i'), findsOneWidget);
    }
    for (int i = 21; i < 80; i++) {
      expect(find.text('$i'), findsNothing);
    }
241 242
  });

243
  testWidgets('GridView - change crossAxisCount', (WidgetTester tester) async {
244
    final List<int> log = <int>[];
245 246

    await tester.pumpWidget(
247
      Directionality(
248
        textDirection: TextDirection.ltr,
249
        child: GridView(
250 251 252
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
          ),
253 254
          children: List<Widget>.generate(40, (int i) {
            return Builder(
255 256
              builder: (BuildContext context) {
                log.add(i);
257 258
                return Container(
                  child: Text('$i'),
259 260 261 262
                );
              }
            );
          }),
263 264 265 266 267 268 269 270 271 272
        ),
      ),
    );

    expect(tester.getSize(find.text('4')), equals(const Size(200.0, 200.0)));

    expect(log, equals(<int>[
      0, 1, 2, 3, // row 0
      4, 5, 6, 7, // row 1
      8, 9, 10, 11, // row 2
273 274
      12, 13, 14, 15, // row 3 (in cached area)
      16, 17, 18, 19, // row 4 (in cached area)
275
    ]));
276 277 278 279 280 281
    for (int i = 0; i < 12; i++) {
      expect(find.text('$i'), findsOneWidget);
    }
    for (int i = 12; i < 40; i++) {
      expect(find.text('$i'), findsNothing);
    }
282 283 284
    log.clear();

    await tester.pumpWidget(
285
      Directionality(
286
        textDirection: TextDirection.ltr,
287
        child: GridView(
288 289 290
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
          ),
291 292
          children: List<Widget>.generate(40, (int i) {
            return Builder(
293 294
              builder: (BuildContext context) {
                log.add(i);
295 296
                return Container(
                  child: Text('$i'),
297 298 299 300
                );
              }
            );
          }),
301 302 303 304 305 306 307 308
        ),
      ),
    );

    expect(log, equals(<int>[
      0, 1, 2, 3, // row 0
      4, 5, 6, 7, // row 1
      8, 9, 10, 11, // row 2
309 310
      12, 13, 14, 15, // row 3 (in cached area)
      16, 17, 18, 19, // row 4 (in cached area)
311 312 313 314 315 316 317
    ]));
    log.clear();

    expect(tester.getSize(find.text('3')), equals(const Size(400.0, 400.0)));
    expect(find.text('4'), findsNothing);
  });

318
  testWidgets('GridView - change maxChildCrossAxisExtent', (WidgetTester tester) async {
319
    final List<int> log = <int>[];
320 321

    await tester.pumpWidget(
322
      Directionality(
323
        textDirection: TextDirection.ltr,
324
        child: GridView(
325 326 327
          gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 200.0,
          ),
328 329
          children: List<Widget>.generate(40, (int i) {
            return Builder(
330 331
              builder: (BuildContext context) {
                log.add(i);
332 333
                return Container(
                  child: Text('$i'),
334 335 336 337
                );
              }
            );
          }),
338 339 340 341 342 343 344 345 346 347
        ),
      ),
    );

    expect(tester.getSize(find.text('4')), equals(const Size(200.0, 200.0)));

    expect(log, equals(<int>[
      0, 1, 2, 3, // row 0
      4, 5, 6, 7, // row 1
      8, 9, 10, 11, // row 2
348 349
      12, 13, 14, 15, // row 3 (in cached area)
      16, 17, 18, 19, // row 4 (in cached area)
350
    ]));
351 352 353 354 355 356
    for (int i = 0; i < 12; i++) {
      expect(find.text('$i'), findsOneWidget);
    }
    for (int i = 12; i < 40; i++) {
      expect(find.text('$i'), findsNothing);
    }
357 358 359
    log.clear();

    await tester.pumpWidget(
360
      Directionality(
361
        textDirection: TextDirection.ltr,
362
        child: GridView(
363 364 365
          gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 400.0,
          ),
366 367
          children: List<Widget>.generate(40, (int i) {
            return Builder(
368 369
              builder: (BuildContext context) {
                log.add(i);
370 371
                return Container(
                  child: Text('$i'),
372 373 374 375
                );
              }
            );
          }),
376 377 378 379 380 381 382 383
        ),
      ),
    );

    expect(log, equals(<int>[
      0, 1, 2, 3, // row 0
      4, 5, 6, 7, // row 1
      8, 9, 10, 11, // row 2
384 385
      12, 13, 14, 15, // row 3 (in cached area)
      16, 17, 18, 19, // row 4 (in cached area)
386 387 388 389 390 391
    ]));
    log.clear();

    expect(tester.getSize(find.text('3')), equals(const Size(400.0, 400.0)));
    expect(find.text('4'), findsNothing);
  });
392

Adam Barth's avatar
Adam Barth committed
393
  testWidgets('One-line GridView paints', (WidgetTester tester) async {
394
    const Color green = Color(0xFF00FF00);
Adam Barth's avatar
Adam Barth committed
395

396
    final Container container = Container(
Adam Barth's avatar
Adam Barth committed
397
      decoration: const BoxDecoration(
398
        color: green,
Adam Barth's avatar
Adam Barth committed
399 400 401
      ),
    );

402
    await tester.pumpWidget(
403
      Directionality(
404
        textDirection: TextDirection.ltr,
405 406
        child: Center(
          child: SizedBox(
407
            height: 200.0,
408
            child: GridView.count(
409
              cacheExtent: 0.0,
410 411 412 413
              crossAxisCount: 2,
              children: <Widget>[ container, container, container, container ],
            ),
          ),
Adam Barth's avatar
Adam Barth committed
414 415
        ),
      ),
416
    );
Adam Barth's avatar
Adam Barth committed
417 418 419 420 421

    expect(find.byType(GridView), paints..rect(color: green)..rect(color: green));
    expect(find.byType(GridView), isNot(paints..rect(color: green)..rect(color: green)..rect(color: green)));
  });

422 423
  testWidgets('GridView in zero context', (WidgetTester tester) async {
    await tester.pumpWidget(
424
      Directionality(
425
        textDirection: TextDirection.ltr,
426 427
        child: Center(
          child: SizedBox(
428 429
            width: 0.0,
            height: 0.0,
430
            child: GridView.count(
431
              crossAxisCount: 4,
432 433 434
              children: List<Widget>.generate(20, (int i) {
                return Container(
                  child: Text('$i'),
435 436 437 438 439 440 441 442
                );
              }),
            ),
          ),
        ),
      ),
    );

443
    expect(find.text('0'), findsNothing);
444 445 446 447 448
    expect(find.text('1'), findsNothing);
  });

  testWidgets('GridView in unbounded context', (WidgetTester tester) async {
    await tester.pumpWidget(
449
      Directionality(
450
        textDirection: TextDirection.ltr,
451 452
        child: SingleChildScrollView(
          child: GridView.count(
453
            crossAxisCount: 4,
454
            shrinkWrap: true,
455 456 457
            children: List<Widget>.generate(20, (int i) {
              return Container(
                child: Text('$i'),
458 459 460 461 462 463 464 465
              );
            }),
          ),
        ),
      ),
    );

    expect(find.text('0'), findsOneWidget);
466
    expect(find.text('19'), findsOneWidget);
467 468
  });

469
  testWidgets('GridView.builder control test', (WidgetTester tester) async {
470
    await tester.pumpWidget(
471
      Directionality(
472
        textDirection: TextDirection.ltr,
473
        child: GridView.builder(
474 475 476
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
          ),
477
          shrinkWrap: true,
478 479
          itemCount: 20,
          itemBuilder: (BuildContext context, int index) {
480 481
            return Container(
              child: Text('$index'),
482
            );
483
          },
484 485 486
        ),
      ),
    );
487 488 489 490 491
    expect(find.text('0'), findsOneWidget);
    expect(find.text('11'), findsOneWidget);
    expect(find.text('12'), findsNothing);
  });

492 493
  testWidgets('GridView.builder with undefined itemCount', (WidgetTester tester) async {
    await tester.pumpWidget(
494
      Directionality(
495
        textDirection: TextDirection.ltr,
496
        child: GridView.builder(
497 498 499 500 501
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
          ),
          shrinkWrap: true,
          itemBuilder: (BuildContext context, int index) {
502 503
            return Container(
              child: Text('$index'),
504 505 506 507 508 509 510 511 512 513 514 515
            );
          },
        ),
      ),
    );
    expect(find.text('0'), findsOneWidget);
    expect(find.text('11'), findsOneWidget);
    await tester.drag(find.byType(GridView), const Offset(0.0, -300.0));
    await tester.pump(const Duration(milliseconds: 200));
    expect(find.text('13'), findsOneWidget);
  });

516
  testWidgets('GridView cross axis layout', (WidgetTester tester) async {
517
    final Key target = UniqueKey();
518 519

    Widget build(TextDirection textDirection) {
520
      return Directionality(
521
        textDirection: textDirection,
522
        child: GridView.count(
523 524
          crossAxisCount: 4,
          children: <Widget>[
525
            Container(key: target),
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
          ],
        ),
      );
    }

    await tester.pumpWidget(build(TextDirection.ltr));

    expect(tester.getTopLeft(find.byKey(target)), Offset.zero);
    expect(tester.getBottomRight(find.byKey(target)), const Offset(200.0, 200.0));

    await tester.pumpWidget(build(TextDirection.rtl));

    expect(tester.getTopLeft(find.byKey(target)), const Offset(600.0, 0.0));
    expect(tester.getBottomRight(find.byKey(target)), const Offset(800.0, 200.0));
  });
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

  testWidgets('GridView crossAxisSpacing', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/27151.
    final Key target = UniqueKey();

    Widget build(TextDirection textDirection) {
      return Directionality(
        textDirection: textDirection,
        child: GridView.count(
          crossAxisCount: 4,
          crossAxisSpacing: 8.0,
          children: <Widget>[
            Container(key: target),
          ],
        ),
      );
    }

    await tester.pumpWidget(build(TextDirection.ltr));

    expect(tester.getTopLeft(find.byKey(target)), Offset.zero);
    expect(tester.getBottomRight(find.byKey(target)), const Offset(194.0, 194.0));

    await tester.pumpWidget(build(TextDirection.rtl));

    expect(tester.getTopLeft(find.byKey(target)), const Offset(606.0, 0.0));
    expect(tester.getBottomRight(find.byKey(target)), const Offset(800.0, 194.0));
  });
569
}