list_view_test.dart 11.5 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5 6 7
// Copyright 2015 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';

8 9
import '../rendering/mock_canvas.dart';

10 11 12 13 14 15 16 17 18 19 20
class TestSliverChildListDelegate extends SliverChildListDelegate {
  TestSliverChildListDelegate(List<Widget> children) : super(children);

  final List<String> log = <String>[];

  @override
  void didFinishLayout(int firstIndex, int lastIndex) {
    log.add('didFinishLayout firstIndex=$firstIndex lastIndex=$lastIndex');
  }
}

Adam Barth's avatar
Adam Barth committed
21
void main() {
22
  testWidgets('ListView default control', (WidgetTester tester) async {
23 24 25 26 27 28 29 30
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new ListView(itemExtent: 100.0),
        ),
      ),
    );
31 32
  });

33
  testWidgets('ListView itemExtent control test', (WidgetTester tester) async {
Adam Barth's avatar
Adam Barth committed
34
    await tester.pumpWidget(
35 36 37 38 39 40 41 42 43 44
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          itemExtent: 200.0,
          children: new List<Widget>.generate(20, (int i) {
            return new Container(
              child: new Text('$i'),
            );
          }),
        ),
Adam Barth's avatar
Adam Barth committed
45 46 47
      ),
    );

48
    final RenderBox box = tester.renderObject<RenderBox>(find.byType(Container).first);
Adam Barth's avatar
Adam Barth committed
49 50 51 52 53
    expect(box.size.height, equals(200.0));

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
Adam Barth's avatar
Adam Barth committed
54
    expect(find.text('3'), findsNothing);
Adam Barth's avatar
Adam Barth committed
55 56
    expect(find.text('4'), findsNothing);

57
    await tester.drag(find.byType(ListView), const Offset(0.0, -250.0));
Adam Barth's avatar
Adam Barth committed
58 59 60 61 62 63 64
    await tester.pump();

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
    expect(find.text('4'), findsOneWidget);
Adam Barth's avatar
Adam Barth committed
65
    expect(find.text('5'), findsNothing);
Adam Barth's avatar
Adam Barth committed
66 67
    expect(find.text('6'), findsNothing);

68
    await tester.drag(find.byType(ListView), const Offset(0.0, 200.0));
Adam Barth's avatar
Adam Barth committed
69 70 71 72 73 74
    await tester.pump();

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
Adam Barth's avatar
Adam Barth committed
75
    expect(find.text('4'), findsNothing);
Adam Barth's avatar
Adam Barth committed
76 77 78
    expect(find.text('5'), findsNothing);
  });

79
  testWidgets('ListView large scroll jump', (WidgetTester tester) async {
80
    final List<int> log = <int>[];
Adam Barth's avatar
Adam Barth committed
81 82

    await tester.pumpWidget(
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          itemExtent: 200.0,
          children: new List<Widget>.generate(20, (int i) {
            return new Builder(
              builder: (BuildContext context) {
                log.add(i);
                return new Container(
                  child: new Text('$i'),
                );
              }
            );
          }),
        ),
Adam Barth's avatar
Adam Barth committed
98 99 100
      ),
    );

101
    expect(log, equals(<int>[0, 1, 2, 3, 4]));
Adam Barth's avatar
Adam Barth committed
102 103
    log.clear();

104 105
    final ScrollableState state = tester.state(find.byType(Scrollable));
    final ScrollPosition position = state.position;
Adam Barth's avatar
Adam Barth committed
106 107 108 109 110
    position.jumpTo(2025.0);

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

111
    expect(log, equals(<int>[8, 9, 10, 11, 12, 13, 14]));
Adam Barth's avatar
Adam Barth committed
112 113 114 115 116 117 118
    log.clear();

    position.jumpTo(975.0);

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

119
    expect(log, equals(<int>[7, 6, 5, 4, 3]));
Adam Barth's avatar
Adam Barth committed
120 121
    log.clear();
  });
122 123 124

  testWidgets('ListView can build out of underflow', (WidgetTester tester) async {
    await tester.pumpWidget(
125 126 127 128 129
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          itemExtent: 100.0,
        ),
130 131 132 133 134 135 136 137 138 139 140
      ),
    );

    expect(find.text('0'), findsNothing);
    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);

    await tester.pumpWidget(
141 142 143 144 145 146 147 148 149 150
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          itemExtent: 100.0,
          children: new List<Widget>.generate(2, (int i) {
            return new Container(
              child: new Text('$i'),
            );
          }),
        ),
151 152 153 154 155 156 157 158 159 160 161
      ),
    );

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsNothing);
    expect(find.text('3'), findsNothing);
    expect(find.text('4'), findsNothing);
    expect(find.text('5'), findsNothing);

    await tester.pumpWidget(
162 163 164 165 166 167 168 169 170 171
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView(
          itemExtent: 100.0,
          children: new List<Widget>.generate(5, (int i) {
            return new Container(
              child: new Text('$i'),
            );
          }),
        ),
172 173 174 175 176 177 178 179 180 181
      ),
    );

    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('2'), findsOneWidget);
    expect(find.text('3'), findsOneWidget);
    expect(find.text('4'), findsOneWidget);
    expect(find.text('5'), findsNothing);
  });
182

183 184
  testWidgets('ListView can build out of overflow padding', (WidgetTester tester) async {
    await tester.pumpWidget(
185 186 187 188 189 190 191 192
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new SizedBox(
            width: 0.0,
            height: 0.0,
            child: new ListView(
              padding: const EdgeInsets.all(8.0),
193
              children: const <Widget>[
Ian Hickson's avatar
Ian Hickson committed
194
                const Text('padded', textDirection: TextDirection.ltr),
195 196
              ],
            ),
197 198 199 200
          ),
        ),
      ),
    );
201
    expect(find.text('padded', skipOffstage: false), findsOneWidget);
202 203
  });

204 205
  testWidgets('ListView with itemExtent in unbounded context', (WidgetTester tester) async {
    await tester.pumpWidget(
206 207 208 209 210 211 212 213 214 215 216 217
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new SingleChildScrollView(
          child: new ListView(
            itemExtent: 100.0,
            shrinkWrap: true,
            children: new List<Widget>.generate(20, (int i) {
              return new Container(
                child: new Text('$i'),
              );
            }),
          ),
218 219 220 221 222 223 224
        ),
      ),
    );

    expect(find.text('0'), findsOneWidget);
    expect(find.text('19'), findsOneWidget);
  });
225

Josh Soref's avatar
Josh Soref committed
226
  testWidgets('didFinishLayout has correct indices', (WidgetTester tester) async {
227 228 229
    final TestSliverChildListDelegate delegate = new TestSliverChildListDelegate(
      new List<Widget>.generate(20, (int i) {
        return new Container(
Ian Hickson's avatar
Ian Hickson committed
230
          child: new Text('$i', textDirection: TextDirection.ltr),
231 232 233 234 235
        );
      })
    );

    await tester.pumpWidget(
236 237 238 239 240 241
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView.custom(
          itemExtent: 110.0,
          childrenDelegate: delegate,
        ),
242 243 244
      ),
    );

245
    expect(delegate.log, equals(<String>['didFinishLayout firstIndex=0 lastIndex=7']));
246 247 248
    delegate.log.clear();

    await tester.pumpWidget(
249 250 251 252 253 254
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new ListView.custom(
          itemExtent: 210.0,
          childrenDelegate: delegate,
        ),
255 256 257
      ),
    );

258
    expect(delegate.log, equals(<String>['didFinishLayout firstIndex=0 lastIndex=4']));
259 260 261 262 263 264 265 266
    delegate.log.clear();

    await tester.drag(find.byType(ListView), const Offset(0.0, -600.0));

    expect(delegate.log, isEmpty);

    await tester.pump();

267
    expect(delegate.log, equals(<String>['didFinishLayout firstIndex=1 lastIndex=6']));
268 269
    delegate.log.clear();
  });
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

  testWidgets('ListView automatically pad MediaQuery on axis', (WidgetTester tester) async {
    EdgeInsets innerMediaQueryPadding;

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new MediaQuery(
          data: const MediaQueryData(
            padding: const EdgeInsets.all(30.0),
          ),
          child: new ListView(
            children: <Widget>[
              const Text('top', textDirection: TextDirection.ltr),
              new Builder(builder: (BuildContext context) {
                innerMediaQueryPadding = MediaQuery.of(context).padding;
                return new Container();
              }),
            ],
          ),
        ),
      ),
    );
    // Automatically apply the top/bottom padding into sliver.
    expect(tester.getTopLeft(find.text('top')).dy, 30.0);
    // Leave left/right padding as is for children.
    expect(innerMediaQueryPadding, const EdgeInsets.symmetric(horizontal: 30.0));
  });
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

  testWidgets('ListView clips if overflow is smaller than cacheExtent', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/17426.

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new Container(
            height: 200.0,
            child: new ListView(
              cacheExtent: 500.0,
              children: <Widget>[
                new Container(
                  height: 90.0,
                ),
                new Container(
                  height: 110.0,
                ),
                new Container(
                  height: 80.0,
                ),
              ],
            ),
          ),
        ),
      ),
    );

    expect(find.byType(Viewport), paints..clipRect());
  });

  testWidgets('ListView does not clips if no overflow', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new Container(
            height: 200.0,
            child: new ListView(
              cacheExtent: 500.0,
              children: <Widget>[
                new Container(
                  height: 100.0,
                ),
              ],
            ),
          ),
        ),
      ),
  );

    expect(find.byType(Viewport), isNot(paints..clipRect()));
  });

  testWidgets('ListView (fixed extent) clips if overflow is smaller than cacheExtent', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/17426.

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new Container(
            height: 200.0,
            child: new ListView(
              itemExtent: 100.0,
              cacheExtent: 500.0,
              children: <Widget>[
                new Container(
                  height: 100.0,
                ),
                new Container(
                  height: 100.0,
                ),
                new Container(
                  height: 100.0,
                ),
              ],
            ),
          ),
        ),
      ),
    );

    expect(find.byType(Viewport), paints..clipRect());
  });

  testWidgets('ListView (fixed extent) does not clips if no overflow', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Center(
          child: new Container(
            height: 200.0,
            child: new ListView(
              itemExtent: 100.0,
              cacheExtent: 500.0,
              children: <Widget>[
                new Container(
                  height: 100.0,
                ),
              ],
            ),
          ),
        ),
      ),
    );

    expect(find.byType(Viewport), isNot(paints..clipRect()));
  });
Adam Barth's avatar
Adam Barth committed
408
}