radio_test.dart 41.8 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 6 7 8
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])

9 10
import 'dart:ui';

11
import 'package:flutter/foundation.dart';
12
import 'package:flutter/material.dart';
13
import 'package:flutter/rendering.dart';
14
import 'package:flutter/services.dart';
15
import 'package:flutter/src/gestures/constants.dart';
16 17
import 'package:flutter_test/flutter_test.dart';

18
import '../rendering/mock_canvas.dart';
19 20
import '../widgets/semantics_tester.dart';

21
void main() {
22 23
  final ThemeData theme = ThemeData();

24
  testWidgets('Radio control test', (WidgetTester tester) async {
25
    final Key key = UniqueKey();
26
    final List<int?> log = <int?>[];
27

28 29 30 31 32 33 34 35 36 37
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Center(
          child: Radio<int>(
            key: key,
            value: 1,
            groupValue: 2,
            onChanged: log.add,
          ),
38 39 40 41 42 43 44 45 46
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, equals(<int>[1]));
    log.clear();

47 48 49 50 51 52 53 54 55 56 57
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Center(
          child: Radio<int>(
            key: key,
            value: 1,
            groupValue: 1,
            onChanged: log.add,
            activeColor: Colors.green[500],
          ),
58 59 60 61 62 63 64 65
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, isEmpty);

66 67 68 69 70 71 72 73 74 75
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Center(
          child: Radio<int>(
            key: key,
            value: 1,
            groupValue: 2,
            onChanged: null,
          ),
76 77 78 79 80 81 82 83
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, isEmpty);
  });
84

85 86
  testWidgets('Radio can be toggled when toggleable is set', (WidgetTester tester) async {
    final Key key = UniqueKey();
87
    final List<int?> log = <int?>[];
88

89 90 91 92 93 94 95 96 97 98 99
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Center(
          child: Radio<int>(
            key: key,
            value: 1,
            groupValue: 2,
            onChanged: log.add,
            toggleable: true,
          ),
100 101 102 103 104 105 106 107 108
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, equals(<int>[1]));
    log.clear();

109 110 111 112 113 114 115 116 117 118 119
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Center(
          child: Radio<int>(
            key: key,
            value: 1,
            groupValue: 1,
            onChanged: log.add,
            toggleable: true,
          ),
120 121 122 123 124 125
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

126
    expect(log, equals(<int?>[null]));
127 128
    log.clear();

129 130 131 132 133 134 135 136 137 138 139
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Center(
          child: Radio<int>(
            key: key,
            value: 1,
            groupValue: null,
            onChanged: log.add,
            toggleable: true,
          ),
140 141 142 143 144 145 146 147 148
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, equals(<int>[1]));
  });

149
  testWidgets('Radio size is configurable by ThemeData.materialTapTargetSize', (WidgetTester tester) async {
150
    final Key key1 = UniqueKey();
151
    await tester.pumpWidget(
152
      Theme(
153
        data: theme.copyWith(materialTapTargetSize: MaterialTapTargetSize.padded),
154
        child: Directionality(
155
          textDirection: TextDirection.ltr,
156 157 158
          child: Material(
            child: Center(
              child: Radio<bool>(
159 160 161
                key: key1,
                groupValue: true,
                value: true,
162
                onChanged: (bool? newValue) { },
163 164 165 166 167 168 169 170
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byKey(key1)), const Size(48.0, 48.0));
171

172
    final Key key2 = UniqueKey();
173
    await tester.pumpWidget(
174
      Theme(
175
        data: theme.copyWith(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
176
        child: Directionality(
177
          textDirection: TextDirection.ltr,
178 179 180
          child: Material(
            child: Center(
              child: Radio<bool>(
181 182 183
                key: key2,
                groupValue: true,
                value: true,
184
                onChanged: (bool? newValue) { },
185 186
              ),
            ),
187 188 189 190 191
          ),
        ),
      ),
    );

192
    expect(tester.getSize(find.byKey(key2)), const Size(40.0, 40.0));
193 194 195
  });


196
  testWidgets('Radio semantics', (WidgetTester tester) async {
197
    final SemanticsTester semantics = SemanticsTester(tester);
198

199 200 201 202 203 204 205 206
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Radio<int>(
          value: 1,
          groupValue: 2,
          onChanged: (int? i) { },
        ),
207 208 209
      ),
    ));

210
    expect(semantics, hasSemantics(TestSemantics.root(
211
      children: <TestSemantics>[
212
        TestSemantics.rootChild(
213 214 215 216 217 218
          id: 1,
          flags: <SemanticsFlag>[
            SemanticsFlag.isInMutuallyExclusiveGroup,
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isEnabled,
219
            SemanticsFlag.isFocusable,
220 221 222 223 224 225 226 227
          ],
          actions: <SemanticsAction>[
            SemanticsAction.tap,
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

228 229 230 231 232 233 234 235
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Radio<int>(
          value: 2,
          groupValue: 2,
          onChanged: (int? i) { },
        ),
236 237 238
      ),
    ));

239
    expect(semantics, hasSemantics(TestSemantics.root(
240
      children: <TestSemantics>[
241
        TestSemantics.rootChild(
242 243 244 245 246 247 248
          id: 1,
          flags: <SemanticsFlag>[
            SemanticsFlag.isInMutuallyExclusiveGroup,
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.isChecked,
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isEnabled,
249
            SemanticsFlag.isFocusable,
250 251 252 253 254 255 256 257
          ],
          actions: <SemanticsAction>[
            SemanticsAction.tap,
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

258 259 260 261 262 263 264 265
    await tester.pumpWidget(Theme(
      data: theme,
      child: const Material(
        child: Radio<int>(
          value: 1,
          groupValue: 2,
          onChanged: null,
        ),
266 267 268
      ),
    ));

269
    expect(semantics, hasSemantics(TestSemantics.root(
270
      children: <TestSemantics>[
271
        TestSemantics.rootChild(
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
          id: 1,
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isInMutuallyExclusiveGroup,
            SemanticsFlag.isFocusable,  // This flag is delayed by 1 frame.
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

    await tester.pump();

    // Now the isFocusable should be gone.
    expect(semantics, hasSemantics(TestSemantics.root(
      children: <TestSemantics>[
        TestSemantics.rootChild(
          id: 1,
290 291 292
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
293
            SemanticsFlag.isInMutuallyExclusiveGroup,
294 295 296 297 298
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

299 300 301 302 303 304 305 306
    await tester.pumpWidget(Theme(
      data: theme,
      child: const Material(
        child: Radio<int>(
          value: 2,
          groupValue: 2,
          onChanged: null,
        ),
307 308 309
      ),
    ));

310
    expect(semantics, hasSemantics(TestSemantics.root(
311
      children: <TestSemantics>[
312
        TestSemantics.rootChild(
313
          id: 1,
314 315 316 317
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.isChecked,
            SemanticsFlag.hasEnabledState,
318
            SemanticsFlag.isInMutuallyExclusiveGroup,
319 320 321 322 323 324 325
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

    semantics.dispose();
  });
326 327

  testWidgets('has semantic events', (WidgetTester tester) async {
328 329
    final SemanticsTester semantics = SemanticsTester(tester);
    final Key key = UniqueKey();
330
    dynamic semanticEvent;
331
    int? radioValue = 2;
332
    tester.binding.defaultBinaryMessenger.setMockDecodedMessageHandler<dynamic>(SystemChannels.accessibility, (dynamic message) async {
333 334 335
      semanticEvent = message;
    });

336 337 338 339 340 341 342 343 344 345 346
    await tester.pumpWidget(Theme(
      data: theme,
      child: Material(
        child: Radio<int>(
          key: key,
          value: 1,
          groupValue: radioValue,
          onChanged: (int? i) {
            radioValue = i;
          },
        ),
347 348 349 350
      ),
    ));

    await tester.tap(find.byKey(key));
351
    final RenderObject object = tester.firstRenderObject(find.byKey(key));
352 353 354 355

    expect(radioValue, 1);
    expect(semanticEvent, <String, dynamic>{
      'type': 'tap',
356
      'nodeId': object.debugSemantics!.id,
357 358
      'data': <String, dynamic>{},
    });
359
    expect(object.debugSemantics!.getSemanticsData().hasAction(SemanticsAction.tap), true);
360 361

    semantics.dispose();
362
    tester.binding.defaultBinaryMessenger.setMockDecodedMessageHandler<dynamic>(SystemChannels.accessibility, null);
363
  });
364

365
  testWidgets('Radio ink ripple is displayed correctly - M2', (WidgetTester tester) async {
366 367 368 369
    final Key painterKey = UniqueKey();
    const Key radioKey = Key('radio');

    await tester.pumpWidget(MaterialApp(
370
      theme: ThemeData(useMaterial3: false),
371 372 373 374 375 376 377 378 379 380 381 382
      home: Scaffold(
        body: RepaintBoundary(
          key: painterKey,
          child: Center(
            child: Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Radio<int>(
                key: radioKey,
                value: 1,
                groupValue: 1,
383
                onChanged: (int? value) { },
384
              ),
385
            ),
386 387 388 389 390 391 392 393 394
          ),
        ),
      ),
    ));

    await tester.press(find.byKey(radioKey));
    await tester.pumpAndSettle();
    await expectLater(
      find.byKey(painterKey),
395
      matchesGoldenFile('radio.ink_ripple.png'),
396
    );
397
  });
398

399 400 401 402 403
  testWidgets('Radio with splash radius set', (WidgetTester tester) async {
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    const double splashRadius = 30;
    Widget buildApp() {
      return MaterialApp(
404
        theme: theme,
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
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Container(
                width: 100,
                height: 100,
                color: Colors.white,
                child: Radio<int>(
                  value: 0,
                  onChanged: (int? newValue) {},
                  focusColor: Colors.orange[500],
                  autofocus: true,
                  groupValue: 0,
                  splashRadius: splashRadius,
                ),
              );
            }),
          ),
        ),
      );
    }
    await tester.pumpWidget(buildApp());
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(
        find.byWidgetPredicate((Widget widget) => widget is Radio<int>),
      )),
432
      paints..circle(color: Colors.orange[500], radius: splashRadius),
433 434 435
    );
  });

436 437 438
  testWidgets('Radio is focusable and has correct focus color', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'Radio');
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
439
    int? groupValue = 0;
440 441 442
    const Key radioKey = Key('radio');
    Widget buildApp({bool enabled = true}) {
      return MaterialApp(
443
        theme: theme,
444 445 446 447 448 449 450 451 452 453
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Container(
                width: 100,
                height: 100,
                color: Colors.white,
                child: Radio<int>(
                  key: radioKey,
                  value: 0,
454
                  onChanged: enabled ? (int? newValue) {
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
                    setState(() {
                      groupValue = newValue;
                    });
                  } : null,
                  focusColor: Colors.orange[500],
                  autofocus: true,
                  focusNode: focusNode,
                  groupValue: groupValue,
                ),
              );
            }),
          ),
        ),
      );
    }
    await tester.pumpWidget(buildApp());

    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isTrue);
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
479 480
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
481
        ..circle(color: Colors.orange[500])
482 483
        ..circle(color: const Color(0xff2196f3))
        ..circle(color: const Color(0xff2196f3)),
484 485 486 487 488 489 490 491 492
    );

    // Check when the radio isn't selected.
    groupValue = 1;
    await tester.pumpWidget(buildApp());
    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isTrue);
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
493 494 495 496 497 498 499 500 501
      theme.useMaterial3
        ? (paints..rect()..circle(color: Colors.orange[500])..circle(color: theme.colorScheme.onSurface))
        : (paints
          ..rect(
              color: const Color(0xffffffff),
              rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
            )
          ..circle(color: Colors.orange[500])
          ..circle(color: const Color(0x8a000000), style: PaintingStyle.stroke, strokeWidth: 2.0)),
502 503 504 505 506 507 508 509 510 511 512 513
    );

    // Check when the radio is selected, but disabled.
    groupValue = 0;
    await tester.pumpWidget(buildApp(enabled: false));
    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isFalse);
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
514 515
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
516 517 518 519 520
        ..circle(color: const Color(0x61000000))
        ..circle(color: const Color(0x61000000)),
    );
  });

521
  testWidgets('Radio can be hovered and has correct hover color', (WidgetTester tester) async {
522
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
523
    int? groupValue = 0;
524 525 526
    const Key radioKey = Key('radio');
    Widget buildApp({bool enabled = true}) {
      return MaterialApp(
527
        theme: theme,
528 529 530 531 532 533 534 535 536 537
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Container(
                width: 100,
                height: 100,
                color: Colors.white,
                child: Radio<int>(
                  key: radioKey,
                  value: 0,
538
                  onChanged: enabled ? (int? newValue) {
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
                    setState(() {
                      groupValue = newValue;
                    });
                  } : null,
                  hoverColor: Colors.orange[500],
                  groupValue: groupValue,
                ),
              );
            }),
          ),
        ),
      );
    }
    await tester.pumpWidget(buildApp());

554
    await tester.pump();
555 556 557 558 559 560
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
561 562
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
563 564
        ..circle(color: const Color(0xff2196f3))
        ..circle(color: const Color(0xff2196f3)),
565 566 567 568 569 570 571 572 573
    );

    // Start hovering
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.moveTo(tester.getCenter(find.byKey(radioKey)));

    // Check when the radio isn't selected.
    groupValue = 1;
    await tester.pumpWidget(buildApp());
574
    await tester.pump();
575 576
    await tester.pumpAndSettle();
    expect(
577 578 579 580 581 582 583 584
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
        ..circle(color: Colors.orange[500])
        ..circle(color: theme.useMaterial3 ? theme.colorScheme.onSurface : const Color(0x8a000000), style: PaintingStyle.stroke, strokeWidth: 2.0),
585 586 587 588 589
    );

    // Check when the radio is selected, but disabled.
    groupValue = 0;
    await tester.pumpWidget(buildApp(enabled: false));
590
    await tester.pump();
591 592 593 594 595 596
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
597 598
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
599 600 601 602 603
        ..circle(color: const Color(0x61000000))
        ..circle(color: const Color(0x61000000)),
    );
  });

604
  testWidgets('Radio can be controlled by keyboard shortcuts', (WidgetTester tester) async {
605
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
606
    int? groupValue = 1;
607 608 609 610 611 612
    const Key radioKey0 = Key('radio0');
    const Key radioKey1 = Key('radio1');
    const Key radioKey2 = Key('radio2');
    final FocusNode focusNode2 = FocusNode(debugLabel: 'radio2');
    Widget buildApp({bool enabled = true}) {
      return MaterialApp(
613
        theme: theme,
614 615 616 617 618 619 620 621 622 623 624 625
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Container(
                width: 200,
                height: 100,
                color: Colors.white,
                child: Row(
                  children: <Widget>[
                    Radio<int>(
                      key: radioKey0,
                      value: 0,
626
                      onChanged: enabled ? (int? newValue) {
627 628 629 630 631 632 633 634 635 636 637
                        setState(() {
                          groupValue = newValue;
                        });
                      } : null,
                      hoverColor: Colors.orange[500],
                      groupValue: groupValue,
                      autofocus: true,
                    ),
                    Radio<int>(
                      key: radioKey1,
                      value: 1,
638
                      onChanged: enabled ? (int? newValue) {
639 640 641 642 643 644 645 646 647 648
                        setState(() {
                          groupValue = newValue;
                        });
                      } : null,
                      hoverColor: Colors.orange[500],
                      groupValue: groupValue,
                    ),
                    Radio<int>(
                      key: radioKey2,
                      value: 2,
649
                      onChanged: enabled ? (int? newValue) {
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
                        setState(() {
                          groupValue = newValue;
                        });
                      } : null,
                      hoverColor: Colors.orange[500],
                      groupValue: groupValue,
                      focusNode: focusNode2,
                    ),
                  ],
                ),
              );
            }),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp());
    await tester.pumpAndSettle();

    await tester.sendKeyEvent(LogicalKeyboardKey.enter);
    await tester.pumpAndSettle();
    // On web, radios don't respond to the enter key.
    expect(groupValue, kIsWeb ? equals(1) : equals(0));

    focusNode2.requestFocus();
    await tester.pumpAndSettle();

    await tester.sendKeyEvent(LogicalKeyboardKey.space);
    await tester.pumpAndSettle();
    expect(groupValue, equals(2));
  });

683 684 685
  testWidgets('Radio responds to density changes.', (WidgetTester tester) async {
    const Key key = Key('test');
    Future<void> buildTest(VisualDensity visualDensity) async {
686
      return tester.pumpWidget(
687
        MaterialApp(
688
          theme: theme,
689 690 691 692 693
          home: Material(
            child: Center(
              child: Radio<int>(
                visualDensity: visualDensity,
                key: key,
694
                onChanged: (int? value) {},
695 696 697 698 699 700 701 702 703
                value: 0,
                groupValue: 0,
              ),
            ),
          ),
        ),
      );
    }

704
    await buildTest(VisualDensity.standard);
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
    final RenderBox box = tester.renderObject(find.byKey(key));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(48, 48)));

    await buildTest(const VisualDensity(horizontal: 3.0, vertical: 3.0));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(60, 60)));

    await buildTest(const VisualDensity(horizontal: -3.0, vertical: -3.0));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(36, 36)));

    await buildTest(const VisualDensity(horizontal: 3.0, vertical: -3.0));
    await tester.pumpAndSettle();
    expect(box.size, equals(const Size(60, 36)));
  });
721 722 723 724 725 726

  testWidgets('Radio changes mouse cursor when hovered', (WidgetTester tester) async {
    const Key key = ValueKey<int>(1);
    // Test Radio() constructor
    await tester.pumpWidget(
      MaterialApp(
727
        theme: theme,
728 729 730 731 732 733 734 735 736 737
        home: Scaffold(
          body: Align(
            alignment: Alignment.topLeft,
            child: Material(
              child: MouseRegion(
                cursor: SystemMouseCursors.forbidden,
                child: Radio<int>(
                  key: key,
                  mouseCursor: SystemMouseCursors.text,
                  value: 1,
738
                  onChanged: (int? v) {},
739 740 741 742 743 744 745 746 747 748 749 750 751 752
                  groupValue: 2,
                ),
              ),
            ),
          ),
        ),
      ),
    );

    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse, pointer: 1);
    await gesture.addPointer(location: tester.getCenter(find.byKey(key)));

    await tester.pump();

753
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.text);
754 755 756 757 758


    // Test default cursor
    await tester.pumpWidget(
      MaterialApp(
759
        theme: theme,
760 761 762 763 764 765 766 767
        home: Scaffold(
          body: Align(
            alignment: Alignment.topLeft,
            child: Material(
              child: MouseRegion(
                cursor: SystemMouseCursors.forbidden,
                child: Radio<int>(
                  value: 1,
768
                  onChanged: (int? v) {},
769 770 771 772 773 774 775 776 777
                  groupValue: 2,
                ),
              ),
            ),
          ),
        ),
      ),
    );

778
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.click);
779 780 781

    // Test default cursor when disabled
    await tester.pumpWidget(
782 783 784
      MaterialApp(
        theme: theme,
        home: const Scaffold(
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
          body: Align(
            alignment: Alignment.topLeft,
            child: Material(
              child: MouseRegion(
                cursor: SystemMouseCursors.forbidden,
                child: Radio<int>(
                  value: 1,
                  onChanged: null,
                  groupValue: 2,
                ),
              ),
            ),
          ),
        ),
      ),
    );

802
    expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic);
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

  testWidgets('Radio button fill color resolves in enabled/disabled states', (WidgetTester tester) async {
    const Color activeEnabledFillColor = Color(0xFF000001);
    const Color activeDisabledFillColor = Color(0xFF000002);
    const Color inactiveEnabledFillColor = Color(0xFF000003);
    const Color inactiveDisabledFillColor = Color(0xFF000004);

    Color getFillColor(Set<MaterialState> states) {
      if (states.contains(MaterialState.disabled)) {
        if (states.contains(MaterialState.selected)) {
          return activeDisabledFillColor;
        }
        return inactiveDisabledFillColor;
      }
      if (states.contains(MaterialState.selected)) {
        return activeEnabledFillColor;
      }
      return inactiveEnabledFillColor;
    }

    final MaterialStateProperty<Color> fillColor =
      MaterialStateColor.resolveWith(getFillColor);

    int? groupValue = 0;
    const Key radioKey = Key('radio');
    Widget buildApp({required bool enabled}) {
      return MaterialApp(
831
        theme: theme,
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
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Container(
                width: 100,
                height: 100,
                color: Colors.white,
                child: Radio<int>(
                  key: radioKey,
                  value: 0,
                  fillColor: fillColor,
                  onChanged: enabled ? (int? newValue) {
                    setState(() {
                      groupValue = newValue;
                    });
                  } : null,
                  groupValue: groupValue,
                ),
              );
            }),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp(enabled: true));

    // Selected and enabled.
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
866 867
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
868 869 870 871 872 873 874 875 876 877 878 879 880
        ..circle(color: activeEnabledFillColor)
        ..circle(color: activeEnabledFillColor),
    );

    // Check when the radio isn't selected.
    groupValue = 1;
    await tester.pumpWidget(buildApp(enabled: true));
    await tester.pumpAndSettle();
    expect(
        Material.of(tester.element(find.byKey(radioKey))),
        paints
          ..rect(
              color: const Color(0xffffffff),
881 882 883
              rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
            )
          ..circle(color: inactiveEnabledFillColor, style: PaintingStyle.stroke, strokeWidth: 2.0),
884 885 886 887 888 889 890 891 892 893 894
    );

    // Check when the radio is selected, but disabled.
    groupValue = 0;
    await tester.pumpWidget(buildApp(enabled: false));
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
895 896
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
897 898 899 900 901 902 903 904 905 906 907 908 909
        ..circle(color: activeDisabledFillColor)
        ..circle(color: activeDisabledFillColor),
    );

    // Check when the radio is unselected and disabled.
    groupValue = 1;
    await tester.pumpWidget(buildApp(enabled: false));
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
910 911
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
912 913 914 915
        ..circle(color: inactiveDisabledFillColor, style: PaintingStyle.stroke, strokeWidth: 2.0),
    );
  });

916 917
  testWidgets('Radio fill color resolves in hovered/focused states', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'radio');
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    const Color hoveredFillColor = Color(0xFF000001);
    const Color focusedFillColor = Color(0xFF000002);

    Color getFillColor(Set<MaterialState> states) {
      if (states.contains(MaterialState.hovered)) {
        return hoveredFillColor;
      }
      if (states.contains(MaterialState.focused)) {
        return focusedFillColor;
      }
      return Colors.transparent;
    }

    final MaterialStateProperty<Color> fillColor =
      MaterialStateColor.resolveWith(getFillColor);

    int? groupValue = 0;
    const Key radioKey = Key('radio');
    Widget buildApp() {
      return MaterialApp(
939
        theme: theme,
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
        home: Material(
          child: Center(
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return Container(
                width: 100,
                height: 100,
                color: Colors.white,
                child: Radio<int>(
                  autofocus: true,
                  focusNode: focusNode,
                  key: radioKey,
                  value: 0,
                  fillColor: fillColor,
                  onChanged: (int? newValue) {
                    setState(() {
                      groupValue = newValue;
                    });
                  },
                  groupValue: groupValue,
                ),
              );
            }),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp());
    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isTrue);
    expect(
      Material.of(tester.element(find.byKey(radioKey))),
972 973 974 975 976 977 978 979 980
      theme.useMaterial3
        ? (paints..rect()..circle(color: theme.colorScheme.primary.withOpacity(0.12))..circle(color: focusedFillColor))
        : (paints
          ..rect(
              color: const Color(0xffffffff),
              rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
            )
          ..circle(color: Colors.black12)
          ..circle(color: focusedFillColor)),
981 982 983 984 985 986 987 988 989 990 991 992 993
    );

    // Start hovering
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.addPointer();
    await gesture.moveTo(tester.getCenter(find.byKey(radioKey)));
    await tester.pumpAndSettle();

    expect(
      Material.of(tester.element(find.byKey(radioKey))),
      paints
        ..rect(
            color: const Color(0xffffffff),
994 995
            rect: const Rect.fromLTRB(350.0, 250.0, 450.0, 350.0),
          )
996
        ..circle(color: theme.useMaterial3 ? theme.colorScheme.primary.withOpacity(0.08) : Colors.black12)
997 998 999
        ..circle(color: hoveredFillColor),
    );
  });
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

  testWidgets('Radio overlay color resolves in active/pressed/focused/hovered states', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'Radio');
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;

    const Color fillColor = Color(0xFF000000);
    const Color activePressedOverlayColor = Color(0xFF000001);
    const Color inactivePressedOverlayColor = Color(0xFF000002);
    const Color hoverOverlayColor = Color(0xFF000003);
    const Color focusOverlayColor = Color(0xFF000004);
    const Color hoverColor = Color(0xFF000005);
    const Color focusColor = Color(0xFF000006);

    Color? getOverlayColor(Set<MaterialState> states) {
      if (states.contains(MaterialState.pressed)) {
        if (states.contains(MaterialState.selected)) {
          return activePressedOverlayColor;
        }
        return inactivePressedOverlayColor;
      }
      if (states.contains(MaterialState.hovered)) {
        return hoverOverlayColor;
      }
      if (states.contains(MaterialState.focused)) {
        return focusOverlayColor;
      }
      return null;
    }
    const double splashRadius = 24.0;

1030
    Finder findRadio() {
1031 1032 1033
      return find.byWidgetPredicate((Widget widget) => widget is Radio<bool>);
    }

1034 1035
    MaterialInkController? getRadioMaterial(WidgetTester tester) {
      return Material.of(tester.element(findRadio()));
1036 1037 1038 1039
    }

    Widget buildRadio({bool active = false, bool focused = false, bool useOverlay = true}) {
      return MaterialApp(
1040
        theme: theme,
1041 1042 1043 1044 1045 1046 1047
        home: Scaffold(
          body: Radio<bool>(
            focusNode: focusNode,
            autofocus: focused,
            value: active,
            groupValue: true,
            onChanged: (_) { },
1048
            fillColor: const MaterialStatePropertyAll<Color>(fillColor),
1049 1050 1051 1052 1053 1054 1055 1056 1057
            overlayColor: useOverlay ? MaterialStateProperty.resolveWith(getOverlayColor) : null,
            hoverColor: hoverColor,
            focusColor: focusColor,
            splashRadius: splashRadius,
          ),
        ),
      );
    }

1058
    await tester.pumpWidget(buildRadio(useOverlay: false));
1059
    await tester.press(findRadio());
1060 1061 1062
    await tester.pumpAndSettle();

    expect(
1063
      getRadioMaterial(tester),
1064 1065 1066 1067 1068 1069 1070 1071 1072
      paints
        ..circle(
          color: fillColor.withAlpha(kRadialReactionAlpha),
          radius: splashRadius,
        ),
      reason: 'Default inactive pressed Radio should have overlay color from fillColor',
    );

    await tester.pumpWidget(buildRadio(active: true, useOverlay: false));
1073
    await tester.press(findRadio());
1074 1075 1076
    await tester.pumpAndSettle();

    expect(
1077
      getRadioMaterial(tester),
1078 1079 1080 1081 1082 1083 1084 1085
      paints
        ..circle(
          color: fillColor.withAlpha(kRadialReactionAlpha),
          radius: splashRadius,
        ),
      reason: 'Default active pressed Radio should have overlay color from fillColor',
    );

1086
    await tester.pumpWidget(buildRadio());
1087
    await tester.press(findRadio());
1088 1089 1090
    await tester.pumpAndSettle();

    expect(
1091
      getRadioMaterial(tester),
1092 1093 1094 1095 1096 1097 1098 1099 1100
      paints
        ..circle(
          color: inactivePressedOverlayColor,
          radius: splashRadius,
        ),
      reason: 'Inactive pressed Radio should have overlay color: $inactivePressedOverlayColor',
    );

    await tester.pumpWidget(buildRadio(active: true));
1101
    await tester.press(findRadio());
1102 1103 1104
    await tester.pumpAndSettle();

    expect(
1105
      getRadioMaterial(tester),
1106 1107 1108 1109 1110 1111 1112 1113
      paints
        ..circle(
          color: activePressedOverlayColor,
          radius: splashRadius,
        ),
      reason: 'Active pressed Radio should have overlay color: $activePressedOverlayColor',
    );

1114
    await tester.pumpWidget(Container());
1115 1116 1117 1118 1119
    await tester.pumpWidget(buildRadio(focused: true));
    await tester.pumpAndSettle();

    expect(focusNode.hasPrimaryFocus, isTrue);
    expect(
1120
      getRadioMaterial(tester),
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
      paints
        ..circle(
          color: focusOverlayColor,
          radius: splashRadius,
        ),
      reason: 'Focused Radio should use overlay color $focusOverlayColor over $focusColor',
    );

    // Start hovering
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.addPointer();
1132
    await gesture.moveTo(tester.getCenter(findRadio()));
1133 1134 1135
    await tester.pumpAndSettle();

    expect(
1136
      getRadioMaterial(tester),
1137 1138 1139 1140 1141 1142 1143 1144
      paints
        ..circle(
          color: hoverOverlayColor,
          radius: splashRadius,
        ),
      reason: 'Hovered Radio should use overlay color $hoverOverlayColor over $hoverColor',
    );
  });
1145 1146 1147 1148 1149 1150

  testWidgets('Do not crash when widget disappears while pointer is down', (WidgetTester tester) async {
    final Key key = UniqueKey();

    Widget buildRadio(bool show) {
      return MaterialApp(
1151
        theme: theme,
1152 1153 1154
        home: Material(
          child: Center(
            child: show ? Radio<bool>(key: key, value: true, groupValue: false, onChanged: (_) { }) : Container(),
1155
          ),
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
        ),
      );
    }

    await tester.pumpWidget(buildRadio(true));
    final Offset center = tester.getCenter(find.byKey(key));
    // Put a pointer down on the screen.
    final TestGesture gesture = await tester.startGesture(center);
    await tester.pump();
    // While the pointer is down, the widget disappears.
    await tester.pumpWidget(buildRadio(false));
    expect(find.byKey(key), findsNothing);
    // Release pointer after widget disappeared.
1169
    await gesture.up();
1170
  });
1171 1172 1173 1174 1175

  testWidgets('disabled radio shows tooltip', (WidgetTester tester) async {
    const String longPressTooltip = 'long press tooltip';
    const String tapTooltip = 'tap tooltip';
    await tester.pumpWidget(
1176 1177 1178
      MaterialApp(
        theme: theme,
        home: const Material(
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
          child: Tooltip(
            message: longPressTooltip,
            child: Radio<bool>(value: true, groupValue: false, onChanged: null),
          ),
        ),
      )
    );

    // Default tooltip shows up after long pressed.
    final Finder tooltip0 = find.byType(Tooltip);
    expect(find.text(longPressTooltip), findsNothing);

    await tester.tap(tooltip0);
    await tester.pump(const Duration(milliseconds: 10));
    expect(find.text(longPressTooltip), findsNothing);

    final TestGesture gestureLongPress = await tester.startGesture(tester.getCenter(tooltip0));
    await tester.pump();
    await tester.pump(kLongPressTimeout);
    await gestureLongPress.up();
    await tester.pump();

    expect(find.text(longPressTooltip), findsOneWidget);

    // Tooltip shows up after tapping when set triggerMode to TooltipTriggerMode.tap.
    await tester.pumpWidget(
1205 1206 1207
      MaterialApp(
        theme: theme,
        home: const Material(
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
          child: Tooltip(
            triggerMode: TooltipTriggerMode.tap,
            message: tapTooltip,
            child: Radio<bool>(value: true, groupValue: false, onChanged: null),
          ),
        ),
      )
    );

    final Finder tooltip1 = find.byType(Tooltip);
    expect(find.text(tapTooltip), findsNothing);

    await tester.tap(tooltip1);
    await tester.pump(const Duration(milliseconds: 10));
    expect(find.text(tapTooltip), findsOneWidget);
  });
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373

  testWidgets('Radio button default colors', (WidgetTester tester) async {
    Widget buildRadio({bool enabled = true, bool selected = true}) {
      return MaterialApp(
        theme: theme,
        home: Scaffold(
          body: Radio<bool>(
            value: true,
            groupValue: true,
            onChanged: enabled ? (_) {} : null,
          ),
        )
      );
    }

    await tester.pumpWidget(buildRadio());
    await tester.pumpAndSettle();

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      paints
        ..circle(color: const Color(0xFF2196F3)) // Outer circle - blue primary value
        ..circle(color: const Color(0xFF2196F3))..restore(), // Inner circle - blue primary value
    );

    await tester.pumpWidget(Container());
    await tester.pumpWidget(buildRadio(selected: false));
    await tester.pumpAndSettle();

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      paints
        ..save()
        ..circle(color: const Color(0xFF2196F3))
        ..restore(),
    );

    await tester.pumpWidget(Container());
    await tester.pumpWidget(buildRadio(enabled: false));
    await tester.pumpAndSettle();

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      theme.useMaterial3
        ? (paints
          ..circle(color: theme.colorScheme.onSurface.withOpacity(0.38)))
        : (paints..circle(color: Colors.black38))
    );
  });

  testWidgets('Radio button default overlay colors in hover/focus/press states', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'Radio');
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;

    final ColorScheme colors = theme.colorScheme;
    final bool material3 = theme.useMaterial3;
    Widget buildRadio({bool enabled = true, bool focused = false, bool selected = true}) {
      return MaterialApp(
        theme: theme,
        home: Scaffold(
          body: Radio<bool>(
            focusNode: focusNode,
            autofocus: focused,
            value: true,
            groupValue: selected,
            onChanged: enabled ? (_) {} : null,
          ),
        ),
      );
    }

    // default selected radio
    await tester.pumpWidget(buildRadio());
    await tester.pumpAndSettle();
    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      material3
        ? (paints..circle(color: colors.primary.withOpacity(1)))
        : (paints..circle(color: colors.secondary))
    );

    // selected radio in pressed state
    await tester.pumpWidget(buildRadio());
    await tester.startGesture(tester.getCenter(find.byType(Radio<bool>)));
    await tester.pumpAndSettle();

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      paints..circle(color: material3
        ? colors.onSurface.withOpacity(0.12)
        : colors.secondary.withAlpha(0x1F))
      ..circle(color: material3
        ? colors.primary.withOpacity(1)
        : colors.secondary
      )
    );

    // unselected radio in pressed state
    await tester.pumpWidget(buildRadio(selected: false));
    await tester.startGesture(tester.getCenter(find.byType(Radio<bool>)));
    await tester.pumpAndSettle();

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      material3
        ? (paints..circle(color: colors.primary.withOpacity(0.12))..circle(color: colors.onSurface.withOpacity(1)))
        : (paints..circle(color: theme.unselectedWidgetColor.withAlpha(0x1F))..circle(color: theme.unselectedWidgetColor))
    );

    // selected radio in focused state
    await tester.pumpWidget(Container()); // reset test
    await tester.pumpWidget(buildRadio(focused: true));
    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isTrue);

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      material3
        ? (paints..circle(color: colors.primary.withOpacity(0.12))..circle(color: colors.primary.withOpacity(1)))
        : (paints..circle(color: theme.focusColor)..circle(color: colors.secondary))
    );

    // unselected radio in focused state
    await tester.pumpWidget(Container()); // reset test
    await tester.pumpWidget(buildRadio(focused: true, selected: false));
    await tester.pumpAndSettle();
    expect(focusNode.hasPrimaryFocus, isTrue);

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      material3
        ? (paints..circle(color: colors.onSurface.withOpacity(0.12))..circle(color: colors.onSurface.withOpacity(1)))
        : (paints..circle(color: theme.focusColor)..circle(color: theme.unselectedWidgetColor))
    );

    // selected radio in hovered state
    await tester.pumpWidget(Container()); // reset test
    await tester.pumpWidget(buildRadio());
    final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await gesture.addPointer();
    await gesture.moveTo(tester.getCenter(find.byType(Radio<bool>)));
    await tester.pumpAndSettle();

    expect(
      Material.of(tester.element(find.byType(Radio<bool>))),
      material3
        ? (paints..circle(color: colors.primary.withOpacity(0.08))..circle(color: colors.primary.withOpacity(1)))
        : (paints..circle(color: theme.hoverColor)..circle(color: colors.secondary))
    );
  });
1374
}