scrollable_restoration_test.dart 17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2014 The Flutter 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/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('CustomScrollView restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: CustomScrollView(
          restorationId: 'list',
          cacheExtent: 0,
          slivers: <Widget>[
            SliverList(
              delegate: SliverChildListDelegate(
                List<Widget>.generate(
                  50,
21
                  (int index) => SizedBox(
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
                    height: 50,
                    child: Text('Tile $index'),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('ListView restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: ListView(
          restorationId: 'list',
          cacheExtent: 0,
          children: List<Widget>.generate(
            50,
44
            (int index) => SizedBox(
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
              height: 50,
              child: Text('Tile $index'),
            ),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('ListView.builder restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: ListView.builder(
          restorationId: 'list',
          cacheExtent: 0,
62
          itemBuilder: (BuildContext context, int index) => SizedBox(
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
            height: 50,
            child: Text('Tile $index'),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('ListView.separated restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: ListView.separated(
          restorationId: 'list',
          cacheExtent: 0,
          itemCount: 50,
          separatorBuilder: (BuildContext context, int index) => const SizedBox.shrink(),
81
          itemBuilder: (BuildContext context, int index) => SizedBox(
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            height: 50,
            child: Text('Tile $index'),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('ListView.custom restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: ListView.custom(
          restorationId: 'list',
          cacheExtent: 0,
          childrenDelegate: SliverChildListDelegate(
            List<Widget>.generate(
              50,
101
              (int index) => SizedBox(
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                height: 50,
                child: Text('Tile $index'),
              ),
            ),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('GridView restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: GridView(
          restorationId: 'grid',
          cacheExtent: 0,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
          children: List<Widget>.generate(
            50,
123
            (int index) => SizedBox(
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
              height: 50,
              child: Text('Tile $index'),
            ),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('GridView.builder restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: GridView.builder(
          restorationId: 'grid',
          cacheExtent: 0,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
142
          itemBuilder: (BuildContext context, int index) => SizedBox(
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            height: 50,
            child: Text('Tile $index'),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('GridView.custom restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: GridView.custom(
          restorationId: 'grid',
          cacheExtent: 0,
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
          childrenDelegate: SliverChildListDelegate(
            List<Widget>.generate(
              50,
163
              (int index) => SizedBox(
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
                height: 50,
                child: Text('Tile $index'),
              ),
            ),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('GridView.count restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: GridView.count(
          restorationId: 'grid',
          cacheExtent: 0,
          crossAxisCount: 1,
          children: List<Widget>.generate(
            50,
185
            (int index) => SizedBox(
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
              height: 50,
              child: Text('Tile $index'),
            ),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('GridView.extent restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: GridView.extent(
          restorationId: 'grid',
          cacheExtent: 0,
          maxCrossAxisExtent: 50,
          children: List<Widget>.generate(
            50,
206
            (int index) => SizedBox(
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
              height: 50,
              child: Text('Tile $index'),
            ),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester);
  });

  testWidgets('SingleChildScrollView restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: SingleChildScrollView(
          restorationId: 'single',
          child: Column(
            children: List<Widget>.generate(
              50,
226
              (int index) => SizedBox(
227 228 229 230 231 232 233 234 235
                height: 50,
                child: Text('Tile $index'),
              ),
            ),
          ),
        ),
      ),
    );

236
    expect(tester.getTopLeft(find.text('Tile 0')), Offset.zero);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    expect(tester.getTopLeft(find.text('Tile 1')), const Offset(0, 50));

    tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(525);
    await tester.pump();

    expect(tester.getTopLeft(find.text('Tile 0')), const Offset(0, -525));
    expect(tester.getTopLeft(find.text('Tile 1')), const Offset(0, -475));

    await tester.restartAndRestore();

    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 525);
    expect(tester.getTopLeft(find.text('Tile 0')), const Offset(0, -525));
    expect(tester.getTopLeft(find.text('Tile 1')), const Offset(0, -475));

    final TestRestorationData data = await tester.getRestorationData();
    tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(0);
    await tester.pump();

255
    expect(tester.getTopLeft(find.text('Tile 0')), Offset.zero);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    expect(tester.getTopLeft(find.text('Tile 1')), const Offset(0, 50));

    await tester.restoreFrom(data);

    expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 525);
    expect(tester.getTopLeft(find.text('Tile 0')), const Offset(0, -525));
    expect(tester.getTopLeft(find.text('Tile 1')), const Offset(0, -475));
  });

  testWidgets('PageView restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: PageView(
          restorationId: 'pager',
          children: List<Widget>.generate(
            50,
272
            (int index) => Text('Tile $index'),
273 274 275 276 277 278 279 280 281 282 283 284 285
          ),
        ),
      ),
    );

    await pageViewScrollAndRestore(tester);
  });

  testWidgets('PageView.builder restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: PageView.builder(
          restorationId: 'pager',
286
          itemBuilder: (BuildContext context, int index) => SizedBox(
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            height: 50,
            child: Text('Tile $index'),
          ),
        ),
      ),
    );

    await pageViewScrollAndRestore(tester);
  });

  testWidgets('PageView.custom restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: PageView.custom(
          restorationId: 'pager',
          childrenDelegate: SliverChildListDelegate(
            List<Widget>.generate(
              50,
305
              (int index) => SizedBox(
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
                height: 50,
                child: Text('Tile $index'),
              ),
            ),
          ),
        ),
      ),
    );

    await pageViewScrollAndRestore(tester);
  });

  testWidgets('ListWheelScrollView restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: ListWheelScrollView(
          restorationId: 'wheel',
          itemExtent: 50,
          children: List<Widget>.generate(
            50,
326
            (int index) => Text('Tile $index'),
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester, secondOffset: 542);
  });

  testWidgets('ListWheelScrollView.useDelegate restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: ListWheelScrollView.useDelegate(
          restorationId: 'wheel',
          itemExtent: 50,
          childDelegate: ListWheelChildListDelegate(
            children: List<Widget>.generate(
              50,
344
              (int index) => SizedBox(
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
                height: 50,
                child: Text('Tile $index'),
              ),
            ),
          ),
        ),
      ),
    );

    await restoreScrollAndVerify(tester, secondOffset: 542);
  });

  testWidgets('NestedScrollView restoration', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: TestHarness(
          height: 200,
          child: NestedScrollView(
            restorationId: 'outer',
            headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                SliverOverlapAbsorber(
                  handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                  sliver: SliverAppBar(
                    title: const Text('Books'),
                    pinned: true,
                    expandedHeight: 150.0,
                    forceElevated: innerBoxIsScrolled,
                  ),
                ),
              ];
            },
            body: ListView(
              restorationId: 'inner',
              cacheExtent: 0,
              children: List<Widget>.generate(
                50,
382
                (int index) => SizedBox(
383 384 385 386 387 388 389 390 391 392
                  height: 50,
                  child: Text('Tile $index'),
                ),
              ),
            ),
          ),
        ),
      ),
    );

393
    expect(tester.renderObject<RenderSliver>(find.byType(SliverAppBar)).geometry!.paintExtent, 150);
394 395 396
    expect(find.text('Tile 0'), findsOneWidget);
    expect(find.text('Tile 10'), findsNothing);

397
    await tester.drag(find.byType(NestedScrollView), const Offset(0, -500));
398 399
    await tester.pump();

400
    expect(tester.renderObject<RenderSliver>(find.byType(SliverAppBar)).geometry!.paintExtent, 56);
401 402 403 404 405
    expect(find.text('Tile 0'), findsNothing);
    expect(find.text('Tile 10'), findsOneWidget);

    await tester.restartAndRestore();

406
    expect(tester.renderObject<RenderSliver>(find.byType(SliverAppBar)).geometry!.paintExtent, 56);
407 408 409 410
    expect(find.text('Tile 0'), findsNothing);
    expect(find.text('Tile 10'), findsOneWidget);

    final TestRestorationData data = await tester.getRestorationData();
411
    await tester.drag(find.byType(NestedScrollView), const Offset(0, 600));
412 413
    await tester.pump();

414
    expect(tester.renderObject<RenderSliver>(find.byType(SliverAppBar)).geometry!.paintExtent, 150);
415 416 417 418 419
    expect(find.text('Tile 0'), findsOneWidget);
    expect(find.text('Tile 10'), findsNothing);

    await tester.restoreFrom(data);

420
    expect(tester.renderObject<RenderSliver>(find.byType(SliverAppBar)).geometry!.paintExtent, 56);
421 422 423 424 425 426 427 428 429 430 431 432
    expect(find.text('Tile 0'), findsNothing);
    expect(find.text('Tile 10'), findsOneWidget);
  });

  testWidgets('RestorationData is flushed even if no frame is scheduled', (WidgetTester tester) async {
    await tester.pumpWidget(
      TestHarness(
        child: ListView(
          restorationId: 'list',
          cacheExtent: 0,
          children: List<Widget>.generate(
            50,
433
            (int index) => SizedBox(
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
              height: 50,
              child: Text('Tile $index'),
            ),
          ),
        ),
      ),
    );

    expect(find.text('Tile 0'), findsOneWidget);
    expect(find.text('Tile 1'), findsOneWidget);
    expect(find.text('Tile 10'), findsNothing);
    expect(find.text('Tile 11'), findsNothing);
    expect(find.text('Tile 12'), findsNothing);

    final TestRestorationData initialData = await tester.getRestorationData();
    final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(ListView)));
    await gesture.moveBy(const Offset(0, -525));
    await tester.pump();

    expect(find.text('Tile 0'), findsNothing);
    expect(find.text('Tile 1'), findsNothing);
    expect(find.text('Tile 10'), findsOneWidget);
    expect(find.text('Tile 11'), findsOneWidget);
    expect(find.text('Tile 12'), findsOneWidget);

459
    // Restoration data hasn't changed.
460 461 462 463
    expect(await tester.getRestorationData(), initialData);

    // Restoration data changes with up event.
    await gesture.up();
464
    await tester.pump();
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    expect(await tester.getRestorationData(), isNot(initialData));
  });
}

Future<void> pageViewScrollAndRestore(WidgetTester tester) async {
  expect(find.text('Tile 0'), findsOneWidget);
  expect(find.text('Tile 10'), findsNothing);

  tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(50.0 * 10);
  await tester.pumpAndSettle();

  expect(find.text('Tile 0'), findsNothing);
  expect(find.text('Tile 10'), findsOneWidget);

  await tester.restartAndRestore();

  expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 50.0 * 10);
  expect(find.text('Tile 0'), findsNothing);
  expect(find.text('Tile 10'), findsOneWidget);

  final TestRestorationData data = await tester.getRestorationData();
  tester.state<ScrollableState>(find.byType(Scrollable)).position.jumpTo(0);
  await tester.pump();

  expect(find.text('Tile 0'), findsOneWidget);
  expect(find.text('Tile 10'), findsNothing);

  await tester.restoreFrom(data);

  expect(tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels, 50.0 * 10);
  expect(find.text('Tile 0'), findsNothing);
  expect(find.text('Tile 10'), findsOneWidget);
}

Future<void> restoreScrollAndVerify(WidgetTester tester, {double secondOffset = 525}) async {
  final Finder findScrollable = find.byElementPredicate((Element e) => e.widget is Scrollable);

  expect(find.text('Tile 0'), findsOneWidget);
  expect(find.text('Tile 1'), findsOneWidget);
  expect(find.text('Tile 10'), findsNothing);
  expect(find.text('Tile 11'), findsNothing);
  expect(find.text('Tile 12'), findsNothing);

  tester.state<ScrollableState>(findScrollable).position.jumpTo(secondOffset);
  await tester.pump();

  expect(find.text('Tile 0'), findsNothing);
  expect(find.text('Tile 1'), findsNothing);
  expect(find.text('Tile 10'), findsOneWidget);
  expect(find.text('Tile 11'), findsOneWidget);
  expect(find.text('Tile 12'), findsOneWidget);

  await tester.restartAndRestore();

  expect(tester.state<ScrollableState>(findScrollable).position.pixels, secondOffset);
  expect(find.text('Tile 0'), findsNothing);
  expect(find.text('Tile 1'), findsNothing);
  expect(find.text('Tile 10'), findsOneWidget);
  expect(find.text('Tile 11'), findsOneWidget);
  expect(find.text('Tile 12'), findsOneWidget);

  final TestRestorationData data = await tester.getRestorationData();
  tester.state<ScrollableState>(findScrollable).position.jumpTo(0);
  await tester.pump();

  expect(find.text('Tile 0'), findsOneWidget);
  expect(find.text('Tile 1'), findsOneWidget);
  expect(find.text('Tile 10'), findsNothing);
  expect(find.text('Tile 11'), findsNothing);
  expect(find.text('Tile 12'), findsNothing);

  await tester.restoreFrom(data);

  expect(tester.state<ScrollableState>(findScrollable).position.pixels, secondOffset);
  expect(find.text('Tile 0'), findsNothing);
  expect(find.text('Tile 1'), findsNothing);
  expect(find.text('Tile 10'), findsOneWidget);
  expect(find.text('Tile 11'), findsOneWidget);
  expect(find.text('Tile 12'), findsOneWidget);
}

class TestHarness extends StatelessWidget {
547
  const TestHarness({super.key, required this.child, this.height = 100});
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

  final Widget child;
  final double height;

  @override
  Widget build(BuildContext context) {
    return RootRestorationScope(
      restorationId: 'root',
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: Align(
          alignment: Alignment.topLeft,
          child: SizedBox(
            height: height,
            width: 50,
            child: child,
          ),
        ),
      ),
    );
  }
}