grid_view_test.dart 12.5 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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';

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

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

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

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

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

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

70
    await tester.drag(find.text('Delaware'), const Offset(0.0, -4000.0));
71 72 73 74 75 76
    await tester.pump();

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

    expect(tester.getCenter(find.text('Tennessee')),
77
        equals(const Offset(300.0, 100.0)));
78 79 80 81 82

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

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

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

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new GridView.extent(
          maxCrossAxisExtent: 200.0,
          children: kStates.map((String state) {
            return new GestureDetector(
              onTap: () {
                log.add(state);
              },
              child: new Container(
                color: const Color(0xFF0000FF),
                child: new Text(state),
              ),
            );
          }).toList(),
        ),
      ),
    );
117 118 119 120 121 122 123 124 125 126 127

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

128
    await tester.drag(find.text('Arkansas'), const Offset(0.0, -4000.0));
129 130 131 132 133
    await tester.pump();

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

    expect(tester.getCenter(find.text('Tennessee')),
134
        equals(const Offset(300.0, 100.0)));
135 136 137 138 139 140

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

141
  testWidgets('GridView large scroll jump', (WidgetTester tester) async {
142
    final List<int> log = <int>[];
143

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

    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
    ]));
    log.clear();

174 175
    final ScrollableState state = tester.state(find.byType(Scrollable));
    final ScrollPosition position = state.position;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    position.jumpTo(3025.0);

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

    expect(log, equals(<int>[
      33, 34, 35, // col 11
      36, 37, 38, // col 12
      39, 40, 41, // col 13
      42, 43, 44, // col 14
    ]));
    log.clear();

    position.jumpTo(975.0);

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

    expect(log, equals(<int>[
      9, 10, 11, // col 3
      12, 13, 14, // col 4
      15, 16, 17, // col 5
      18, 19, 20, // col 6
    ]));
    log.clear();
  });

203
  testWidgets('GridView - change crossAxisCount', (WidgetTester tester) async {
204
    final List<int> log = <int>[];
205 206

    await tester.pumpWidget(
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new GridView(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
          ),
          children: new List<Widget>.generate(40, (int i) {
            return new Builder(
              builder: (BuildContext context) {
                log.add(i);
                return new Container(
                  child: new Text('$i'),
                );
              }
            );
          }),
223 224 225 226 227 228 229 230 231 232 233 234 235 236
        ),
      ),
    );

    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
    ]));
    log.clear();

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

    expect(log, equals(<int>[
      0, 1, 2, 3, // row 0
      4, 5, 6, 7, // row 1
      8, 9, 10, 11, // row 2
    ]));
    log.clear();

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

268
  testWidgets('GridView - change maxChildCrossAxisExtent', (WidgetTester tester) async {
269
    final List<int> log = <int>[];
270 271

    await tester.pumpWidget(
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new GridView(
          gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 200.0,
          ),
          children: new List<Widget>.generate(40, (int i) {
            return new Builder(
              builder: (BuildContext context) {
                log.add(i);
                return new Container(
                  child: new Text('$i'),
                );
              }
            );
          }),
288 289 290 291 292 293 294 295 296 297 298 299 300 301
        ),
      ),
    );

    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
    ]));
    log.clear();

    await tester.pumpWidget(
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new GridView(
          gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
            maxCrossAxisExtent: 400.0,
          ),
          children: new List<Widget>.generate(40, (int i) {
            return new Builder(
              builder: (BuildContext context) {
                log.add(i);
                return new Container(
                  child: new Text('$i'),
                );
              }
            );
          }),
318 319 320 321 322 323 324 325 326 327 328 329 330 331
        ),
      ),
    );

    expect(log, equals(<int>[
      0, 1, 2, 3, // row 0
      4, 5, 6, 7, // row 1
      8, 9, 10, 11, // row 2
    ]));
    log.clear();

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

Adam Barth's avatar
Adam Barth committed
333 334 335 336 337
  testWidgets('One-line GridView paints', (WidgetTester tester) async {
    const Color green = const Color(0xFF00FF00);

    final Container container = new Container(
      decoration: const BoxDecoration(
338
        color: green,
Adam Barth's avatar
Adam Barth committed
339 340 341
      ),
    );

342 343 344 345 346 347 348 349 350 351 352
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new SizedBox(
            height: 200.0,
            child: new GridView.count(
              crossAxisCount: 2,
              children: <Widget>[ container, container, container, container ],
            ),
          ),
Adam Barth's avatar
Adam Barth committed
353 354
        ),
      ),
355
    );
Adam Barth's avatar
Adam Barth committed
356 357 358 359 360

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

361 362
  testWidgets('GridView in zero context', (WidgetTester tester) async {
    await tester.pumpWidget(
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new SizedBox(
            width: 0.0,
            height: 0.0,
            child: new GridView.count(
              crossAxisCount: 4,
              children: new List<Widget>.generate(20, (int i) {
                return new Container(
                  child: new Text('$i'),
                );
              }),
            ),
          ),
        ),
      ),
    );

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsNothing);
  });

  testWidgets('GridView in unbounded context', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new SingleChildScrollView(
391 392
          child: new GridView.count(
            crossAxisCount: 4,
393
            shrinkWrap: true,
394 395 396 397 398 399 400 401 402 403 404
            children: new List<Widget>.generate(20, (int i) {
              return new Container(
                child: new Text('$i'),
              );
            }),
          ),
        ),
      ),
    );

    expect(find.text('0'), findsOneWidget);
405
    expect(find.text('19'), findsOneWidget);
406 407
  });

408
  testWidgets('GridView.builder control test', (WidgetTester tester) async {
409
    await tester.pumpWidget(
410 411 412 413 414 415
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new GridView.builder(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 4,
          ),
416
          shrinkWrap: true,
417 418
          itemCount: 20,
          itemBuilder: (BuildContext context, int index) {
419
            return new Container(
420
              child: new Text('$index'),
421
            );
422
          },
423 424 425
        ),
      ),
    );
426 427 428 429 430
    expect(find.text('0'), findsOneWidget);
    expect(find.text('11'), findsOneWidget);
    expect(find.text('12'), findsNothing);
  });

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
  testWidgets('GridView cross axis layout', (WidgetTester tester) async {
    final Key target = new UniqueKey();

    Widget build(TextDirection textDirection) {
      return new Directionality(
        textDirection: textDirection,
        child: new GridView.count(
          crossAxisCount: 4,
          children: <Widget>[
            new 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(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));
  });
456
}