chip_test.dart 137 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
import 'package:flutter/scheduler.dart';
9
import 'package:flutter_test/flutter_test.dart';
10
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
11
import '../widgets/semantics_tester.dart';
12 13
import 'feedback_tester.dart';

14
Finder findRenderChipElement() {
15
  return find.byElementPredicate((Element e) => '${e.renderObject.runtimeType}' == '_RenderChip');
16
}
17

18 19 20 21 22 23 24 25 26
RenderBox getMaterialBox(WidgetTester tester) {
  return tester.firstRenderObject<RenderBox>(
    find.descendant(
      of: find.byType(RawChip),
      matching: find.byType(CustomPaint),
    ),
  );
}

27 28 29 30 31 32 33 34 35
Material getMaterial(WidgetTester tester) {
  return tester.widget<Material>(
    find.descendant(
      of: find.byType(RawChip),
      matching: find.byType(Material),
    ),
  );
}

36 37 38 39 40 41 42 43 44 45
IconThemeData getIconData(WidgetTester tester) {
  final IconTheme iconTheme = tester.firstWidget(
    find.descendant(
      of: find.byType(RawChip),
      matching: find.byType(IconTheme),
    ),
  );
  return iconTheme.data;
}

46
DefaultTextStyle getLabelStyle(WidgetTester tester, String labelText) {
47
  return tester.widget(
48 49
    find.ancestor(
      of: find.text(labelText),
50
      matching: find.byType(DefaultTextStyle),
51
    ).first,
52 53 54
  );
}

55 56 57 58
dynamic getRenderChip(WidgetTester tester) {
  if (!tester.any(findRenderChipElement())) {
    return null;
  }
59
  final Element element = tester.element(findRenderChipElement().first);
60 61 62
  return element.renderObject;
}

63
// ignore: avoid_dynamic_calls
64
double getSelectProgress(WidgetTester tester) => getRenderChip(tester)?.checkmarkAnimation?.value as double;
65
// ignore: avoid_dynamic_calls
66
double getAvatarDrawerProgress(WidgetTester tester) => getRenderChip(tester)?.avatarDrawerAnimation?.value as double;
67
// ignore: avoid_dynamic_calls
68
double getDeleteDrawerProgress(WidgetTester tester) => getRenderChip(tester)?.deleteDrawerAnimation?.value as double;
69

70
/// Adds the basic requirements for a Chip.
71
Widget wrapForChip({
72
  required Widget child,
73 74
  TextDirection textDirection = TextDirection.ltr,
  double textScaleFactor = 1.0,
75
  Brightness brightness = Brightness.light,
76
  bool? useMaterial3,
77
}) {
78
  return MaterialApp(
79
    theme: ThemeData(brightness: brightness, useMaterial3: useMaterial3),
80
    home: Directionality(
81
      textDirection: textDirection,
82
      child: MediaQuery(
83
        data: MediaQueryData(textScaleFactor: textScaleFactor),
84
        child: Material(child: child),
85 86 87 88 89
      ),
    ),
  );
}

90 91 92 93
/// Tests that a [Chip] that has its size constrained by its parent is
/// further constraining the size of its child, the label widget.
/// Optionally, adding an avatar or delete icon to the chip should not
/// cause the chip or label to exceed its constrained height.
94
Future<void> testConstrainedLabel(
95
  WidgetTester tester, {
96 97
  CircleAvatar? avatar,
  VoidCallback? onDeleted,
98 99 100 101 102
}) async {
  const double labelWidth = 100.0;
  const double labelHeight = 50.0;
  const double chipParentWidth = 75.0;
  const double chipParentHeight = 25.0;
103
  final Key labelKey = UniqueKey();
104 105

  await tester.pumpWidget(
106
    wrapForChip(
107
      child: Center(
108
        child: SizedBox(
109 110
          width: chipParentWidth,
          height: chipParentHeight,
111
          child: Chip(
112
            avatar: avatar,
113
            label: SizedBox(
114 115 116
              key: labelKey,
              width: labelWidth,
              height: labelHeight,
117
            ),
118
            onDeleted: onDeleted,
119 120 121
          ),
        ),
      ),
122 123
    ),
  );
124

125 126 127
  final Size labelSize = tester.getSize(find.byKey(labelKey));
  expect(labelSize.width, lessThan(chipParentWidth));
  expect(labelSize.height, lessThanOrEqualTo(chipParentHeight));
128

129 130 131 132
  final Size chipSize = tester.getSize(find.byType(Chip));
  expect(chipSize.width, chipParentWidth);
  expect(chipSize.height, chipParentHeight);
}
133

134
void doNothing() {}
135

136
Widget chipWithOptionalDeleteButton({
137 138
  Key? deleteButtonKey,
  Key? labelKey,
139
  required bool deletable,
140
  TextDirection textDirection = TextDirection.ltr,
141
  String? chipTooltip,
142
  String? deleteButtonTooltipMessage,
143
  VoidCallback? onPressed = doNothing,
144
  bool? useMaterial3,
145
}) {
146
  return wrapForChip(
147
    textDirection: textDirection,
148
    useMaterial3: useMaterial3,
149 150 151
    child: Wrap(
      children: <Widget>[
        RawChip(
152
          tooltip: chipTooltip,
153
          onPressed: onPressed,
154
          onDeleted: deletable ? doNothing : null,
155
          deleteIcon: Icon(Icons.close, key: deleteButtonKey),
156
          deleteButtonTooltipMessage: deleteButtonTooltipMessage,
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
          label: Text(
            deletable
              ? 'Chip with Delete Button'
              : 'Chip without Delete Button',
            key: labelKey,
          ),
        ),
      ],
    ),
  );
}

bool offsetsAreClose(Offset a, Offset b) => (a - b).distance < 1.0;
bool radiiAreClose(double a, double b) => (a - b).abs() < 1.0;

// Ripple pattern matches if there exists at least one ripple
// with the [expectedCenter] and [expectedRadius].
// This ensures the existence of a ripple.
PaintPattern ripplePattern(Offset expectedCenter, double expectedRadius) {
  return paints
    ..something((Symbol method, List<dynamic> arguments) {
178
        if (method != #drawCircle) {
179
          return false;
180
        }
181 182
        final Offset center = arguments[0] as Offset;
        final double radius = arguments[1] as double;
183 184 185 186 187 188 189 190 191 192 193
        return offsetsAreClose(center, expectedCenter) && radiiAreClose(radius, expectedRadius);
      }
    );
}

// Unique ripple pattern matches if there does not exist ripples
// other than ones with the [expectedCenter] and [expectedRadius].
// This ensures the nonexistence of two different ripples.
PaintPattern uniqueRipplePattern(Offset expectedCenter, double expectedRadius) {
  return paints
    ..everything((Symbol method, List<dynamic> arguments) {
194
        if (method != #drawCircle) {
195
          return true;
196
        }
197 198
        final Offset center = arguments[0] as Offset;
        final double radius = arguments[1] as double;
199
        if (offsetsAreClose(center, expectedCenter) && radiiAreClose(radius, expectedRadius)) {
200
          return true;
201
        }
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        throw '''
              Expected: center == $expectedCenter, radius == $expectedRadius
              Found: center == $center radius == $radius''';
      }
    );
}

// Finds any container of a tooltip.
Finder findTooltipContainer(String tooltipText) {
  return find.ancestor(
    of: find.text(tooltipText),
    matching: find.byType(Container),
  );
}

217
void main() {
218
  testWidgetsWithLeakTracking('M2 Chip defaults', (WidgetTester tester) async {
219 220
    late TextTheme textTheme;

221 222
    Widget buildFrame(Brightness brightness) {
      return MaterialApp(
223
        theme: ThemeData(brightness: brightness, useMaterial3: false),
224 225
        home: Scaffold(
          body: Center(
226 227 228 229 230 231 232 233 234
            child: Builder(
              builder: (BuildContext context) {
                textTheme = Theme.of(context).textTheme;
                return Chip(
                  avatar: const CircleAvatar(child: Text('A')),
                  label: const Text('Chip A'),
                  onDeleted: () { },
                );
              },
235 236 237 238 239 240 241
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(Brightness.light));
242
    expect(getMaterialBox(tester), paints..rrect()..circle(color: const Color(0xff1976d2)));
243 244 245 246 247 248 249
    expect(tester.getSize(find.byType(Chip)), const Size(156.0, 48.0));
    expect(getMaterial(tester).color, null);
    expect(getMaterial(tester).elevation, 0);
    expect(getMaterial(tester).shape, const StadiumBorder());
    expect(getIconData(tester).color?.value, 0xffffffff);
    expect(getIconData(tester).opacity, null);
    expect(getIconData(tester).size, null);
250 251 252

    TextStyle labelStyle = getLabelStyle(tester, 'Chip A').style;
    expect(labelStyle.color?.value, 0xde000000);
253 254 255 256 257 258 259 260 261 262 263 264 265
    expect(labelStyle.fontFamily, textTheme.bodyLarge?.fontFamily);
    expect(labelStyle.fontFamilyFallback, textTheme.bodyLarge?.fontFamilyFallback);
    expect(labelStyle.fontFeatures, textTheme.bodyLarge?.fontFeatures);
    expect(labelStyle.fontSize, textTheme.bodyLarge?.fontSize);
    expect(labelStyle.fontStyle, textTheme.bodyLarge?.fontStyle);
    expect(labelStyle.fontWeight, textTheme.bodyLarge?.fontWeight);
    expect(labelStyle.height, textTheme.bodyLarge?.height);
    expect(labelStyle.inherit, textTheme.bodyLarge?.inherit);
    expect(labelStyle.leadingDistribution, textTheme.bodyLarge?.leadingDistribution);
    expect(labelStyle.letterSpacing, textTheme.bodyLarge?.letterSpacing);
    expect(labelStyle.overflow, textTheme.bodyLarge?.overflow);
    expect(labelStyle.textBaseline, textTheme.bodyLarge?.textBaseline);
    expect(labelStyle.wordSpacing, textTheme.bodyLarge?.wordSpacing);
266 267 268

    await tester.pumpWidget(buildFrame(Brightness.dark));
    await tester.pumpAndSettle(); // Theme transition animation
269
    expect(getMaterialBox(tester), paints..rrect(color: const Color(0x1fffffff)));
270 271 272 273 274 275 276
    expect(tester.getSize(find.byType(Chip)), const Size(156.0, 48.0));
    expect(getMaterial(tester).color, null);
    expect(getMaterial(tester).elevation, 0);
    expect(getMaterial(tester).shape, const StadiumBorder());
    expect(getIconData(tester).color?.value, 0xffffffff);
    expect(getIconData(tester).opacity, null);
    expect(getIconData(tester).size, null);
277 278 279

    labelStyle = getLabelStyle(tester, 'Chip A').style;
    expect(labelStyle.color?.value, 0xdeffffff);
280 281 282 283 284 285 286 287 288 289 290 291 292
    expect(labelStyle.fontFamily, textTheme.bodyLarge?.fontFamily);
    expect(labelStyle.fontFamilyFallback, textTheme.bodyLarge?.fontFamilyFallback);
    expect(labelStyle.fontFeatures, textTheme.bodyLarge?.fontFeatures);
    expect(labelStyle.fontSize, textTheme.bodyLarge?.fontSize);
    expect(labelStyle.fontStyle, textTheme.bodyLarge?.fontStyle);
    expect(labelStyle.fontWeight, textTheme.bodyLarge?.fontWeight);
    expect(labelStyle.height, textTheme.bodyLarge?.height);
    expect(labelStyle.inherit, textTheme.bodyLarge?.inherit);
    expect(labelStyle.leadingDistribution, textTheme.bodyLarge?.leadingDistribution);
    expect(labelStyle.letterSpacing, textTheme.bodyLarge?.letterSpacing);
    expect(labelStyle.overflow, textTheme.bodyLarge?.overflow);
    expect(labelStyle.textBaseline, textTheme.bodyLarge?.textBaseline);
    expect(labelStyle.wordSpacing, textTheme.bodyLarge?.wordSpacing);
293
  });
294

295
  testWidgetsWithLeakTracking('M3 Chip defaults', (WidgetTester tester) async {
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
    late TextTheme textTheme;
    final ThemeData lightTheme = ThemeData.light(useMaterial3: true);
    final ThemeData darkTheme = ThemeData.dark(useMaterial3: true);

    Widget buildFrame(ThemeData theme) {
      return MaterialApp(
        theme: theme,
        home: Scaffold(
          body: Center(
            child: Builder(
              builder: (BuildContext context) {
                textTheme = Theme.of(context).textTheme;
                return Chip(
                  avatar: const CircleAvatar(child: Text('A')),
                  label: const Text('Chip A'),
                  onDeleted: () { },
                );
              },
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame(lightTheme));
    expect(getMaterial(tester).color, null);
    expect(getMaterial(tester).elevation, 0);
    expect(getMaterial(tester).shape, RoundedRectangleBorder(
      side: BorderSide(color: lightTheme.colorScheme.outline),
      borderRadius: BorderRadius.circular(8.0),
    ));
    expect(getIconData(tester).color, lightTheme.colorScheme.primary);
    expect(getIconData(tester).opacity, null);
    expect(getIconData(tester).size, 18);

    TextStyle labelStyle = getLabelStyle(tester, 'Chip A').style;
    expect(labelStyle.color, textTheme.labelLarge?.color);
    expect(labelStyle.fontFamily, textTheme.labelLarge?.fontFamily);
    expect(labelStyle.fontFamilyFallback, textTheme.labelLarge?.fontFamilyFallback);
    expect(labelStyle.fontFeatures, textTheme.labelLarge?.fontFeatures);
    expect(labelStyle.fontSize, textTheme.labelLarge?.fontSize);
    expect(labelStyle.fontStyle, textTheme.labelLarge?.fontStyle);
    expect(labelStyle.fontWeight, textTheme.labelLarge?.fontWeight);
    expect(labelStyle.height, textTheme.labelLarge?.height);
    expect(labelStyle.inherit, textTheme.labelLarge?.inherit);
    expect(labelStyle.leadingDistribution, textTheme.labelLarge?.leadingDistribution);
    expect(labelStyle.letterSpacing, textTheme.labelLarge?.letterSpacing);
    expect(labelStyle.overflow, textTheme.labelLarge?.overflow);
    expect(labelStyle.textBaseline, textTheme.labelLarge?.textBaseline);
    expect(labelStyle.wordSpacing, textTheme.labelLarge?.wordSpacing);

    await tester.pumpWidget(buildFrame(darkTheme));
    await tester.pumpAndSettle(); // Theme transition animation
    expect(getMaterial(tester).color, null);
    expect(getMaterial(tester).elevation, 0);
    expect(getMaterial(tester).shape, RoundedRectangleBorder(
      side: BorderSide(color: darkTheme.colorScheme.outline),
      borderRadius: BorderRadius.circular(8.0),
    ));
    expect(getIconData(tester).color, darkTheme.colorScheme.primary);
    expect(getIconData(tester).opacity, null);
    expect(getIconData(tester).size, 18);

    labelStyle = getLabelStyle(tester, 'Chip A').style;
    expect(labelStyle.color, textTheme.labelLarge?.color);
    expect(labelStyle.fontFamily, textTheme.labelLarge?.fontFamily);
    expect(labelStyle.fontFamilyFallback, textTheme.labelLarge?.fontFamilyFallback);
    expect(labelStyle.fontFeatures, textTheme.labelLarge?.fontFeatures);
    expect(labelStyle.fontSize, textTheme.labelLarge?.fontSize);
    expect(labelStyle.fontStyle, textTheme.labelLarge?.fontStyle);
    expect(labelStyle.fontWeight, textTheme.labelLarge?.fontWeight);
    expect(labelStyle.height, textTheme.labelLarge?.height);
    expect(labelStyle.inherit, textTheme.labelLarge?.inherit);
    expect(labelStyle.leadingDistribution, textTheme.labelLarge?.leadingDistribution);
    expect(labelStyle.letterSpacing, textTheme.labelLarge?.letterSpacing);
    expect(labelStyle.overflow, textTheme.labelLarge?.overflow);
    expect(labelStyle.textBaseline, textTheme.labelLarge?.textBaseline);
    expect(labelStyle.wordSpacing, textTheme.labelLarge?.wordSpacing);
  });

376
  testWidgetsWithLeakTracking('Chip control test', (WidgetTester tester) async {
377
    final FeedbackTester feedback = FeedbackTester();
378
    final List<String> deletedChipLabels = <String>[];
379
    await tester.pumpWidget(
380
      wrapForChip(
381
        child: Column(
382
          children: <Widget>[
383
            Chip(
384
              avatar: const CircleAvatar(child: Text('A')),
385 386 387 388 389 390
              label: const Text('Chip A'),
              onDeleted: () {
                deletedChipLabels.add('A');
              },
              deleteButtonTooltipMessage: 'Delete chip A',
            ),
391
            Chip(
392
              avatar: const CircleAvatar(child: Text('B')),
393 394 395 396 397 398 399 400
              label: const Text('Chip B'),
              onDeleted: () {
                deletedChipLabels.add('B');
              },
              deleteButtonTooltipMessage: 'Delete chip B',
            ),
          ],
        ),
401
      ),
402
    );
403

404 405 406
    expect(tester.widget(find.byTooltip('Delete chip A')), isNotNull);
    expect(tester.widget(find.byTooltip('Delete chip B')), isNotNull);

407 408
    expect(feedback.clickSoundCount, 0);

409 410 411
    expect(deletedChipLabels, isEmpty);
    await tester.tap(find.byTooltip('Delete chip A'));
    expect(deletedChipLabels, equals(<String>['A']));
412 413 414 415

    await tester.pumpAndSettle(const Duration(seconds: 1));
    expect(feedback.clickSoundCount, 1);

416 417 418 419 420 421
    await tester.tap(find.byTooltip('Delete chip B'));
    expect(deletedChipLabels, equals(<String>['A', 'B']));

    await tester.pumpAndSettle(const Duration(seconds: 1));
    expect(feedback.clickSoundCount, 2);

422
    feedback.dispose();
423
  });
424

425
  testWidgetsWithLeakTracking(
426 427 428 429 430 431 432 433
    'Chip does not constrain size of label widget if it does not exceed '
    'the available space',
    (WidgetTester tester) async {
      const double labelWidth = 50.0;
      const double labelHeight = 30.0;
      final Key labelKey = UniqueKey();

      await tester.pumpWidget(
434
        wrapForChip(
435
          child: Center(
436
            child: SizedBox(
437 438 439 440 441
              width: 500.0,
              height: 500.0,
              child: Column(
                children: <Widget>[
                  Chip(
442
                    label: SizedBox(
443 444 445 446
                      key: labelKey,
                      width: labelWidth,
                      height: labelHeight,
                    ),
447
                  ),
448 449
                ],
              ),
450 451 452
            ),
          ),
        ),
453
      );
454

455 456 457 458 459
      final Size labelSize = tester.getSize(find.byKey(labelKey));
      expect(labelSize.width, labelWidth);
      expect(labelSize.height, labelHeight);
    },
  );
460

461
  testWidgetsWithLeakTracking(
462 463 464
    'Chip constrains the size of the label widget when it exceeds the '
    'available space',
    (WidgetTester tester) async {
465
      await testConstrainedLabel(tester);
466 467
    },
  );
468

469
  testWidgetsWithLeakTracking(
470 471 472
    'Chip constrains the size of the label widget when it exceeds the '
    'available space and the avatar is present',
    (WidgetTester tester) async {
473
      await testConstrainedLabel(
474 475 476 477 478
        tester,
        avatar: const CircleAvatar(child: Text('A')),
      );
    },
  );
479

480
  testWidgetsWithLeakTracking(
481 482 483
    'Chip constrains the size of the label widget when it exceeds the '
    'available space and the delete icon is present',
    (WidgetTester tester) async {
484
      await testConstrainedLabel(
485 486 487 488 489
        tester,
        onDeleted: () { },
      );
    },
  );
490

491
  testWidgetsWithLeakTracking(
492 493 494
    'Chip constrains the size of the label widget when it exceeds the '
    'available space and both avatar and delete icons are present',
    (WidgetTester tester) async {
495
      await testConstrainedLabel(
496 497 498 499 500 501
        tester,
        avatar: const CircleAvatar(child: Text('A')),
        onDeleted: () { },
      );
    },
  );
502

503
  testWidgetsWithLeakTracking(
504 505 506 507
    'Chip constrains the avatar, label, and delete icons to the bounds of '
    'the chip when it exceeds the available space',
    (WidgetTester tester) async {
      // Regression test for https://github.com/flutter/flutter/issues/11523
508
      Widget chipBuilder (String text, {Widget? avatar, VoidCallback? onDeleted}) {
509 510
        return MaterialApp(
          home: Scaffold(
511
            body: SizedBox(
512 513 514 515 516 517 518 519
              width: 150,
              child: Column(
                children: <Widget>[
                  Chip(
                    avatar: avatar,
                    label: Text(text),
                    onDeleted: onDeleted,
                  ),
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
        );
      }

      void chipRectContains(Rect chipRect, Rect rect) {
        expect(chipRect.contains(rect.topLeft), true);
        expect(chipRect.contains(rect.topRight), true);
        expect(chipRect.contains(rect.bottomLeft), true);
        expect(chipRect.contains(rect.bottomRight), true);
      }

      Rect chipRect;
      Rect avatarRect;
      Rect labelRect;
      Rect deleteIconRect;
      const String text = 'Very long text that will be clipped';

      await tester.pumpWidget(chipBuilder(text));

      chipRect = tester.getRect(find.byType(Chip));
      labelRect = tester.getRect(find.text(text));
      chipRectContains(chipRect, labelRect);

      await tester.pumpWidget(chipBuilder(
        text,
        avatar: const CircleAvatar(child: Text('A')),
      ));
      await tester.pumpAndSettle();

      chipRect = tester.getRect(find.byType(Chip));
      avatarRect = tester.getRect(find.byType(CircleAvatar));
      chipRectContains(chipRect, avatarRect);

      labelRect = tester.getRect(find.text(text));
      chipRectContains(chipRect, labelRect);

      await tester.pumpWidget(chipBuilder(
        text,
        avatar: const CircleAvatar(child: Text('A')),
        onDeleted: () {},
      ));
      await tester.pumpAndSettle();

      chipRect = tester.getRect(find.byType(Chip));
      avatarRect = tester.getRect(find.byType(CircleAvatar));
      chipRectContains(chipRect, avatarRect);

      labelRect = tester.getRect(find.text(text));
      chipRectContains(chipRect, labelRect);

      deleteIconRect = tester.getRect(find.byIcon(Icons.cancel));
      chipRectContains(chipRect, deleteIconRect);
    },
  );
577

578
  testWidgetsWithLeakTracking('Chip in row works ok', (WidgetTester tester) async {
579
    const TextStyle style = TextStyle(fontSize: 10.0);
580
    await tester.pumpWidget(
581
      wrapForChip(
582
        useMaterial3: false,
583 584
        child: const Row(
          children: <Widget>[
585
            Chip(label: Text('Test'), labelStyle: style),
586
          ],
587 588 589
        ),
      ),
    );
590
    expect(tester.getSize(find.byType(Text)), const Size(40.0, 10.0));
591
    expect(tester.getSize(find.byType(Chip)), const Size(64.0, 48.0));
592
    await tester.pumpWidget(
593
      wrapForChip(
594 595
        child: const Row(
          children: <Widget>[
596
            Flexible(child: Chip(label: Text('Test'), labelStyle: style)),
597
          ],
598 599 600 601
        ),
      ),
    );
    expect(tester.getSize(find.byType(Text)), const Size(40.0, 10.0));
602
    expect(tester.getSize(find.byType(Chip)), const Size(64.0, 48.0));
603
    await tester.pumpWidget(
604
      wrapForChip(
605 606
        child: const Row(
          children: <Widget>[
607
            Expanded(child: Chip(label: Text('Test'), labelStyle: style)),
608
          ],
609 610 611 612
        ),
      ),
    );
    expect(tester.getSize(find.byType(Text)), const Size(40.0, 10.0));
613
    expect(tester.getSize(find.byType(Chip)), const Size(800.0, 48.0));
614
  });
615

616
  testWidgetsWithLeakTracking('Chip responds to materialTapTargetSize', (WidgetTester tester) async {
617
      await tester.pumpWidget(
618
        wrapForChip(
619
          useMaterial3: false,
620 621
          child: const Column(
            children: <Widget>[
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
              Chip(
                label: Text('X'),
                materialTapTargetSize: MaterialTapTargetSize.padded,
              ),
              Chip(
                label: Text('X'),
                materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
            ],
          ),
        ),
      );
      expect(tester.getSize(find.byType(Chip).first), const Size(48.0, 48.0));
      expect(tester.getSize(find.byType(Chip).last), const Size(38.0, 32.0));
    },
  );

639
  testWidgetsWithLeakTracking('Delete button tap target is the right proportion of the chip', (WidgetTester tester) async {
640 641 642
    final UniqueKey deleteKey = UniqueKey();
    bool calledDelete = false;
    await tester.pumpWidget(
643
      wrapForChip(
644 645 646 647 648 649 650 651 652 653 654 655 656
        child: Column(
          children: <Widget>[
            Chip(
              label: const Text('Really Long Label'),
              deleteIcon: Icon(Icons.delete, key: deleteKey),
              onDeleted: () {
                calledDelete = true;
              },
            ),
          ],
        ),
      ),
    );
657 658 659

    // Test correct tap target size.
    await tester.tapAt(tester.getCenter(find.byKey(deleteKey)) - const Offset(18.0, 0.0)); // Half the width of the delete button + right label padding.
660 661 662 663
    await tester.pump();
    expect(calledDelete, isTrue);
    calledDelete = false;

664 665
    // Test incorrect tap target size.
    await tester.tapAt(tester.getCenter(find.byKey(deleteKey)) - const Offset(19.0, 0.0));
666 667 668 669 670
    await tester.pump();
    expect(calledDelete, isFalse);
    calledDelete = false;

    await tester.pumpWidget(
671
      wrapForChip(
672 673 674 675
        child: Column(
          children: <Widget>[
            Chip(
              label: const SizedBox(), // Short label
676
              deleteIcon: Icon(Icons.cancel, key: deleteKey),
677 678 679 680 681 682 683 684
              onDeleted: () {
                calledDelete = true;
              },
            ),
          ],
        ),
      ),
    );
685 686 687 688 689 690 691

    // Chip width is 48 with padding, 40 without padding, so halfway is at 20. Cancel
    // icon is 24x24, so since 24 > 20 the split location should be halfway across the
    // chip, which is at 12 + 8 = 20 from the right side. Since the split is just
    // slightly less than 50%, 8 from the center of the delete button should hit the
    // chip, not the delete button.
    await tester.tapAt(tester.getCenter(find.byKey(deleteKey)) - const Offset(7.0, 0.0));
692 693 694 695
    await tester.pump();
    expect(calledDelete, isTrue);
    calledDelete = false;

696
    await tester.tapAt(tester.getCenter(find.byKey(deleteKey)) - const Offset(8.0, 0.0));
697 698 699 700
    await tester.pump();
    expect(calledDelete, isFalse);
  });

701
  testWidgetsWithLeakTracking('Chip elements are ordered horizontally for locale', (WidgetTester tester) async {
702
    final UniqueKey iconKey = UniqueKey();
703 704
    late final OverlayEntry entry;
    addTearDown(() => entry..remove()..dispose());
705
    final Widget test = Overlay(
706
      initialEntries: <OverlayEntry>[
707
        entry = OverlayEntry(
708
          builder: (BuildContext context) {
709 710 711
            return Material(
              child: Chip(
                deleteIcon: Icon(Icons.delete, key: iconKey),
712
                onDeleted: () { },
713
                label: const Text('ABC'),
714 715 716 717 718 719 720 721
              ),
            );
          },
        ),
      ],
    );

    await tester.pumpWidget(
722
      wrapForChip(
723 724
        child: test,
        textDirection: TextDirection.rtl,
725 726
      ),
    );
727 728
    await tester.pumpAndSettle(const Duration(milliseconds: 500));
    expect(tester.getCenter(find.text('ABC')).dx, greaterThan(tester.getCenter(find.byKey(iconKey)).dx));
729
    await tester.pumpWidget(
730
      wrapForChip(
731
        child: test,
732 733
      ),
    );
734 735
    await tester.pumpAndSettle(const Duration(milliseconds: 500));
    expect(tester.getCenter(find.text('ABC')).dx, lessThan(tester.getCenter(find.byKey(iconKey)).dx));
736 737
  });

738
  testWidgetsWithLeakTracking('Chip responds to textScaleFactor', (WidgetTester tester) async {
739
    await tester.pumpWidget(
740
      wrapForChip(
741
        useMaterial3: false,
742 743
        child: const Column(
          children: <Widget>[
744 745 746
            Chip(
              avatar: CircleAvatar(child: Text('A')),
              label: Text('Chip A'),
747
            ),
748 749 750
            Chip(
              avatar: CircleAvatar(child: Text('B')),
              label: Text('Chip B'),
751 752
            ),
          ],
753 754 755 756 757 758
        ),
      ),
    );

    expect(
      tester.getSize(find.text('Chip A')),
759
      const Size(84.0, 14.0),
760 761 762
    );
    expect(
      tester.getSize(find.text('Chip B')),
763
      const Size(84.0, 14.0),
764
    );
765 766
    expect(tester.getSize(find.byType(Chip).first), const Size(132.0, 48.0));
    expect(tester.getSize(find.byType(Chip).last), const Size(132.0, 48.0));
767 768

    await tester.pumpWidget(
769
      wrapForChip(
770
        textScaleFactor: 3.0,
771 772
        child: const Column(
          children: <Widget>[
773 774 775
            Chip(
              avatar: CircleAvatar(child: Text('A')),
              label: Text('Chip A'),
776
            ),
777 778 779
            Chip(
              avatar: CircleAvatar(child: Text('B')),
              label: Text('Chip B'),
780 781
            ),
          ],
782 783 784 785
        ),
      ),
    );

786 787 788 789
    expect(tester.getSize(find.text('Chip A')), const Size(252.0, 42.0));
    expect(tester.getSize(find.text('Chip B')), const Size(252.0, 42.0));
    expect(tester.getSize(find.byType(Chip).first), const Size(310.0, 50.0));
    expect(tester.getSize(find.byType(Chip).last), const Size(310.0, 50.0));
790 791 792

    // Check that individual text scales are taken into account.
    await tester.pumpWidget(
793
      wrapForChip(
794 795
        child: const Column(
          children: <Widget>[
796 797 798
            Chip(
              avatar: CircleAvatar(child: Text('A')),
              label: Text('Chip A', textScaleFactor: 3.0),
799
            ),
800 801 802
            Chip(
              avatar: CircleAvatar(child: Text('B')),
              label: Text('Chip B'),
803 804
            ),
          ],
805 806 807 808
        ),
      ),
    );

809 810 811 812
    expect(tester.getSize(find.text('Chip A')), const Size(252.0, 42.0));
    expect(tester.getSize(find.text('Chip B')), const Size(84.0, 14.0));
    expect(tester.getSize(find.byType(Chip).first), const Size(318.0, 50.0));
    expect(tester.getSize(find.byType(Chip).last), const Size(132.0, 48.0));
813
  });
814

815
  testWidgetsWithLeakTracking('Labels can be non-text widgets', (WidgetTester tester) async {
816 817
    final Key keyA = GlobalKey();
    final Key keyB = GlobalKey();
818
    await tester.pumpWidget(
819
      wrapForChip(
820
        useMaterial3: false,
821
        child: Column(
822
          children: <Widget>[
823
            Chip(
824
              avatar: const CircleAvatar(child: Text('A')),
825
              label: Text('Chip A', key: keyA),
826
            ),
827
            Chip(
828
              avatar: const CircleAvatar(child: Text('B')),
829
              label: SizedBox(key: keyB, width: 10.0, height: 10.0),
830 831
            ),
          ],
832 833 834 835
        ),
      ),
    );

836
    expect(tester.getSize(find.byKey(keyA)), const Size(84.0, 14.0));
837
    expect(tester.getSize(find.byKey(keyB)), const Size(10.0, 10.0));
838
    expect(tester.getSize(find.byType(Chip).first), const Size(132.0, 48.0));
839
    expect(tester.getSize(find.byType(Chip).last), const Size(58.0, 48.0));
840
  });
841

842
  testWidgetsWithLeakTracking('Avatars can be non-circle avatar widgets', (WidgetTester tester) async {
843
    final Key keyA = GlobalKey();
844
    await tester.pumpWidget(
845
      wrapForChip(
846
        child: Column(
847
          children: <Widget>[
848
            Chip(
849
              avatar: SizedBox(key: keyA, width: 20.0, height: 20.0),
850 851 852
              label: const Text('Chip A'),
            ),
          ],
853 854 855 856 857 858 859
        ),
      ),
    );

    expect(tester.getSize(find.byKey(keyA)), equals(const Size(20.0, 20.0)));
  });

860
  testWidgetsWithLeakTracking('Delete icons can be non-icon widgets', (WidgetTester tester) async {
861
    final Key keyA = GlobalKey();
862
    await tester.pumpWidget(
863
      wrapForChip(
864
        child: Column(
865
          children: <Widget>[
866
            Chip(
867
              deleteIcon: SizedBox(key: keyA, width: 20.0, height: 20.0),
868
              label: const Text('Chip A'),
869
              onDeleted: () { },
870 871
            ),
          ],
872 873 874 875 876 877 878
        ),
      ),
    );

    expect(tester.getSize(find.byKey(keyA)), equals(const Size(20.0, 20.0)));
  });

879
  testWidgets('Chip padding - LTR', (WidgetTester tester) async {
880 881
    final GlobalKey keyA = GlobalKey();
    final GlobalKey keyB = GlobalKey();
882 883 884

    late final OverlayEntry entry;
    addTearDown(() => entry..remove()..dispose());
885
    await tester.pumpWidget(
886
      wrapForChip(
887
        child: Overlay(
888
          initialEntries: <OverlayEntry>[
889
            entry = OverlayEntry(
890
              builder: (BuildContext context) {
891 892 893 894
                return Material(
                  child: Center(
                    child: Chip(
                      avatar: Placeholder(key: keyA),
895
                      label: SizedBox(
896 897 898
                        key: keyB,
                        width: 40.0,
                        height: 40.0,
899
                      ),
900
                      onDeleted: () { },
901
                    ),
902 903 904 905 906
                  ),
                );
              },
            ),
          ],
907 908 909
        ),
      ),
    );
910 911
    expect(tester.getTopLeft(find.byKey(keyA)), const Offset(332.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyA)), const Offset(372.0, 320.0));
912 913
    expect(tester.getTopLeft(find.byKey(keyB)), const Offset(380.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyB)), const Offset(420.0, 320.0));
914 915
    expect(tester.getTopLeft(find.byType(Icon)), const Offset(439.0, 291.0));
    expect(tester.getBottomRight(find.byType(Icon)), const Offset(457.0, 309.0));
916 917
  });

918
  testWidgets('Chip padding - RTL', (WidgetTester tester) async {
919 920
    final GlobalKey keyA = GlobalKey();
    final GlobalKey keyB = GlobalKey();
921 922 923 924

    late final OverlayEntry entry;
    addTearDown(() => entry..remove()..dispose());

925
    await tester.pumpWidget(
926
      wrapForChip(
927
        textDirection: TextDirection.rtl,
928
        child: Overlay(
929
          initialEntries: <OverlayEntry>[
930
            entry = OverlayEntry(
931
              builder: (BuildContext context) {
932 933 934 935
                return Material(
                  child: Center(
                    child: Chip(
                      avatar: Placeholder(key: keyA),
936
                      label: SizedBox(
937 938 939
                        key: keyB,
                        width: 40.0,
                        height: 40.0,
940
                      ),
941
                      onDeleted: () { },
942
                    ),
943 944 945 946 947
                  ),
                );
              },
            ),
          ],
948 949 950
        ),
      ),
    );
951

952 953
    expect(tester.getTopLeft(find.byKey(keyA)), const Offset(428.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyA)), const Offset(468.0, 320.0));
954 955
    expect(tester.getTopLeft(find.byKey(keyB)), const Offset(380.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyB)), const Offset(420.0, 320.0));
956 957 958 959
    expect(tester.getTopLeft(find.byType(Icon)), const Offset(343.0, 291.0));
    expect(tester.getBottomRight(find.byType(Icon)), const Offset(361.0, 309.0));
  });

960
  testWidgetsWithLeakTracking('Avatar drawer works as expected on RawChip', (WidgetTester tester) async {
961
    final GlobalKey labelKey = GlobalKey();
962
    Future<void> pushChip({ Widget? avatar }) async {
963
      return tester.pumpWidget(
964
        wrapForChip(
965
          useMaterial3: false,
966
          child: Wrap(
967
            children: <Widget>[
968
              RawChip(
969
                avatar: avatar,
970
                label: Text('Chip', key: labelKey),
971 972 973
                shape: const StadiumBorder(),
              ),
            ],
974 975 976 977 978 979 980
          ),
        ),
      );
    }

    // No avatar
    await pushChip();
981
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
982
    final GlobalKey avatarKey = GlobalKey();
983 984 985

    // Add an avatar
    await pushChip(
986
      avatar: Container(
987 988 989 990 991 992 993
        key: avatarKey,
        color: const Color(0xff000000),
        width: 40.0,
        height: 40.0,
      ),
    );
    // Avatar drawer should start out closed.
994
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
995
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
996 997
    expect(tester.getTopLeft(find.byKey(avatarKey)), equals(const Offset(-20.0, 12.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
998 999 1000

    await tester.pump(const Duration(milliseconds: 20));
    // Avatar drawer should start expanding.
1001
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(81.2, epsilon: 0.1));
1002
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1003 1004
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(-18.8, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(13.2, epsilon: 0.1));
1005 1006

    await tester.pump(const Duration(milliseconds: 20));
1007
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(86.7, epsilon: 0.1));
1008
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1009 1010
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(-13.3, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(18.6, epsilon: 0.1));
1011 1012

    await tester.pump(const Duration(milliseconds: 20));
1013
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(94.7, epsilon: 0.1));
1014
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1015 1016
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(-5.3, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(26.7, epsilon: 0.1));
1017 1018

    await tester.pump(const Duration(milliseconds: 20));
1019
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(99.5, epsilon: 0.1));
1020
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1021 1022
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(-0.5, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(31.5, epsilon: 0.1));
1023 1024 1025 1026

    // Wait for being done with animation, and make sure it didn't change
    // height.
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
1027
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
1028
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1029 1030
    expect(tester.getTopLeft(find.byKey(avatarKey)), equals(const Offset(4.0, 12.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(36.0, 17.0)));
1031 1032 1033 1034

    // Remove the avatar again
    await pushChip();
    // Avatar drawer should start out open.
1035
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
1036
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1037 1038
    expect(tester.getTopLeft(find.byKey(avatarKey)), equals(const Offset(4.0, 12.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(36.0, 17.0)));
1039 1040 1041

    await tester.pump(const Duration(milliseconds: 20));
    // Avatar drawer should start contracting.
1042
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(102.9, epsilon: 0.1));
1043
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1044 1045
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(2.9, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(34.9, epsilon: 0.1));
1046 1047

    await tester.pump(const Duration(milliseconds: 20));
1048
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(98.0, epsilon: 0.1));
1049
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1050 1051
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(-2.0, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(30.0, epsilon: 0.1));
1052 1053

    await tester.pump(const Duration(milliseconds: 20));
1054
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(84.1, epsilon: 0.1));
1055
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1056 1057
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(-15.9, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(16.1, epsilon: 0.1));
1058 1059

    await tester.pump(const Duration(milliseconds: 20));
1060
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(80.0, epsilon: 0.1));
1061
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
1062 1063
    expect(tester.getTopLeft(find.byKey(avatarKey)).dx, moreOrLessEquals(-20.0, epsilon: 0.1));
    expect(tester.getTopLeft(find.byKey(labelKey)).dx, moreOrLessEquals(12.0, epsilon: 0.1));
1064 1065 1066 1067

    // Wait for being done with animation, make sure it didn't change
    // height, and make sure that the avatar is no longer drawn.
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
1068 1069
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
1070
    expect(find.byKey(avatarKey), findsNothing);
1071
  });
1072

1073
  testWidgetsWithLeakTracking('Delete button drawer works as expected on RawChip', (WidgetTester tester) async {
1074 1075
    const Key labelKey = Key('label');
    const Key deleteButtonKey = Key('delete');
1076
    bool wasDeleted = false;
1077
    Future<void> pushChip({ bool deletable = false }) async {
1078
      return tester.pumpWidget(
1079
        wrapForChip(
1080
          useMaterial3: false,
1081
          child: Wrap(
1082
            children: <Widget>[
1083 1084
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
1085
                  onDeleted: deletable
1086 1087 1088 1089 1090 1091
                    ? () {
                        setState(() {
                          wasDeleted = true;
                        });
                      }
                    : null,
1092 1093
                  deleteIcon: Container(width: 40.0, height: 40.0, color: Colors.blue, key: deleteButtonKey),
                  label: const Text('Chip', key: labelKey),
1094 1095 1096 1097
                  shape: const StadiumBorder(),
                );
              }),
            ],
1098 1099 1100 1101 1102 1103 1104
          ),
        ),
      );
    }

    // No delete button
    await pushChip();
1105
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
1106 1107 1108 1109

    // Add a delete button
    await pushChip(deletable: true);
    // Delete button drawer should start out closed.
1110
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
1111
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1112 1113
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)), equals(const Offset(52.0, 12.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
1114 1115 1116

    await tester.pump(const Duration(milliseconds: 20));
    // Delete button drawer should start expanding.
1117
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(81.2, epsilon: 0.1));
1118
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1119
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(53.2, epsilon: 0.1));
1120
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
1121 1122

    await tester.pump(const Duration(milliseconds: 20));
1123
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(86.7, epsilon: 0.1));
1124
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1125
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(58.7, epsilon: 0.1));
1126 1127

    await tester.pump(const Duration(milliseconds: 20));
1128
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(94.7, epsilon: 0.1));
1129
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1130
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(66.7, epsilon: 0.1));
1131 1132

    await tester.pump(const Duration(milliseconds: 20));
1133
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(99.5, epsilon: 0.1));
1134
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1135
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(71.5, epsilon: 0.1));
1136 1137 1138 1139

    // Wait for being done with animation, and make sure it didn't change
    // height.
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
1140
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
1141
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1142 1143
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)), equals(const Offset(76.0, 12.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

    // Test the tap work for the delete button, but not the rest of the chip.
    expect(wasDeleted, isFalse);
    await tester.tap(find.byKey(labelKey));
    expect(wasDeleted, isFalse);
    await tester.tap(find.byKey(deleteButtonKey));
    expect(wasDeleted, isTrue);

    // Remove the delete button again
    await pushChip();
    // Delete button drawer should start out open.
1155
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
1156
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1157 1158
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)), equals(const Offset(76.0, 12.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
1159 1160 1161

    await tester.pump(const Duration(milliseconds: 20));
    // Delete button drawer should start contracting.
1162
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(103.8, epsilon: 0.1));
1163
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1164
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(75.8, epsilon: 0.1));
1165
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
1166 1167

    await tester.pump(const Duration(milliseconds: 20));
1168
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(102.9, epsilon: 0.1));
1169
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1170
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(74.9, epsilon: 0.1));
1171 1172

    await tester.pump(const Duration(milliseconds: 20));
1173
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(101.0, epsilon: 0.1));
1174
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1175
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(73.0, epsilon: 0.1));
1176 1177

    await tester.pump(const Duration(milliseconds: 20));
1178
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(97.5, epsilon: 0.1));
1179
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1180
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(69.5, epsilon: 0.1));
1181 1182 1183 1184

    // Wait for being done with animation, make sure it didn't change
    // height, and make sure that the delete button is no longer drawn.
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
1185 1186
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
1187
    expect(find.byKey(deleteButtonKey), findsNothing);
1188
  });
1189

1190
  testWidgetsWithLeakTracking('Delete button takes up at most half of the chip', (WidgetTester tester) async {
1191 1192 1193 1194 1195
    final UniqueKey chipKey = UniqueKey();
    bool chipPressed = false;
    bool deletePressed = false;

    await tester.pumpWidget(
1196
      wrapForChip(
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
        child: Wrap(
          children: <Widget>[
            RawChip(
              key: chipKey,
              onPressed: () {
                chipPressed = true;
              },
              onDeleted: () {
                deletePressed = true;
              },
              label: const Text(''),
              ),
          ],
        ),
      ),
    );

    await tester.tapAt(tester.getCenter(find.byKey(chipKey)));
    await tester.pump();
    expect(chipPressed, isTrue);
    expect(deletePressed, isFalse);
    chipPressed = false;

    await tester.tapAt(tester.getCenter(find.byKey(chipKey)) + const Offset(1.0, 0.0));
    await tester.pump();
    expect(chipPressed, isFalse);
    expect(deletePressed, isTrue);
  });

1226
  testWidgetsWithLeakTracking('Chip creates centered, unique ripple when label is tapped', (WidgetTester tester) async {
1227 1228 1229 1230
    final UniqueKey labelKey = UniqueKey();
    final UniqueKey deleteButtonKey = UniqueKey();

    await tester.pumpWidget(
1231
      chipWithOptionalDeleteButton(
1232
        useMaterial3: false,
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
        labelKey: labelKey,
        deleteButtonKey: deleteButtonKey,
        deletable: true,
      ),
    );

    final RenderBox box = getMaterialBox(tester);

    // Taps at a location close to the center of the label.
    final Offset centerOfLabel = tester.getCenter(find.byKey(labelKey));
    final Offset tapLocationOfLabel = centerOfLabel + const Offset(-10, -10);
    final TestGesture gesture = await tester.startGesture(tapLocationOfLabel);
    await tester.pump();

    // Waits for 100 ms.
    await tester.pump(const Duration(milliseconds: 100));

    // There should be one unique, centered ink ripple.
    expect(box, ripplePattern(const Offset(163.0, 6.0), 20.9));
    expect(box, uniqueRipplePattern(const Offset(163.0, 6.0), 20.9));

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for 100 ms again.
    await tester.pump(const Duration(milliseconds: 100));

    // The ripple should grow, with the same center.
    expect(box, ripplePattern(const Offset(163.0, 6.0), 41.8));
    expect(box, uniqueRipplePattern(const Offset(163.0, 6.0), 41.8));

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for a very long time.
    await tester.pumpAndSettle();

    // There should still be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    await gesture.up();
1274
  });
1275

1276
  testWidgetsWithLeakTracking('Delete button is focusable', (WidgetTester tester) async {
1277 1278 1279 1280
    final GlobalKey labelKey = GlobalKey();
    final GlobalKey deleteButtonKey = GlobalKey();

    await tester.pumpWidget(
1281
      chipWithOptionalDeleteButton(
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
        labelKey: labelKey,
        deleteButtonKey: deleteButtonKey,
        deletable: true,
      ),
    );

    Focus.of(deleteButtonKey.currentContext!).requestFocus();
    await tester.pump();

    // They shouldn't have the same focus node.
    expect(Focus.of(deleteButtonKey.currentContext!), isNot(equals(Focus.of(labelKey.currentContext!))));
    expect(Focus.of(deleteButtonKey.currentContext!).hasFocus, isTrue);
    expect(Focus.of(deleteButtonKey.currentContext!).hasPrimaryFocus, isTrue);
    // Delete button is a child widget of the Chip, so the Chip should have focus if
    // the delete button does.
    expect(Focus.of(labelKey.currentContext!).hasFocus, isTrue);
    expect(Focus.of(labelKey.currentContext!).hasPrimaryFocus, isFalse);

    Focus.of(labelKey.currentContext!).requestFocus();
    await tester.pump();

    expect(Focus.of(deleteButtonKey.currentContext!).hasFocus, isFalse);
    expect(Focus.of(deleteButtonKey.currentContext!).hasPrimaryFocus, isFalse);
    expect(Focus.of(labelKey.currentContext!).hasFocus, isTrue);
    expect(Focus.of(labelKey.currentContext!).hasPrimaryFocus, isTrue);
  });

1309
  testWidgetsWithLeakTracking('Delete button creates non-centered, unique ripple when tapped', (WidgetTester tester) async {
1310 1311 1312 1313
    final UniqueKey labelKey = UniqueKey();
    final UniqueKey deleteButtonKey = UniqueKey();

    await tester.pumpWidget(
1314
      chipWithOptionalDeleteButton(
1315
        useMaterial3: false,
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
        labelKey: labelKey,
        deleteButtonKey: deleteButtonKey,
        deletable: true,
      ),
    );

    final RenderBox box = getMaterialBox(tester);

    // Taps at a location close to the center of the delete icon.
    final Offset centerOfDeleteButton = tester.getCenter(find.byKey(deleteButtonKey));
    final Offset tapLocationOfDeleteButton = centerOfDeleteButton + const Offset(-10, -10);
    final TestGesture gesture = await tester.startGesture(tapLocationOfDeleteButton);
    await tester.pump();

    // Waits for 200 ms.
    await tester.pump(const Duration(milliseconds: 100));
    await tester.pump(const Duration(milliseconds: 100));

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
    // There should be one unique ink ripple.
    expect(box, ripplePattern(const Offset(3.0, 3.0), 1.44));
    expect(box, uniqueRipplePattern(const Offset(3.0, 3.0), 1.44));

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for 200 ms again.
    await tester.pump(const Duration(milliseconds: 100));
    await tester.pump(const Duration(milliseconds: 100));

    // The ripple should grow, but the center should move,
    // Towards the center of the delete icon.
    expect(box, ripplePattern(const Offset(5.0, 5.0), 4.32));
    expect(box, uniqueRipplePattern(const Offset(5.0, 5.0), 4.32));

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for a very long time.
    // This is pressing and holding the delete button.
    await tester.pumpAndSettle();

    // There should be a tooltip.
    expect(findTooltipContainer('Delete'), findsOneWidget);

    await gesture.up();
  });

1363
  testWidgetsWithLeakTracking('Delete button in a chip with null onPressed creates ripple when tapped', (WidgetTester tester) async {
1364 1365 1366 1367
    final UniqueKey labelKey = UniqueKey();
    final UniqueKey deleteButtonKey = UniqueKey();

    await tester.pumpWidget(
1368
      chipWithOptionalDeleteButton(
1369
        useMaterial3: false,
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
        labelKey: labelKey,
        onPressed: null,
        deleteButtonKey: deleteButtonKey,
        deletable: true,
      ),
    );

    final RenderBox box = getMaterialBox(tester);

    // Taps at a location close to the center of the delete icon.
    final Offset centerOfDeleteButton = tester.getCenter(find.byKey(deleteButtonKey));
    final Offset tapLocationOfDeleteButton = centerOfDeleteButton + const Offset(-10, -10);
    final TestGesture gesture = await tester.startGesture(tapLocationOfDeleteButton);
    await tester.pump();

    // Waits for 200 ms.
    await tester.pump(const Duration(milliseconds: 100));
    await tester.pump(const Duration(milliseconds: 100));
1388 1389

    // There should be one unique ink ripple.
1390 1391
    expect(box, ripplePattern(const Offset(3.0, 3.0), 1.44));
    expect(box, uniqueRipplePattern(const Offset(3.0, 3.0), 1.44));
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for 200 ms again.
    await tester.pump(const Duration(milliseconds: 100));
    await tester.pump(const Duration(milliseconds: 100));

    // The ripple should grow, but the center should move,
    // Towards the center of the delete icon.
1402 1403
    expect(box, ripplePattern(const Offset(5.0, 5.0), 4.32));
    expect(box, uniqueRipplePattern(const Offset(5.0, 5.0), 4.32));
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for a very long time.
    // This is pressing and holding the delete button.
    await tester.pumpAndSettle();

    // There should be a tooltip.
    expect(findTooltipContainer('Delete'), findsOneWidget);

    await gesture.up();
1416
  });
1417

1418
  testWidgetsWithLeakTracking('RTL delete button responds to tap on the left of the chip', (WidgetTester tester) async {
1419 1420 1421 1422 1423
    // Creates an RTL chip with a delete button.
    final UniqueKey labelKey = UniqueKey();
    final UniqueKey deleteButtonKey = UniqueKey();

    await tester.pumpWidget(
1424
      chipWithOptionalDeleteButton(
1425 1426 1427 1428 1429 1430 1431 1432 1433
        labelKey: labelKey,
        deleteButtonKey: deleteButtonKey,
        deletable: true,
        textDirection: TextDirection.rtl,
      ),
    );

    // Taps at a location close to the center of the delete icon,
    // Which is on the left side of the chip.
1434
    final Offset topLeftOfInkWell = tester.getTopLeft(find.byType(InkWell).first);
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
    final Offset tapLocation = topLeftOfInkWell + const Offset(8, 8);
    final TestGesture gesture = await tester.startGesture(tapLocation);
    await tester.pump();

    await tester.pumpAndSettle();

    // The existence of a 'Delete' tooltip indicates the delete icon is tapped,
    // Instead of the label.
    expect(findTooltipContainer('Delete'), findsOneWidget);

    await gesture.up();
1446
  });
1447

1448
  testWidgetsWithLeakTracking('Chip without delete button creates correct ripple', (WidgetTester tester) async {
1449 1450 1451 1452
    // Creates a chip with a delete button.
    final UniqueKey labelKey = UniqueKey();

    await tester.pumpWidget(
1453
      chipWithOptionalDeleteButton(
1454
        useMaterial3: false,
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
        labelKey: labelKey,
        deletable: false,
      ),
    );

    final RenderBox box = getMaterialBox(tester);

    // Taps at a location close to the bottom-right corner of the chip.
    final Offset bottomRightOfInkWell = tester.getBottomRight(find.byType(InkWell));
    final Offset tapLocation = bottomRightOfInkWell + const Offset(-10, -10);
    final TestGesture gesture = await tester.startGesture(tapLocation);
    await tester.pump();

    // Waits for 100 ms.
    await tester.pump(const Duration(milliseconds: 100));

    // There should be exactly one ink-creating widget.
    expect(find.byType(InkWell), findsOneWidget);
    expect(find.byType(InkResponse), findsNothing);

    // There should be one unique, centered ink ripple.
    expect(box, ripplePattern(const Offset(378.0, 22.0), 37.9));
    expect(box, uniqueRipplePattern(const Offset(378.0, 22.0), 37.9));

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for 100 ms again.
    await tester.pump(const Duration(milliseconds: 100));

    // The ripple should grow, with the same center.
    // This indicates that the tap is not on a delete icon.
    expect(box, ripplePattern(const Offset(378.0, 22.0), 75.8));
    expect(box, uniqueRipplePattern(const Offset(378.0, 22.0), 75.8));

    // There should be no tooltip.
    expect(findTooltipContainer('Delete'), findsNothing);

    // Waits for a very long time.
    await tester.pumpAndSettle();

    // There should still be no tooltip.
    // This indicates that the tap is not on a delete icon.
    expect(findTooltipContainer('Delete'), findsNothing);

    await gesture.up();
1501
  });
1502

1503
  testWidgetsWithLeakTracking('Selection with avatar works as expected on RawChip', (WidgetTester tester) async {
1504
    bool selected = false;
1505
    final UniqueKey labelKey = UniqueKey();
1506
    Future<void> pushChip({ Widget? avatar, bool selectable = false }) async {
1507
      return tester.pumpWidget(
1508
        wrapForChip(
1509
          useMaterial3: false,
1510
          child: Wrap(
1511
            children: <Widget>[
1512 1513
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
1514
                  avatar: avatar,
1515
                  onSelected: selectable
1516 1517 1518 1519 1520 1521
                    ? (bool value) {
                        setState(() {
                          selected = value;
                        });
                      }
                    : null,
1522
                  selected: selected,
1523
                  label: Text('Long Chip Label', key: labelKey),
1524 1525 1526 1527
                  shape: const StadiumBorder(),
                );
              }),
            ],
1528 1529 1530 1531 1532 1533
          ),
        ),
      );
    }

    // With avatar, but not selectable.
1534
    final UniqueKey avatarKey = UniqueKey();
1535
    await pushChip(
1536
      avatar: SizedBox(width: 40.0, height: 40.0, key: avatarKey),
1537
    );
1538
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(258.0, 48.0)));
1539 1540 1541

    // Turn on selection.
    await pushChip(
1542
      avatar: SizedBox(width: 40.0, height: 40.0, key: avatarKey),
1543 1544 1545 1546 1547 1548 1549
      selectable: true,
    );
    await tester.pumpAndSettle();

    // Simulate a tap on the label to select the chip.
    await tester.tap(find.byKey(labelKey));
    expect(selected, equals(true));
1550
    expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
1551 1552
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
1553
    expect(getSelectProgress(tester), moreOrLessEquals(0.002, epsilon: 0.01));
1554 1555 1556
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 50));
1557
    expect(getSelectProgress(tester), moreOrLessEquals(0.54, epsilon: 0.01));
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 100));
    expect(getSelectProgress(tester), equals(1.0));
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pumpAndSettle();
    // Simulate another tap on the label to deselect the chip.
    await tester.tap(find.byKey(labelKey));
    expect(selected, equals(false));
1568
    expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
1569 1570
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 20));
1571
    expect(getSelectProgress(tester), moreOrLessEquals(0.875, epsilon: 0.01));
1572 1573 1574
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 20));
1575
    expect(getSelectProgress(tester), moreOrLessEquals(0.13, epsilon: 0.01));
1576 1577 1578 1579 1580 1581
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 100));
    expect(getSelectProgress(tester), equals(0.0));
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
1582
  });
1583

1584
  testWidgetsWithLeakTracking('Selection without avatar works as expected on RawChip', (WidgetTester tester) async {
1585
    bool selected = false;
1586
    final UniqueKey labelKey = UniqueKey();
1587
    Future<void> pushChip({ bool selectable = false }) async {
1588
      return tester.pumpWidget(
1589
        wrapForChip(
1590
          useMaterial3: false,
1591
          child: Wrap(
1592
            children: <Widget>[
1593 1594
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
1595
                  onSelected: selectable
1596 1597 1598 1599 1600 1601
                    ? (bool value) {
                        setState(() {
                          selected = value;
                        });
                      }
                    : null,
1602
                  selected: selected,
1603
                  label: Text('Long Chip Label', key: labelKey),
1604 1605 1606 1607
                  shape: const StadiumBorder(),
                );
              }),
            ],
1608 1609 1610 1611 1612 1613 1614
          ),
        ),
      );
    }

    // Without avatar, but not selectable.
    await pushChip();
1615
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(234.0, 48.0)));
1616 1617 1618 1619 1620 1621 1622 1623

    // Turn on selection.
    await pushChip(selectable: true);
    await tester.pumpAndSettle();

    // Simulate a tap on the label to select the chip.
    await tester.tap(find.byKey(labelKey));
    expect(selected, equals(true));
1624
    expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
1625 1626
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
1627 1628
    expect(getSelectProgress(tester), moreOrLessEquals(0.002, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.459, epsilon: 0.01));
1629 1630
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 50));
1631 1632
    expect(getSelectProgress(tester), moreOrLessEquals(0.54, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.92, epsilon: 0.01));
1633 1634 1635 1636 1637 1638 1639 1640 1641
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 100));
    expect(getSelectProgress(tester), equals(1.0));
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pumpAndSettle();
    // Simulate another tap on the label to deselect the chip.
    await tester.tap(find.byKey(labelKey));
    expect(selected, equals(false));
1642
    expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
1643 1644
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 20));
1645 1646
    expect(getSelectProgress(tester), moreOrLessEquals(0.875, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.96, epsilon: 0.01));
1647 1648
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 20));
1649 1650
    expect(getSelectProgress(tester), moreOrLessEquals(0.13, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.75, epsilon: 0.01));
1651 1652 1653 1654 1655
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 100));
    expect(getSelectProgress(tester), equals(0.0));
    expect(getAvatarDrawerProgress(tester), equals(0.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
1656
  });
1657

1658
  testWidgetsWithLeakTracking('Activation works as expected on RawChip', (WidgetTester tester) async {
1659
    bool selected = false;
1660
    final UniqueKey labelKey = UniqueKey();
1661
    Future<void> pushChip({ Widget? avatar, bool selectable = false }) async {
1662
      return tester.pumpWidget(
1663
        wrapForChip(
1664
          useMaterial3: false,
1665
          child: Wrap(
1666
            children: <Widget>[
1667 1668
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
1669
                  avatar: avatar,
1670
                  onSelected: selectable
1671 1672 1673 1674 1675 1676
                    ? (bool value) {
                        setState(() {
                          selected = value;
                        });
                      }
                    : null,
1677
                  selected: selected,
1678
                  label: Text('Long Chip Label', key: labelKey),
1679 1680 1681 1682 1683
                  shape: const StadiumBorder(),
                  showCheckmark: false,
                );
              }),
            ],
1684 1685 1686 1687 1688
          ),
        ),
      );
    }

1689
    final UniqueKey avatarKey = UniqueKey();
1690
    await pushChip(
1691
      avatar: SizedBox(width: 40.0, height: 40.0, key: avatarKey),
1692 1693 1694 1695 1696 1697
      selectable: true,
    );
    await tester.pumpAndSettle();

    await tester.tap(find.byKey(labelKey));
    expect(selected, equals(true));
1698
    expect(SchedulerBinding.instance.transientCallbackCount, equals(2));
1699 1700
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
1701
    expect(getSelectProgress(tester), moreOrLessEquals(0.002, epsilon: 0.01));
1702 1703 1704
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 50));
1705
    expect(getSelectProgress(tester), moreOrLessEquals(0.54, epsilon: 0.01));
1706 1707 1708 1709 1710 1711 1712
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 100));
    expect(getSelectProgress(tester), equals(1.0));
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pumpAndSettle();
1713
  });
1714

1715
  testWidgetsWithLeakTracking('Chip uses ThemeData chip theme if present', (WidgetTester tester) async {
1716
    final ThemeData theme = ThemeData(
1717
      useMaterial3: false,
1718 1719 1720 1721 1722 1723
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final ChipThemeData chipTheme = theme.chipTheme;

    Widget buildChip(ChipThemeData data) {
1724
      return wrapForChip(
1725
        child: Theme(
1726 1727
          data: theme,
          child: const InputChip(
1728
            label: Text('Label'),
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
          ),
        ),
      );
    }

    await tester.pumpWidget(buildChip(chipTheme));

    final RenderBox materialBox = tester.firstRenderObject<RenderBox>(
      find.descendant(
        of: find.byType(RawChip),
        matching: find.byType(CustomPaint),
      ),
    );

1743
    expect(materialBox, paints..rrect(color: chipTheme.disabledColor));
1744 1745
  });

1746
  testWidgetsWithLeakTracking('Chip merges ChipThemeData label style with the provided label style', (WidgetTester tester) async {
1747 1748
    // The font family should be preserved even if the chip overrides some label style properties
    final ThemeData theme = ThemeData(
1749
      fontFamily: 'MyFont',
1750 1751 1752
    );

    Widget buildChip() {
1753
      return wrapForChip(
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
        child: Theme(
          data: theme,
          child: const Chip(
            label: Text('Label'),
            labelStyle: TextStyle(fontWeight: FontWeight.w200),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildChip());

1766
    final TextStyle labelStyle = getLabelStyle(tester, 'Label').style;
1767
    expect(labelStyle.inherit, false);
1768 1769 1770 1771
    expect(labelStyle.fontFamily, 'MyFont');
    expect(labelStyle.fontWeight, FontWeight.w200);
  });

1772
  testWidgetsWithLeakTracking('ChipTheme labelStyle with inherit:true', (WidgetTester tester) async {
1773
    Widget buildChip() {
1774
      return wrapForChip(
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
        child: Theme(
          data: ThemeData.light().copyWith(
            chipTheme: const ChipThemeData(
              labelStyle: TextStyle(height: 4), // inherit: true
            ),
          ),
          child: const Chip(label: Text('Label')), // labeStyle: null
        ),
      );
    }

    await tester.pumpWidget(buildChip());
    final TextStyle labelStyle = getLabelStyle(tester, 'Label').style;
    expect(labelStyle.inherit, true); // because chipTheme.labelStyle.merge(null)
    expect(labelStyle.height, 4);
  });

1792
  testWidgetsWithLeakTracking('Chip does not merge inherit:false label style with the theme label style', (WidgetTester tester) async {
1793
    Widget buildChip() {
1794
      return wrapForChip(
1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
        child: Theme(
          data: ThemeData(fontFamily: 'MyFont'),
          child: const DefaultTextStyle(
            style: TextStyle(height: 8),
            child: Chip(
              label: Text('Label'),
              labelStyle: TextStyle(fontWeight: FontWeight.w200, inherit: false),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildChip());
    final TextStyle labelStyle = getLabelStyle(tester, 'Label').style;
    expect(labelStyle.inherit, false);
    expect(labelStyle.fontFamily, null);
    expect(labelStyle.height, null);
    expect(labelStyle.fontWeight, FontWeight.w200);
  });

1816
  testWidgetsWithLeakTracking('Chip size is configurable by ThemeData.materialTapTargetSize', (WidgetTester tester) async {
1817
    final Key key1 = UniqueKey();
1818
    await tester.pumpWidget(
1819
      wrapForChip(
1820
        child: Theme(
1821
          data: ThemeData(useMaterial3: false, materialTapTargetSize: MaterialTapTargetSize.padded),
1822 1823
          child: Center(
            child: RawChip(
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
              key: key1,
              label: const Text('test'),
            ),
          ),
        ),
      ),
    );

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

1834
    final Key key2 = UniqueKey();
1835
    await tester.pumpWidget(
1836
      wrapForChip(
1837
        child: Theme(
1838
          data: ThemeData(useMaterial3: false, materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
1839 1840
          child: Center(
            child: RawChip(
1841 1842 1843 1844 1845 1846 1847 1848 1849
              key: key2,
              label: const Text('test'),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byKey(key2)), const Size(80.0, 32.0));
1850
  });
1851

1852
  testWidgets('Chip uses the right theme colors for the right components', (WidgetTester tester) async {
1853
    final ThemeData themeData = ThemeData(
1854 1855 1856
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
1857 1858 1859
    final ChipThemeData defaultChipTheme = ChipThemeData.fromDefaults(
      brightness: themeData.brightness,
      secondaryColor: Colors.blue,
1860
      labelStyle: themeData.textTheme.bodyLarge!,
1861
    );
1862 1863
    bool value = false;
    Widget buildApp({
1864 1865 1866
      ChipThemeData? chipTheme,
      Widget? avatar,
      Widget? deleteIcon,
1867 1868 1869 1870
      bool isSelectable = true,
      bool isPressable = false,
      bool isDeletable = true,
      bool showCheckmark = true,
1871
    }) {
1872
      chipTheme ??= defaultChipTheme;
1873
      return wrapForChip(
1874
        child: Theme(
1875
          data: themeData,
1876
          child: ChipTheme(
1877
            data: chipTheme,
1878 1879
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return RawChip(
1880
                showCheckmark: showCheckmark,
1881
                onDeleted: isDeletable ? () { } : null,
1882 1883 1884
                avatar: avatar,
                deleteIcon: deleteIcon,
                isEnabled: isSelectable || isPressable,
1885
                shape: chipTheme?.shape,
1886
                selected: isSelectable && value,
1887
                label: Text('$value'),
1888
                onSelected: isSelectable
1889 1890 1891 1892 1893 1894
                  ? (bool newValue) {
                      setState(() {
                        value = newValue;
                      });
                    }
                  : null,
1895
                onPressed: isPressable
1896 1897 1898 1899 1900 1901
                  ? () {
                      setState(() {
                        value = true;
                      });
                    }
                  : null,
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
              );
            }),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp());

    RenderBox materialBox = getMaterialBox(tester);
    IconThemeData iconData = getIconData(tester);
1913
    DefaultTextStyle labelStyle = getLabelStyle(tester, 'false');
1914 1915

    // Check default theme for enabled widget.
1916
    expect(materialBox, paints..rrect(color: defaultChipTheme.backgroundColor));
1917 1918 1919 1920 1921
    expect(iconData.color, equals(const Color(0xde000000)));
    expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));
    await tester.tap(find.byType(RawChip));
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
1922
    expect(materialBox, paints..rrect(color: defaultChipTheme.selectedColor));
1923 1924 1925 1926
    await tester.tap(find.byType(RawChip));
    await tester.pumpAndSettle();

    // Check default theme with disabled widget.
1927
    await tester.pumpWidget(buildApp(isSelectable: false));
1928 1929
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
1930
    labelStyle = getLabelStyle(tester, 'false');
1931
    expect(materialBox, paints..rrect(color: defaultChipTheme.disabledColor));
1932 1933 1934
    expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));

    // Apply a custom theme.
1935 1936 1937 1938
    const Color customColor1 = Color(0xcafefeed);
    const Color customColor2 = Color(0xdeadbeef);
    const Color customColor3 = Color(0xbeefcafe);
    const Color customColor4 = Color(0xaddedabe);
1939
    final ChipThemeData customTheme = defaultChipTheme.copyWith(
1940 1941 1942 1943 1944 1945
      brightness: Brightness.dark,
      backgroundColor: customColor1,
      disabledColor: customColor2,
      selectedColor: customColor3,
      deleteIconColor: customColor4,
    );
1946
    await tester.pumpWidget(buildApp(chipTheme: customTheme));
1947 1948 1949
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
    iconData = getIconData(tester);
1950
    labelStyle = getLabelStyle(tester, 'false');
1951 1952

    // Check custom theme for enabled widget.
1953
    expect(materialBox, paints..rrect(color: customTheme.backgroundColor));
1954 1955 1956 1957 1958
    expect(iconData.color, equals(customTheme.deleteIconColor));
    expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));
    await tester.tap(find.byType(RawChip));
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
1959
    expect(materialBox, paints..rrect(color: customTheme.selectedColor));
1960 1961 1962 1963 1964
    await tester.tap(find.byType(RawChip));
    await tester.pumpAndSettle();

    // Check custom theme with disabled widget.
    await tester.pumpWidget(buildApp(
1965
      chipTheme: customTheme,
1966 1967 1968 1969
      isSelectable: false,
    ));
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
1970
    labelStyle = getLabelStyle(tester, 'false');
1971
    expect(materialBox, paints..rrect(color: customTheme.disabledColor));
1972 1973
    expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));
  });
1974 1975

  group('Chip semantics', () {
1976
    testWidgetsWithLeakTracking('label only', (WidgetTester tester) async {
1977
      final SemanticsTester semanticsTester = SemanticsTester(tester);
1978

1979 1980
      await tester.pumpWidget(const MaterialApp(
        home: Material(
1981 1982
          child: RawChip(
            label: Text('test'),
1983 1984 1985 1986
          ),
        ),
      ));

1987 1988 1989
      expect(
        semanticsTester,
        hasSemantics(
1990
          TestSemantics.root(
1991
            children: <TestSemantics>[
1992
              TestSemantics(
1993 1994
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
1995
                  TestSemantics(
1996
                    children: <TestSemantics>[
1997
                      TestSemantics(
1998 1999 2000 2001 2002
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
2003 2004 2005 2006
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                            ],
2007 2008
                          ),
                        ],
2009 2010 2011 2012 2013 2014
                      ),
                    ],
                  ),
                ],
              ),
            ],
2015 2016 2017 2018 2019 2020
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2021 2022 2023
      semanticsTester.dispose();
    });

2024
    testWidgetsWithLeakTracking('delete', (WidgetTester tester) async {
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
      final SemanticsTester semanticsTester = SemanticsTester(tester);

      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
            label: const Text('test'),
            onDeleted: () { },
          ),
        ),
      ));

2036 2037 2038
      expect(
        semanticsTester,
        hasSemantics(
2039 2040 2041 2042 2043 2044 2045 2046
          TestSemantics.root(
            children: <TestSemantics>[
              TestSemantics(
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
                  TestSemantics(
                    children: <TestSemantics>[
                      TestSemantics(
2047
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
2048 2049
                        children: <TestSemantics>[
                          TestSemantics(
2050
                            label: 'test',
2051
                            textDirection: TextDirection.ltr,
2052 2053 2054 2055
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                            ],
2056 2057
                            children: <TestSemantics>[
                              TestSemantics(
2058
                                tooltip: 'Delete',
2059
                                actions: <SemanticsAction>[SemanticsAction.tap],
2060 2061 2062
                                textDirection: TextDirection.ltr,
                                flags: <SemanticsFlag>[
                                  SemanticsFlag.isButton,
2063
                                  SemanticsFlag.isFocusable,
2064 2065
                                ],
                              ),
2066 2067 2068 2069 2070 2071 2072 2073 2074
                            ],
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
2075 2076 2077 2078 2079 2080
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2081 2082 2083
      semanticsTester.dispose();
    });

2084
    testWidgetsWithLeakTracking('with onPressed', (WidgetTester tester) async {
2085
      final SemanticsTester semanticsTester = SemanticsTester(tester);
2086

2087 2088 2089
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
2090
            label: const Text('test'),
2091
            onPressed: () { },
2092 2093 2094 2095
          ),
        ),
      ));

2096 2097 2098
      expect(
        semanticsTester,
        hasSemantics(
2099
          TestSemantics.root(
2100
            children: <TestSemantics>[
2101
              TestSemantics(
2102 2103
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
2104
                  TestSemantics(
2105
                    children: <TestSemantics> [
2106
                      TestSemantics(
2107 2108 2109 2110 2111 2112 2113
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
2114
                              SemanticsFlag.isButton,
2115 2116 2117 2118 2119
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                          ),
2120 2121 2122 2123 2124 2125 2126
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
2127 2128 2129 2130 2131 2132
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2133 2134 2135 2136 2137

      semanticsTester.dispose();
    });


2138
    testWidgetsWithLeakTracking('with onSelected', (WidgetTester tester) async {
2139
      final SemanticsTester semanticsTester = SemanticsTester(tester);
2140 2141
      bool selected = false;

2142 2143 2144
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
2145 2146 2147 2148 2149 2150 2151 2152 2153
            label: const Text('test'),
            selected: selected,
            onSelected: (bool value) {
              selected = value;
            },
          ),
        ),
      ));

2154 2155 2156
      expect(
        semanticsTester,
        hasSemantics(
2157
          TestSemantics.root(
2158
            children: <TestSemantics>[
2159
              TestSemantics(
2160 2161
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
2162
                  TestSemantics(
2163
                    children: <TestSemantics>[
2164
                      TestSemantics(
2165 2166 2167 2168 2169 2170 2171
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
2172
                              SemanticsFlag.isButton,
2173 2174 2175 2176 2177
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                          ),
2178 2179 2180 2181 2182 2183 2184
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
2185 2186 2187 2188 2189 2190
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2191 2192

      await tester.tap(find.byType(RawChip));
2193 2194 2195
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
            label: const Text('test'),
            selected: selected,
            onSelected: (bool value) {
              selected = value;
            },
          ),
        ),
      ));

      expect(selected, true);
2206 2207 2208
      expect(
        semanticsTester,
        hasSemantics(
2209
          TestSemantics.root(
2210
            children: <TestSemantics>[
2211
              TestSemantics(
2212 2213
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
2214
                  TestSemantics(
2215
                    children: <TestSemantics>[
2216
                      TestSemantics(
2217 2218 2219 2220 2221 2222 2223
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
2224
                              SemanticsFlag.isButton,
2225 2226 2227 2228 2229 2230
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                              SemanticsFlag.isSelected,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                          ),
2231 2232 2233 2234 2235 2236 2237
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
2238 2239 2240 2241 2242 2243
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2244 2245 2246 2247

      semanticsTester.dispose();
    });

2248
    testWidgetsWithLeakTracking('disabled', (WidgetTester tester) async {
2249
      final SemanticsTester semanticsTester = SemanticsTester(tester);
2250

2251 2252 2253
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
2254
            isEnabled: false,
2255
            onPressed: () { },
2256 2257 2258 2259 2260
            label: const Text('test'),
          ),
        ),
      ));

2261 2262 2263
      expect(
        semanticsTester,
        hasSemantics(
2264
          TestSemantics.root(
2265
            children: <TestSemantics>[
2266
              TestSemantics(
2267 2268
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
2269
                  TestSemantics(
2270
                    children: <TestSemantics>[
2271
                      TestSemantics(
2272 2273 2274 2275 2276
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
2277 2278 2279 2280
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                            ],
2281 2282 2283
                            actions: <SemanticsAction>[],
                          ),
                        ],
2284 2285 2286
                      ),
                    ],
                  ),
2287 2288 2289
                ],
              ),
            ],
2290 2291 2292 2293 2294 2295
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2296 2297 2298 2299

      semanticsTester.dispose();
    });

2300
    testWidgetsWithLeakTracking('tapEnabled explicitly false', (WidgetTester tester) async {
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311
      final SemanticsTester semanticsTester = SemanticsTester(tester);

      await tester.pumpWidget(const MaterialApp(
        home: Material(
          child: RawChip(
            tapEnabled: false,
            label: Text('test'),
          ),
        ),
      ));

2312 2313 2314
      expect(
        semanticsTester,
        hasSemantics(
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
          TestSemantics.root(
            children: <TestSemantics>[
              TestSemantics(
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
                  TestSemantics(
                    children: <TestSemantics>[
                      TestSemantics(
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[], // Must not be a button when tapping is disabled.
                            actions: <SemanticsAction>[],
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
2338 2339 2340 2341 2342 2343
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2344 2345 2346 2347

      semanticsTester.dispose();
    });

2348
    testWidgetsWithLeakTracking('enabled when tapEnabled and canTap', (WidgetTester tester) async {
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
      final SemanticsTester semanticsTester = SemanticsTester(tester);

      // These settings make a Chip which can be tapped, both in general and at this moment.
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
            onPressed: () {},
            label: const Text('test'),
          ),
        ),
      ));

2361 2362 2363
      expect(
        semanticsTester,
        hasSemantics(
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
          TestSemantics.root(
            children: <TestSemantics>[
              TestSemantics(
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
                  TestSemantics(
                    children: <TestSemantics>[
                      TestSemantics(
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
2392 2393 2394 2395 2396 2397
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2398 2399 2400 2401

      semanticsTester.dispose();
    });

2402
    testWidgetsWithLeakTracking('disabled when tapEnabled but not canTap', (WidgetTester tester) async {
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
      final SemanticsTester semanticsTester = SemanticsTester(tester);
        // These settings make a Chip which _could_ be tapped, but not currently (ensures `canTap == false`).
        await tester.pumpWidget(const MaterialApp(
        home: Material(
          child: RawChip(
            label: Text('test'),
          ),
        ),
      ));

2413 2414 2415
      expect(
        semanticsTester,
        hasSemantics(
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437
          TestSemantics.root(
            children: <TestSemantics>[
              TestSemantics(
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
                  TestSemantics(
                    children: <TestSemantics>[
                      TestSemantics(
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                            ],
                          ),
                        ],
                      ),
                    ],
                  ),
2438 2439 2440
                ],
              ),
            ],
2441 2442 2443 2444 2445 2446
          ),
          ignoreTransform: true,
          ignoreId: true,
          ignoreRect: true,
        ),
      );
2447 2448 2449 2450 2451

      semanticsTester.dispose();
    });
  });

2452
  testWidgetsWithLeakTracking('can be tapped outside of chip delete icon', (WidgetTester tester) async {
2453 2454
    bool deleted = false;
    await tester.pumpWidget(
2455
      wrapForChip(
2456
        child: Row(
2457
          children: <Widget>[
2458
            Chip(
2459
              materialTapTargetSize: MaterialTapTargetSize.padded,
2460
              shape: const RoundedRectangleBorder(),
2461
              avatar: const CircleAvatar(child: Text('A')),
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
              label: const Text('Chip A'),
              onDeleted: () {
                deleted = true;
              },
              deleteIcon: const Icon(Icons.delete),
            ),
          ],
        ),
      ),
    );

    await tester.tapAt(tester.getTopRight(find.byType(Chip)) - const Offset(2.0, -2.0));
    await tester.pumpAndSettle();
    expect(deleted, true);
  });

2478
  testWidgetsWithLeakTracking('Chips can be tapped', (WidgetTester tester) async {
jslavitz's avatar
jslavitz committed
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: RawChip(
            label: Text('raw chip'),
          ),
        ),
      ),
    );

    await tester.tap(find.byType(RawChip));
    expect(tester.takeException(), null);
  });

2493
  testWidgetsWithLeakTracking('Chip elevation and shadow color work correctly', (WidgetTester tester) async {
2494
    final ThemeData theme = ThemeData(
2495
      useMaterial3: false,
2496 2497 2498 2499 2500 2501
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );

    final ChipThemeData chipTheme = theme.chipTheme;

2502
    InputChip inputChip = const InputChip(label: Text('Label'));
2503 2504

    Widget buildChip(ChipThemeData data) {
2505
      return wrapForChip(
2506 2507 2508 2509 2510 2511 2512 2513
        child: Theme(
          data: theme,
          child: inputChip,
        ),
      );
    }

    await tester.pumpWidget(buildChip(chipTheme));
2514 2515
    Material material = getMaterial(tester);
    expect(material.elevation, 0.0);
2516
    expect(material.shadowColor, Colors.black);
2517

2518 2519 2520
    inputChip = const InputChip(
      label: Text('Label'),
      elevation: 4.0,
2521 2522
      shadowColor: Colors.green,
      selectedShadowColor: Colors.blue,
2523
    );
2524 2525 2526

    await tester.pumpWidget(buildChip(chipTheme));
    await tester.pumpAndSettle();
2527 2528
    material = getMaterial(tester);
    expect(material.elevation, 4.0);
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
    expect(material.shadowColor, Colors.green);

    inputChip = const InputChip(
      label: Text('Label'),
      selected: true,
      shadowColor: Colors.green,
      selectedShadowColor: Colors.blue,
    );

    await tester.pumpWidget(buildChip(chipTheme));
    await tester.pumpAndSettle();
    material = getMaterial(tester);
    expect(material.shadowColor, Colors.blue);
2542 2543
  });

2544
  testWidgetsWithLeakTracking('can be tapped outside of chip body', (WidgetTester tester) async {
2545 2546
    bool pressed = false;
    await tester.pumpWidget(
2547
      wrapForChip(
2548
        child: Row(
2549
          children: <Widget>[
2550
            InputChip(
2551
              materialTapTargetSize: MaterialTapTargetSize.padded,
2552
              shape: const RoundedRectangleBorder(),
2553
              avatar: const CircleAvatar(child: Text('A')),
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568
              label: const Text('Chip A'),
              onPressed: () {
                pressed = true;
              },
            ),
          ],
        ),
      ),
    );

    await tester.tapAt(tester.getRect(find.byType(InputChip)).topCenter);
    await tester.pumpAndSettle();
    expect(pressed, true);
  });

2569
  testWidgetsWithLeakTracking('is hitTestable', (WidgetTester tester) async {
2570
    await tester.pumpWidget(
2571
      wrapForChip(
2572
        child: InputChip(
2573
          shape: const RoundedRectangleBorder(),
2574
          avatar: const CircleAvatar(child: Text('A')),
2575
          label: const Text('Chip A'),
2576
          onPressed: () { },
2577 2578 2579 2580
        ),
      ),
    );

2581
    expect(find.byType(InputChip).hitTestable(), findsOneWidget);
2582
  });
2583 2584 2585 2586 2587 2588 2589

  void checkChipMaterialClipBehavior(WidgetTester tester, Clip clipBehavior) {
    final Iterable<Material> materials = tester.widgetList<Material>(find.byType(Material));
    expect(materials.length, 2);
    expect(materials.last.clipBehavior, clipBehavior);
  }

2590
  testWidgetsWithLeakTracking('Chip clipBehavior properly passes through to the Material', (WidgetTester tester) async {
2591
    const Text label = Text('label');
2592
    await tester.pumpWidget(wrapForChip(child: const Chip(label: label)));
2593
    checkChipMaterialClipBehavior(tester, Clip.none);
2594

2595
    await tester.pumpWidget(wrapForChip(child: const Chip(label: label, clipBehavior: Clip.antiAlias)));
2596 2597
    checkChipMaterialClipBehavior(tester, Clip.antiAlias);
  });
2598 2599

  testWidgets('selected chip and avatar draw darkened layer within avatar circle', (WidgetTester tester) async {
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
    await tester.pumpWidget(
      wrapForChip(
        useMaterial3: false,
        child: const FilterChip(
          avatar: CircleAvatar(child: Text('t')),
          label: Text('test'),
          selected: true,
          onSelected: null,
        ),
      ),
    );
2611 2612 2613 2614 2615
    final RenderBox rawChip = tester.firstRenderObject<RenderBox>(
      find.descendant(
        of: find.byType(RawChip),
        matching: find.byWidgetPredicate((Widget widget) {
          return widget.runtimeType.toString() == '_ChipRenderWidget';
2616
        }),
2617
      ),
2618 2619 2620 2621 2622 2623 2624
    );
    const Color selectScrimColor = Color(0x60191919);
    expect(rawChip, paints..path(color: selectScrimColor, includes: <Offset>[
      const Offset(10, 10),
    ], excludes: <Offset>[
      const Offset(4, 4),
    ]));
2625
  });
2626

2627
  testWidgetsWithLeakTracking('Chips should use InkWell instead of InkResponse.', (WidgetTester tester) async {
2628 2629 2630 2631 2632
    // Regression test for https://github.com/flutter/flutter/issues/28646
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: ActionChip(
2633
            onPressed: () { },
2634 2635 2636 2637 2638 2639 2640
            label: const Text('action chip'),
          ),
        ),
      ),
    );
    expect(find.byType(InkWell), findsOneWidget);
  });
2641

2642
  testWidgets('Chip uses stateful color for text color in different states', (WidgetTester tester) async {
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
    final FocusNode focusNode = FocusNode();

    const Color pressedColor = Color(0x00000001);
    const Color hoverColor = Color(0x00000002);
    const Color focusedColor = Color(0x00000003);
    const Color defaultColor = Color(0x00000004);
    const Color selectedColor = Color(0x00000005);
    const Color disabledColor = Color(0x00000006);

    Color getTextColor(Set<MaterialState> states) {
2653
      if (states.contains(MaterialState.disabled)) {
2654
        return disabledColor;
2655
      }
2656

2657
      if (states.contains(MaterialState.pressed)) {
2658
        return pressedColor;
2659
      }
2660

2661
      if (states.contains(MaterialState.hovered)) {
2662
        return hoverColor;
2663
      }
2664

2665
      if (states.contains(MaterialState.focused)) {
2666
        return focusedColor;
2667
      }
2668

2669
      if (states.contains(MaterialState.selected)) {
2670
        return selectedColor;
2671
      }
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691

      return defaultColor;
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
        home: Scaffold(
          body: Focus(
            focusNode: focusNode,
            child: ChoiceChip(
              label: const Text('Chip'),
              selected: selected,
              onSelected: enabled ? (_) {} : null,
              labelStyle: TextStyle(color: MaterialStateColor.resolveWith(getTextColor)),
            ),
          ),
        ),
      );
    }
    Color textColor() {
2692
      return tester.renderObject<RenderParagraph>(find.text('Chip')).text.style!.color!;
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
    }

    // Default, not disabled.
    await tester.pumpWidget(chipWidget());
    expect(textColor(), equals(defaultColor));

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
    expect(textColor(), selectedColor);

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
    expect(textColor(), focusedColor);

    // Hovered.
    final Offset center = tester.getCenter(find.byType(ChoiceChip));
    final TestGesture gesture = await tester.createGesture(
      kind: PointerDeviceKind.mouse,
    );
    await gesture.addPointer();
    await gesture.moveTo(center);
    await tester.pumpAndSettle();
    expect(textColor(), hoverColor);

    // Pressed.
    await gesture.down(center);
    await tester.pumpAndSettle();
    expect(textColor(), pressedColor);

    // Disabled.
    await tester.pumpWidget(chipWidget(enabled: false));
    await tester.pumpAndSettle();
    expect(textColor(), disabledColor);
  });
2729

2730
  testWidgets('Chip uses stateful border side color in different states', (WidgetTester tester) async {
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742
    final FocusNode focusNode = FocusNode();

    const Color pressedColor = Color(0x00000001);
    const Color hoverColor = Color(0x00000002);
    const Color focusedColor = Color(0x00000003);
    const Color defaultColor = Color(0x00000004);
    const Color selectedColor = Color(0x00000005);
    const Color disabledColor = Color(0x00000006);

    BorderSide getBorderSide(Set<MaterialState> states) {
      Color sideColor = defaultColor;

2743
      if (states.contains(MaterialState.disabled)) {
2744
        sideColor = disabledColor;
2745
      } else if (states.contains(MaterialState.pressed)) {
2746
        sideColor = pressedColor;
2747
      } else if (states.contains(MaterialState.hovered)) {
2748
        sideColor = hoverColor;
2749
      } else if (states.contains(MaterialState.focused)) {
2750
        sideColor = focusedColor;
2751
      } else if (states.contains(MaterialState.selected)) {
2752
        sideColor = selectedColor;
2753
      }
2754

2755
      return BorderSide(color: sideColor);
2756 2757 2758 2759
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
2760
        theme: ThemeData(useMaterial3: false),
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
        home: Scaffold(
          body: Focus(
            focusNode: focusNode,
            child: ChoiceChip(
              label: const Text('Chip'),
              selected: selected,
              onSelected: enabled ? (_) {} : null,
              side: _MaterialStateBorderSide(getBorderSide),
            ),
          ),
        ),
      );
    }

    // Default, not disabled.
    await tester.pumpWidget(chipWidget());
2777
    expect(find.byType(RawChip), paints..rrect()..rrect(color: defaultColor));
2778 2779 2780

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
2781
    expect(find.byType(RawChip), paints..rrect()..rrect(color: selectedColor));
2782 2783 2784 2785 2786

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
2787
    expect(find.byType(RawChip), paints..rrect()..rrect(color: focusedColor));
2788 2789 2790 2791 2792 2793 2794 2795 2796

    // Hovered.
    final Offset center = tester.getCenter(find.byType(ChoiceChip));
    final TestGesture gesture = await tester.createGesture(
      kind: PointerDeviceKind.mouse,
    );
    await gesture.addPointer();
    await gesture.moveTo(center);
    await tester.pumpAndSettle();
2797
    expect(find.byType(RawChip), paints..rrect()..rrect(color: hoverColor));
2798 2799 2800 2801

    // Pressed.
    await gesture.down(center);
    await tester.pumpAndSettle();
2802
    expect(find.byType(RawChip), paints..rrect()..rrect(color: pressedColor));
2803 2804 2805 2806

    // Disabled.
    await tester.pumpWidget(chipWidget(enabled: false));
    await tester.pumpAndSettle();
2807
    expect(find.byType(RawChip), paints..rrect()..rrect(color: disabledColor));
2808 2809
  });

2810
  testWidgets('Chip uses stateful border side color from resolveWith', (WidgetTester tester) async {
2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
    final FocusNode focusNode = FocusNode();

    const Color pressedColor = Color(0x00000001);
    const Color hoverColor = Color(0x00000002);
    const Color focusedColor = Color(0x00000003);
    const Color defaultColor = Color(0x00000004);
    const Color selectedColor = Color(0x00000005);
    const Color disabledColor = Color(0x00000006);

    BorderSide getBorderSide(Set<MaterialState> states) {
      Color sideColor = defaultColor;

2823
      if (states.contains(MaterialState.disabled)) {
2824
        sideColor = disabledColor;
2825
      } else if (states.contains(MaterialState.pressed)) {
2826
        sideColor = pressedColor;
2827
      } else if (states.contains(MaterialState.hovered)) {
2828
        sideColor = hoverColor;
2829
      } else if (states.contains(MaterialState.focused)) {
2830
        sideColor = focusedColor;
2831
      } else if (states.contains(MaterialState.selected)) {
2832
        sideColor = selectedColor;
2833
      }
2834

2835
      return BorderSide(color: sideColor);
2836 2837 2838 2839
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
2840
        theme: ThemeData(useMaterial3: false),
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
        home: Scaffold(
          body: Focus(
            focusNode: focusNode,
            child: ChoiceChip(
              label: const Text('Chip'),
              selected: selected,
              onSelected: enabled ? (_) {} : null,
              side: MaterialStateBorderSide.resolveWith(getBorderSide),
            ),
          ),
        ),
      );
    }

    // Default, not disabled.
    await tester.pumpWidget(chipWidget());
2857
    expect(find.byType(RawChip), paints..rrect()..rrect(color: defaultColor));
2858 2859 2860

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
2861
    expect(find.byType(RawChip), paints..rrect()..rrect(color: selectedColor));
2862 2863 2864 2865 2866

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
2867
    expect(find.byType(RawChip), paints..rrect()..rrect(color: focusedColor));
2868 2869 2870 2871 2872 2873 2874 2875 2876

    // Hovered.
    final Offset center = tester.getCenter(find.byType(ChoiceChip));
    final TestGesture gesture = await tester.createGesture(
      kind: PointerDeviceKind.mouse,
    );
    await gesture.addPointer();
    await gesture.moveTo(center);
    await tester.pumpAndSettle();
2877
    expect(find.byType(RawChip), paints..rrect()..rrect(color: hoverColor));
2878 2879 2880 2881

    // Pressed.
    await gesture.down(center);
    await tester.pumpAndSettle();
2882
    expect(find.byType(RawChip), paints..rrect()..rrect(color: pressedColor));
2883 2884 2885 2886

    // Disabled.
    await tester.pumpWidget(chipWidget(enabled: false));
    await tester.pumpAndSettle();
2887
    expect(find.byType(RawChip), paints..rrect()..rrect(color: disabledColor));
2888 2889 2890

  });

2891
  testWidgets('Chip uses stateful nullable border side color from resolveWith', (WidgetTester tester) async {
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905
    final FocusNode focusNode = FocusNode();

    const Color pressedColor = Color(0x00000001);
    const Color hoverColor = Color(0x00000002);
    const Color focusedColor = Color(0x00000003);
    const Color defaultColor = Color(0x00000004);
    const Color disabledColor = Color(0x00000006);

    const Color fallbackThemeColor = Color(0x00000007);
    const BorderSide defaultBorderSide = BorderSide(color: fallbackThemeColor, width: 10.0);

    BorderSide? getBorderSide(Set<MaterialState> states) {
      Color sideColor = defaultColor;

2906
      if (states.contains(MaterialState.disabled)) {
2907
        sideColor = disabledColor;
2908
      } else if (states.contains(MaterialState.pressed)) {
2909
        sideColor = pressedColor;
2910
      } else if (states.contains(MaterialState.hovered)) {
2911
        sideColor = hoverColor;
2912
      } else if (states.contains(MaterialState.focused)) {
2913
        sideColor = focusedColor;
2914
      } else if (states.contains(MaterialState.selected)) {
2915
        return null;
2916
      }
2917

2918
      return BorderSide(color: sideColor);
2919 2920 2921 2922
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
2923
        theme: ThemeData(useMaterial3: false),
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
        home: Scaffold(
          body: Focus(
            focusNode: focusNode,
            child: ChipTheme(
              data: ThemeData.light().chipTheme.copyWith(
                side: defaultBorderSide,
              ),
              child: ChoiceChip(
                label: const Text('Chip'),
                selected: selected,
                onSelected: enabled ? (_) {} : null,
                side: MaterialStateBorderSide.resolveWith(getBorderSide),
              ),
            ),
          ),
        ),
      );
    }

    // Default, not disabled.
    await tester.pumpWidget(chipWidget());
2945
    expect(find.byType(RawChip), paints..rrect()..rrect(color: defaultColor));
2946 2947 2948 2949 2950

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
    // Because the resolver returns `null` for this value, we should fall back
    // to the theme
2951
    expect(find.byType(RawChip), paints..rrect()..rrect(color: fallbackThemeColor));
2952 2953 2954 2955 2956

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
2957
    expect(find.byType(RawChip), paints..rrect()..rrect(color: focusedColor));
2958 2959 2960 2961 2962 2963 2964 2965 2966

    // Hovered.
    final Offset center = tester.getCenter(find.byType(ChoiceChip));
    final TestGesture gesture = await tester.createGesture(
      kind: PointerDeviceKind.mouse,
    );
    await gesture.addPointer();
    await gesture.moveTo(center);
    await tester.pumpAndSettle();
2967
    expect(find.byType(RawChip), paints..rrect()..rrect(color: hoverColor));
2968 2969 2970 2971

    // Pressed.
    await gesture.down(center);
    await tester.pumpAndSettle();
2972
    expect(find.byType(RawChip), paints..rrect()..rrect(color: pressedColor));
2973 2974 2975 2976

    // Disabled.
    await tester.pumpWidget(chipWidget(enabled: false));
    await tester.pumpAndSettle();
2977
    expect(find.byType(RawChip), paints..rrect()..rrect(color: disabledColor));
2978 2979
  });

2980
  testWidgets('Chip uses stateful shape in different states', (WidgetTester tester) async {
2981 2982 2983
    final FocusNode focusNode = FocusNode();
    OutlinedBorder? getShape(Set<MaterialState> states) {

2984
      if (states.contains(MaterialState.disabled)) {
2985
        return const BeveledRectangleBorder();
2986
      } else if (states.contains(MaterialState.pressed)) {
2987
        return const CircleBorder();
2988
      } else if (states.contains(MaterialState.hovered)) {
2989
        return const ContinuousRectangleBorder();
2990
      } else if (states.contains(MaterialState.focused)) {
2991
        return const RoundedRectangleBorder();
2992
      } else if (states.contains(MaterialState.selected)) {
2993
        return const BeveledRectangleBorder();
2994
      }
2995 2996 2997 2998 2999 3000

      return null;
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
3001
        theme: ThemeData(useMaterial3: false),
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
        home: Scaffold(
          body: Focus(
            focusNode: focusNode,
            child: ChoiceChip(
              selected: selected,
              label: const Text('Chip'),
              shape: _MaterialStateOutlinedBorder(getShape),
              onSelected: enabled ? (_) {} : null,
            ),
          ),
        ),
      );
    }

    // Default, not disabled. Defers to default shape.
    await tester.pumpWidget(chipWidget());
    expect(getMaterial(tester).shape, isA<StadiumBorder>());

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
    expect(getMaterial(tester).shape, isA<BeveledRectangleBorder>());

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
    expect(getMaterial(tester).shape, isA<RoundedRectangleBorder>());

    // Hovered.
    final Offset center = tester.getCenter(find.byType(ChoiceChip));
    final TestGesture gesture = await tester.createGesture(
      kind: PointerDeviceKind.mouse,
    );
    await gesture.addPointer();
    await gesture.moveTo(center);
    await tester.pumpAndSettle();
    expect(getMaterial(tester).shape, isA<ContinuousRectangleBorder>());

    // Pressed.
    await gesture.down(center);
    await tester.pumpAndSettle();
    expect(getMaterial(tester).shape, isA<CircleBorder>());

    // Disabled.
    await tester.pumpWidget(chipWidget(enabled: false));
    await tester.pumpAndSettle();
    expect(getMaterial(tester).shape, isA<BeveledRectangleBorder>());
  });

3051
  testWidgetsWithLeakTracking('Chip defers to theme, if shape and side resolves to null', (WidgetTester tester) async {
3052 3053
    const OutlinedBorder themeShape = StadiumBorder();
    const OutlinedBorder selectedShape = RoundedRectangleBorder();
3054 3055
    const BorderSide themeBorderSide = BorderSide(color: Color(0x00000001));
    const BorderSide selectedBorderSide = BorderSide(color: Color(0x00000002));
3056 3057

    OutlinedBorder? getShape(Set<MaterialState> states) {
3058
      if (states.contains(MaterialState.selected)) {
3059
        return selectedShape;
3060
      }
3061 3062 3063 3064
      return null;
    }

    BorderSide? getBorderSide(Set<MaterialState> states) {
3065
      if (states.contains(MaterialState.selected)) {
3066
        return selectedBorderSide;
3067
      }
3068 3069 3070 3071 3072 3073
      return null;
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
        theme: ThemeData(
3074
          useMaterial3: false,
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094
          chipTheme: ThemeData.light().chipTheme.copyWith(
            shape: themeShape,
            side: themeBorderSide,
          ),
        ),
        home: Scaffold(
          body: ChoiceChip(
            selected: selected,
            label: const Text('Chip'),
            shape: _MaterialStateOutlinedBorder(getShape),
            side: _MaterialStateBorderSide(getBorderSide),
            onSelected: enabled ? (_) {} : null,
          ),
        ),
      );
    }

    // Default, not disabled. Defer to theme.
    await tester.pumpWidget(chipWidget());
    expect(getMaterial(tester).shape, isA<StadiumBorder>());
3095
    expect(find.byType(RawChip), paints..rrect()..rrect(color: themeBorderSide.color));
3096 3097 3098 3099

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
    expect(getMaterial(tester).shape, isA<RoundedRectangleBorder>());
3100
    expect(find.byType(RawChip), paints..rect()..drrect(color: selectedBorderSide.color));
3101 3102
  });

3103
  testWidgetsWithLeakTracking('Chip responds to density changes.', (WidgetTester tester) async {
3104 3105 3106 3107 3108
    const Key key = Key('test');
    const Key textKey = Key('test text');
    const Key iconKey = Key('test icon');
    const Key avatarKey = Key('test avatar');
    Future<void> buildTest(VisualDensity visualDensity) async {
3109
      return tester.pumpWidget(
3110
        MaterialApp(
3111
          theme: ThemeData(useMaterial3: false),
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
          home: Material(
            child: Center(
              child: Column(
                children: <Widget>[
                  InputChip(
                    visualDensity: visualDensity,
                    key: key,
                    onPressed: () {},
                    onDeleted: () {},
                    label: const Text('Test', key: textKey),
                    deleteIcon: const Icon(Icons.delete, key: iconKey),
                    avatar: const Icon(Icons.play_arrow, key: avatarKey),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    }

    // The Chips only change in size vertically in response to density, so
    // horizontal changes aren't expected.
    await buildTest(VisualDensity.standard);
    Rect box = tester.getRect(find.byKey(key));
    Rect textBox = tester.getRect(find.byKey(textKey));
    Rect iconBox = tester.getRect(find.byKey(iconKey));
    Rect avatarBox = tester.getRect(find.byKey(avatarKey));
    expect(box.size, equals(const Size(128, 32.0 + 16.0)));
    expect(textBox.size, equals(const Size(56, 14)));
    expect(iconBox.size, equals(const Size(24, 24)));
    expect(avatarBox.size, equals(const Size(24, 24)));
    expect(textBox.top, equals(17));
    expect(box.bottom - textBox.bottom, equals(17));
    expect(textBox.left, equals(372));
    expect(box.right - textBox.right, equals(36));

    // Try decreasing density (with higher density numbers).
    await buildTest(const VisualDensity(horizontal: 3.0, vertical: 3.0));
    box = tester.getRect(find.byKey(key));
    textBox = tester.getRect(find.byKey(textKey));
    iconBox = tester.getRect(find.byKey(iconKey));
    avatarBox = tester.getRect(find.byKey(avatarKey));
    expect(box.size, equals(const Size(128, 60)));
    expect(textBox.size, equals(const Size(56, 14)));
    expect(iconBox.size, equals(const Size(24, 24)));
    expect(avatarBox.size, equals(const Size(24, 24)));
    expect(textBox.top, equals(23));
    expect(box.bottom - textBox.bottom, equals(23));
    expect(textBox.left, equals(372));
    expect(box.right - textBox.right, equals(36));

    // Try increasing density (with lower density numbers).
    await buildTest(const VisualDensity(horizontal: -3.0, vertical: -3.0));
    box = tester.getRect(find.byKey(key));
    textBox = tester.getRect(find.byKey(textKey));
    iconBox = tester.getRect(find.byKey(iconKey));
    avatarBox = tester.getRect(find.byKey(avatarKey));
    expect(box.size, equals(const Size(128, 36)));
    expect(textBox.size, equals(const Size(56, 14)));
    expect(iconBox.size, equals(const Size(24, 24)));
    expect(avatarBox.size, equals(const Size(24, 24)));
    expect(textBox.top, equals(11));
    expect(box.bottom - textBox.bottom, equals(11));
    expect(textBox.left, equals(372));
    expect(box.right - textBox.right, equals(36));

    // Now test that horizontal and vertical are wired correctly. Negating the
    // horizontal should have no change over what's above.
    await buildTest(const VisualDensity(horizontal: 3.0, vertical: -3.0));
    await tester.pumpAndSettle();
    box = tester.getRect(find.byKey(key));
    textBox = tester.getRect(find.byKey(textKey));
    iconBox = tester.getRect(find.byKey(iconKey));
    avatarBox = tester.getRect(find.byKey(avatarKey));
    expect(box.size, equals(const Size(128, 36)));
    expect(textBox.size, equals(const Size(56, 14)));
    expect(iconBox.size, equals(const Size(24, 24)));
    expect(avatarBox.size, equals(const Size(24, 24)));
    expect(textBox.top, equals(11));
    expect(box.bottom - textBox.bottom, equals(11));
    expect(textBox.left, equals(372));
    expect(box.right - textBox.right, equals(36));

    // Make sure the "Comfortable" setting is the spec'd size
    await buildTest(VisualDensity.comfortable);
    await tester.pumpAndSettle();
    box = tester.getRect(find.byKey(key));
    expect(box.size, equals(const Size(128, 28.0 + 16.0)));

    // Make sure the "Compact" setting is the spec'd size
    await buildTest(VisualDensity.compact);
    await tester.pumpAndSettle();
    box = tester.getRect(find.byKey(key));
    expect(box.size, equals(const Size(128, 24.0 + 16.0)));
  });

3209
  testWidgetsWithLeakTracking('Chip delete button tooltip is disabled if deleteButtonTooltipMessage is empty', (WidgetTester tester) async {
3210 3211
    final UniqueKey deleteButtonKey = UniqueKey();
    await tester.pumpWidget(
3212
      chipWithOptionalDeleteButton(
3213 3214
        deleteButtonKey: deleteButtonKey,
        deletable: true,
3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
        deleteButtonTooltipMessage: '',
      ),
    );

    // Hover over the delete icon of the chip
    final Offset centerOfDeleteButton = tester.getCenter(find.byKey(deleteButtonKey));
    final TestGesture hoverGesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await hoverGesture.moveTo(centerOfDeleteButton);
    addTearDown(hoverGesture.removePointer);

    await tester.pump();

    // Wait for some more time while hovering over the delete button
    await tester.pumpAndSettle();

    // There should be no delete button tooltip
    expect(findTooltipContainer(''), findsNothing);
  });

3234
  testWidgetsWithLeakTracking('Disabling delete button tooltip does not disable chip tooltip', (WidgetTester tester) async {
3235 3236
    final UniqueKey deleteButtonKey = UniqueKey();
    await tester.pumpWidget(
3237
      chipWithOptionalDeleteButton(
3238 3239 3240
        deleteButtonKey: deleteButtonKey,
        deletable: true,
        deleteButtonTooltipMessage: '',
3241 3242 3243 3244
        chipTooltip: 'Chip Tooltip',
      ),
    );

3245
    // Hover over the delete icon of the chip
3246 3247 3248 3249 3250 3251 3252
    final Offset centerOfDeleteButton = tester.getCenter(find.byKey(deleteButtonKey));
    final TestGesture hoverGesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await hoverGesture.moveTo(centerOfDeleteButton);
    addTearDown(hoverGesture.removePointer);

    await tester.pump();

3253
    // Wait for some more time while hovering over the delete button
3254 3255
    await tester.pumpAndSettle();

3256
    // There should be no delete button tooltip
3257
    expect(findTooltipContainer(''), findsNothing);
3258 3259 3260 3261
    // There should be a chip tooltip, however.
    expect(findTooltipContainer('Chip Tooltip'), findsOneWidget);
  });

3262
  testWidgetsWithLeakTracking('Triggering delete button tooltip does not trigger Chip tooltip', (WidgetTester tester) async {
3263 3264
    final UniqueKey deleteButtonKey = UniqueKey();
    await tester.pumpWidget(
3265
      chipWithOptionalDeleteButton(
3266 3267 3268 3269 3270 3271
        deleteButtonKey: deleteButtonKey,
        deletable: true,
        chipTooltip: 'Chip Tooltip',
      ),
    );

3272
    // Hover over the delete icon of the chip
3273 3274 3275 3276 3277 3278 3279
    final Offset centerOfDeleteButton = tester.getCenter(find.byKey(deleteButtonKey));
    final TestGesture hoverGesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
    await hoverGesture.moveTo(centerOfDeleteButton);
    addTearDown(hoverGesture.removePointer);

    await tester.pump();

3280
    // Wait for some more time while hovering over the delete button
3281 3282
    await tester.pumpAndSettle();

3283 3284
    // There should not be a chip tooltip
    expect(findTooltipContainer('Chip Tooltip'), findsNothing);
3285
    // There should be a delete button tooltip
3286 3287 3288
    expect(findTooltipContainer('Delete'), findsOneWidget);
  });

3289
  testWidgetsWithLeakTracking('intrinsicHeight implementation meets constraints', (WidgetTester tester) async {
3290
    // Regression test for https://github.com/flutter/flutter/issues/49478.
3291
    await tester.pumpWidget(wrapForChip(
3292 3293 3294 3295 3296 3297 3298 3299
      child: const Chip(
        label: Text('text'),
        padding: EdgeInsets.symmetric(horizontal: 20),
      ),
    ));

    expect(tester.takeException(), isNull);
  });
3300

3301
  testWidgetsWithLeakTracking('Chip background color and shape are drawn on Ink', (WidgetTester tester) async {
3302 3303 3304 3305
    const Color backgroundColor = Color(0xff00ff00);
    const OutlinedBorder shape = ContinuousRectangleBorder();

    await tester.pumpWidget(wrapForChip(
3306
      useMaterial3: false,
3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322
      child: const RawChip(
        label: Text('text'),
        backgroundColor: backgroundColor,
        shape: shape,
      ),
    ));

    final Ink ink = tester.widget(find.descendant(
      of: find.byType(RawChip),
      matching: find.byType(Ink),
    ));
    final ShapeDecoration decoration = ink.decoration! as ShapeDecoration;
    expect(decoration.color, backgroundColor);
    expect(decoration.shape, shape);
  });

3323
  testWidgets('Chip highlight color is drawn on top of the backgroundColor', (WidgetTester tester) async {
3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
    final FocusNode focusNode = FocusNode(debugLabel: 'RawChip');
    tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
    const Color backgroundColor = Color(0xff00ff00);

    await tester.pumpWidget(wrapForChip(
      child: RawChip(
        label: const Text('text'),
        backgroundColor: backgroundColor,
        autofocus: true,
        focusNode: focusNode,
        onPressed: () {},
      ),
    ));

    await tester.pumpAndSettle();

    expect(focusNode.hasPrimaryFocus, isTrue);
    expect(
      find.byType(Material).last,
      paints
        // Background color is drawn first.
        ..rrect(color: backgroundColor)
        // Highlight color is drawn on top of the background color.
        ..rect(color: const Color(0x1f000000)),
    );
  });
3350

3351
  testWidgetsWithLeakTracking('RawChip.color resolves material states', (WidgetTester tester) async {
3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
    const Color disabledSelectedColor = Color(0xffffff00);
    const Color disabledColor = Color(0xff00ff00);
    const Color backgroundColor = Color(0xff0000ff);
    const Color selectedColor = Color(0xffff0000);
    Widget buildApp({ required bool enabled, required bool selected }) {
      return wrapForChip(
        useMaterial3: true,
        child: RawChip(
          isEnabled: enabled,
          selected: selected,
          color: MaterialStateProperty.resolveWith((Set<MaterialState> states) {
            if (states.contains(MaterialState.disabled) && states.contains(MaterialState.selected)) {
              return disabledSelectedColor;
            }
            if (states.contains(MaterialState.disabled)) {
              return disabledColor;
            }
            if (states.contains(MaterialState.selected)) {
              return selectedColor;
            }
            return backgroundColor;
          }),
          label: const Text('RawChip'),
        ),
      );
    }

    // Test enabled chip.
    await tester.pumpWidget(buildApp(enabled: true, selected: false));

    // Enabled chip should have the provided backgroundColor.
    expect(getMaterialBox(tester), paints..rrect(color: backgroundColor));

    // Test disabled chip.
    await tester.pumpWidget(buildApp(enabled: false, selected: false));
    await tester.pumpAndSettle();

    // Disabled chip should have the provided disabledColor.
    expect(getMaterialBox(tester), paints..rrect(color: disabledColor));

    // Test enabled & selected chip.
    await tester.pumpWidget(buildApp(enabled: true, selected: true));
    await tester.pumpAndSettle();

    // Enabled & selected chip should have the provided selectedColor.
    expect(getMaterialBox(tester), paints..rrect(color: selectedColor));

    // Test disabled & selected chip.
    await tester.pumpWidget(buildApp(enabled: false, selected: true));
    await tester.pumpAndSettle();

    // Disabled & selected chip should have the provided disabledSelectedColor.
    expect(getMaterialBox(tester), paints..rrect(color: disabledSelectedColor));
  });

3407
  testWidgetsWithLeakTracking('RawChip uses provided state color properties', (WidgetTester tester) async {
3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
    const Color disabledColor = Color(0xff00ff00);
    const Color backgroundColor = Color(0xff0000ff);
    const Color selectedColor = Color(0xffff0000);
    Widget buildApp({ required bool enabled, required bool selected }) {
      return wrapForChip(
        useMaterial3: true,
        child: RawChip(
          isEnabled: enabled,
          selected: selected,
          disabledColor: disabledColor,
          backgroundColor: backgroundColor,
          selectedColor: selectedColor,
          label: const Text('RawChip'),
        ),
      );
    }

    // Test enabled chip.
    await tester.pumpWidget(buildApp(enabled: true, selected: false));

    // Enabled chip should have the provided backgroundColor.
    expect(getMaterialBox(tester), paints..rrect(color: backgroundColor));

    // Test disabled chip.
    await tester.pumpWidget(buildApp(enabled: false, selected: false));
    await tester.pumpAndSettle();

    // Disabled chip should have the provided disabledColor.
    expect(getMaterialBox(tester), paints..rrect(color: disabledColor));

    // Test enabled & selected chip.
    await tester.pumpWidget(buildApp(enabled: true, selected: true));
    await tester.pumpAndSettle();

    // Enabled & selected chip should have the provided selectedColor.
    expect(getMaterialBox(tester), paints..rrect(color: selectedColor));
  });

3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482
  testWidgets('Delete button tap target area does not include label', (WidgetTester tester) async {
    bool calledDelete = false;
    await tester.pumpWidget(
      wrapForChip(
        child: Column(
          children: <Widget>[
            Chip(
              label: const Text('Chip'),
              onDeleted: () {
                calledDelete = true;
              },
            ),
          ],
        ),
      ),
    );

    // Tap on the delete button.
    await tester.tapAt(tester.getCenter(find.byType(Icon)));
    await tester.pump();
    expect(calledDelete, isTrue);
    calledDelete = false;

    final Offset labelCenter = tester.getCenter(find.text('Chip'));

    // Tap on the label.
    await tester.tapAt(labelCenter);
    await tester.pump();
    expect(calledDelete, isFalse);

    // Tap before end of the label.
    final Size labelSize = tester.getSize(find.text('Chip'));
    await tester.tapAt(Offset(labelCenter.dx + (labelSize.width / 2) - 1, labelCenter.dy));
    await tester.pump();
    expect(calledDelete, isFalse);

    // Tap after end of the label.
3483
    await tester.tapAt(Offset(labelCenter.dx + (labelSize.width / 2) + 0.01, labelCenter.dy));
3484 3485 3486 3487
    await tester.pump();
    expect(calledDelete, isTrue);
  });

3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566
  // This is a regression test for https://github.com/flutter/flutter/pull/133615.
  testWidgets('Material3 - Custom shape without provided side uses default side', (WidgetTester tester) async {
    final ThemeData theme = ThemeData(useMaterial3: true);
    await tester.pumpWidget(
      MaterialApp(
        theme: theme,
        home: const Material(
          child: Center(
            child: RawChip(
              // No side provided.
              shape: StadiumBorder(),
              label: Text('RawChip'),
            ),
          ),
        ),
      ),
    );

    // Chip should have the default side.
    expect(
      getMaterial(tester).shape,
      StadiumBorder(side: BorderSide(color: theme.colorScheme.outline)),
    );
  });

  testWidgets("Material3 - RawChip.shape's side is used when provided", (WidgetTester tester) async {
    Widget buildChip({ OutlinedBorder? shape, BorderSide? side }) {
      return MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: Material(
          child: Center(
            child: RawChip(
              shape: shape,
              side: side,
              label: const Text('RawChip'),
            ),
          ),
        ),
      );
    }

    // Test [RawChip.shape] with a side.
    await tester.pumpWidget(buildChip(
      shape: const RoundedRectangleBorder(
        side: BorderSide(color: Color(0xffff00ff)),
        borderRadius: BorderRadius.all(Radius.circular(7.0)),
      )),
    );

    // Chip should have the provided shape and the side from [RawChip.shape].
    expect(
      getMaterial(tester).shape,
      const RoundedRectangleBorder(
        side: BorderSide(color: Color(0xffff00ff)),
        borderRadius: BorderRadius.all(Radius.circular(7.0)),
      ),
    );

    // Test [RawChip.shape] with a side and [RawChip.side].
    await tester.pumpWidget(buildChip(
      shape: const RoundedRectangleBorder(
        side: BorderSide(color: Color(0xffff00ff)),
        borderRadius: BorderRadius.all(Radius.circular(7.0)),
      ),
      side: const BorderSide(color: Color(0xfffff000))),
    );
    await tester.pumpAndSettle();

    // Chip use shape from [RawChip.shape] and the side from [RawChip.side].
    // [RawChip.shape]'s side should be ignored.
    expect(
      getMaterial(tester).shape,
      const RoundedRectangleBorder(
        side: BorderSide(color: Color(0xfffff000)),
        borderRadius: BorderRadius.all(Radius.circular(7.0)),
      ),
    );
  });

3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
  group('Material 2', () {
    // These tests are only relevant for Material 2. Once Material 2
    // support is deprecated and the APIs are removed, these tests
    // can be deleted.

    testWidgets('M2 Chip defaults', (WidgetTester tester) async {
      late TextTheme textTheme;

      Widget buildFrame(Brightness brightness) {
        return MaterialApp(
          theme: ThemeData(brightness: brightness, useMaterial3: false),
          home: Scaffold(
            body: Center(
              child: Builder(
                builder: (BuildContext context) {
                  textTheme = Theme.of(context).textTheme;
                  return Chip(
                    avatar: const CircleAvatar(child: Text('A')),
                    label: const Text('Chip A'),
                    onDeleted: () { },
                  );
                },
              ),
            ),
          ),
        );
      }

      await tester.pumpWidget(buildFrame(Brightness.light));
      expect(getMaterialBox(tester), paints..rrect()..circle(color: const Color(0xff1976d2)));
      expect(tester.getSize(find.byType(Chip)), const Size(156.0, 48.0));
      expect(getMaterial(tester).color, null);
      expect(getMaterial(tester).elevation, 0);
      expect(getMaterial(tester).shape, const StadiumBorder());
      expect(getIconData(tester).color?.value, 0xffffffff);
      expect(getIconData(tester).opacity, null);
      expect(getIconData(tester).size, null);

      TextStyle labelStyle = getLabelStyle(tester, 'Chip A').style;
      expect(labelStyle.color?.value, 0xde000000);
      expect(labelStyle.fontFamily, textTheme.bodyLarge?.fontFamily);
      expect(labelStyle.fontFamilyFallback, textTheme.bodyLarge?.fontFamilyFallback);
      expect(labelStyle.fontFeatures, textTheme.bodyLarge?.fontFeatures);
      expect(labelStyle.fontSize, textTheme.bodyLarge?.fontSize);
      expect(labelStyle.fontStyle, textTheme.bodyLarge?.fontStyle);
      expect(labelStyle.fontWeight, textTheme.bodyLarge?.fontWeight);
      expect(labelStyle.height, textTheme.bodyLarge?.height);
      expect(labelStyle.inherit, textTheme.bodyLarge?.inherit);
      expect(labelStyle.leadingDistribution, textTheme.bodyLarge?.leadingDistribution);
      expect(labelStyle.letterSpacing, textTheme.bodyLarge?.letterSpacing);
      expect(labelStyle.overflow, textTheme.bodyLarge?.overflow);
      expect(labelStyle.textBaseline, textTheme.bodyLarge?.textBaseline);
      expect(labelStyle.wordSpacing, textTheme.bodyLarge?.wordSpacing);

      await tester.pumpWidget(buildFrame(Brightness.dark));
      await tester.pumpAndSettle(); // Theme transition animation
      expect(getMaterialBox(tester), paints..rrect(color: const Color(0x1fffffff)));
      expect(tester.getSize(find.byType(Chip)), const Size(156.0, 48.0));
      expect(getMaterial(tester).color, null);
      expect(getMaterial(tester).elevation, 0);
      expect(getMaterial(tester).shape, const StadiumBorder());
      expect(getIconData(tester).color?.value, 0xffffffff);
      expect(getIconData(tester).opacity, null);
      expect(getIconData(tester).size, null);

      labelStyle = getLabelStyle(tester, 'Chip A').style;
      expect(labelStyle.color?.value, 0xdeffffff);
      expect(labelStyle.fontFamily, textTheme.bodyLarge?.fontFamily);
      expect(labelStyle.fontFamilyFallback, textTheme.bodyLarge?.fontFamilyFallback);
      expect(labelStyle.fontFeatures, textTheme.bodyLarge?.fontFeatures);
      expect(labelStyle.fontSize, textTheme.bodyLarge?.fontSize);
      expect(labelStyle.fontStyle, textTheme.bodyLarge?.fontStyle);
      expect(labelStyle.fontWeight, textTheme.bodyLarge?.fontWeight);
      expect(labelStyle.height, textTheme.bodyLarge?.height);
      expect(labelStyle.inherit, textTheme.bodyLarge?.inherit);
      expect(labelStyle.leadingDistribution, textTheme.bodyLarge?.leadingDistribution);
      expect(labelStyle.letterSpacing, textTheme.bodyLarge?.letterSpacing);
      expect(labelStyle.overflow, textTheme.bodyLarge?.overflow);
      expect(labelStyle.textBaseline, textTheme.bodyLarge?.textBaseline);
      expect(labelStyle.wordSpacing, textTheme.bodyLarge?.wordSpacing);
    });

    testWidgets('Chip uses the right theme colors for the right components', (WidgetTester tester) async {
      final ThemeData themeData = ThemeData(
        platform: TargetPlatform.android,
        primarySwatch: Colors.blue,
        useMaterial3: false,
      );
      final ChipThemeData defaultChipTheme = ChipThemeData.fromDefaults(
        brightness: themeData.brightness,
        secondaryColor: Colors.blue,
        labelStyle: themeData.textTheme.bodyLarge!,
      );
      bool value = false;
      Widget buildApp({
        ChipThemeData? chipTheme,
        Widget? avatar,
        Widget? deleteIcon,
        bool isSelectable = true,
        bool isPressable = false,
        bool isDeletable = true,
        bool showCheckmark = true,
      }) {
        chipTheme ??= defaultChipTheme;
        return wrapForChip(
          child: Theme(
            data: themeData,
            child: ChipTheme(
              data: chipTheme,
              child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
                  showCheckmark: showCheckmark,
                  onDeleted: isDeletable ? () { } : null,
                  avatar: avatar,
                  deleteIcon: deleteIcon,
                  isEnabled: isSelectable || isPressable,
                  shape: chipTheme?.shape,
                  selected: isSelectable && value,
                  label: Text('$value'),
                  onSelected: isSelectable
                    ? (bool newValue) {
                        setState(() {
                          value = newValue;
                        });
                      }
                    : null,
                  onPressed: isPressable
                    ? () {
                        setState(() {
                          value = true;
                        });
                      }
                    : null,
                );
              }),
            ),
          ),
        );
      }

      await tester.pumpWidget(buildApp());

      RenderBox materialBox = getMaterialBox(tester);
      IconThemeData iconData = getIconData(tester);
      DefaultTextStyle labelStyle = getLabelStyle(tester, 'false');

      // Check default theme for enabled chip.
      expect(materialBox, paints..rrect(color: defaultChipTheme.backgroundColor));
      expect(iconData.color, equals(const Color(0xde000000)));
      expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));

      // Check default theme for disabled chip.
      await tester.pumpWidget(buildApp(isSelectable: false));
      await tester.pumpAndSettle();
      materialBox = getMaterialBox(tester);
      labelStyle = getLabelStyle(tester, 'false');
      expect(materialBox, paints..rrect(color: defaultChipTheme.disabledColor));
      expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));

      // Check default theme for enabled and selected chip.
      await tester.pumpWidget(buildApp());
      await tester.pumpAndSettle();
      await tester.tap(find.byType(RawChip));
      await tester.pumpAndSettle();
      materialBox = getMaterialBox(tester);
      expect(materialBox, paints..rrect(color: defaultChipTheme.selectedColor));

      // Check default theme for disabled and selected chip.
      await tester.pumpWidget(buildApp(isSelectable: false));
      await tester.pumpAndSettle();
      materialBox = getMaterialBox(tester);
      labelStyle = getLabelStyle(tester, 'true');
      expect(materialBox, paints..rrect(color: defaultChipTheme.disabledColor));
      expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));

      // Enable the chip again.
      await tester.pumpWidget(buildApp());
      await tester.pumpAndSettle();
      // Tap to unselect the chip.
      await tester.tap(find.byType(RawChip));
      await tester.pumpAndSettle();

      // Apply a custom theme.
      const Color customColor1 = Color(0xcafefeed);
      const Color customColor2 = Color(0xdeadbeef);
      const Color customColor3 = Color(0xbeefcafe);
      const Color customColor4 = Color(0xaddedabe);
      final ChipThemeData customTheme = defaultChipTheme.copyWith(
        brightness: Brightness.dark,
        backgroundColor: customColor1,
        disabledColor: customColor2,
        selectedColor: customColor3,
        deleteIconColor: customColor4,
      );
      await tester.pumpWidget(buildApp(chipTheme: customTheme));
      await tester.pumpAndSettle();
      materialBox = getMaterialBox(tester);
      iconData = getIconData(tester);
      labelStyle = getLabelStyle(tester, 'false');

      // Check custom theme for enabled chip.
      expect(materialBox, paints..rrect(color: customTheme.backgroundColor));
      expect(iconData.color, equals(customTheme.deleteIconColor));
      expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));

      // Check custom theme with disabled widget.
      await tester.pumpWidget(buildApp(
        chipTheme: customTheme,
        isSelectable: false,
      ));
      await tester.pumpAndSettle();
      materialBox = getMaterialBox(tester);
      labelStyle = getLabelStyle(tester, 'false');
      expect(materialBox, paints..rrect(color: customTheme.disabledColor));
      expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));

      // Check custom theme for enabled and selected chip.
      await tester.pumpWidget(buildApp(chipTheme: customTheme));
      await tester.pumpAndSettle();
      await tester.tap(find.byType(RawChip));
      await tester.pumpAndSettle();
      materialBox = getMaterialBox(tester);
      expect(materialBox, paints..rrect(color: customTheme.selectedColor));

      // Check custom theme for disabled and selected chip.
      await tester.pumpWidget(buildApp(
        chipTheme: customTheme,
        isSelectable: false,
      ));
      await tester.pumpAndSettle();
      materialBox = getMaterialBox(tester);
      labelStyle = getLabelStyle(tester, 'true');
      expect(materialBox, paints..rrect(color: customTheme.disabledColor));
      expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));
    });
  });
3803
}
3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821

class _MaterialStateOutlinedBorder extends StadiumBorder implements MaterialStateOutlinedBorder {
  const _MaterialStateOutlinedBorder(this.resolver);

  final MaterialPropertyResolver<OutlinedBorder?> resolver;

  @override
  OutlinedBorder? resolve(Set<MaterialState> states) => resolver(states);
}

class _MaterialStateBorderSide extends MaterialStateBorderSide {
  const _MaterialStateBorderSide(this.resolver);

  final MaterialPropertyResolver<BorderSide?> resolver;

  @override
  BorderSide? resolve(Set<MaterialState> states) => resolver(states);
}