sliver_semantics_test.dart 23 KB
Newer Older
1 2 3 4 5 6 7 8
// 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/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
9
import 'package:vector_math/vector_math_64.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

import 'semantics_tester.dart';

void main() {
  testWidgets('excludeFromScrollable works correctly', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    const double appBarExpandedHeight = 200.0;

    final ScrollController scrollController = new ScrollController();
    final List<Widget> listChildren = new List<Widget>.generate(30, (int i) {
      return new Container(
        height: appBarExpandedHeight,
        child: new Text('Item $i'),
      );
    });
26 27 28
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
29 30 31 32
        data: const MediaQueryData(),
        child: new CustomScrollView(
          controller: scrollController,
          slivers: <Widget>[
33
            const SliverAppBar(
34 35 36 37 38 39 40 41 42
              pinned: true,
              expandedHeight: appBarExpandedHeight,
              title: const Text('Semantics Test with Slivers'),
            ),
            new SliverList(
              delegate: new SliverChildListDelegate(listChildren),
            ),
          ],
        ),
43
      ),
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    ));

    // AppBar is child of node with semantic scroll actions.
    expect(semantics, hasSemantics(
        new TestSemantics.root(
          children: <TestSemantics>[
            new TestSemantics.rootChild(
              id: 1,
              tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
              children: <TestSemantics>[
                new TestSemantics(
                  id: 5,
                  actions: SemanticsAction.scrollUp.index,
                  children: <TestSemantics>[
                    new TestSemantics(
                      id: 2,
60
                      label: 'Item 0',
61 62 63
                    ),
                    new TestSemantics(
                      id: 3,
64
                      label: 'Item 1',
65 66 67
                    ),
                    new TestSemantics(
                      id: 4,
68
                      label: 'Semantics Test with Slivers',
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
                    ),
                  ],
                ),
              ],
            )
          ],
        ),
        ignoreRect: true,
        ignoreTransform: true,
    ));

    // Scroll down far enough to reach the pinned state of the app bar.
    scrollController.jumpTo(appBarExpandedHeight);
    await tester.pump();

    // App bar is NOT a child of node with semantic scroll actions.
    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 1,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 5,
                actions: SemanticsAction.scrollUp.index | SemanticsAction.scrollDown.index,
                children: <TestSemantics>[
                  new TestSemantics(
97
                    id: 2,
98 99 100
                    label: 'Item 0',
                  ),
                  new TestSemantics(
101
                    id: 3,
102 103 104
                    label: 'Item 1',
                  ),
                  new TestSemantics(
105
                    id: 6,
106 107 108 109
                    label: 'Item 2',
                  ),
                ],
              ),
110 111 112 113 114
              new TestSemantics(
                id: 7,
                label: 'Semantics Test with Slivers',
                tags: <SemanticsTag>[RenderSemanticsGestureHandler.excludeFromScrolling],
              ),
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            ],
          )
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    // Scroll halfway back to the top, app bar is no longer in pinned state.
    scrollController.jumpTo(appBarExpandedHeight / 2);
    await tester.pump();

    // AppBar is child of node with semantic scroll actions.
    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 1,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 5,
                actions: SemanticsAction.scrollUp.index | SemanticsAction.scrollDown.index,
                children: <TestSemantics>[
139 140 141 142 143 144 145 146 147 148 149 150
                  new TestSemantics(
                    id: 2,
                    label: 'Item 0',
                  ),
                  new TestSemantics(
                    id: 3,
                    label: 'Item 1',
                  ),
                  new TestSemantics(
                    id: 6,
                    label: 'Item 2',
                  ),
151 152 153 154
                  new TestSemantics(
                    id: 8,
                    label: 'Semantics Test with Slivers',
                  ),
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 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 203 204 205 206
                ],
              ),
            ],
          )
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    semantics.dispose();
  });

  testWidgets('Offscreen sliver are not included in semantics tree', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    const double containerHeight = 200.0;

    final ScrollController scrollController = new ScrollController(
      initialScrollOffset: containerHeight * 1.5,
    );
    final List<Widget> slivers = new List<Widget>.generate(30, (int i) {
      return new SliverToBoxAdapter(
        child: new Container(
          height: containerHeight,
          child: new Text('Item $i'),
        ),
      );
    });
    await tester.pumpWidget(
      new Center(
        child: new SizedBox(
          height: containerHeight,
          child: new CustomScrollView(
            controller: scrollController,
            slivers: slivers,
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 9,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 12,
                actions: SemanticsAction.scrollUp.index | SemanticsAction.scrollDown.index,
                children: <TestSemantics>[
207
                  new TestSemantics(
208 209
                    id: 10,
                    label: 'Item 2',
210 211
                  ),
                  new TestSemantics(
212
                    id: 11,
213 214
                    label: 'Item 1',
                  ),
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
                ],
              ),
            ],
          )
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    semantics.dispose();
  });

  testWidgets('SemanticsNodes of Slivers are in paint order', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final List<Widget> slivers = new List<Widget>.generate(5, (int i) {
      return new SliverToBoxAdapter(
        child: new Container(
          height: 20.0,
          child: new Text('Item $i'),
        ),
      );
    });
    await tester.pumpWidget(
      new CustomScrollView(
        slivers: slivers,
      ),
    );
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258
    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 13,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 19,
                children: <TestSemantics>[
                  new TestSemantics(
                    id: 14,
                    label: 'Item 4',
                  ),
259
                  new TestSemantics(
260 261 262 263 264
                    id: 15,
                    label: 'Item 3',
                  ),
                  new TestSemantics(
                    id: 16,
265 266
                    label: 'Item 2',
                  ),
267 268 269 270 271 272 273 274
                  new TestSemantics(
                    id: 17,
                    label: 'Item 1',
                  ),
                  new TestSemantics(
                    id: 18,
                    label: 'Item 0',
                  ),
275 276 277 278 279 280 281 282 283
                ],
              ),
            ],
          )
        ],
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));
284 285

    semantics.dispose();
286
  });
287 288 289 290 291 292 293 294 295 296 297

  testWidgets('SemanticsNodes of a sliver fully covered by another overlapping sliver are excluded', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final List<Widget> listChildren = new List<Widget>.generate(10, (int i) {
      return new Container(
        height: 200.0,
        child: new Text('Item $i'),
      );
    });
    final ScrollController controller = new ScrollController(initialScrollOffset: 280.0);
298 299 300
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
301
        data: const MediaQueryData(),
302 303
        child: new CustomScrollView(
          slivers: <Widget>[
304
            const SliverAppBar(
305 306 307 308 309 310 311 312 313 314
              pinned: true,
              expandedHeight: 100.0,
            ),
            new SliverList(
              delegate: new SliverChildListDelegate(listChildren),
            ),
          ],
          controller: controller,
        ),
      ),
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

    // 'Item 0' is covered by app bar.
    expect(semantics, isNot(includesNodeWith(label: 'Item 0')));

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 20,
            rect: TestSemantics.fullScreen,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 25,
                actions: SemanticsAction.scrollUp.index | SemanticsAction.scrollDown.index,
                rect: TestSemantics.fullScreen,
                children: <TestSemantics>[
                  // Item 0 is missing because its covered by the app bar.
                  new TestSemantics(
                    id: 21,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    // Item 1 starts 20.0dp below edge, so there would be room for Item 0.
                    transform: new Matrix4.translation(new Vector3(0.0, 20.0, 0.0)),
                    label: 'Item 1',
                  ),
                  new TestSemantics(
                    id: 22,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, 220.0, 0.0)),
                    label: 'Item 2',
                  ),
                  new TestSemantics(
                    id: 23,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, 420.0, 0.0)),
                    label: 'Item 3',
                  ),
                ],
              ),
              new TestSemantics(
                id: 24,
                rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 56.0),
                tags: <SemanticsTag>[RenderSemanticsGestureHandler.excludeFromScrolling],
              ),
            ],
          )
        ],
      ),
    ));

    semantics.dispose();
  });

  testWidgets('Slivers fully covered by another overlapping sliver are excluded', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final ScrollController controller = new ScrollController(initialScrollOffset: 280.0);
    final List<Widget> slivers = new List<Widget>.generate(10, (int i) {
      return new SliverToBoxAdapter(
        child: new Container(
          height: 200.0,
          child: new Text('Item $i'),
        ),
      );
    });
381 382 383
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
384
        data: const MediaQueryData(),
385 386 387
        child: new CustomScrollView(
          controller: controller,
          slivers: <Widget>[
388
            const SliverAppBar(
389 390 391 392 393 394
              pinned: true,
              expandedHeight: 100.0,
            ),
          ]..addAll(slivers),
        ),
      ),
395
    ));
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 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 456 457 458

    // 'Item 0' is covered by app bar.
    expect(semantics, isNot(includesNodeWith(label: 'Item 0')));

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 26,
            rect: TestSemantics.fullScreen,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 31,
                actions: SemanticsAction.scrollUp.index | SemanticsAction.scrollDown.index,
                rect: TestSemantics.fullScreen,
                children: <TestSemantics>[
                  new TestSemantics(
                    id: 27,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, 420.0, 0.0)),
                    label: 'Item 3',
                  ),
                  new TestSemantics(
                    id: 28,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, 220.0, 0.0)),
                    label: 'Item 2',
                  ),
                  new TestSemantics(
                    id: 29,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    // Item 1 starts 20.0dp below edge, so there would be room for Item 0.
                    transform: new Matrix4.translation(new Vector3(0.0, 20.0, 0.0)),
                    label: 'Item 1',
                  ),
                  // Item 0 is missing because its covered by the app bar.
                ],
              ),
              new TestSemantics(
                id: 30,
                rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 56.0),
                tags: <SemanticsTag>[RenderSemanticsGestureHandler.excludeFromScrolling],
              ),
            ],
          )
        ],
      ),
    ));

    semantics.dispose();
  });

  testWidgets('SemanticsNodes of a sliver fully covered by another overlapping sliver are excluded (reverse)', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final List<Widget> listChildren = new List<Widget>.generate(10, (int i) {
      return new Container(
        height: 200.0,
        child: new Text('Item $i'),
      );
    });
    final ScrollController controller = new ScrollController(initialScrollOffset: 280.0);
459 460 461
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
462
        data: const MediaQueryData(),
463 464 465
        child: new CustomScrollView(
          reverse: true,  // This is the important setting for this test.
          slivers: <Widget>[
466
            const SliverAppBar(
467 468 469 470 471 472 473 474 475 476
              pinned: true,
              expandedHeight: 100.0,
            ),
            new SliverList(
              delegate: new SliverChildListDelegate(listChildren),
            ),
          ],
          controller: controller,
        ),
      ),
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

    // 'Item 0' is covered by app bar.
    expect(semantics, isNot(includesNodeWith(label: 'Item 0')));

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 32,
            rect: TestSemantics.fullScreen,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 37,
                actions: SemanticsAction.scrollUp.index | SemanticsAction.scrollDown.index,
                rect: TestSemantics.fullScreen,
                children: <TestSemantics>[
                  // Item 0 is missing because its covered by the app bar.
                  new TestSemantics(
                    id: 33,
                    // Item 1 ends at 580dp, so there would be 20dp space for Item 0.
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, 380.0, 0.0)),
                    label: 'Item 1',
                  ),
                  new TestSemantics(
                    id: 34,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, 180.0, 0.0)),
                    label: 'Item 2',
                  ),
                  new TestSemantics(
                    id: 35,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, -20.0, 0.0)),
                    label: 'Item 3',
                  ),
                ],
              ),
              new TestSemantics(
                id: 36,
                rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 56.0),
                transform: new Matrix4.translation(new Vector3(0.0, 544.0, 0.0)),
                tags: <SemanticsTag>[RenderSemanticsGestureHandler.excludeFromScrolling],
              ),
            ],
          )
        ],
      ),
    ));

    semantics.dispose();
  });

  testWidgets('Slivers fully covered by another overlapping sliver are excluded (reverse)', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final ScrollController controller = new ScrollController(initialScrollOffset: 280.0);
    final List<Widget> slivers = new List<Widget>.generate(10, (int i) {
      return new SliverToBoxAdapter(
        child: new Container(
          height: 200.0,
          child: new Text('Item $i'),
        ),
      );
    });
544 545 546
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
547
        data: const MediaQueryData(),
548 549 550 551
        child: new CustomScrollView(
          reverse: true,  // This is the important setting for this test.
          controller: controller,
          slivers: <Widget>[
552
            const SliverAppBar(
553 554 555 556 557 558
              pinned: true,
              expandedHeight: 100.0,
            ),
          ]..addAll(slivers),
        ),
      ),
559
    ));
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

    // 'Item 0' is covered by app bar.
    expect(semantics, isNot(includesNodeWith(label: 'Item 0')));

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 38,
            rect: TestSemantics.fullScreen,
            tags: <SemanticsTag>[RenderSemanticsGestureHandler.useTwoPaneSemantics],
            children: <TestSemantics>[
              new TestSemantics(
                id: 43,
                actions: SemanticsAction.scrollUp.index | SemanticsAction.scrollDown.index,
                rect: TestSemantics.fullScreen,
                children: <TestSemantics>[
                  new TestSemantics(
                    id: 39,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, -20.0, 0.0)),
                    label: 'Item 3',
                  ),
                  new TestSemantics(
                    id: 40,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    transform: new Matrix4.translation(new Vector3(0.0, 180.0, 0.0)),
                    label: 'Item 2',
                  ),
                  new TestSemantics(
                    id: 41,
                    rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 200.0),
                    // Item 1 ends at 580dp, so there would be 20dp space for Item 0.
                    transform: new Matrix4.translation(new Vector3(0.0, 380.0, 0.0)),
                    label: 'Item 1',
                  ),
                  // Item 0 is missing because its covered by the app bar.
                ],
              ),
              new TestSemantics(
                id: 42,
                rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 56.0),
                transform: new Matrix4.translation(new Vector3(0.0, 544.0, 0.0)),
                tags: <SemanticsTag>[RenderSemanticsGestureHandler.excludeFromScrolling],
              ),
            ],
          )
        ],
      ),
    ));

    semantics.dispose();
  });

  testWidgets('Slivers fully covered by another overlapping sliver are excluded (with center sliver)', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    final ScrollController controller = new ScrollController(initialScrollOffset: 280.0);
    final GlobalKey forwardAppBarKey = new GlobalKey(debugLabel: 'forward app bar');
    final List<Widget> forwardChildren = new List<Widget>.generate(10, (int i) {
      return new Container(
        height: 200.0,
        child: new Text('Forward Item $i'),
      );
    });
    final List<Widget> backwardChildren = new List<Widget>.generate(10, (int i) {
      return new Container(
        height: 200.0,
        child: new Text('Backward Item $i'),
      );
    });
631 632 633
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new MediaQuery(
634
        data: const MediaQueryData(),
635 636 637 638 639 640 641 642 643 644
        child: new Scrollable(
          controller: controller,
          viewportBuilder: (BuildContext context, ViewportOffset offset) {
            return new Viewport(
              offset: offset,
              center: forwardAppBarKey,
              slivers: <Widget>[
                new SliverList(
                  delegate: new SliverChildListDelegate(backwardChildren),
                ),
645
                const SliverAppBar(
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
                  pinned: true,
                  expandedHeight: 100.0,
                  flexibleSpace: const FlexibleSpaceBar(
                    title: const Text('Backward app bar'),
                  ),
                ),
                new SliverAppBar(
                  pinned: true,
                  key: forwardAppBarKey,
                  expandedHeight: 100.0,
                  flexibleSpace: const FlexibleSpaceBar(
                    title: const Text('Forward app bar'),
                  ),
                ),
                new SliverList(
                  delegate: new SliverChildListDelegate(forwardChildren),
                ),
              ],
            );
          },
        ),
      ),
668
    ));
669 670 671 672 673 674 675 676 677 678 679 680 681

    // 'Forward Item 0' is covered by app bar.
    expect(semantics, isNot(includesNodeWith(label: 'Forward Item 0')));
    expect(semantics, includesNodeWith(label: 'Forward Item 1'));

    controller.jumpTo(-880.0);
    await tester.pumpAndSettle();
    expect(semantics, isNot(includesNodeWith(label: 'Backward Item 0')));
    expect(semantics, includesNodeWith(label: 'Backward Item 1'));

    semantics.dispose();
  });

682
}