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

5
import 'package:flutter/gestures.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8 9 10 11
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('text contrast guideline', () {
12 13
    testWidgets('black text on white background - Text Widget - direct style',
        (WidgetTester tester) async {
14
      final SemanticsHandle handle = tester.ensureSemantics();
15 16 17 18 19 20
      await tester.pumpWidget(
        _boilerplate(
          const Text(
            'this is a test',
            style: TextStyle(fontSize: 14.0, color: Colors.black),
          ),
21
        ),
22
      );
23 24 25 26
      await expectLater(tester, meetsGuideline(textContrastGuideline));
      handle.dispose();
    });

27 28 29 30
    testWidgets('Multiple text with same label', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(
        _boilerplate(
31 32
          const Column(
            children: <Widget>[
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
              Text(
                'this is a test',
                style: TextStyle(fontSize: 14.0, color: Colors.black),
              ),
              Text(
                'this is a test',
                style: TextStyle(fontSize: 14.0, color: Colors.black),
              ),
            ],
          ),
        ),
      );
      await expectLater(tester, meetsGuideline(textContrastGuideline));
      handle.dispose();
    });

    testWidgets(
      'Multiple text with same label but Nodes excluded from '
      'semantic tree have failing contrast should pass a11y guideline ',
      (WidgetTester tester) async {
        final SemanticsHandle handle = tester.ensureSemantics();
        await tester.pumpWidget(
          _boilerplate(
56 57
            const Column(
              children: <Widget>[
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
                Text(
                  'this is a test',
                  style: TextStyle(fontSize: 14.0, color: Colors.black),
                ),
                SizedBox(height: 50),
                Text(
                  'this is a test',
                  style: TextStyle(fontSize: 14.0, color: Colors.black),
                ),
                SizedBox(height: 50),
                ExcludeSemantics(
                  child: Text(
                    'this is a test',
                    style: TextStyle(fontSize: 14.0, color: Colors.white),
                  ),
                ),
              ],
            ),
          ),
        );
        await expectLater(tester, meetsGuideline(textContrastGuideline));
        handle.dispose();
    });

82 83
    testWidgets('white text on black background - Text Widget - direct style',
        (WidgetTester tester) async {
84
      final SemanticsHandle handle = tester.ensureSemantics();
85 86 87 88 89 90 91 92 93 94
      await tester.pumpWidget(
        _boilerplate(
          Container(
            width: 200.0,
            height: 200.0,
            color: Colors.black,
            child: const Text(
              'this is a test',
              style: TextStyle(fontSize: 14.0, color: Colors.white),
            ),
95 96
          ),
        ),
97
      );
98 99 100 101
      await expectLater(tester, meetsGuideline(textContrastGuideline));
      handle.dispose();
    });

102 103 104 105 106 107 108 109 110
    testWidgets('White text on white background fails contrast test',
      (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
    await tester.pumpWidget(
      _boilerplate(
        Container(
          width: 200.0,
          height: 300.0,
          color: Colors.white,
111 112
          child: const Column(
            children: <Widget>[
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
              Text(
                'this is a white text',
                style: TextStyle(fontSize: 14.0, color: Colors.white),
              ),
              SizedBox(height: 50),
              Text(
                'this is a black text test1',
                style: TextStyle(fontSize: 14.0, color: Colors.black),
              ),
              SizedBox(height: 50),
              Text(
                'this is a black text test2',
                style: TextStyle(fontSize: 14.0, color: Colors.black),
              ),
            ],
          ),
        ),
      ),
    );
    await expectLater(tester, doesNotMeetGuideline(textContrastGuideline));
    handle.dispose();
  });

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    const Color surface = Color(0xFFF0F0F0);

    /// Shades of blue with contrast ratio of 2.9, 4.4, 4.5 from [surface].
    const Color blue29 = Color(0xFF7E7EFB);
    const Color blue44 = Color(0xFF5757FF);
    const Color blue45 = Color(0xFF5252FF);
    const List<TextStyle> textStylesMeetingGuideline = <TextStyle>[
      TextStyle(color: blue44, backgroundColor: surface, fontSize: 18),
      TextStyle(color: blue44, backgroundColor: surface, fontSize: 14, fontWeight: FontWeight.bold),
      TextStyle(color: blue45, backgroundColor: surface),
    ];
    const List<TextStyle> textStylesDoesNotMeetingGuideline = <TextStyle>[
      TextStyle(color: blue44, backgroundColor: surface),
      TextStyle(color: blue29, backgroundColor: surface, fontSize: 18),
    ];

152
    Widget appWithTextWidget(TextStyle style) => _boilerplate(
153 154 155 156 157 158
      Text('this is text', style: style.copyWith(height: 30.0)),
    );

    for (final TextStyle style in textStylesMeetingGuideline) {
      testWidgets('text with style $style', (WidgetTester tester) async {
        final SemanticsHandle handle = tester.ensureSemantics();
159
        await tester.pumpWidget(appWithTextWidget(style));
160 161 162 163 164 165 166 167
        await expectLater(tester, meetsGuideline(textContrastGuideline));
        handle.dispose();
      });
    }

    for (final TextStyle style in textStylesDoesNotMeetingGuideline) {
      testWidgets('text with $style', (WidgetTester tester) async {
        final SemanticsHandle handle = tester.ensureSemantics();
168
        await tester.pumpWidget(appWithTextWidget(style));
169 170 171 172 173 174 175
        await expectLater(tester, doesNotMeetGuideline(textContrastGuideline));
        handle.dispose();
      });
    }

    testWidgets('black text on white background - Text Widget - direct style',
        (WidgetTester tester) async {
176
      final SemanticsHandle handle = tester.ensureSemantics();
177 178 179 180 181
      await tester.pumpWidget(
        _boilerplate(
          const Text(
            'this is a test',
            style: TextStyle(fontSize: 14.0, color: Colors.black),
182 183
          ),
        ),
184
      );
185 186 187 188
      await expectLater(tester, meetsGuideline(textContrastGuideline));
      handle.dispose();
    });

189 190
    testWidgets('white text on black background - Text Widget - direct style',
        (WidgetTester tester) async {
191
      final SemanticsHandle handle = tester.ensureSemantics();
192 193 194
      await tester.pumpWidget(
        _boilerplate(
          Container(
195 196 197
            width: 200.0,
            height: 200.0,
            color: Colors.black,
198 199 200 201
            child: const Text(
              'this is a test',
              style: TextStyle(fontSize: 14.0, color: Colors.white),
            ),
202 203
          ),
        ),
204
      );
205 206 207 208
      await expectLater(tester, meetsGuideline(textContrastGuideline));
      handle.dispose();
    });

209 210
    testWidgets('Material text field - amber on amber',
        (WidgetTester tester) async {
211
      final SemanticsHandle handle = tester.ensureSemantics();
212 213 214
      await tester.pumpWidget(
        _boilerplate(
          Container(
215 216 217
            width: 200.0,
            height: 200.0,
            color: Colors.amberAccent,
218
            child: TextField(
219
              style: const TextStyle(color: Colors.amber),
220
              controller: TextEditingController(text: 'this is a test'),
221 222 223
            ),
          ),
        ),
224
      );
225 226 227 228
      await expectLater(tester, doesNotMeetGuideline(textContrastGuideline));
      handle.dispose();
    });

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    testWidgets('Correctly identify failures in complex transforms', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(
        _boilerplate(
          Padding(
            padding: const EdgeInsets.only(left: 100),
            child: Semantics(
              container: true,
              child: Padding(
                padding: const EdgeInsets.only(left: 100),
                child: Semantics(
                  container: true,
                  child: Container(
                    width: 100.0,
                    height: 200.0,
                    color: Colors.amberAccent,
                    child: const Text(
                      'this',
                      style: TextStyle(color: Colors.amber),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      );
      await expectLater(tester, doesNotMeetGuideline(textContrastGuideline));
      handle.dispose();
    });

260 261
    testWidgets('Material text field - default style',
        (WidgetTester tester) async {
262
      final SemanticsHandle handle = tester.ensureSemantics();
263 264 265 266 267 268
      await tester.pumpWidget(
        _boilerplate(
          SizedBox(
            width: 100,
            child: TextField(
              controller: TextEditingController(text: 'this is a test'),
269 270 271 272
            ),
          ),
        ),
      );
273
      await tester.idle();
274 275 276 277
      await expectLater(tester, meetsGuideline(textContrastGuideline));
      handle.dispose();
    });

278
    testWidgets('Material2: yellow text on yellow background fails with correct message',
279
        (WidgetTester tester) async {
280
      final SemanticsHandle handle = tester.ensureSemantics();
281 282
      await tester.pumpWidget(
        _boilerplate(
283
          useMaterial3: false,
284 285 286 287 288 289 290 291
          Container(
            width: 200.0,
            height: 200.0,
            color: Colors.yellow,
            child: const Text(
              'this is a test',
              style: TextStyle(fontSize: 14.0, color: Colors.yellowAccent),
            ),
292 293
          ),
        ),
294
      );
295 296
      final Evaluation result = await textContrastGuideline.evaluate(tester);
      expect(result.passed, false);
297 298 299 300 301 302 303 304 305 306
      expect(
        result.reason,
        'SemanticsNode#4(Rect.fromLTRB(300.0, 200.0, 500.0, 400.0), '
        'label: "this is a test", textDirection: ltr):\n'
        'Expected contrast ratio of at least 4.5 but found 1.17 for a font '
        'size of 14.0.\n'
        'The computed colors was:\n'
        'light - Color(0xfffafafa), dark - Color(0xffffeb3b)\n'
         'See also: https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html',
      );
307 308
      handle.dispose();
    });
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
    testWidgets('Material3: yellow text on yellow background fails with correct message',
        (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(
        _boilerplate(
          useMaterial3: true,
          Container(
            width: 200.0,
            height: 200.0,
            color: Colors.yellow,
            child: const Text(
              'this is a test',
              style: TextStyle(fontSize: 14.0, color: Colors.yellowAccent),
            ),
          ),
        ),
      );
      final Evaluation result = await textContrastGuideline.evaluate(tester);
      expect(result.passed, false);
      expect(
        result.reason,
        'SemanticsNode#4(Rect.fromLTRB(300.0, 200.0, 500.0, 400.0), '
        'label: "this is a test", textDirection: ltr):\n'
        'Expected contrast ratio of at least 4.5 but found 1.19 for a font '
        'size of 14.0.\n'
        'The computed colors was:\n'
        'light - Color(0xfffffbfe), dark - Color(0xffffeb3b)\n'
         'See also: https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html',
      );
      handle.dispose();
    });

342 343
    testWidgets('label without corresponding text is skipped',
        (WidgetTester tester) async {
344
      final SemanticsHandle handle = tester.ensureSemantics();
345 346 347 348 349 350 351 352 353 354
      await tester.pumpWidget(
        _boilerplate(
          Semantics(
            label: 'This is not text',
            container: true,
            child: const SizedBox(
              width: 200.0,
              height: 200.0,
              child: Placeholder(),
            ),
355 356
          ),
        ),
357
      );
358 359 360 361 362 363 364 365

      final Evaluation result = await textContrastGuideline.evaluate(tester);
      expect(result.passed, true);
      handle.dispose();
    });

    testWidgets('offscreen text is skipped', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
366 367 368 369 370 371 372 373 374 375 376 377 378 379
      await tester.pumpWidget(
        _boilerplate(
          Stack(
            children: <Widget>[
              Positioned(
                left: -300.0,
                child: Container(
                  width: 200.0,
                  height: 200.0,
                  color: Colors.yellow,
                  child: const Text(
                    'this is a test',
                    style: TextStyle(fontSize: 14.0, color: Colors.yellowAccent),
                  ),
380 381
                ),
              ),
382 383 384 385
            ],
          ),
        ),
      );
386 387 388 389 390

      final Evaluation result = await textContrastGuideline.evaluate(tester);
      expect(result.passed, true);
      handle.dispose();
    });
391

392 393
    testWidgets('Disabled button is excluded from text contrast guideline',
        (WidgetTester tester) async {
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
      // Regression test https://github.com/flutter/flutter/issues/94428
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(
        _boilerplate(
          ElevatedButton(
            onPressed: null,
            child: Container(
              width: 200.0,
              height: 200.0,
              color: Colors.yellow,
              child: const Text(
                'this is a test',
                style: TextStyle(fontSize: 14.0, color: Colors.yellowAccent),
              ),
            ),
          ),
410
        ),
411 412 413 414
      );
      await expectLater(tester, meetsGuideline(textContrastGuideline));
      handle.dispose();
    });
415 416
  });

417
  group('custom minimum contrast guideline', () {
418
    Widget iconWidget({
419 420 421 422
      IconData icon = Icons.search,
      required Color color,
      required Color background,
    }) {
423 424 425 426 427 428 429
      return Container(
        padding: const EdgeInsets.all(8.0),
        color: background,
        child: Icon(icon, color: color),
      );
    }

430
    Widget textWidget({
431 432 433 434
      String text = 'Text',
      required Color color,
      required Color background,
    }) {
435 436 437 438 439 440 441
      return Container(
        padding: const EdgeInsets.all(8.0),
        color: background,
        child: Text(text, style: TextStyle(color: color)),
      );
    }

442
    Widget rowWidget(List<Widget> widgets) => _boilerplate(Row(children: widgets));
443

444 445 446
    final Finder findIcons = find.byWidgetPredicate((Widget widget) => widget is Icon);
    final Finder findTexts = find.byWidgetPredicate((Widget widget) => widget is Text);
    final Finder findIconsAndTexts = find.byWidgetPredicate((Widget widget) => widget is Icon || widget is Text);
447 448

    testWidgets('Black icons on white background', (WidgetTester tester) async {
449 450 451
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.black, background: Colors.white),
        iconWidget(color: Colors.black, background: Colors.white),
452 453
      ]));

454 455 456 457
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
458 459 460
    });

    testWidgets('Black icons on black background', (WidgetTester tester) async {
461 462 463
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.black, background: Colors.black),
        iconWidget(color: Colors.black, background: Colors.black),
464 465
      ]));

466 467 468 469
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
470 471
    });

472 473
    testWidgets('White icons on black background ("dark mode")',
        (WidgetTester tester) async {
474 475 476
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.white, background: Colors.black),
        iconWidget(color: Colors.white, background: Colors.black),
477 478
      ]));

479 480 481 482
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
483 484 485
    });

    testWidgets('Using different icons', (WidgetTester tester) async {
486 487 488 489 490
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.black, background: Colors.white, icon: Icons.more_horiz),
        iconWidget(color: Colors.black, background: Colors.white, icon: Icons.description),
        iconWidget(color: Colors.black, background: Colors.white, icon: Icons.image),
        iconWidget(color: Colors.black, background: Colors.white, icon: Icons.beach_access),
491 492
      ]));

493 494 495 496
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
497 498
    });

499 500
    testWidgets('One invalid instance fails entire test',
        (WidgetTester tester) async {
501 502 503
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.black, background: Colors.white),
        iconWidget(color: Colors.black, background: Colors.black),
504 505
      ]));

506 507 508 509
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
510 511
    });

512 513
    testWidgets('White on different colors, passing',
        (WidgetTester tester) async {
514 515 516 517 518
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.white, background: Colors.red[800]!, icon: Icons.more_horiz),
        iconWidget(color: Colors.white, background: Colors.green[800]!, icon: Icons.description),
        iconWidget(color: Colors.white, background: Colors.blue[800]!, icon: Icons.image),
        iconWidget(color: Colors.white, background: Colors.purple[800]!, icon: Icons.beach_access),
519 520
      ]));

521 522
      await expectLater(tester,
          meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
523 524
    });

525 526
    testWidgets('White on different colors, failing',
        (WidgetTester tester) async {
527 528 529 530 531
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.white, background: Colors.red[200]!, icon: Icons.more_horiz),
        iconWidget(color: Colors.white, background: Colors.green[400]!, icon: Icons.description),
        iconWidget(color: Colors.white, background: Colors.blue[600]!, icon: Icons.image),
        iconWidget(color: Colors.white, background: Colors.purple[800]!, icon: Icons.beach_access),
532 533
      ]));

534 535 536 537
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
538 539 540
    });

    testWidgets('Absence of icons, passing', (WidgetTester tester) async {
541
      await tester.pumpWidget(rowWidget(<Widget>[]));
542

543 544 545 546
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
547 548
    });

549 550
    testWidgets('Absence of icons, passing - 2nd test',
        (WidgetTester tester) async {
551 552 553
      await tester.pumpWidget(rowWidget(<Widget>[
        textWidget(color: Colors.black, background: Colors.white),
        textWidget(color: Colors.black, background: Colors.black),
554 555
      ]));

556 557 558 559
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
560 561
    });

562 563
    testWidgets('Guideline ignores widgets of other types',
        (WidgetTester tester) async {
564 565 566 567 568
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.black, background: Colors.white),
        iconWidget(color: Colors.black, background: Colors.white),
        textWidget(color: Colors.black, background: Colors.white),
        textWidget(color: Colors.black, background: Colors.black),
569 570
      ]));

571 572 573 574 575 576 577 578 579 580 581 582
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findTexts)),
      );
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIconsAndTexts)),
      );
583 584 585
    });

    testWidgets('Custom minimum ratio - Icons', (WidgetTester tester) async {
586 587 588
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.blue, background: Colors.white),
        iconWidget(color: Colors.black, background: Colors.white),
589 590
      ]));

591 592 593 594 595 596 597 598
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons, minimumRatio: 3.0)),
      );
599 600 601
    });

    testWidgets('Custom minimum ratio - Texts', (WidgetTester tester) async {
602 603 604
      await tester.pumpWidget(rowWidget(<Widget>[
        textWidget(color: Colors.blue, background: Colors.white),
        textWidget(color: Colors.black, background: Colors.white),
605 606
      ]));

607 608 609 610 611 612 613 614
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findTexts)),
      );
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findTexts, minimumRatio: 3.0)),
      );
615 616
    });

617 618 619
    testWidgets(
        'Custom minimum ratio - Different standards for icons and texts',
        (WidgetTester tester) async {
620 621 622 623 624
      await tester.pumpWidget(rowWidget(<Widget>[
        iconWidget(color: Colors.blue, background: Colors.white),
        iconWidget(color: Colors.black, background: Colors.white),
        textWidget(color: Colors.blue, background: Colors.white),
        textWidget(color: Colors.black, background: Colors.white),
625 626
      ]));

627 628 629 630 631 632 633 634
      await expectLater(
        tester,
        doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
      );
      await expectLater(
        tester,
        meetsGuideline(CustomMinimumContrastGuideline(finder: findTexts, minimumRatio: 3.0)),
      );
635 636 637
    });
  });

638 639 640 641
  group('tap target size guideline', () {
    testWidgets('Tappable box at 48 by 48', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(
642
        SizedBox(
643 644
          width: 48.0,
          height: 48.0,
645
          child: GestureDetector(onTap: () {}),
646 647 648 649 650 651 652 653 654
        ),
      ));
      await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
      handle.dispose();
    });

    testWidgets('Tappable box at 47 by 48', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(
655
        SizedBox(
656 657
          width: 47.0,
          height: 48.0,
658
          child: GestureDetector(onTap: () {}),
659 660
        ),
      ));
661 662 663 664
      await expectLater(
        tester,
        doesNotMeetGuideline(androidTapTargetGuideline),
      );
665 666 667 668 669 670
      handle.dispose();
    });

    testWidgets('Tappable box at 48 by 47', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(
671
        SizedBox(
672 673
          width: 48.0,
          height: 47.0,
674
          child: GestureDetector(onTap: () {}),
675 676
        ),
      ));
677 678 679 680
      await expectLater(
        tester,
        doesNotMeetGuideline(androidTapTargetGuideline),
      );
681 682 683
      handle.dispose();
    });

684 685
    testWidgets('Tappable box at 48 by 48 shrunk by transform',
        (WidgetTester tester) async {
686 687
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(
688
        Transform.scale(
689
          scale: 0.5, // should have new height of 24 by 24.
690
          child: SizedBox(
691 692
            width: 48.0,
            height: 48.0,
693
            child: GestureDetector(onTap: () {}),
694 695 696
          ),
        ),
      ));
697 698 699 700
      await expectLater(
        tester,
        doesNotMeetGuideline(androidTapTargetGuideline),
      );
701 702 703
      handle.dispose();
    });

704 705
    testWidgets('Too small tap target fails with the correct message',
        (WidgetTester tester) async {
706 707
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(
708
        SizedBox(
709 710
          width: 48.0,
          height: 47.0,
711
          child: GestureDetector(onTap: () {}),
712 713 714 715
        ),
      ));
      final Evaluation result = await androidTapTargetGuideline.evaluate(tester);
      expect(result.passed, false);
716 717 718 719 720 721 722 723
      expect(
        result.reason,
        'SemanticsNode#4(Rect.fromLTRB(376.0, 276.5, 424.0, 323.5), '
        'actions: [tap]): expected tap '
        'target size of at least Size(48.0, 48.0), '
        'but found Size(48.0, 47.0)\n'
        'See also: https://support.google.com/accessibility/android/answer/7101858?hl=en',
      );
724 725
      handle.dispose();
    });
726

727 728
    testWidgets('Box that overlaps edge of window is skipped',
        (WidgetTester tester) async {
729
      final SemanticsHandle handle = tester.ensureSemantics();
730
      final Widget smallBox = SizedBox(
731 732
        width: 48.0,
        height: 47.0,
733
        child: GestureDetector(onTap: () {}),
734 735
      );
      await tester.pumpWidget(
736 737
        MaterialApp(
          home: Stack(
738
            children: <Widget>[
739
              Positioned(
740 741 742 743 744 745 746 747 748 749 750 751 752
                left: 0.0,
                top: -1.0,
                child: smallBox,
              ),
            ],
          ),
        ),
      );

      final Evaluation overlappingTopResult = await androidTapTargetGuideline.evaluate(tester);
      expect(overlappingTopResult.passed, true);

      await tester.pumpWidget(
753 754
        MaterialApp(
          home: Stack(
755
            children: <Widget>[
756
              Positioned(
757 758 759 760 761 762 763 764 765 766 767 768 769
                left: -1.0,
                top: 0.0,
                child: smallBox,
              ),
            ],
          ),
        ),
      );

      final Evaluation overlappingLeftResult = await androidTapTargetGuideline.evaluate(tester);
      expect(overlappingLeftResult.passed, true);

      await tester.pumpWidget(
770 771
        MaterialApp(
          home: Stack(
772
            children: <Widget>[
773
              Positioned(
774 775 776 777 778 779 780 781 782 783 784 785
                bottom: -1.0,
                child: smallBox,
              ),
            ],
          ),
        ),
      );

      final Evaluation overlappingBottomResult = await androidTapTargetGuideline.evaluate(tester);
      expect(overlappingBottomResult.passed, true);

      await tester.pumpWidget(
786 787
        MaterialApp(
          home: Stack(
788
            children: <Widget>[
789
              Positioned(
790 791 792 793 794 795 796 797 798 799 800 801 802
                right: -1.0,
                child: smallBox,
              ),
            ],
          ),
        ),
      );

      final Evaluation overlappingRightResult = await androidTapTargetGuideline.evaluate(tester);
      expect(overlappingRightResult.passed, true);
      handle.dispose();
    });

803 804
    testWidgets('Does not fail on mergedIntoParent child',
        (WidgetTester tester) async {
805
      final SemanticsHandle handle = tester.ensureSemantics();
806 807 808 809 810 811 812 813 814 815 816
      await tester.pumpWidget(_boilerplate(MergeSemantics(
        child: Semantics(
          container: true,
          child: SizedBox(
            width: 50.0,
            height: 50.0,
            child: Semantics(
              container: true,
              child: GestureDetector(
                onTap: () {},
                child: const SizedBox(width: 4.0, height: 4.0),
817
              ),
818
            ),
819
          ),
820 821
        ),
      )));
822 823 824 825 826

      final Evaluation overlappingRightResult = await androidTapTargetGuideline.evaluate(tester);
      expect(overlappingRightResult.passed, true);
      handle.dispose();
    });
827 828 829 830 831 832 833 834

    testWidgets('Does not fail on links', (WidgetTester tester) async {
      Widget textWithLink() {
        return Builder(
          builder: (BuildContext context) {
            return RichText(
              text: TextSpan(
                children: <InlineSpan>[
835
                  const TextSpan(text: 'See examples at '),
836 837
                  TextSpan(
                    text: 'flutter repo',
838
                    recognizer: TapGestureRecognizer()..onTap = () {},
839 840 841 842 843 844 845 846 847 848 849 850 851 852
                  ),
                ],
              ),
            );
          },
        );
      }

      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(textWithLink()));

      await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
      handle.dispose();
    });
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

    testWidgets('Tap size test can handle partially off-screen items', (WidgetTester tester) async {
      final ScrollController controller = ScrollController();
      await tester.pumpWidget(
          MaterialApp(
            home: Scaffold(
              appBar: AppBar(title: const Text('Foo')),
              body: ListView(
                  controller: controller,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.only(left: 10, right: 10),
                      child: SizedBox(
                        width: 100,
                        height: 100,
                        child: Semantics(container: true, onTap: () {}, child: const Text('hello'))),
                    ),
                    Container(
                      height: 1000,
                      color: Colors.red,
                    ),
                  ]
              ),
            ),
          )
      );
      controller.jumpTo(90);
      await tester.pump();
      await expectLater(tester, meetsGuideline(iOSTapTargetGuideline));
    });
883
  });
884

885
  group('Labeled tappable node guideline', () {
886 887 888 889
    testWidgets('Passes when node is labeled', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(Semantics(
        container: true,
890
        onTap: () {},
891
        label: 'test',
892
        child: const SizedBox(width: 10.0, height: 10.0),
893 894 895 896 897
      )));
      final Evaluation result = await labeledTapTargetGuideline.evaluate(tester);
      expect(result.passed, true);
      handle.dispose();
    });
898 899
    testWidgets('Fails if long-press has no label',
        (WidgetTester tester) async {
900 901 902
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(Semantics(
        container: true,
903
        onLongPress: () {},
904
        label: '',
905
        child: const SizedBox(width: 10.0, height: 10.0),
906 907 908 909 910 911 912 913 914 915
      )));
      final Evaluation result = await labeledTapTargetGuideline.evaluate(tester);
      expect(result.passed, false);
      handle.dispose();
    });

    testWidgets('Fails if tap has no label', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(Semantics(
        container: true,
916
        onTap: () {},
917
        label: '',
918
        child: const SizedBox(width: 10.0, height: 10.0),
919 920 921 922 923 924
      )));
      final Evaluation result = await labeledTapTargetGuideline.evaluate(tester);
      expect(result.passed, false);
      handle.dispose();
    });

925 926
    testWidgets('Passes if tap is merged into labeled node',
        (WidgetTester tester) async {
927 928 929
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(Semantics(
        container: true,
930
        onLongPress: () {},
931 932 933 934 935 936 937 938 939 940
        label: '',
        child: Semantics(
          label: 'test',
          child: const SizedBox(width: 10.0, height: 10.0),
        ),
      )));
      final Evaluation result = await labeledTapTargetGuideline.evaluate(tester);
      expect(result.passed, true);
      handle.dispose();
    });
941 942 943 944 945 946 947 948

    testWidgets('Passes if text field does not have label', (WidgetTester tester) async {
      final SemanticsHandle handle = tester.ensureSemantics();
      await tester.pumpWidget(_boilerplate(const TextField()));
      final Evaluation result = await labeledTapTargetGuideline.evaluate(tester);
      expect(result.passed, true);
      handle.dispose();
    });
949 950
  });

951 952
  testWidgets('regression test for material widget',
      (WidgetTester tester) async {
953
    final SemanticsHandle handle = tester.ensureSemantics();
954 955 956 957 958 959
    await tester.pumpWidget(MaterialApp(
      theme: ThemeData.light(),
      home: Scaffold(
        backgroundColor: Colors.white,
        body: ElevatedButton(
          style: ElevatedButton.styleFrom(
960
            backgroundColor: const Color(0xFFFBBC04),
961 962 963 964
            elevation: 0,
          ),
          onPressed: () {},
          child: const Text('Button', style: TextStyle(color: Colors.black)),
965 966 967 968 969 970
        ),
      ),
    ));
    await expectLater(tester, meetsGuideline(textContrastGuideline));
    handle.dispose();
  });
971 972
}

973 974 975 976 977 978 979 980
Widget _boilerplate(Widget child, { bool? useMaterial3 }) {
  return MaterialApp(
    theme: ThemeData(useMaterial3: useMaterial3),
    home: Scaffold(
      body: Center(child: child),
    ),
  );
}