list_view_builder_test.dart 13.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8 9 10 11

import 'test_widgets.dart';

void main() {
12
  testWidgetsWithLeakTracking('ListView.builder mount/dismount smoke test', (WidgetTester tester) async {
13
    final List<int> callbackTracker = <int>[];
14 15 16 17 18

    // the root view is 800x600 in the test environment
    // so if our widget is 100 pixels tall, it should fit exactly 6 times.

    Widget builder() {
19
      return Directionality(
20
        textDirection: TextDirection.ltr,
21 22
        child: FlipWidget(
          left: ListView.builder(
23 24 25
            itemExtent: 100.0,
            itemBuilder: (BuildContext context, int index) {
              callbackTracker.add(index);
26
              return SizedBox(
27
                key: ValueKey<int>(index),
28
                height: 100.0,
29
                child: Text('$index'),
30 31 32 33
              );
            },
          ),
          right: const Text('Not Today'),
34 35 36 37 38 39
        ),
      );
    }

    await tester.pumpWidget(builder());

40
    final FlipWidgetState testWidget = tester.state(find.byType(FlipWidget));
41

42 43 44 45 46
    expect(callbackTracker, equals(<int>[
      0, 1, 2, 3, 4, 5, // visible in viewport
      6, 7, 8, // in caching area
    ]));
    check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[ 6, 7, 8]);
47 48 49 50 51 52 53 54 55 56 57

    callbackTracker.clear();
    testWidget.flip();
    await tester.pump();

    expect(callbackTracker, equals(<int>[]));

    callbackTracker.clear();
    testWidget.flip();
    await tester.pump();

58 59 60 61 62
    expect(callbackTracker, equals(<int>[
      0, 1, 2, 3, 4, 5,
      6, 7, 8, // in caching area
    ]));
    check(visible: <int>[0, 1, 2, 3, 4, 5], hidden: <int>[ 6, 7, 8]);
63 64
  });

65
  testWidgetsWithLeakTracking('ListView.builder vertical', (WidgetTester tester) async {
66
    final List<int> callbackTracker = <int>[];
67 68 69 70 71

    // the root view is 800x600 in the test environment
    // so if our widget is 200 pixels tall, it should fit exactly 3 times.
    // but if we are offset by 300 pixels, there will be 4, numbered 1-4.

72
    Widget itemBuilder(BuildContext context, int index) {
73
      callbackTracker.add(index);
74
      return SizedBox(
75
        key: ValueKey<int>(index),
76 77
        width: 500.0, // this should be ignored
        height: 400.0, // should be overridden by itemExtent
78
        child: Text('$index', textDirection: TextDirection.ltr),
79
      );
80
    }
81

82
    Widget buildWidget() {
83 84 85
      final ScrollController controller = ScrollController(initialScrollOffset: 300.0);
      addTearDown(controller.dispose);

86
      return Directionality(
87
        textDirection: TextDirection.ltr,
88 89
        child: FlipWidget(
          left: ListView.builder(
90
            controller: controller,
91 92 93
            itemExtent: 200.0,
            itemBuilder: itemBuilder,
          ),
Ian Hickson's avatar
Ian Hickson committed
94
          right: const Text('Not Today'),
95 96 97 98 99
        ),
      );
    }

    void jumpTo(double newScrollOffset) {
Adam Barth's avatar
Adam Barth committed
100
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
101 102 103 104 105
      scrollable.position.jumpTo(newScrollOffset);
    }

    await tester.pumpWidget(buildWidget());

106 107 108 109 110 111
    expect(callbackTracker, equals(<int>[
      0, // in caching area
      1, 2, 3, 4,
      5, // in caching area
    ]));
    check(visible: <int>[1, 2, 3, 4], hidden: <int>[0, 5]);
112 113 114 115 116 117 118
    callbackTracker.clear();

    jumpTo(400.0);
    // now only 3 should fit, numbered 2-4.

    await tester.pumpWidget(buildWidget());

119 120 121 122 123 124
    expect(callbackTracker, equals(<int>[
      0, 1, // in caching area
      2, 3, 4,
      5, 6, // in caching area
    ]));
    check(visible: <int>[2, 3, 4], hidden: <int>[0, 1, 5, 6]);
125 126 127 128 129 130 131
    callbackTracker.clear();

    jumpTo(500.0);
    // now 4 should fit, numbered 2-5.

    await tester.pumpWidget(buildWidget());

132 133 134 135 136 137
    expect(callbackTracker, equals(<int>[
      0, 1, // in caching area
      2, 3, 4, 5,
      6, // in caching area
    ]));
    check(visible: <int>[2, 3, 4, 5], hidden: <int>[0, 1, 6]);
138 139 140
    callbackTracker.clear();
  });

141
  testWidgetsWithLeakTracking('ListView.builder horizontal', (WidgetTester tester) async {
142
    final List<int> callbackTracker = <int>[];
143 144 145 146 147

    // the root view is 800x600 in the test environment
    // so if our widget is 200 pixels wide, it should fit exactly 4 times.
    // but if we are offset by 300 pixels, there will be 5, numbered 1-5.

148
    Widget itemBuilder(BuildContext context, int index) {
149
      callbackTracker.add(index);
150
      return SizedBox(
151
        key: ValueKey<int>(index),
152 153
        width: 400.0, // this should be overridden by itemExtent
        height: 500.0, // this should be ignored
154
        child: Text('$index'),
155
      );
156
    }
157

158
    Widget buildWidget() {
159 160 161
      final ScrollController controller = ScrollController(initialScrollOffset: 300.0);
      addTearDown(controller.dispose);

162
      return Directionality(
163
        textDirection: TextDirection.ltr,
164 165
        child: FlipWidget(
          left: ListView.builder(
166
            controller: controller,
167 168 169 170 171
            itemBuilder: itemBuilder,
            itemExtent: 200.0,
            scrollDirection: Axis.horizontal,
          ),
          right: const Text('Not Today'),
172 173 174 175 176
        ),
      );
    }

    void jumpTo(double newScrollOffset) {
Adam Barth's avatar
Adam Barth committed
177
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
178 179 180 181 182
      scrollable.position.jumpTo(newScrollOffset);
    }

    await tester.pumpWidget(buildWidget());

183 184 185 186 187 188
    expect(callbackTracker, equals(<int>[
      0, // in caching area
      1, 2, 3, 4, 5,
      6, // in caching area
    ]));
    check(visible: <int>[1, 2, 3, 4, 5], hidden: <int>[0, 6]);
189 190 191 192 193 194 195
    callbackTracker.clear();

    jumpTo(400.0);
    // now only 4 should fit, numbered 2-5.

    await tester.pumpWidget(buildWidget());

196 197 198 199 200 201
    expect(callbackTracker, equals(<int>[
      0, 1, // in caching area
      2, 3, 4, 5,
      6, 7, // in caching area
    ]));
    check(visible: <int>[2, 3, 4, 5], hidden: <int>[0, 1, 6, 7]);
202 203 204 205 206 207 208
    callbackTracker.clear();

    jumpTo(500.0);
    // now only 5 should fit, numbered 2-6.

    await tester.pumpWidget(buildWidget());

209 210 211 212 213 214
    expect(callbackTracker, equals(<int>[
      0, 1, // in caching area
      2, 3, 4, 5, 6,
      7, // in caching area
    ]));
    check(visible: <int>[2, 3, 4, 5, 6], hidden: <int>[0, 1, 7]);
215 216 217
    callbackTracker.clear();
  });

218
  testWidgetsWithLeakTracking('ListView.builder 10 items, 2-3 items visible', (WidgetTester tester) async {
219
    final List<int> callbackTracker = <int>[];
220 221 222 223 224

    // The root view is 800x600 in the test environment and our list
    // items are 300 tall. Scrolling should cause two or three items
    // to be built.

225
    Widget itemBuilder(BuildContext context, int index) {
226
      callbackTracker.add(index);
227
      return Text('$index', key: ValueKey<int>(index), textDirection: TextDirection.ltr);
228
    }
229

230
    final Widget testWidget = Directionality(
231
      textDirection: TextDirection.ltr,
232
      child: ListView.builder(
233 234 235 236
        itemBuilder: itemBuilder,
        itemExtent: 300.0,
        itemCount: 10,
      ),
237 238 239
    );

    void jumpTo(double newScrollOffset) {
Adam Barth's avatar
Adam Barth committed
240
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
241 242 243 244
      scrollable.position.jumpTo(newScrollOffset);
    }

    await tester.pumpWidget(testWidget);
245 246
    expect(callbackTracker, equals(<int>[0, 1, 2]));
    check(visible: <int>[0, 1], hidden: <int>[2]);
247 248 249 250 251
    callbackTracker.clear();

    jumpTo(150.0);
    await tester.pump();

252 253
    expect(callbackTracker, equals(<int>[3]));
    check(visible: <int>[0, 1, 2], hidden: <int>[3]);
254 255 256 257 258
    callbackTracker.clear();

    jumpTo(600.0);
    await tester.pump();

259 260
    expect(callbackTracker, equals(<int>[4]));
    check(visible: <int>[2, 3], hidden: <int>[0, 1, 4]);
261 262 263 264 265
    callbackTracker.clear();

    jumpTo(750.0);
    await tester.pump();

266 267
    expect(callbackTracker, equals(<int>[5]));
    check(visible: <int>[2, 3, 4], hidden: <int>[0, 1, 5]);
268 269 270
    callbackTracker.clear();
  });

271
  testWidgetsWithLeakTracking('ListView.builder 30 items with big jump, using prototypeItem', (WidgetTester tester) async {
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    final List<int> callbackTracker = <int>[];

    // The root view is 800x600 in the test environment and our list
    // items are 300 tall. Scrolling should cause two or three items
    // to be built.

    Widget itemBuilder(BuildContext context, int index) {
      callbackTracker.add(index);
      return Text('$index', key: ValueKey<int>(index), textDirection: TextDirection.ltr);
    }

    final Widget testWidget = Directionality(
      textDirection: TextDirection.ltr,
      child: ListView.builder(
        itemBuilder: itemBuilder,
        prototypeItem: const SizedBox(
          width: 800,
          height: 300,
        ),
        itemCount: 30,
      ),
    );

    void jumpTo(double newScrollOffset) {
      final ScrollableState scrollable = tester.state(find.byType(Scrollable));
      scrollable.position.jumpTo(newScrollOffset);
    }

    await tester.pumpWidget(testWidget);

    // 2 is in the cache area, but not visible.
    expect(callbackTracker, equals(<int>[0, 1, 2]));
    final List<int> initialExpectedHidden = List<int>.generate(28, (int i) => i + 2);
    check(visible: <int>[0, 1], hidden: initialExpectedHidden);
    callbackTracker.clear();

    // Jump to the end of the ListView.
    jumpTo(8400);
    await tester.pump();

    // 27 is in the cache area, but not visible.
    expect(callbackTracker, equals(<int>[27, 28, 29]));
    final List<int> finalExpectedHidden = List<int>.generate(28, (int i) => i);
    check(visible: <int>[28, 29], hidden: finalExpectedHidden);
    callbackTracker.clear();
  });

319
  testWidgetsWithLeakTracking('ListView.separated', (WidgetTester tester) async {
320
    Widget buildFrame({ required int itemCount }) {
321
      return Directionality(
322
        textDirection: TextDirection.ltr,
323
        child: ListView.separated(
324 325
          itemCount: itemCount,
          itemBuilder: (BuildContext context, int index) {
326
            return SizedBox(
327
              height: 100.0,
328
              child: Text('i$index'),
329 330 331
            );
          },
          separatorBuilder: (BuildContext context, int index) {
332
            return SizedBox(
333
              height: 10.0,
334
              child: Text('s$index'),
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
            );
          },
        ),
      );
    }

    await tester.pumpWidget(buildFrame(itemCount: 0));
    expect(find.text('i0'), findsNothing);
    expect(find.text('s0'), findsNothing);

    await tester.pumpWidget(buildFrame(itemCount: 1));
    expect(find.text('i0'), findsOneWidget);
    expect(find.text('s0'), findsNothing);

    await tester.pumpWidget(buildFrame(itemCount: 2));
    expect(find.text('i0'), findsOneWidget);
    expect(find.text('s0'), findsOneWidget);
    expect(find.text('i1'), findsOneWidget);
    expect(find.text('s1'), findsNothing);

    // ListView's height is 600, so items i0-i5 and s0-s4 fit.
    await tester.pumpWidget(buildFrame(itemCount: 25));
357
    for (final String s in <String>['i0', 's0', 'i1', 's1', 'i2', 's2', 'i3', 's3', 'i4', 's4', 'i5']) {
358
      expect(find.text(s), findsOneWidget);
359
    }
360 361 362
    expect(find.text('s5'), findsNothing);
    expect(find.text('i6'), findsNothing);
  });
363 364


365
  testWidgetsWithLeakTracking('ListView.separated uses correct semanticChildCount', (WidgetTester tester) async {
366
    Widget buildFrame({ required int itemCount}) {
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 408 409 410
      return Directionality(
        textDirection: TextDirection.ltr,
        child: ListView.separated(
          itemCount: itemCount,
          itemBuilder: (BuildContext context, int index) {
            return SizedBox(
              height: 100.0,
              child: Text('i$index'),
            );
          },
          separatorBuilder: (BuildContext context, int index) {
            return SizedBox(
              height: 10.0,
              child: Text('s$index'),
            );
          },
        ),
      );
    }

    Scrollable scrollable() {
      return tester.widget<Scrollable>(
        find.descendant(
          of: find.byType(ListView),
          matching: find.byType(Scrollable),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(itemCount: 0));
    expect(scrollable().semanticChildCount, 0);

    await tester.pumpWidget(buildFrame(itemCount: 1));
    expect(scrollable().semanticChildCount, 1);

    await tester.pumpWidget(buildFrame(itemCount: 2));
    expect(scrollable().semanticChildCount, 2);

    await tester.pumpWidget(buildFrame(itemCount: 3));
    expect(scrollable().semanticChildCount, 3);

    await tester.pumpWidget(buildFrame(itemCount: 4));
    expect(scrollable().semanticChildCount, 4);
  });
411 412

  // Regression test for https://github.com/flutter/flutter/issues/72292
413
  testWidgetsWithLeakTracking('ListView.builder and SingleChildScrollView can work well together', (WidgetTester tester) async {
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    Widget builder(int itemCount) {
      return Directionality(
        textDirection: TextDirection.ltr,
        child: SingleChildScrollView(
          child: ListView.builder(
            shrinkWrap: true,
            itemExtent: 35,
            itemCount: itemCount,
            itemBuilder: (BuildContext context, int index) {
              return const Text('I love Flutter.');
            },
          ),
        ),
      );
    }

    await tester.pumpWidget(builder(1));
    // Trigger relayout and garbage collect.
    await tester.pumpWidget(builder(2));
  });
434
}
435

436
void check({ List<int> visible = const <int>[], List<int> hidden = const <int>[] }) {
437
  for (final int i in visible) {
438 439
    expect(find.text('$i'), findsOneWidget);
  }
440
  for (final int i in hidden) {
441 442 443
    expect(find.text('$i'), findsNothing);
  }
}