File size: 31,984 Bytes
88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 b0336a8 88b9d23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 207 208 209 210 211 212 213 214 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 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 459 460 461 462 463 464 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 547 548 549 550 551 552 553 554 555 556 557 558 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 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 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 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 | // 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/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('text contrast guideline', () {
testWidgets('black text on white background - Text Widget - direct style', (
WidgetTester tester,
) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
const 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', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
const Column(
children: <Widget>[
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(
const Column(
children: <Widget>[
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();
});
testWidgets('white text on black background - Text Widget - direct style', (
WidgetTester tester,
) async {
final SemanticsHandle handle = tester.ensureSemantics();
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),
),
),
),
);
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});
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,
child: const Column(
children: <Widget>[
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();
});
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),
];
Widget appWithTextWidget(TextStyle style) =>
_boilerplate(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();
await tester.pumpWidget(appWithTextWidget(style));
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();
await tester.pumpWidget(appWithTextWidget(style));
await expectLater(tester, doesNotMeetGuideline(textContrastGuideline));
handle.dispose();
});
}
testWidgets('black text on white background - Text Widget - direct style', (
WidgetTester tester,
) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
const Text('this is a test', style: TextStyle(fontSize: 14.0, color: Colors.black)),
),
);
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});
testWidgets('white text on black background - Text Widget - direct style', (
WidgetTester tester,
) async {
final SemanticsHandle handle = tester.ensureSemantics();
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),
),
),
),
);
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});
testWidgets('Material text field - amber on amber', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
Container(
width: 200.0,
height: 200.0,
color: Colors.amberAccent,
child: TextField(
style: const TextStyle(color: Colors.amber),
controller: TextEditingController(text: 'this is a test'),
),
),
),
);
await expectLater(tester, doesNotMeetGuideline(textContrastGuideline));
handle.dispose();
});
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();
});
testWidgets('Material text field - default style', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
SizedBox(
width: 100,
child: TextField(controller: TextEditingController(text: 'this is a test')),
),
),
);
await tester.idle();
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});
testWidgets('Material2: yellow text on yellow background fails with correct message', (
WidgetTester tester,
) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
useMaterial3: false,
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.17 for a font '
'size of 14.0.\n'
'The computed colors was:\n'
'light - ${const Color(0xfffafafa)}, dark - ${const Color(0xffffeb3b)}\n'
'See also: https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html',
);
handle.dispose();
});
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.16 for a font '
'size of 14.0.\n'
'The computed colors was:\n'
'light - ${const Color(0xfffef7ff)}, dark - ${const Color(0xffffeb3b)}\n'
'See also: https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html',
);
handle.dispose();
});
testWidgets('label without corresponding text is skipped', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
Semantics(
label: 'This is not text',
container: true,
child: const SizedBox(width: 200.0, height: 200.0, child: Placeholder()),
),
),
);
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();
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),
),
),
),
],
),
),
);
final Evaluation result = await textContrastGuideline.evaluate(tester);
expect(result.passed, true);
handle.dispose();
});
testWidgets('Disabled button is excluded from text contrast guideline', (
WidgetTester tester,
) async {
// 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),
),
),
),
),
);
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});
});
group('custom minimum contrast guideline', () {
Widget iconWidget({
IconData icon = Icons.search,
required Color color,
required Color background,
}) {
return Container(
padding: const EdgeInsets.all(8.0),
color: background,
child: Icon(icon, color: color),
);
}
Widget textWidget({String text = 'Text', required Color color, required Color background}) {
return Container(
padding: const EdgeInsets.all(8.0),
color: background,
child: Text(text, style: TextStyle(color: color)),
);
}
Widget rowWidget(List<Widget> widgets) => _boilerplate(Row(children: widgets));
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,
);
testWidgets('Black icons on white background', (WidgetTester tester) async {
await tester.pumpWidget(
rowWidget(<Widget>[
iconWidget(color: Colors.black, background: Colors.white),
iconWidget(color: Colors.black, background: Colors.white),
]),
);
await expectLater(tester, meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
});
testWidgets('Black icons on black background', (WidgetTester tester) async {
await tester.pumpWidget(
rowWidget(<Widget>[
iconWidget(color: Colors.black, background: Colors.black),
iconWidget(color: Colors.black, background: Colors.black),
]),
);
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
);
});
testWidgets('White icons on black background ("dark mode")', (WidgetTester tester) async {
await tester.pumpWidget(
rowWidget(<Widget>[
iconWidget(color: Colors.white, background: Colors.black),
iconWidget(color: Colors.white, background: Colors.black),
]),
);
await expectLater(tester, meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
});
testWidgets('Using different icons', (WidgetTester tester) async {
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),
]),
);
await expectLater(tester, meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
});
testWidgets('One invalid instance fails entire test', (WidgetTester tester) async {
await tester.pumpWidget(
rowWidget(<Widget>[
iconWidget(color: Colors.black, background: Colors.white),
iconWidget(color: Colors.black, background: Colors.black),
]),
);
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
);
});
testWidgets('White on different colors, passing', (WidgetTester tester) async {
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,
),
]),
);
await expectLater(tester, meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
});
testWidgets('White on different colors, failing', (WidgetTester tester) async {
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,
),
]),
);
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
);
});
testWidgets('Absence of icons, passing', (WidgetTester tester) async {
await tester.pumpWidget(rowWidget(<Widget>[]));
await expectLater(tester, meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
});
testWidgets('Absence of icons, passing - 2nd test', (WidgetTester tester) async {
await tester.pumpWidget(
rowWidget(<Widget>[
textWidget(color: Colors.black, background: Colors.white),
textWidget(color: Colors.black, background: Colors.black),
]),
);
await expectLater(tester, meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
});
testWidgets('Guideline ignores widgets of other types', (WidgetTester tester) async {
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),
]),
);
await expectLater(tester, meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons)));
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findTexts)),
);
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIconsAndTexts)),
);
});
testWidgets('Custom minimum ratio - Icons', (WidgetTester tester) async {
await tester.pumpWidget(
rowWidget(<Widget>[
iconWidget(color: Colors.blue, background: Colors.white),
iconWidget(color: Colors.black, background: Colors.white),
]),
);
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
);
await expectLater(
tester,
meetsGuideline(CustomMinimumContrastGuideline(finder: findIcons, minimumRatio: 3.0)),
);
});
testWidgets('Custom minimum ratio - Texts', (WidgetTester tester) async {
await tester.pumpWidget(
rowWidget(<Widget>[
textWidget(color: Colors.blue, background: Colors.white),
textWidget(color: Colors.black, background: Colors.white),
]),
);
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findTexts)),
);
await expectLater(
tester,
meetsGuideline(CustomMinimumContrastGuideline(finder: findTexts, minimumRatio: 3.0)),
);
});
testWidgets('Custom minimum ratio - Different standards for icons and texts', (
WidgetTester tester,
) async {
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),
]),
);
await expectLater(
tester,
doesNotMeetGuideline(CustomMinimumContrastGuideline(finder: findIcons)),
);
await expectLater(
tester,
meetsGuideline(CustomMinimumContrastGuideline(finder: findTexts, minimumRatio: 3.0)),
);
});
});
group('tap target size guideline', () {
testWidgets('Tappable box at 48 by 48', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(SizedBox(width: 48.0, height: 48.0, child: GestureDetector(onTap: () {}))),
);
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(SizedBox(width: 47.0, height: 48.0, child: GestureDetector(onTap: () {}))),
);
await expectLater(tester, doesNotMeetGuideline(androidTapTargetGuideline));
handle.dispose();
});
testWidgets('Tappable box at 48 by 47', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(SizedBox(width: 48.0, height: 47.0, child: GestureDetector(onTap: () {}))),
);
await expectLater(tester, doesNotMeetGuideline(androidTapTargetGuideline));
handle.dispose();
});
testWidgets('Tappable box at 48 by 48 shrunk by transform', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
Transform.scale(
scale: 0.5, // should have new height of 24 by 24.
child: SizedBox(width: 48.0, height: 48.0, child: GestureDetector(onTap: () {})),
),
),
);
await expectLater(tester, doesNotMeetGuideline(androidTapTargetGuideline));
handle.dispose();
});
testWidgets('Too small tap target fails with the correct message', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(SizedBox(width: 48.0, height: 47.0, child: GestureDetector(onTap: () {}))),
);
final Evaluation result = await androidTapTargetGuideline.evaluate(tester);
expect(result.passed, false);
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',
);
handle.dispose();
});
testWidgets('Box that overlaps edge of window is skipped', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
final Widget smallBox = SizedBox(
width: 48.0,
height: 47.0,
child: GestureDetector(onTap: () {}),
);
await tester.pumpWidget(
MaterialApp(
home: Stack(children: <Widget>[Positioned(left: 0.0, top: -1.0, child: smallBox)]),
),
);
final Evaluation overlappingTopResult = await androidTapTargetGuideline.evaluate(tester);
expect(overlappingTopResult.passed, true);
await tester.pumpWidget(
MaterialApp(
home: Stack(children: <Widget>[Positioned(left: -1.0, top: 0.0, child: smallBox)]),
),
);
final Evaluation overlappingLeftResult = await androidTapTargetGuideline.evaluate(tester);
expect(overlappingLeftResult.passed, true);
await tester.pumpWidget(
MaterialApp(home: Stack(children: <Widget>[Positioned(bottom: -1.0, child: smallBox)])),
);
final Evaluation overlappingBottomResult = await androidTapTargetGuideline.evaluate(tester);
expect(overlappingBottomResult.passed, true);
await tester.pumpWidget(
MaterialApp(home: Stack(children: <Widget>[Positioned(right: -1.0, child: smallBox)])),
);
final Evaluation overlappingRightResult = await androidTapTargetGuideline.evaluate(tester);
expect(overlappingRightResult.passed, true);
handle.dispose();
});
testWidgets('Does not fail on mergedIntoParent child', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
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),
),
),
),
),
),
),
);
final Evaluation overlappingRightResult = await androidTapTargetGuideline.evaluate(tester);
expect(overlappingRightResult.passed, true);
handle.dispose();
});
testWidgets('Does not fail on links', (WidgetTester tester) async {
Widget textWithLink() {
return Builder(
builder: (BuildContext context) {
return RichText(
text: TextSpan(
children: <InlineSpan>[
const TextSpan(text: 'See examples at '),
TextSpan(text: 'flutter repo', recognizer: TapGestureRecognizer()..onTap = () {}),
],
),
);
},
);
}
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(_boilerplate(textWithLink()));
await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
handle.dispose();
});
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));
});
});
group('Labeled tappable node guideline', () {
testWidgets('Passes when node is labeled', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
Semantics(
container: true,
onTap: () {},
label: 'test',
child: const SizedBox(width: 10.0, height: 10.0),
),
),
);
final Evaluation result = await labeledTapTargetGuideline.evaluate(tester);
expect(result.passed, true);
handle.dispose();
});
testWidgets('Fails if long-press has no label', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
Semantics(
container: true,
onLongPress: () {},
label: '',
child: const SizedBox(width: 10.0, height: 10.0),
),
),
);
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,
onTap: () {},
label: '',
child: const SizedBox(width: 10.0, height: 10.0),
),
),
);
final Evaluation result = await labeledTapTargetGuideline.evaluate(tester);
expect(result.passed, false);
handle.dispose();
});
testWidgets('Passes if tap is merged into labeled node', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
_boilerplate(
Semantics(
container: true,
onLongPress: () {},
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();
});
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();
});
});
testWidgets('regression test for material widget', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.white,
body: ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFFFBBC04), elevation: 0),
onPressed: () {},
child: const Text('Button', style: TextStyle(color: Colors.black)),
),
),
),
);
await expectLater(tester, meetsGuideline(textContrastGuideline));
handle.dispose();
});
}
Widget _boilerplate(Widget child, {bool? useMaterial3}) {
return MaterialApp(
theme: ThemeData(useMaterial3: useMaterial3),
home: Scaffold(body: Center(child: child)),
);
}
|