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 5 6
// 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';
import 'package:flutter/widgets.dart';
7
import 'package:flutter/gestures.dart' show DragStartBehavior;
8

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

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

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

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

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

62
    await tester.drag(find.text('Arkansas'), const Offset(0.0, -200.0));
63 64 65 66 67 68 69 70 71 72 73
    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();
    }

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

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

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

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

87
    await tester.drag(find.text('Tennessee'), const Offset(0.0, 200.0));
88 89 90 91 92 93 94 95 96 97 98
    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();
  });

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

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

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

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

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

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

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

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

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

    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
177
      9, 10, 11, // col 3 (in cached area)
178 179 180
    ]));
    log.clear();

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

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

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

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

205 206 207 208 209 210 211 212 213 214
    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);
    }

215 216 217 218 219 220
    position.jumpTo(975.0);

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

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

    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);
    }
239 240
  });

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

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

    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
271 272
      12, 13, 14, 15, // row 3 (in cached area)
      16, 17, 18, 19, // row 4 (in cached area)
273
    ]));
274 275 276 277 278 279
    for (int i = 0; i < 12; i++) {
      expect(find.text('$i'), findsOneWidget);
    }
    for (int i = 12; i < 40; i++) {
      expect(find.text('$i'), findsNothing);
    }
280 281 282
    log.clear();

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

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

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

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

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

    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
346 347
      12, 13, 14, 15, // row 3 (in cached area)
      16, 17, 18, 19, // row 4 (in cached area)
348
    ]));
349 350 351 352 353 354
    for (int i = 0; i < 12; i++) {
      expect(find.text('$i'), findsOneWidget);
    }
    for (int i = 12; i < 40; i++) {
      expect(find.text('$i'), findsNothing);
    }
355 356 357
    log.clear();

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

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

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

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

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

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

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

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

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

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

    expect(find.text('0'), findsOneWidget);
464
    expect(find.text('19'), findsOneWidget);
465 466
  });

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

490 491
  testWidgets('GridView.builder with undefined itemCount', (WidgetTester tester) async {
    await tester.pumpWidget(
492
      Directionality(
493
        textDirection: TextDirection.ltr,
494
        child: GridView.builder(
495 496 497 498 499
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
          ),
          shrinkWrap: true,
          itemBuilder: (BuildContext context, int index) {
500 501
            return Container(
              child: Text('$index'),
502 503 504 505 506 507 508 509 510 511 512 513
            );
          },
        ),
      ),
    );
    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);
  });

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

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

    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));
  });
539 540 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

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