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

5 6
import 'dart:ui' show window;

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

13
import '../rendering/mock_canvas.dart';
14
import '../widgets/semantics_tester.dart';
15 16
import 'feedback_tester.dart';

17 18 19
Finder findRenderChipElement() {
  return find.byElementPredicate((Element e) => '${e.runtimeType}' == '_RenderChipElement');
}
20

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

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

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

DefaultTextStyle getLabelStyle(WidgetTester tester) {
  return tester.widget(
51
    find.descendant(
52 53
      of: find.byType(RawChip),
      matching: find.byType(DefaultTextStyle),
54
    ).last,
55 56 57
  );
}

58 59 60 61 62 63 64 65
dynamic getRenderChip(WidgetTester tester) {
  if (!tester.any(findRenderChipElement())) {
    return null;
  }
  final Element element = tester.element(findRenderChipElement());
  return element.renderObject;
}

66 67 68 69
double getSelectProgress(WidgetTester tester) => getRenderChip(tester)?.checkmarkAnimation?.value as double;
double getAvatarDrawerProgress(WidgetTester tester) => getRenderChip(tester)?.avatarDrawerAnimation?.value as double;
double getDeleteDrawerProgress(WidgetTester tester) => getRenderChip(tester)?.deleteDrawerAnimation?.value as double;
double getEnableProgress(WidgetTester tester) => getRenderChip(tester)?.enableAnimation?.value as double;
70

71 72
/// Adds the basic requirements for a Chip.
Widget _wrapForChip({
73
  required Widget child,
74 75
  TextDirection textDirection = TextDirection.ltr,
  double textScaleFactor = 1.0,
76
  Brightness brightness = Brightness.light,
77
}) {
78
  return MaterialApp(
79
    theme: ThemeData(brightness: brightness),
80
    home: Directionality(
81
      textDirection: textDirection,
82 83 84
      child: MediaQuery(
        data: MediaQueryData.fromWindow(window).copyWith(textScaleFactor: textScaleFactor),
        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
Widget _selectedInputChip({ Color? checkmarkColor }) {
135 136 137 138 139 140 141 142
  return InputChip(
    label: const Text('InputChip'),
    selected: true,
    showCheckmark: true,
    checkmarkColor: checkmarkColor,
  );
}

143
Widget _selectedFilterChip({ Color? checkmarkColor }) {
144 145 146 147 148 149 150 151 152 153 154
  return FilterChip(
    label: const Text('InputChip'),
    selected: true,
    showCheckmark: true,
    checkmarkColor: checkmarkColor,
    onSelected: (bool _) { },
  );
}

Future<void> _pumpCheckmarkChip(
  WidgetTester tester, {
155 156
  required Widget chip,
  Color? themeColor,
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  Brightness brightness = Brightness.light,
}) async {
  await tester.pumpWidget(
    _wrapForChip(
      brightness: brightness,
      child: Builder(
        builder: (BuildContext context) {
          final ChipThemeData chipTheme = ChipTheme.of(context);
          return ChipTheme(
            data: themeColor == null ? chipTheme : chipTheme.copyWith(
              checkmarkColor: themeColor,
            ),
            child: chip,
          );
        },
172
      ),
173
    ),
174 175 176 177 178 179
  );
}

void _expectCheckmarkColor(Finder finder, Color color) {
  expect(
    finder,
180 181 182 183 184 185 186
    paints
      // The first path that is painted is the selection overlay. We do not care
      // how it is painted but it has to be added it to this pattern so that the
      // check mark can be checked next.
      ..path()
      // The second path that is painted is the check mark.
      ..path(color: color),
187 188 189
  );
}

190
Widget _chipWithOptionalDeleteButton({
191 192 193
  UniqueKey? deleteButtonKey,
  UniqueKey? labelKey,
  required bool deletable,
194
  TextDirection textDirection = TextDirection.ltr,
195
  bool hasDeleteButtonTooltip = true,
196 197 198 199 200 201 202 203 204
}){
  return _wrapForChip(
    textDirection: textDirection,
    child: Wrap(
      children: <Widget>[
        RawChip(
          onPressed: () {},
          onDeleted: deletable ? () {} : null,
          deleteIcon: Icon(Icons.close, key: deleteButtonKey),
205
          useDeleteButtonTooltip: hasDeleteButtonTooltip,
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
          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) {
        if (method != #drawCircle)
          return false;
229 230
        final Offset center = arguments[0] as Offset;
        final double radius = arguments[1] as double;
231 232 233 234 235 236 237 238 239 240 241 242 243
        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) {
        if (method != #drawCircle)
          return true;
244 245
        final Offset center = arguments[0] as Offset;
        final double radius = arguments[1] as double;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        if (offsetsAreClose(center, expectedCenter) && radiiAreClose(radius, expectedRadius))
          return true;
        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),
  );
}

263
void main() {
264
  testWidgets('Chip control test', (WidgetTester tester) async {
265
    final FeedbackTester feedback = FeedbackTester();
266
    final List<String> deletedChipLabels = <String>[];
267 268
    await tester.pumpWidget(
      _wrapForChip(
269
        child: Column(
270
          children: <Widget>[
271
            Chip(
272
              avatar: const CircleAvatar(child: Text('A')),
273 274 275 276 277 278
              label: const Text('Chip A'),
              onDeleted: () {
                deletedChipLabels.add('A');
              },
              deleteButtonTooltipMessage: 'Delete chip A',
            ),
279
            Chip(
280
              avatar: const CircleAvatar(child: Text('B')),
281 282 283 284 285 286 287 288
              label: const Text('Chip B'),
              onDeleted: () {
                deletedChipLabels.add('B');
              },
              deleteButtonTooltipMessage: 'Delete chip B',
            ),
          ],
        ),
289
      ),
290
    );
291

292 293 294
    expect(tester.widget(find.byTooltip('Delete chip A')), isNotNull);
    expect(tester.widget(find.byTooltip('Delete chip B')), isNotNull);

295 296
    expect(feedback.clickSoundCount, 0);

297 298 299
    expect(deletedChipLabels, isEmpty);
    await tester.tap(find.byTooltip('Delete chip A'));
    expect(deletedChipLabels, equals(<String>['A']));
300 301 302 303

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

304 305 306 307 308 309
    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);

310
    feedback.dispose();
311
  });
312

313
  testWidgets(
314 315 316 317 318 319 320 321 322 323
    '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(
        _wrapForChip(
          child: Center(
324
            child: SizedBox(
325 326 327 328 329
              width: 500.0,
              height: 500.0,
              child: Column(
                children: <Widget>[
                  Chip(
330
                    label: SizedBox(
331 332 333 334
                      key: labelKey,
                      width: labelWidth,
                      height: labelHeight,
                    ),
335
                  ),
336 337
                ],
              ),
338 339 340
            ),
          ),
        ),
341
      );
342

343 344 345 346 347
      final Size labelSize = tester.getSize(find.byKey(labelKey));
      expect(labelSize.width, labelWidth);
      expect(labelSize.height, labelHeight);
    },
  );
348

349
  testWidgets(
350 351 352 353 354 355
    'Chip constrains the size of the label widget when it exceeds the '
    'available space',
    (WidgetTester tester) async {
      await _testConstrainedLabel(tester);
    },
  );
356

357
  testWidgets(
358 359 360 361 362 363 364 365 366
    'Chip constrains the size of the label widget when it exceeds the '
    'available space and the avatar is present',
    (WidgetTester tester) async {
      await _testConstrainedLabel(
        tester,
        avatar: const CircleAvatar(child: Text('A')),
      );
    },
  );
367

368
  testWidgets(
369 370 371 372 373 374 375 376 377
    'Chip constrains the size of the label widget when it exceeds the '
    'available space and the delete icon is present',
    (WidgetTester tester) async {
      await _testConstrainedLabel(
        tester,
        onDeleted: () { },
      );
    },
  );
378

379
  testWidgets(
380 381 382 383 384 385 386 387 388 389
    '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 {
      await _testConstrainedLabel(
        tester,
        avatar: const CircleAvatar(child: Text('A')),
        onDeleted: () { },
      );
    },
  );
390

391
  testWidgets(
392 393 394 395
    '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
396
      Widget chipBuilder (String text, {Widget? avatar, VoidCallback? onDeleted}) {
397 398
        return MaterialApp(
          home: Scaffold(
399
            body: SizedBox(
400 401 402 403 404 405 406 407
              width: 150,
              child: Column(
                children: <Widget>[
                  Chip(
                    avatar: avatar,
                    label: Text(text),
                    onDeleted: onDeleted,
                  ),
408
                ],
409
              ),
410 411
            ),
          ),
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        );
      }

      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);
    },
  );
465

466
  testWidgets('Chip in row works ok', (WidgetTester tester) async {
467
    const TextStyle style = TextStyle(fontFamily: 'Ahem', fontSize: 10.0);
468
    await tester.pumpWidget(
469
      _wrapForChip(
470
        child: Row(
471
          children: const <Widget>[
472
            Chip(label: Text('Test'), labelStyle: style),
473
          ],
474 475 476
        ),
      ),
    );
477
    expect(tester.getSize(find.byType(Text)), const Size(40.0, 10.0));
478
    expect(tester.getSize(find.byType(Chip)), const Size(64.0, 48.0));
479
    await tester.pumpWidget(
480
      _wrapForChip(
481
        child: Row(
482
          children: const <Widget>[
483
            Flexible(child: Chip(label: Text('Test'), labelStyle: style)),
484
          ],
485 486 487 488
        ),
      ),
    );
    expect(tester.getSize(find.byType(Text)), const Size(40.0, 10.0));
489
    expect(tester.getSize(find.byType(Chip)), const Size(64.0, 48.0));
490
    await tester.pumpWidget(
491
      _wrapForChip(
492
        child: Row(
493
          children: const <Widget>[
494
            Expanded(child: Chip(label: Text('Test'), labelStyle: style)),
495
          ],
496 497 498 499
        ),
      ),
    );
    expect(tester.getSize(find.byType(Text)), const Size(40.0, 10.0));
500
    expect(tester.getSize(find.byType(Chip)), const Size(800.0, 48.0));
501
  });
502

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
  testWidgets('Chip responds to materialTapTargetSize', (WidgetTester tester) async {
      await tester.pumpWidget(
        _wrapForChip(
          child: Column(
            children: const <Widget>[
              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));
    },
  );

525
  testWidgets('Chip elements are ordered horizontally for locale', (WidgetTester tester) async {
526 527
    final UniqueKey iconKey = UniqueKey();
    final Widget test = Overlay(
528
      initialEntries: <OverlayEntry>[
529
        OverlayEntry(
530
          builder: (BuildContext context) {
531 532 533
            return Material(
              child: Chip(
                deleteIcon: Icon(Icons.delete, key: iconKey),
534
                onDeleted: () { },
535
                label: const Text('ABC'),
536 537 538 539 540 541 542 543
              ),
            );
          },
        ),
      ],
    );

    await tester.pumpWidget(
544 545 546
      _wrapForChip(
        child: test,
        textDirection: TextDirection.rtl,
547 548
      ),
    );
549 550
    await tester.pumpAndSettle(const Duration(milliseconds: 500));
    expect(tester.getCenter(find.text('ABC')).dx, greaterThan(tester.getCenter(find.byKey(iconKey)).dx));
551
    await tester.pumpWidget(
552 553 554
      _wrapForChip(
        textDirection: TextDirection.ltr,
        child: test,
555 556
      ),
    );
557 558
    await tester.pumpAndSettle(const Duration(milliseconds: 500));
    expect(tester.getCenter(find.text('ABC')).dx, lessThan(tester.getCenter(find.byKey(iconKey)).dx));
559 560
  });

561 562
  testWidgets('Chip responds to textScaleFactor', (WidgetTester tester) async {
    await tester.pumpWidget(
563
      _wrapForChip(
564
        child: Column(
565
          children: const <Widget>[
566 567 568
            Chip(
              avatar: CircleAvatar(child: Text('A')),
              label: Text('Chip A'),
569
            ),
570 571 572
            Chip(
              avatar: CircleAvatar(child: Text('B')),
              label: Text('Chip B'),
573 574
            ),
          ],
575 576 577 578 579 580 581 582
        ),
      ),
    );

    // TODO(gspencer): Update this test when the font metric bug is fixed to remove the anyOfs.
    // https://github.com/flutter/flutter/issues/12357
    expect(
      tester.getSize(find.text('Chip A')),
583
      anyOf(const Size(84.0, 14.0), const Size(83.0, 14.0)),
584 585 586
    );
    expect(
      tester.getSize(find.text('Chip B')),
587
      anyOf(const Size(84.0, 14.0), const Size(83.0, 14.0)),
588
    );
589 590
    expect(tester.getSize(find.byType(Chip).first), anyOf(const Size(132.0, 48.0), const Size(131.0, 48.0)));
    expect(tester.getSize(find.byType(Chip).last), anyOf(const Size(132.0, 48.0), const Size(131.0, 48.0)));
591 592

    await tester.pumpWidget(
593 594
      _wrapForChip(
        textScaleFactor: 3.0,
595
        child: Column(
596
          children: const <Widget>[
597 598 599
            Chip(
              avatar: CircleAvatar(child: Text('A')),
              label: Text('Chip A'),
600
            ),
601 602 603
            Chip(
              avatar: CircleAvatar(child: Text('B')),
              label: Text('Chip B'),
604 605
            ),
          ],
606 607 608 609 610 611
        ),
      ),
    );

    // TODO(gspencer): Update this test when the font metric bug is fixed to remove the anyOfs.
    // https://github.com/flutter/flutter/issues/12357
612 613
    expect(tester.getSize(find.text('Chip A')), anyOf(const Size(252.0, 42.0), const Size(251.0, 42.0)));
    expect(tester.getSize(find.text('Chip B')), anyOf(const Size(252.0, 42.0), const Size(251.0, 42.0)));
614
    expect(tester.getSize(find.byType(Chip).first).width, anyOf(310.0, 311.0));
615
    expect(tester.getSize(find.byType(Chip).first).height, equals(50.0));
616
    expect(tester.getSize(find.byType(Chip).last).width, anyOf(310.0, 311.0));
617
    expect(tester.getSize(find.byType(Chip).last).height, equals(50.0));
618 619 620

    // Check that individual text scales are taken into account.
    await tester.pumpWidget(
621
      _wrapForChip(
622
        child: Column(
623
          children: const <Widget>[
624 625 626
            Chip(
              avatar: CircleAvatar(child: Text('A')),
              label: Text('Chip A', textScaleFactor: 3.0),
627
            ),
628 629 630
            Chip(
              avatar: CircleAvatar(child: Text('B')),
              label: Text('Chip B'),
631 632
            ),
          ],
633 634 635 636 637 638
        ),
      ),
    );

    // TODO(gspencer): Update this test when the font metric bug is fixed to remove the anyOfs.
    // https://github.com/flutter/flutter/issues/12357
639 640
    expect(tester.getSize(find.text('Chip A')), anyOf(const Size(252.0, 42.0), const Size(251.0, 42.0)));
    expect(tester.getSize(find.text('Chip B')), anyOf(const Size(84.0, 14.0), const Size(83.0, 14.0)));
641 642
    expect(tester.getSize(find.byType(Chip).first).width, anyOf(318.0, 319.0));
    expect(tester.getSize(find.byType(Chip).first).height, equals(50.0));
643
    expect(tester.getSize(find.byType(Chip).last), anyOf(const Size(132.0, 48.0), const Size(131.0, 48.0)));
644
  });
645 646

  testWidgets('Labels can be non-text widgets', (WidgetTester tester) async {
647 648
    final Key keyA = GlobalKey();
    final Key keyB = GlobalKey();
649
    await tester.pumpWidget(
650
      _wrapForChip(
651
        child: Column(
652
          children: <Widget>[
653
            Chip(
654
              avatar: const CircleAvatar(child: Text('A')),
655
              label: Text('Chip A', key: keyA),
656
            ),
657
            Chip(
658
              avatar: const CircleAvatar(child: Text('B')),
659
              label: SizedBox(key: keyB, width: 10.0, height: 10.0),
660 661
            ),
          ],
662 663 664 665 666 667 668 669
        ),
      ),
    );

    // TODO(gspencer): Update this test when the font metric bug is fixed to remove the anyOfs.
    // https://github.com/flutter/flutter/issues/12357
    expect(
      tester.getSize(find.byKey(keyA)),
670
      anyOf(const Size(84.0, 14.0), const Size(83.0, 14.0)),
671 672 673 674
    );
    expect(tester.getSize(find.byKey(keyB)), const Size(10.0, 10.0));
    expect(
      tester.getSize(find.byType(Chip).first),
675
      anyOf(const Size(132.0, 48.0), const Size(131.0, 48.0)),
676
    );
677
    expect(tester.getSize(find.byType(Chip).last), const Size(58.0, 48.0));
678
  });
679

680
  testWidgets('Avatars can be non-circle avatar widgets', (WidgetTester tester) async {
681
    final Key keyA = GlobalKey();
682
    await tester.pumpWidget(
683
      _wrapForChip(
684
        child: Column(
685
          children: <Widget>[
686
            Chip(
687
              avatar: SizedBox(key: keyA, width: 20.0, height: 20.0),
688 689 690
              label: const Text('Chip A'),
            ),
          ],
691 692 693 694 695 696 697 698
        ),
      ),
    );

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

  testWidgets('Delete icons can be non-icon widgets', (WidgetTester tester) async {
699
    final Key keyA = GlobalKey();
700
    await tester.pumpWidget(
701
      _wrapForChip(
702
        child: Column(
703
          children: <Widget>[
704
            Chip(
705
              deleteIcon: SizedBox(key: keyA, width: 20.0, height: 20.0),
706
              label: const Text('Chip A'),
707
              onDeleted: () { },
708 709
            ),
          ],
710 711 712 713 714 715 716
        ),
      ),
    );

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

717
  testWidgets('Chip padding - LTR', (WidgetTester tester) async {
718 719
    final GlobalKey keyA = GlobalKey();
    final GlobalKey keyB = GlobalKey();
720
    await tester.pumpWidget(
721 722
      _wrapForChip(
        textDirection: TextDirection.ltr,
723
        child: Overlay(
724
          initialEntries: <OverlayEntry>[
725
            OverlayEntry(
726
              builder: (BuildContext context) {
727 728 729 730
                return Material(
                  child: Center(
                    child: Chip(
                      avatar: Placeholder(key: keyA),
731
                      label: SizedBox(
732 733 734
                        key: keyB,
                        width: 40.0,
                        height: 40.0,
735
                      ),
736
                      onDeleted: () { },
737
                    ),
738 739 740 741 742
                  ),
                );
              },
            ),
          ],
743 744 745
        ),
      ),
    );
746 747
    expect(tester.getTopLeft(find.byKey(keyA)), const Offset(332.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyA)), const Offset(372.0, 320.0));
748 749
    expect(tester.getTopLeft(find.byKey(keyB)), const Offset(380.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyB)), const Offset(420.0, 320.0));
750 751
    expect(tester.getTopLeft(find.byType(Icon)), const Offset(439.0, 291.0));
    expect(tester.getBottomRight(find.byType(Icon)), const Offset(457.0, 309.0));
752 753 754
  });

  testWidgets('Chip padding - RTL', (WidgetTester tester) async {
755 756
    final GlobalKey keyA = GlobalKey();
    final GlobalKey keyB = GlobalKey();
757
    await tester.pumpWidget(
758 759
      _wrapForChip(
        textDirection: TextDirection.rtl,
760
        child: Overlay(
761
          initialEntries: <OverlayEntry>[
762
            OverlayEntry(
763
              builder: (BuildContext context) {
764 765 766 767
                return Material(
                  child: Center(
                    child: Chip(
                      avatar: Placeholder(key: keyA),
768
                      label: SizedBox(
769 770 771
                        key: keyB,
                        width: 40.0,
                        height: 40.0,
772
                      ),
773
                      onDeleted: () { },
774
                    ),
775 776 777 778 779
                  ),
                );
              },
            ),
          ],
780 781 782
        ),
      ),
    );
783

784 785
    expect(tester.getTopLeft(find.byKey(keyA)), const Offset(428.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyA)), const Offset(468.0, 320.0));
786 787
    expect(tester.getTopLeft(find.byKey(keyB)), const Offset(380.0, 280.0));
    expect(tester.getBottomRight(find.byKey(keyB)), const Offset(420.0, 320.0));
788 789 790 791 792
    expect(tester.getTopLeft(find.byType(Icon)), const Offset(343.0, 291.0));
    expect(tester.getBottomRight(find.byType(Icon)), const Offset(361.0, 309.0));
  });

  testWidgets('Avatar drawer works as expected on RawChip', (WidgetTester tester) async {
793
    final GlobalKey labelKey = GlobalKey();
794
    Future<void> pushChip({ Widget? avatar }) async {
795
      return tester.pumpWidget(
796
        _wrapForChip(
797
          child: Wrap(
798
            children: <Widget>[
799
              RawChip(
800
                avatar: avatar,
801
                label: Text('Chip', key: labelKey),
802 803 804
                shape: const StadiumBorder(),
              ),
            ],
805 806 807 808 809 810 811
          ),
        ),
      );
    }

    // No avatar
    await pushChip();
812
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
813
    final GlobalKey avatarKey = GlobalKey();
814 815 816

    // Add an avatar
    await pushChip(
817
      avatar: Container(
818 819 820 821 822 823 824
        key: avatarKey,
        color: const Color(0xff000000),
        width: 40.0,
        height: 40.0,
      ),
    );
    // Avatar drawer should start out closed.
825
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
826
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
827 828
    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)));
829 830 831

    await tester.pump(const Duration(milliseconds: 20));
    // Avatar drawer should start expanding.
832
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(81.2, epsilon: 0.1));
833
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
834 835
    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));
836 837

    await tester.pump(const Duration(milliseconds: 20));
838
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(86.7, epsilon: 0.1));
839
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
840 841
    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));
842 843

    await tester.pump(const Duration(milliseconds: 20));
844
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(94.7, epsilon: 0.1));
845
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
846 847
    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));
848 849

    await tester.pump(const Duration(milliseconds: 20));
850
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(99.5, epsilon: 0.1));
851
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
852 853
    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));
854 855 856 857

    // Wait for being done with animation, and make sure it didn't change
    // height.
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
858
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
859
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
860 861
    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)));
862 863 864 865

    // Remove the avatar again
    await pushChip();
    // Avatar drawer should start out open.
866
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
867
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
868 869
    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)));
870 871 872

    await tester.pump(const Duration(milliseconds: 20));
    // Avatar drawer should start contracting.
873
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(102.9, epsilon: 0.1));
874
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
875 876
    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));
877 878

    await tester.pump(const Duration(milliseconds: 20));
879
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(98.0, epsilon: 0.1));
880
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
881 882
    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));
883 884

    await tester.pump(const Duration(milliseconds: 20));
885
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(84.1, epsilon: 0.1));
886
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
887 888
    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));
889 890

    await tester.pump(const Duration(milliseconds: 20));
891
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(80.0, epsilon: 0.1));
892
    expect(tester.getSize(find.byKey(avatarKey)), equals(const Size(24.0, 24.0)));
893 894
    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));
895 896 897 898

    // 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));
899 900
    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)));
901
    expect(find.byKey(avatarKey), findsNothing);
902
  });
903 904

  testWidgets('Delete button drawer works as expected on RawChip', (WidgetTester tester) async {
905 906
    const Key labelKey = Key('label');
    const Key deleteButtonKey = Key('delete');
907
    bool wasDeleted = false;
908
    Future<void> pushChip({ bool deletable = false }) async {
909
      return tester.pumpWidget(
910
        _wrapForChip(
911
          child: Wrap(
912
            children: <Widget>[
913 914
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
915
                  onDeleted: deletable
916 917 918 919 920 921
                    ? () {
                        setState(() {
                          wasDeleted = true;
                        });
                      }
                    : null,
922 923
                  deleteIcon: Container(width: 40.0, height: 40.0, color: Colors.blue, key: deleteButtonKey),
                  label: const Text('Chip', key: labelKey),
924 925 926 927
                  shape: const StadiumBorder(),
                );
              }),
            ],
928 929 930 931 932 933 934
          ),
        ),
      );
    }

    // No delete button
    await pushChip();
935
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
936 937 938 939

    // Add a delete button
    await pushChip(deletable: true);
    // Delete button drawer should start out closed.
940
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
941
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
942 943
    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)));
944 945 946

    await tester.pump(const Duration(milliseconds: 20));
    // Delete button drawer should start expanding.
947
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(81.2, epsilon: 0.1));
948
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
949
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(53.2, epsilon: 0.1));
950
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
951 952

    await tester.pump(const Duration(milliseconds: 20));
953
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(86.7, epsilon: 0.1));
954
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
955
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(58.7, epsilon: 0.1));
956 957

    await tester.pump(const Duration(milliseconds: 20));
958
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(94.7, epsilon: 0.1));
959
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
960
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(66.7, epsilon: 0.1));
961 962

    await tester.pump(const Duration(milliseconds: 20));
963
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(99.5, epsilon: 0.1));
964
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
965
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(71.5, epsilon: 0.1));
966 967 968 969

    // Wait for being done with animation, and make sure it didn't change
    // height.
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
970
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
971
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
972 973
    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)));
974 975 976 977 978 979 980 981 982 983 984

    // 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.
985
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
986
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
987 988
    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)));
989 990 991

    await tester.pump(const Duration(milliseconds: 20));
    // Delete button drawer should start contracting.
992
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(103.8, epsilon: 0.1));
993
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
994
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(75.8, epsilon: 0.1));
995
    expect(tester.getTopLeft(find.byKey(labelKey)), equals(const Offset(12.0, 17.0)));
996 997

    await tester.pump(const Duration(milliseconds: 20));
998
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(102.9, epsilon: 0.1));
999
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1000
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(74.9, epsilon: 0.1));
1001 1002

    await tester.pump(const Duration(milliseconds: 20));
1003
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(101.0, epsilon: 0.1));
1004
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1005
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(73.0, epsilon: 0.1));
1006 1007

    await tester.pump(const Duration(milliseconds: 20));
1008
    expect(tester.getSize(find.byType(RawChip)).width, moreOrLessEquals(97.5, epsilon: 0.1));
1009
    expect(tester.getSize(find.byKey(deleteButtonKey)), equals(const Size(24.0, 24.0)));
1010
    expect(tester.getTopLeft(find.byKey(deleteButtonKey)).dx, moreOrLessEquals(69.5, epsilon: 0.1));
1011 1012 1013 1014

    // 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));
1015 1016
    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)));
1017
    expect(find.byKey(deleteButtonKey), findsNothing);
1018
  });
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

  testWidgets('Chip creates centered, unique ripple when label is tapped', (WidgetTester tester) async {
    // Creates a chip with a delete button.
    final UniqueKey labelKey = UniqueKey();
    final UniqueKey deleteButtonKey = UniqueKey();

    await tester.pumpWidget(
      _chipWithOptionalDeleteButton(
        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 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(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();
1072
  });
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

  testWidgets('Delete button creates non-centered, unique ripple when tapped', (WidgetTester tester) async {
    // Creates a chip with a delete button.
    final UniqueKey labelKey = UniqueKey();
    final UniqueKey deleteButtonKey = UniqueKey();

    await tester.pumpWidget(
      _chipWithOptionalDeleteButton(
        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));

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

    // There should be one unique ink ripple.
    expect(box, ripplePattern(const Offset(3.0, 3.0), 3.5));
    expect(box, uniqueRipplePattern(const Offset(3.0, 3.0), 3.5));

    // 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), 10.5));
    expect(box, uniqueRipplePattern(const Offset(5.0, 5.0), 10.5));

    // 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();
1130
  });
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

  testWidgets('RTL delete button responds to tap on the left of the chip', (WidgetTester tester) async {
    // Creates an RTL chip with a delete button.
    final UniqueKey labelKey = UniqueKey();
    final UniqueKey deleteButtonKey = UniqueKey();

    await tester.pumpWidget(
      _chipWithOptionalDeleteButton(
        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.
    final Offset topLeftOfInkWell = tester.getTopLeft(find.byType(InkWell));
    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();
1160
  });
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213

  testWidgets('Chip without delete button creates correct ripple', (WidgetTester tester) async {
    // Creates a chip with a delete button.
    final UniqueKey labelKey = UniqueKey();

    await tester.pumpWidget(
      _chipWithOptionalDeleteButton(
        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();
1214
  });
1215 1216 1217

  testWidgets('Selection with avatar works as expected on RawChip', (WidgetTester tester) async {
    bool selected = false;
1218
    final UniqueKey labelKey = UniqueKey();
1219
    Future<void> pushChip({ Widget? avatar, bool selectable = false }) async {
1220
      return tester.pumpWidget(
1221
        _wrapForChip(
1222
          child: Wrap(
1223
            children: <Widget>[
1224 1225
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
1226 1227
                  avatar: avatar,
                  onSelected: selectable != null
1228 1229 1230 1231 1232 1233
                    ? (bool value) {
                        setState(() {
                          selected = value;
                        });
                      }
                    : null,
1234
                  selected: selected,
1235
                  label: Text('Chip', key: labelKey),
1236 1237 1238 1239 1240 1241 1242
                  shape: const StadiumBorder(),
                  showCheckmark: true,
                  tapEnabled: true,
                  isEnabled: true,
                );
              }),
            ],
1243 1244 1245 1246 1247 1248
          ),
        ),
      );
    }

    // With avatar, but not selectable.
1249
    final UniqueKey avatarKey = UniqueKey();
1250
    await pushChip(
1251
      avatar: SizedBox(width: 40.0, height: 40.0, key: avatarKey),
1252
    );
1253
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(104.0, 48.0)));
1254 1255 1256

    // Turn on selection.
    await pushChip(
1257
      avatar: SizedBox(width: 40.0, height: 40.0, key: avatarKey),
1258 1259 1260 1261 1262 1263 1264
      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));
1265
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
1266 1267
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
1268
    expect(getSelectProgress(tester), moreOrLessEquals(0.002, epsilon: 0.01));
1269 1270 1271
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 50));
1272
    expect(getSelectProgress(tester), moreOrLessEquals(0.54, epsilon: 0.01));
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    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));
1283
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
1284 1285
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 20));
1286
    expect(getSelectProgress(tester), moreOrLessEquals(0.875, epsilon: 0.01));
1287 1288 1289
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 20));
1290
    expect(getSelectProgress(tester), moreOrLessEquals(0.13, epsilon: 0.01));
1291 1292 1293 1294 1295 1296
    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));
1297
  });
1298 1299 1300

  testWidgets('Selection without avatar works as expected on RawChip', (WidgetTester tester) async {
    bool selected = false;
1301
    final UniqueKey labelKey = UniqueKey();
1302
    Future<void> pushChip({ bool selectable = false }) async {
1303
      return tester.pumpWidget(
1304
        _wrapForChip(
1305
          child: Wrap(
1306
            children: <Widget>[
1307 1308
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
1309
                  onSelected: selectable != null
1310 1311 1312 1313 1314 1315
                    ? (bool value) {
                        setState(() {
                          selected = value;
                        });
                      }
                    : null,
1316
                  selected: selected,
1317
                  label: Text('Chip', key: labelKey),
1318 1319 1320 1321 1322 1323 1324
                  shape: const StadiumBorder(),
                  showCheckmark: true,
                  tapEnabled: true,
                  isEnabled: true,
                );
              }),
            ],
1325 1326 1327 1328 1329 1330 1331
          ),
        ),
      );
    }

    // Without avatar, but not selectable.
    await pushChip();
1332
    expect(tester.getSize(find.byType(RawChip)), equals(const Size(80.0, 48.0)));
1333 1334 1335 1336 1337 1338 1339 1340

    // 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));
1341
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
1342 1343
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
1344 1345
    expect(getSelectProgress(tester), moreOrLessEquals(0.002, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.459, epsilon: 0.01));
1346 1347
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 50));
1348 1349
    expect(getSelectProgress(tester), moreOrLessEquals(0.54, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.92, epsilon: 0.01));
1350 1351 1352 1353 1354 1355 1356 1357 1358
    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));
1359
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
1360 1361
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 20));
1362 1363
    expect(getSelectProgress(tester), moreOrLessEquals(0.875, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.96, epsilon: 0.01));
1364 1365
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 20));
1366 1367
    expect(getSelectProgress(tester), moreOrLessEquals(0.13, epsilon: 0.01));
    expect(getAvatarDrawerProgress(tester), moreOrLessEquals(0.75, epsilon: 0.01));
1368 1369 1370 1371 1372
    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));
1373
  });
1374 1375 1376

  testWidgets('Activation works as expected on RawChip', (WidgetTester tester) async {
    bool selected = false;
1377
    final UniqueKey labelKey = UniqueKey();
1378
    Future<void> pushChip({ Widget? avatar, bool selectable = false }) async {
1379
      return tester.pumpWidget(
1380
        _wrapForChip(
1381
          child: Wrap(
1382
            children: <Widget>[
1383 1384
              StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
                return RawChip(
1385 1386
                  avatar: avatar,
                  onSelected: selectable != null
1387 1388 1389 1390 1391 1392
                    ? (bool value) {
                        setState(() {
                          selected = value;
                        });
                      }
                    : null,
1393
                  selected: selected,
1394
                  label: Text('Chip', key: labelKey),
1395 1396 1397 1398 1399 1400 1401
                  shape: const StadiumBorder(),
                  showCheckmark: false,
                  tapEnabled: true,
                  isEnabled: true,
                );
              }),
            ],
1402 1403 1404 1405 1406
          ),
        ),
      );
    }

1407
    final UniqueKey avatarKey = UniqueKey();
1408
    await pushChip(
1409
      avatar: SizedBox(width: 40.0, height: 40.0, key: avatarKey),
1410 1411 1412 1413 1414 1415
      selectable: true,
    );
    await tester.pumpAndSettle();

    await tester.tap(find.byKey(labelKey));
    expect(selected, equals(true));
1416
    expect(SchedulerBinding.instance!.transientCallbackCount, equals(2));
1417 1418
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
1419
    expect(getSelectProgress(tester), moreOrLessEquals(0.002, epsilon: 0.01));
1420 1421 1422
    expect(getAvatarDrawerProgress(tester), equals(1.0));
    expect(getDeleteDrawerProgress(tester), equals(0.0));
    await tester.pump(const Duration(milliseconds: 50));
1423
    expect(getSelectProgress(tester), moreOrLessEquals(0.54, epsilon: 0.01));
1424 1425 1426 1427 1428 1429 1430
    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();
1431
  });
1432 1433

  testWidgets('Chip uses ThemeData chip theme if present', (WidgetTester tester) async {
1434
    final ThemeData theme = ThemeData(
1435 1436 1437 1438 1439 1440 1441 1442
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );
    final ChipThemeData chipTheme = theme.chipTheme;

    Widget buildChip(ChipThemeData data) {
      return _wrapForChip(
        textDirection: TextDirection.ltr,
1443
        child: Theme(
1444 1445
          data: theme,
          child: const InputChip(
1446
            label: Text('Label'),
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
          ),
        ),
      );
    }

    await tester.pumpWidget(buildChip(chipTheme));

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

1461
    expect(materialBox, paints..path(color: chipTheme.disabledColor));
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
  testWidgets('Chip merges ChipThemeData label style with the provided label style', (WidgetTester tester) async {
    // The font family should be preserved even if the chip overrides some label style properties
    final ThemeData theme = ThemeData(
      fontFamily: 'MyFont'
    );

    Widget buildChip() {
      return _wrapForChip(
        textDirection: TextDirection.ltr,
        child: Theme(
          data: theme,
          child: const Chip(
            label: Text('Label'),
            labelStyle: TextStyle(fontWeight: FontWeight.w200),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildChip());

    final TextStyle labelStyle = getLabelStyle(tester).style;
    expect(labelStyle.fontFamily, 'MyFont');
    expect(labelStyle.fontWeight, FontWeight.w200);
  });

1490
  testWidgets('Chip size is configurable by ThemeData.materialTapTargetSize', (WidgetTester tester) async {
1491
    final Key key1 = UniqueKey();
1492 1493
    await tester.pumpWidget(
      _wrapForChip(
1494 1495 1496 1497
        child: Theme(
          data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.padded),
          child: Center(
            child: RawChip(
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
              key: key1,
              label: const Text('test'),
            ),
          ),
        ),
      ),
    );

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

1508
    final Key key2 = UniqueKey();
1509 1510
    await tester.pumpWidget(
      _wrapForChip(
1511 1512 1513 1514
        child: Theme(
          data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
          child: Center(
            child: RawChip(
1515 1516 1517 1518 1519 1520 1521 1522 1523
              key: key2,
              label: const Text('test'),
            ),
          ),
        ),
      ),
    );

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

1526
  testWidgets('Chip uses the right theme colors for the right components', (WidgetTester tester) async {
1527
    final ThemeData themeData = ThemeData(
1528 1529 1530
      platform: TargetPlatform.android,
      primarySwatch: Colors.blue,
    );
1531
    final ChipThemeData defaultChipTheme = themeData.chipTheme;
1532 1533
    bool value = false;
    Widget buildApp({
1534 1535 1536
      ChipThemeData? chipTheme,
      Widget? avatar,
      Widget? deleteIcon,
1537 1538 1539 1540
      bool isSelectable = true,
      bool isPressable = false,
      bool isDeletable = true,
      bool showCheckmark = true,
1541
    }) {
1542
      chipTheme ??= defaultChipTheme;
1543
      return _wrapForChip(
1544
        child: Theme(
1545
          data: themeData,
1546
          child: ChipTheme(
1547
            data: chipTheme,
1548 1549
            child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
              return RawChip(
1550
                showCheckmark: showCheckmark,
1551
                onDeleted: isDeletable ? () { } : null,
1552 1553 1554 1555
                tapEnabled: true,
                avatar: avatar,
                deleteIcon: deleteIcon,
                isEnabled: isSelectable || isPressable,
1556
                shape: chipTheme?.shape,
1557
                selected: isSelectable && value,
1558
                label: Text('$value'),
1559
                onSelected: isSelectable
1560 1561 1562 1563 1564 1565
                  ? (bool newValue) {
                      setState(() {
                        value = newValue;
                      });
                    }
                  : null,
1566
                onPressed: isPressable
1567 1568 1569 1570 1571 1572
                  ? () {
                      setState(() {
                        value = true;
                      });
                    }
                  : null,
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
              );
            }),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildApp());

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

    // Check default theme for enabled widget.
1587
    expect(materialBox, paints..path(color: defaultChipTheme.backgroundColor));
1588 1589 1590 1591 1592
    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);
1593
    expect(materialBox, paints..path(color: defaultChipTheme.selectedColor));
1594 1595 1596 1597 1598 1599 1600 1601
    await tester.tap(find.byType(RawChip));
    await tester.pumpAndSettle();

    // Check default theme with disabled widget.
    await tester.pumpWidget(buildApp(isSelectable: false, isPressable: false, isDeletable: true));
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
    labelStyle = getLabelStyle(tester);
1602
    expect(materialBox, paints..path(color: defaultChipTheme.disabledColor));
1603 1604 1605
    expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));

    // Apply a custom theme.
1606 1607 1608 1609
    const Color customColor1 = Color(0xcafefeed);
    const Color customColor2 = Color(0xdeadbeef);
    const Color customColor3 = Color(0xbeefcafe);
    const Color customColor4 = Color(0xaddedabe);
1610
    final ChipThemeData customTheme = defaultChipTheme.copyWith(
1611 1612 1613 1614 1615 1616
      brightness: Brightness.dark,
      backgroundColor: customColor1,
      disabledColor: customColor2,
      selectedColor: customColor3,
      deleteIconColor: customColor4,
    );
1617
    await tester.pumpWidget(buildApp(chipTheme: customTheme));
1618 1619 1620 1621 1622 1623
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
    iconData = getIconData(tester);
    labelStyle = getLabelStyle(tester);

    // Check custom theme for enabled widget.
1624
    expect(materialBox, paints..path(color: customTheme.backgroundColor));
1625 1626 1627 1628 1629
    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);
1630
    expect(materialBox, paints..path(color: customTheme.selectedColor));
1631 1632 1633 1634 1635
    await tester.tap(find.byType(RawChip));
    await tester.pumpAndSettle();

    // Check custom theme with disabled widget.
    await tester.pumpWidget(buildApp(
1636
      chipTheme: customTheme,
1637 1638 1639 1640 1641 1642 1643
      isSelectable: false,
      isPressable: false,
      isDeletable: true,
    ));
    await tester.pumpAndSettle();
    materialBox = getMaterialBox(tester);
    labelStyle = getLabelStyle(tester);
1644
    expect(materialBox, paints..path(color: customTheme.disabledColor));
1645 1646
    expect(labelStyle.style.color, equals(Colors.black.withAlpha(0xde)));
  });
1647 1648 1649

  group('Chip semantics', () {
    testWidgets('label only', (WidgetTester tester) async {
1650
      final SemanticsTester semanticsTester = SemanticsTester(tester);
1651

1652 1653
      await tester.pumpWidget(const MaterialApp(
        home: Material(
1654 1655
          child: RawChip(
            label: Text('test'),
1656 1657 1658 1659 1660
          ),
        ),
      ));

      expect(semanticsTester, hasSemantics(
1661
          TestSemantics.root(
1662
            children: <TestSemantics>[
1663
              TestSemantics(
1664 1665
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
1666
                  TestSemantics(
1667
                    children: <TestSemantics>[
1668
                      TestSemantics(
1669 1670 1671 1672 1673
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
1674 1675 1676 1677
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                            ],
1678 1679
                          ),
                        ],
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));
      semanticsTester.dispose();
    });

1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
    testWidgets('delete', (WidgetTester tester) async {
      final SemanticsTester semanticsTester = SemanticsTester(tester);

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

      expect(semanticsTester, hasSemantics(
          TestSemantics.root(
            children: <TestSemantics>[
              TestSemantics(
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
                  TestSemantics(
                    children: <TestSemantics>[
                      TestSemantics(
1711
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
1712 1713
                        children: <TestSemantics>[
                          TestSemantics(
1714
                            label: 'test',
1715
                            textDirection: TextDirection.ltr,
1716 1717 1718 1719
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                            ],
1720 1721 1722 1723 1724 1725 1726 1727 1728
                            children: <TestSemantics>[
                              TestSemantics(
                                label: 'Delete',
                                actions: <SemanticsAction>[SemanticsAction.tap],
                                textDirection: TextDirection.ltr,
                                flags: <SemanticsFlag>[
                                  SemanticsFlag.isButton,
                                ],
                              ),
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
                            ],
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));
      semanticsTester.dispose();
    });

1742
    testWidgets('with onPressed', (WidgetTester tester) async {
1743
      final SemanticsTester semanticsTester = SemanticsTester(tester);
1744

1745 1746 1747
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
1748
            label: const Text('test'),
1749
            onPressed: () { },
1750 1751 1752 1753 1754
          ),
        ),
      ));

      expect(semanticsTester, hasSemantics(
1755
          TestSemantics.root(
1756
            children: <TestSemantics>[
1757
              TestSemantics(
1758 1759
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
1760
                  TestSemantics(
1761
                    children: <TestSemantics> [
1762
                      TestSemantics(
1763 1764 1765 1766 1767 1768 1769
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
1770
                              SemanticsFlag.isButton,
1771 1772 1773 1774 1775
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                          ),
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));

      semanticsTester.dispose();
    });


    testWidgets('with onSelected', (WidgetTester tester) async {
1790
      final SemanticsTester semanticsTester = SemanticsTester(tester);
1791 1792
      bool selected = false;

1793 1794 1795
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
            isEnabled: true,
            label: const Text('test'),
            selected: selected,
            onSelected: (bool value) {
              selected = value;
            },
          ),
        ),
      ));

      expect(semanticsTester, hasSemantics(
1807
          TestSemantics.root(
1808
            children: <TestSemantics>[
1809
              TestSemantics(
1810 1811
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
1812
                  TestSemantics(
1813
                    children: <TestSemantics>[
1814
                      TestSemantics(
1815 1816 1817 1818 1819 1820 1821
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
1822
                              SemanticsFlag.isButton,
1823 1824 1825 1826 1827
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                          ),
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));

      await tester.tap(find.byType(RawChip));
1838 1839 1840
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
            isEnabled: true,
            label: const Text('test'),
            selected: selected,
            onSelected: (bool value) {
              selected = value;
            },
          ),
        ),
      ));

      expect(selected, true);
      expect(semanticsTester, hasSemantics(
1853
          TestSemantics.root(
1854
            children: <TestSemantics>[
1855
              TestSemantics(
1856 1857
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
1858
                  TestSemantics(
1859
                    children: <TestSemantics>[
1860
                      TestSemantics(
1861 1862 1863 1864 1865 1866 1867
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
1868
                              SemanticsFlag.isButton,
1869 1870 1871 1872 1873 1874
                              SemanticsFlag.isEnabled,
                              SemanticsFlag.isFocusable,
                              SemanticsFlag.isSelected,
                            ],
                            actions: <SemanticsAction>[SemanticsAction.tap],
                          ),
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));

      semanticsTester.dispose();
    });

    testWidgets('disabled', (WidgetTester tester) async {
1888
      final SemanticsTester semanticsTester = SemanticsTester(tester);
1889

1890 1891 1892
      await tester.pumpWidget(MaterialApp(
        home: Material(
          child: RawChip(
1893
            isEnabled: false,
1894
            onPressed: () { },
1895 1896 1897 1898 1899 1900
            label: const Text('test'),
          ),
        ),
      ));

      expect(semanticsTester, hasSemantics(
1901
          TestSemantics.root(
1902
            children: <TestSemantics>[
1903
              TestSemantics(
1904 1905
                textDirection: TextDirection.ltr,
                children: <TestSemantics>[
1906
                  TestSemantics(
1907
                    children: <TestSemantics>[
1908
                      TestSemantics(
1909 1910 1911 1912 1913
                        flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
                        children: <TestSemantics>[
                          TestSemantics(
                            label: 'test',
                            textDirection: TextDirection.ltr,
1914 1915 1916 1917
                            flags: <SemanticsFlag>[
                              SemanticsFlag.hasEnabledState,
                              SemanticsFlag.isButton,
                            ],
1918 1919 1920
                            actions: <SemanticsAction>[],
                          ),
                        ],
1921 1922 1923
                      ),
                    ],
                  ),
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));

      semanticsTester.dispose();
    });

    testWidgets('tapEnabled explicitly false', (WidgetTester tester) async {
      final SemanticsTester semanticsTester = SemanticsTester(tester);

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

      expect(semanticsTester, hasSemantics(
          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>[],
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));

      semanticsTester.dispose();
    });

    testWidgets('enabled when tapEnabled and canTap', (WidgetTester tester) async {
      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(
            isEnabled: true,
            tapEnabled: true,
            onPressed: () {},
            label: const Text('test'),
          ),
        ),
      ));

      expect(semanticsTester, hasSemantics(
          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],
                          ),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));

      semanticsTester.dispose();
    });

    testWidgets('disabled when tapEnabled but not canTap', (WidgetTester tester) async {
      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(
            isEnabled: true,
            tapEnabled: true,
            label: Text('test'),
          ),
        ),
      ));

      expect(semanticsTester, hasSemantics(
          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,
                            ],
                          ),
                        ],
                      ),
                    ],
                  ),
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
                ],
              ),
            ],
          ), ignoreTransform: true, ignoreId: true, ignoreRect: true));

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

  testWidgets('can be tapped outside of chip delete icon', (WidgetTester tester) async {
    bool deleted = false;
    await tester.pumpWidget(
      _wrapForChip(
2071
        child: Row(
2072
          children: <Widget>[
2073
            Chip(
2074
              materialTapTargetSize: MaterialTapTargetSize.padded,
2075
              shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
2076
              avatar: const CircleAvatar(child: Text('A')),
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
              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);
  });

jslavitz's avatar
jslavitz committed
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
  testWidgets('Chips can be tapped', (WidgetTester tester) async {
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: ChoiceChip(
            selected: false,
            label: Text('choice chip'),
          ),
        ),
      ),
    );

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

    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: RawChip(
            selected: false,
            label: Text('raw chip'),
          ),
        ),
      ),
    );

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

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: ActionChip(
2126
            onPressed: () { },
jslavitz's avatar
jslavitz committed
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
            label: const Text('action chip'),
          ),
        ),
      ),
    );

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

    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: FilterChip(
2140
            onSelected: (bool valueChanged) { },
jslavitz's avatar
jslavitz committed
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
            selected: false,
            label: const Text('filter chip'),
          ),
        ),
      ),
    );

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

    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: InputChip(
            selected: false,
            label: Text('input chip'),
          ),
        ),
      ),
    );

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

2166
  testWidgets('Chip elevation and shadow color work correctly', (WidgetTester tester) async {
2167 2168 2169 2170 2171 2172 2173
    final ThemeData theme = ThemeData(
      platform: TargetPlatform.android,
      primarySwatch: Colors.red,
    );

    final ChipThemeData chipTheme = theme.chipTheme;

2174
    InputChip inputChip = const InputChip(label: Text('Label'));
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186

    Widget buildChip(ChipThemeData data) {
      return _wrapForChip(
        textDirection: TextDirection.ltr,
        child: Theme(
          data: theme,
          child: inputChip,
        ),
      );
    }

    await tester.pumpWidget(buildChip(chipTheme));
2187 2188
    Material material = getMaterial(tester);
    expect(material.elevation, 0.0);
2189
    expect(material.shadowColor, Colors.black);
2190

2191 2192 2193
    inputChip = const InputChip(
      label: Text('Label'),
      elevation: 4.0,
2194 2195
      shadowColor: Colors.green,
      selectedShadowColor: Colors.blue,
2196
    );
2197 2198 2199

    await tester.pumpWidget(buildChip(chipTheme));
    await tester.pumpAndSettle();
2200 2201
    material = getMaterial(tester);
    expect(material.elevation, 4.0);
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
    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);
2215 2216
  });

2217 2218 2219 2220
  testWidgets('can be tapped outside of chip body', (WidgetTester tester) async {
    bool pressed = false;
    await tester.pumpWidget(
      _wrapForChip(
2221
        child: Row(
2222
          children: <Widget>[
2223
            InputChip(
2224
              materialTapTargetSize: MaterialTapTargetSize.padded,
2225
              shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
2226
              avatar: const CircleAvatar(child: Text('A')),
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
              label: const Text('Chip A'),
              onPressed: () {
                pressed = true;
              },
            ),
          ],
        ),
      ),
    );

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

  testWidgets('is hitTestable', (WidgetTester tester) async {
    await tester.pumpWidget(
      _wrapForChip(
2245
        child: InputChip(
2246
          shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
2247
          avatar: const CircleAvatar(child: Text('A')),
2248
          label: const Text('Chip A'),
2249
          onPressed: () { },
2250 2251 2252 2253 2254 2255
        ),
      ),
    );

    expect(find.byType(InputChip).hitTestable(at: Alignment.center), findsOneWidget);
  });
2256 2257 2258 2259 2260 2261 2262 2263

  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);
  }

  testWidgets('Chip clipBehavior properly passes through to the Material', (WidgetTester tester) async {
2264 2265 2266
    const Text label = Text('label');
    await tester.pumpWidget(_wrapForChip(child: const Chip(label: label)));
    checkChipMaterialClipBehavior(tester, Clip.none);
2267

2268 2269
    await tester.pumpWidget(_wrapForChip(child: const Chip(label: label, clipBehavior: Clip.antiAlias)));
    checkChipMaterialClipBehavior(tester, Clip.antiAlias);
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
  });

  testWidgets('ChoiceChip clipBehavior properly passes through to the Material', (WidgetTester tester) async {
    const Text label = Text('label');
    await tester.pumpWidget(_wrapForChip(child: const ChoiceChip(label: label, selected: false)));
    checkChipMaterialClipBehavior(tester, Clip.none);

    await tester.pumpWidget(_wrapForChip(child: const ChoiceChip(label: label, selected: false, clipBehavior: Clip.antiAlias)));
    checkChipMaterialClipBehavior(tester, Clip.antiAlias);
  });

  testWidgets('FilterChip clipBehavior properly passes through to the Material', (WidgetTester tester) async {
    const Text label = Text('label');
2283
    await tester.pumpWidget(_wrapForChip(child: FilterChip(label: label, onSelected: (bool b) { })));
2284 2285
    checkChipMaterialClipBehavior(tester, Clip.none);

2286
    await tester.pumpWidget(_wrapForChip(child: FilterChip(label: label, onSelected: (bool b) { }, clipBehavior: Clip.antiAlias)));
2287 2288 2289 2290 2291
    checkChipMaterialClipBehavior(tester, Clip.antiAlias);
  });

  testWidgets('ActionChip clipBehavior properly passes through to the Material', (WidgetTester tester) async {
    const Text label = Text('label');
2292
    await tester.pumpWidget(_wrapForChip(child: ActionChip(label: label, onPressed: () { })));
2293 2294
    checkChipMaterialClipBehavior(tester, Clip.none);

2295
    await tester.pumpWidget(_wrapForChip(child: ActionChip(label: label, clipBehavior: Clip.antiAlias, onPressed: () { })));
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
    checkChipMaterialClipBehavior(tester, Clip.antiAlias);
  });

  testWidgets('InputChip clipBehavior properly passes through to the Material', (WidgetTester tester) async {
    const Text label = Text('label');
    await tester.pumpWidget(_wrapForChip(child: const InputChip(label: label)));
    checkChipMaterialClipBehavior(tester, Clip.none);

    await tester.pumpWidget(_wrapForChip(child: const InputChip(label: label, clipBehavior: Clip.antiAlias)));
    checkChipMaterialClipBehavior(tester, Clip.antiAlias);
  });
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319

  testWidgets('selected chip and avatar draw darkened layer within avatar circle', (WidgetTester tester) async {
    await tester.pumpWidget(_wrapForChip(child: const FilterChip(
      avatar: CircleAvatar(child: Text('t')),
      label: Text('test'),
      selected: true,
      onSelected: null,
    )));
    final RenderBox rawChip = tester.firstRenderObject<RenderBox>(
      find.descendant(
        of: find.byType(RawChip),
        matching: find.byWidgetPredicate((Widget widget) {
          return widget.runtimeType.toString() == '_ChipRenderWidget';
2320
        }),
2321
      ),
2322 2323 2324 2325 2326 2327 2328
    );
    const Color selectScrimColor = Color(0x60191919);
    expect(rawChip, paints..path(color: selectScrimColor, includes: <Offset>[
      const Offset(10, 10),
    ], excludes: <Offset>[
      const Offset(4, 4),
    ]));
2329
  });
2330 2331 2332 2333 2334 2335 2336

  testWidgets('Chips should use InkWell instead of InkResponse.', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/28646
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: ActionChip(
2337
            onPressed: () { },
2338 2339 2340 2341 2342 2343 2344
            label: const Text('action chip'),
          ),
        ),
      ),
    );
    expect(find.byType(InkWell), findsOneWidget);
  });
2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 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

  testWidgets('Chip uses stateful color for text color in different states', (WidgetTester tester) async {
    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) {
      if (states.contains(MaterialState.disabled))
        return disabledColor;

      if (states.contains(MaterialState.pressed))
        return pressedColor;

      if (states.contains(MaterialState.hovered))
        return hoverColor;

      if (states.contains(MaterialState.focused))
        return focusedColor;

      if (states.contains(MaterialState.selected))
        return selectedColor;

      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() {
2391
      return tester.renderObject<RenderParagraph>(find.text('Chip')).text.style!.color!;
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
    }

    // 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);

    // Teardown.
    await gesture.removePointer();
  });
2431

2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
  testWidgets('Chip uses stateful border side color in different states', (WidgetTester tester) async {
    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;

      if (states.contains(MaterialState.disabled))
        sideColor = disabledColor;

      else if (states.contains(MaterialState.pressed))
        sideColor = pressedColor;

      else if (states.contains(MaterialState.hovered))
        sideColor = hoverColor;

      else if (states.contains(MaterialState.focused))
        sideColor = focusedColor;

      else if (states.contains(MaterialState.selected))
        sideColor = selectedColor;

      return BorderSide(color: sideColor, width: 1);
    }

    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,
              side: _MaterialStateBorderSide(getBorderSide),
            ),
          ),
        ),
      );
    }

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

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
    expect(find.byType(RawChip), paints..rrect(color: selectedColor));

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
    expect(find.byType(RawChip), paints..rrect(color: 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(find.byType(RawChip), paints..rrect(color: hoverColor));

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

    // Disabled.
    await tester.pumpWidget(chipWidget(enabled: false));
    await tester.pumpAndSettle();
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
    expect(find.byType(RawChip), paints..rrect(color: disabledColor));

    // Teardown.
    await gesture.removePointer();
  });

  testWidgets('Chip uses stateful border side color from resolveWith', (WidgetTester tester) async {
    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;

      if (states.contains(MaterialState.disabled))
        sideColor = disabledColor;

      else if (states.contains(MaterialState.pressed))
        sideColor = pressedColor;

      else if (states.contains(MaterialState.hovered))
        sideColor = hoverColor;

      else if (states.contains(MaterialState.focused))
        sideColor = focusedColor;

      else if (states.contains(MaterialState.selected))
        sideColor = selectedColor;

      return BorderSide(color: sideColor, width: 1);
    }

    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,
              side: MaterialStateBorderSide.resolveWith(getBorderSide),
            ),
          ),
        ),
      );
    }

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

    // Selected.
    await tester.pumpWidget(chipWidget(selected: true));
    expect(find.byType(RawChip), paints..rrect(color: selectedColor));

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
    expect(find.byType(RawChip), paints..rrect(color: 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(find.byType(RawChip), paints..rrect(color: hoverColor));

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

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

    // Teardown.
    await gesture.removePointer();
  });

  testWidgets('Chip uses stateful nullable border side color from resolveWith', (WidgetTester tester) async {
    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;

      if (states.contains(MaterialState.disabled))
        sideColor = disabledColor;

      else if (states.contains(MaterialState.pressed))
        sideColor = pressedColor;

      else if (states.contains(MaterialState.hovered))
        sideColor = hoverColor;

      else if (states.contains(MaterialState.focused))
        sideColor = focusedColor;

      else if (states.contains(MaterialState.selected))
        return null;

      return BorderSide(color: sideColor, width: 1);
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
        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());
    expect(find.byType(RawChip), paints..rrect(color: defaultColor));

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

    // Focused.
    final FocusNode chipFocusNode = focusNode.children.first;
    chipFocusNode.requestFocus();
    await tester.pumpAndSettle();
    expect(find.byType(RawChip), paints..rrect(color: 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(find.byType(RawChip), paints..rrect(color: hoverColor));

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

    // Disabled.
    await tester.pumpWidget(chipWidget(enabled: false));
    await tester.pumpAndSettle();
2691 2692 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 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821
    expect(find.byType(RawChip), paints..rrect(color: disabledColor));

    // Teardown.
    await gesture.removePointer();
  });

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

      if (states.contains(MaterialState.disabled))
        return const BeveledRectangleBorder();

      else if (states.contains(MaterialState.pressed))
        return const CircleBorder();

      else if (states.contains(MaterialState.hovered))
        return const ContinuousRectangleBorder();

      else if (states.contains(MaterialState.focused))
        return const RoundedRectangleBorder();

      else if (states.contains(MaterialState.selected))
        return const BeveledRectangleBorder();

      return null;
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
        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>());

    // Teardown.
    await gesture.removePointer();
  });

  testWidgets('Chip defers to theme, if shape and side resolves to null', (WidgetTester tester) async {
    const OutlinedBorder themeShape = StadiumBorder();
    const OutlinedBorder selectedShape = RoundedRectangleBorder();
    const BorderSide themeBorderSide = BorderSide(color: Color(0x00000001), width: 1);
    const BorderSide selectedBorderSide = BorderSide(color: Color(0x00000002), width: 1);

    OutlinedBorder? getShape(Set<MaterialState> states) {
      if (states.contains(MaterialState.selected))
        return selectedShape;
      return null;
    }

    BorderSide? getBorderSide(Set<MaterialState> states) {
      if (states.contains(MaterialState.selected))
        return selectedBorderSide;
      return null;
    }

    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
        theme: ThemeData(
          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>());
    expect(find.byType(RawChip), paints..rrect(color: themeBorderSide.color));

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

2822 2823 2824 2825 2826 2827 2828
  testWidgets('loses focus when disabled', (WidgetTester tester) async {
    final FocusNode focusNode = FocusNode(debugLabel: 'InputChip');
    await tester.pumpWidget(
      _wrapForChip(
        child: InputChip(
          focusNode: focusNode,
          autofocus: true,
2829
          shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
          avatar: const CircleAvatar(child: Text('A')),
          label: const Text('Chip A'),
          onPressed: () { },
        ),
      ),
    );
    await tester.pump();
    expect(focusNode.hasPrimaryFocus, isTrue);

    await tester.pumpWidget(
      _wrapForChip(
        child: InputChip(
          focusNode: focusNode,
          autofocus: true,
2844
          shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
          avatar: const CircleAvatar(child: Text('A')),
          label: const Text('Chip A'),
          onPressed: null,
        ),
      ),
    );
    await tester.pump();
    expect(focusNode.hasPrimaryFocus, isFalse);
  });

  testWidgets('cannot be traversed to when disabled', (WidgetTester tester) async {
    final FocusNode focusNode1 = FocusNode(debugLabel: 'InputChip 1');
    final FocusNode focusNode2 = FocusNode(debugLabel: 'InputChip 2');
    await tester.pumpWidget(
      _wrapForChip(
        child: Column(
          children: <Widget>[
            InputChip(
              focusNode: focusNode1,
              autofocus: true,
              label: const Text('Chip A'),
              onPressed: () { },
            ),
            InputChip(
              focusNode: focusNode2,
              autofocus: true,
              label: const Text('Chip B'),
              onPressed: null,
            ),
          ],
        ),
      ),
    );
    await tester.pump();
    expect(focusNode1.hasPrimaryFocus, isTrue);
    expect(focusNode2.hasPrimaryFocus, isFalse);

    expect(focusNode1.nextFocus(), isTrue);

    await tester.pump();
    expect(focusNode1.hasPrimaryFocus, isTrue);
    expect(focusNode2.hasPrimaryFocus, isFalse);
  });
2888

2889 2890 2891 2892 2893 2894
  testWidgets('Chip responds to density changes.', (WidgetTester tester) async {
    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 {
2895
      return tester.pumpWidget(
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993
        MaterialApp(
          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)));
  });

2994 2995 2996 2997 2998 2999 3000 3001 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 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120
  testWidgets('Input chip check mark color is determined by platform brightness when light', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedInputChip(),
      brightness: Brightness.light,
    );

    _expectCheckmarkColor(
      find.byType(InputChip),
      Colors.black.withAlpha(0xde),
    );
  });

  testWidgets('Filter chip check mark color is determined by platform brightness when light', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedFilterChip(),
      brightness: Brightness.light,
    );

    _expectCheckmarkColor(
      find.byType(FilterChip),
      Colors.black.withAlpha(0xde),
    );
  });

  testWidgets('Input chip check mark color is determined by platform brightness when dark', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedInputChip(),
      brightness: Brightness.dark,
    );

    _expectCheckmarkColor(
      find.byType(InputChip),
      Colors.white.withAlpha(0xde),
    );
  });

  testWidgets('Filter chip check mark color is determined by platform brightness when dark', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedFilterChip(),
      brightness: Brightness.dark,
    );

    _expectCheckmarkColor(
      find.byType(FilterChip),
      Colors.white.withAlpha(0xde),
    );
  });

  testWidgets('Input chip check mark color can be set by the chip theme', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedInputChip(),
      themeColor: const Color(0xff00ff00),
    );

    _expectCheckmarkColor(
      find.byType(InputChip),
      const Color(0xff00ff00),
    );
  });

  testWidgets('Filter chip check mark color can be set by the chip theme', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedFilterChip(),
      themeColor: const Color(0xff00ff00),
    );

    _expectCheckmarkColor(
      find.byType(FilterChip),
      const Color(0xff00ff00),
    );
  });

  testWidgets('Input chip check mark color can be set by the chip constructor', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedInputChip(checkmarkColor: const Color(0xff00ff00)),
    );

    _expectCheckmarkColor(
      find.byType(InputChip),
      const Color(0xff00ff00),
    );
  });

  testWidgets('Filter chip check mark color can be set by the chip constructor', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedFilterChip(checkmarkColor: const Color(0xff00ff00)),
    );

    _expectCheckmarkColor(
      find.byType(FilterChip),
      const Color(0xff00ff00),
    );
  });

  testWidgets('Input chip check mark color is set by chip constructor even when a theme color is specified', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedInputChip(checkmarkColor: const Color(0xffff0000)),
      themeColor: const Color(0xff00ff00),
    );

    _expectCheckmarkColor(
      find.byType(InputChip),
      const Color(0xffff0000),
    );
  });

  testWidgets('Filter chip check mark color is set by chip constructor even when a theme color is specified', (WidgetTester tester) async {
    await _pumpCheckmarkChip(
      tester,
      chip: _selectedFilterChip(checkmarkColor: const Color(0xffff0000)),
      themeColor: const Color(0xff00ff00),
    );

    _expectCheckmarkColor(
      find.byType(FilterChip),
      const Color(0xffff0000),
    );
  });
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

  testWidgets('Chip delete button tooltip can be disabled', (WidgetTester tester) async {
    await tester.pumpWidget(
      _chipWithOptionalDeleteButton(
        deletable: true,
        hasDeleteButtonTooltip: false,
      )
    );

    // Tap at the delete icon of the chip, which is at the right
    // side of the chip
    final Offset topRightOfInkwell = tester.getTopLeft(find.byType(InkWell));
    final Offset tapLocationOfDeleteButton = topRightOfInkwell + const Offset(8, 8);
    final TestGesture tapGesture = await tester.startGesture(tapLocationOfDeleteButton);

    await tester.pump();

    // Wait for some more time while pressing and holding the delete button
    await tester.pumpAndSettle();

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

    await tapGesture.up();
  });
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157

  testWidgets('intrinsicHeight implementation meets constraints', (WidgetTester tester) async {
    // Regression text for https://github.com/flutter/flutter/issues/49478.
    await tester.pumpWidget(_wrapForChip(
      child: const Chip(
        label: Text('text'),
        padding: EdgeInsets.symmetric(horizontal: 20),
      ),
    ));

    expect(tester.takeException(), isNull);
  });
3158
}
3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176

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);
}