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

5
import 'package:flutter/gestures.dart';
6
import 'package:flutter/material.dart';
7 8 9 10 11 12 13 14 15 16 17 18 19 20
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

import '../rendering/mock_canvas.dart';

RenderBox getMaterialBox(WidgetTester tester) {
  return tester.firstRenderObject<RenderBox>(
    find.descendant(
      of: find.byType(RawChip),
      matching: find.byType(CustomPaint),
    ),
  );
}

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

30 31
DefaultTextStyle getLabelStyle(WidgetTester tester) {
  return tester.widget(
32 33 34 35
    find.descendant(
      of: find.byType(RawChip),
      matching: find.byType(DefaultTextStyle),
    ).last,
36 37 38 39
  );
}

void main() {
40 41 42 43
  test('ChipThemeData copyWith, ==, hashCode basics', () {
    expect(const ChipThemeData(), const ChipThemeData().copyWith());
    expect(const ChipThemeData().hashCode, const ChipThemeData().copyWith().hashCode);
  });
44

45 46 47 48 49 50 51 52
  test('ChipThemeData defaults', () {
    const ChipThemeData themeData = ChipThemeData();
    expect(themeData.backgroundColor, null);
    expect(themeData.deleteIconColor, null);
    expect(themeData.disabledColor, null);
    expect(themeData.selectedColor, null);
    expect(themeData.secondarySelectedColor, null);
    expect(themeData.shadowColor, null);
53
    expect(themeData.surfaceTintColor, null);
54 55 56 57 58 59 60 61 62 63 64 65
    expect(themeData.selectedShadowColor, null);
    expect(themeData.showCheckmark, null);
    expect(themeData.checkmarkColor, null);
    expect(themeData.labelPadding, null);
    expect(themeData.padding, null);
    expect(themeData.side, null);
    expect(themeData.shape, null);
    expect(themeData.labelStyle, null);
    expect(themeData.secondaryLabelStyle, null);
    expect(themeData.brightness, null);
    expect(themeData.elevation, null);
    expect(themeData.pressElevation, null);
66 67
  });

68 69 70
  testWidgets('Default ChipThemeData debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const ChipThemeData().debugFillProperties(builder);
71

72 73 74 75 76 77
    final List<String> description = builder.properties
      .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
      .map((DiagnosticsNode node) => node.toString())
      .toList();

    expect(description, <String>[]);
78 79
  });

80 81 82 83 84 85 86 87 88
  testWidgets('ChipThemeData implements debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const ChipThemeData(
      backgroundColor: Color(0xfffffff0),
      deleteIconColor: Color(0xfffffff1),
      disabledColor: Color(0xfffffff2),
      selectedColor: Color(0xfffffff3),
      secondarySelectedColor: Color(0xfffffff4),
      shadowColor: Color(0xfffffff5),
89
      surfaceTintColor: Color(0xfffffff8),
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      selectedShadowColor: Color(0xfffffff6),
      showCheckmark: true,
      checkmarkColor: Color(0xfffffff7),
      labelPadding: EdgeInsets.all(1),
      padding: EdgeInsets.all(2),
      side: BorderSide(width: 10),
      shape: RoundedRectangleBorder(),
      labelStyle: TextStyle(fontSize: 10),
      secondaryLabelStyle: TextStyle(fontSize: 20),
      brightness: Brightness.dark,
      elevation: 5,
      pressElevation: 6,
    ).debugFillProperties(builder);

    final List<String> description = builder.properties
        .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
        .map((DiagnosticsNode node) => node.toString())
        .toList();

    expect(description, <String>[
      'backgroundColor: Color(0xfffffff0)',
      'deleteIconColor: Color(0xfffffff1)',
      'disabledColor: Color(0xfffffff2)',
      'selectedColor: Color(0xfffffff3)',
      'secondarySelectedColor: Color(0xfffffff4)',
      'shadowColor: Color(0xfffffff5)',
116
      'surfaceTintColor: Color(0xfffffff8)',
117
      'selectedShadowColor: Color(0xfffffff6)',
118
      'showCheckmark: true',
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
      'checkMarkColor: Color(0xfffffff7)',
      'labelPadding: EdgeInsets.all(1.0)',
      'padding: EdgeInsets.all(2.0)',
      'side: BorderSide(Color(0xff000000), 10.0, BorderStyle.solid)',
      'shape: RoundedRectangleBorder(BorderSide(Color(0xff000000), 0.0, BorderStyle.none), BorderRadius.zero)',
      'labelStyle: TextStyle(inherit: true, size: 10.0)',
      'secondaryLabelStyle: TextStyle(inherit: true, size: 20.0)',
      'brightness: dark',
      'elevation: 5.0',
      'pressElevation: 6.0',
    ]);
  });

  testWidgets('Chip uses ThemeData chip theme', (WidgetTester tester) async {
    const ChipThemeData chipTheme = ChipThemeData(
      backgroundColor: Color(0xff112233),
      elevation: 4,
      padding: EdgeInsets.all(50),
      labelPadding: EdgeInsets.all(25),
      shape: RoundedRectangleBorder(),
      labelStyle: TextStyle(fontSize: 32),
140 141
    );

142 143 144 145 146
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData.light().copyWith(
          chipTheme: chipTheme,
        ),
147
        home: Directionality(
148
          textDirection: TextDirection.ltr,
nt4f04uNd's avatar
nt4f04uNd committed
149 150
          child: Material(
            child: Center(
151 152 153
              child: RawChip(
                label: const SizedBox(width: 100, height: 100),
                onSelected: (bool newValue) { },
154 155 156 157
              ),
            ),
          ),
        ),
158 159
      ),
    );
160 161

    final RenderBox materialBox = getMaterialBox(tester);
162
    expect(materialBox, paints..path(color: chipTheme.backgroundColor));
163 164 165 166
    expect(getMaterial(tester).elevation, chipTheme.elevation);
    expect(tester.getSize(find.byType(RawChip)), const Size(250, 250)); // label + padding + labelPadding
    expect(getMaterial(tester).shape, chipTheme.shape);
    expect(getLabelStyle(tester).style.fontSize, 32);
167 168
  });

169 170 171 172 173 174 175 176
  testWidgets('Chip uses ChipTheme', (WidgetTester tester) async {
    const ChipThemeData chipTheme = ChipThemeData(
      backgroundColor: Color(0xff112233),
      elevation: 4,
      padding: EdgeInsets.all(50),
      labelPadding: EdgeInsets.all(25),
      labelStyle: TextStyle(fontSize: 32),
      shape: RoundedRectangleBorder(),
177
    );
178 179 180 181 182 183 184 185

    const ChipThemeData shadowedChipTheme = ChipThemeData(
      backgroundColor: Color(0xff332211),
      elevation: 3,
      padding: EdgeInsets.all(5),
      labelPadding: EdgeInsets.all(10),
      labelStyle: TextStyle(fontSize: 64),
      shape: CircleBorder(),
186
    );
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData.light().copyWith(
          chipTheme: shadowedChipTheme,
        ),
        home: ChipTheme(
          data: chipTheme,
          child: Builder(
            builder: (BuildContext context) {
              return Directionality(
                textDirection: TextDirection.ltr,
                child: Material(
                  child: Center(
                    child: RawChip(
                      label: const SizedBox(width: 100, height: 100),
                      onSelected: (bool newValue) { },
                    ),
205 206
                  ),
                ),
207 208
              );
            },
209 210
          ),
        ),
211 212
      ),
    );
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    final RenderBox materialBox = getMaterialBox(tester);
    expect(materialBox, paints..path(color: chipTheme.backgroundColor));
    expect(tester.getSize(find.byType(RawChip)), const Size(250, 250)); // label + padding + labelPadding
    expect(getMaterial(tester).elevation, chipTheme.elevation);
    expect(getMaterial(tester).shape, chipTheme.shape);
    expect(getLabelStyle(tester).style.fontSize, 32);
  });

  testWidgets('Chip uses constructor parameters', (WidgetTester tester) async {
    const ChipThemeData shadowedChipTheme = ChipThemeData(
      backgroundColor: Color(0xff112233),
      elevation: 4,
      padding: EdgeInsets.all(5),
      labelPadding: EdgeInsets.all(2),
      labelStyle: TextStyle(),
      shape: RoundedRectangleBorder(),
    );

    const Color backgroundColor = Color(0xff332211);
    const double elevation = 3;
    const double fontSize = 32;
    const OutlinedBorder shape = CircleBorder();

    await tester.pumpWidget(
      MaterialApp(
        home: ChipTheme(
          data: shadowedChipTheme,
          child: Builder(
            builder: (BuildContext context) {
              return Directionality(
                textDirection: TextDirection.ltr,
                child: Material(
                  child: Center(
                    child: RawChip(
                      backgroundColor: backgroundColor,
                      elevation: elevation,
                      padding: const EdgeInsets.all(50),
                      labelPadding:const EdgeInsets.all(25),
                      labelStyle: const TextStyle(fontSize: fontSize),
                      shape: shape,
                      label: const SizedBox(width: 100, height: 100),
                      onSelected: (bool newValue) { },
                    ),
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
265 266

    final RenderBox materialBox = getMaterialBox(tester);
267 268 269 270 271 272 273 274
    expect(materialBox, paints..path(color: backgroundColor));
    expect(tester.getSize(find.byType(RawChip)), const Size(250, 250)); // label + padding + labelPadding
    expect(getMaterial(tester).elevation, elevation);
    expect(getMaterial(tester).shape, shape);
    expect(getLabelStyle(tester).style.fontSize, 32);
  });

  testWidgets('ChipTheme.fromDefaults', (WidgetTester tester) async {
275
    const TextStyle labelStyle = TextStyle();
276 277 278
    ChipThemeData chipTheme = ChipThemeData.fromDefaults(
      brightness: Brightness.light,
      secondaryColor: Colors.red,
279
      labelStyle: labelStyle,
280
    );
281 282 283 284 285 286
    expect(chipTheme.backgroundColor, Colors.black.withAlpha(0x1f));
    expect(chipTheme.deleteIconColor, Colors.black.withAlpha(0xde));
    expect(chipTheme.disabledColor, Colors.black.withAlpha(0x0c));
    expect(chipTheme.selectedColor, Colors.black.withAlpha(0x3d));
    expect(chipTheme.secondarySelectedColor, Colors.red.withAlpha(0x3d));
    expect(chipTheme.shadowColor, Colors.black);
287
    expect(chipTheme.surfaceTintColor, null);
288 289 290 291 292 293 294 295 296 297 298 299
    expect(chipTheme.selectedShadowColor, Colors.black);
    expect(chipTheme.showCheckmark, true);
    expect(chipTheme.checkmarkColor, null);
    expect(chipTheme.labelPadding, null);
    expect(chipTheme.padding, const EdgeInsets.all(4.0));
    expect(chipTheme.side, null);
    expect(chipTheme.shape, null);
    expect(chipTheme.labelStyle, labelStyle.copyWith(color: Colors.black.withAlpha(0xde)));
    expect(chipTheme.secondaryLabelStyle, labelStyle.copyWith(color: Colors.red.withAlpha(0xde)));
    expect(chipTheme.brightness, Brightness.light);
    expect(chipTheme.elevation, 0.0);
    expect(chipTheme.pressElevation, 8.0);
300

301 302 303 304 305
    chipTheme = ChipThemeData.fromDefaults(
      brightness: Brightness.dark,
      secondaryColor: Colors.tealAccent[200]!,
      labelStyle: const TextStyle(),
    );
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    expect(chipTheme.backgroundColor, Colors.white.withAlpha(0x1f));
    expect(chipTheme.deleteIconColor, Colors.white.withAlpha(0xde));
    expect(chipTheme.disabledColor, Colors.white.withAlpha(0x0c));
    expect(chipTheme.selectedColor, Colors.white.withAlpha(0x3d));
    expect(chipTheme.secondarySelectedColor, Colors.tealAccent[200]!.withAlpha(0x3d));
    expect(chipTheme.shadowColor, Colors.black);
    expect(chipTheme.selectedShadowColor, Colors.black);
    expect(chipTheme.showCheckmark, true);
    expect(chipTheme.checkmarkColor, null);
    expect(chipTheme.labelPadding, null);
    expect(chipTheme.padding, const EdgeInsets.all(4.0));
    expect(chipTheme.side, null);
    expect(chipTheme.shape, null);
    expect(chipTheme.labelStyle, labelStyle.copyWith(color: Colors.white.withAlpha(0xde)));
    expect(chipTheme.secondaryLabelStyle, labelStyle.copyWith(color: Colors.tealAccent[200]!.withAlpha(0xde)));
    expect(chipTheme.brightness, Brightness.dark);
    expect(chipTheme.elevation, 0.0);
    expect(chipTheme.pressElevation, 8.0);
324
  });
325

326

327
  testWidgets('ChipThemeData generates correct opacities for defaults', (WidgetTester tester) async {
328 329
    const Color customColor1 = Color(0xcafefeed);
    const Color customColor2 = Color(0xdeadbeef);
330
    final TextStyle customStyle = ThemeData.fallback().textTheme.bodyText1!.copyWith(color: customColor2);
331

332
    final ChipThemeData lightTheme = ChipThemeData.fromDefaults(
333 334 335 336 337 338 339 340 341 342
      secondaryColor: customColor1,
      brightness: Brightness.light,
      labelStyle: customStyle,
    );

    expect(lightTheme.backgroundColor, equals(Colors.black.withAlpha(0x1f)));
    expect(lightTheme.deleteIconColor, equals(Colors.black.withAlpha(0xde)));
    expect(lightTheme.disabledColor, equals(Colors.black.withAlpha(0x0c)));
    expect(lightTheme.selectedColor, equals(Colors.black.withAlpha(0x3d)));
    expect(lightTheme.secondarySelectedColor, equals(customColor1.withAlpha(0x3d)));
343
    expect(lightTheme.labelPadding, isNull);
344
    expect(lightTheme.padding, equals(const EdgeInsets.all(4.0)));
345 346
    expect(lightTheme.side, isNull);
    expect(lightTheme.shape, isNull);
347 348
    expect(lightTheme.labelStyle?.color, equals(Colors.black.withAlpha(0xde)));
    expect(lightTheme.secondaryLabelStyle?.color, equals(customColor1.withAlpha(0xde)));
349 350
    expect(lightTheme.brightness, equals(Brightness.light));

351
    final ChipThemeData darkTheme = ChipThemeData.fromDefaults(
352 353 354 355 356 357 358 359 360 361
      secondaryColor: customColor1,
      brightness: Brightness.dark,
      labelStyle: customStyle,
    );

    expect(darkTheme.backgroundColor, equals(Colors.white.withAlpha(0x1f)));
    expect(darkTheme.deleteIconColor, equals(Colors.white.withAlpha(0xde)));
    expect(darkTheme.disabledColor, equals(Colors.white.withAlpha(0x0c)));
    expect(darkTheme.selectedColor, equals(Colors.white.withAlpha(0x3d)));
    expect(darkTheme.secondarySelectedColor, equals(customColor1.withAlpha(0x3d)));
362
    expect(darkTheme.labelPadding, isNull);
363
    expect(darkTheme.padding, equals(const EdgeInsets.all(4.0)));
364 365
    expect(darkTheme.side, isNull);
    expect(darkTheme.shape, isNull);
366 367
    expect(darkTheme.labelStyle?.color, equals(Colors.white.withAlpha(0xde)));
    expect(darkTheme.secondaryLabelStyle?.color, equals(customColor1.withAlpha(0xde)));
368 369
    expect(darkTheme.brightness, equals(Brightness.dark));

370
    final ChipThemeData customTheme = ChipThemeData.fromDefaults(
371 372 373 374 375
      primaryColor: customColor1,
      secondaryColor: customColor2,
      labelStyle: customStyle,
    );

376
    //expect(customTheme.backgroundColor, equals(customColor1.withAlpha(0x1f)));
377 378 379 380
    expect(customTheme.deleteIconColor, equals(customColor1.withAlpha(0xde)));
    expect(customTheme.disabledColor, equals(customColor1.withAlpha(0x0c)));
    expect(customTheme.selectedColor, equals(customColor1.withAlpha(0x3d)));
    expect(customTheme.secondarySelectedColor, equals(customColor2.withAlpha(0x3d)));
381
    expect(customTheme.labelPadding, isNull);
382
    expect(customTheme.padding, equals(const EdgeInsets.all(4.0)));
383 384
    expect(customTheme.side, isNull);
    expect(customTheme.shape, isNull);
385 386
    expect(customTheme.labelStyle?.color, equals(customColor1.withAlpha(0xde)));
    expect(customTheme.secondaryLabelStyle?.color, equals(customColor2.withAlpha(0xde)));
387 388 389 390
    expect(customTheme.brightness, equals(Brightness.light));
  });

  testWidgets('ChipThemeData lerps correctly', (WidgetTester tester) async {
391
    final ChipThemeData chipThemeBlack = ChipThemeData.fromDefaults(
392 393
      secondaryColor: Colors.black,
      brightness: Brightness.dark,
394
      labelStyle: ThemeData.fallback().textTheme.bodyText1!.copyWith(color: Colors.black),
395 396
    ).copyWith(
      elevation: 1.0,
397
      labelPadding: const EdgeInsets.symmetric(horizontal: 8.0),
398
      shape: const StadiumBorder(),
399
      side: const BorderSide(),
400
      pressElevation: 4.0,
401
      shadowColor: Colors.black,
402
      surfaceTintColor: Colors.black,
403
      selectedShadowColor: Colors.black,
404
      showCheckmark: false,
405
      checkmarkColor: Colors.black,
406
    );
407
    final ChipThemeData chipThemeWhite = ChipThemeData.fromDefaults(
408 409
      secondaryColor: Colors.white,
      brightness: Brightness.light,
410
      labelStyle: ThemeData.fallback().textTheme.bodyText1!.copyWith(color: Colors.white),
411 412 413
    ).copyWith(
      padding: const EdgeInsets.all(2.0),
      labelPadding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
414 415
      shape: const BeveledRectangleBorder(),
      side: const BorderSide(color: Colors.white),
416 417
      elevation: 5.0,
      pressElevation: 10.0,
418
      shadowColor: Colors.white,
419
      surfaceTintColor: Colors.white,
420
      selectedShadowColor: Colors.white,
421
      showCheckmark: true,
422
      checkmarkColor: Colors.white,
423 424
    );

425
    final ChipThemeData lerp = ChipThemeData.lerp(chipThemeBlack, chipThemeWhite, 0.5)!;
426
    const Color middleGrey = Color(0xff7f7f7f);
427 428 429 430 431
    expect(lerp.backgroundColor, equals(middleGrey.withAlpha(0x1f)));
    expect(lerp.deleteIconColor, equals(middleGrey.withAlpha(0xde)));
    expect(lerp.disabledColor, equals(middleGrey.withAlpha(0x0c)));
    expect(lerp.selectedColor, equals(middleGrey.withAlpha(0x3d)));
    expect(lerp.secondarySelectedColor, equals(middleGrey.withAlpha(0x3d)));
432
    expect(lerp.shadowColor, equals(middleGrey));
433
    expect(lerp.surfaceTintColor, equals(middleGrey));
434
    expect(lerp.selectedShadowColor, equals(middleGrey));
435
    expect(lerp.showCheckmark, equals(true));
436 437
    expect(lerp.labelPadding, equals(const EdgeInsets.all(4.0)));
    expect(lerp.padding, equals(const EdgeInsets.all(3.0)));
438 439
    expect(lerp.side!.color, equals(middleGrey));
    expect(lerp.shape, isA<BeveledRectangleBorder>());
440 441
    expect(lerp.labelStyle?.color, equals(middleGrey.withAlpha(0xde)));
    expect(lerp.secondaryLabelStyle?.color, equals(middleGrey.withAlpha(0xde)));
442
    expect(lerp.brightness, equals(Brightness.light));
443 444
    expect(lerp.elevation, 3.0);
    expect(lerp.pressElevation, 7.0);
445
    expect(lerp.checkmarkColor, equals(middleGrey));
446 447 448

    expect(ChipThemeData.lerp(null, null, 0.25), isNull);

449
    final ChipThemeData lerpANull25 = ChipThemeData.lerp(null, chipThemeWhite, 0.25)!;
450 451 452 453 454
    expect(lerpANull25.backgroundColor, equals(Colors.black.withAlpha(0x08)));
    expect(lerpANull25.deleteIconColor, equals(Colors.black.withAlpha(0x38)));
    expect(lerpANull25.disabledColor, equals(Colors.black.withAlpha(0x03)));
    expect(lerpANull25.selectedColor, equals(Colors.black.withAlpha(0x0f)));
    expect(lerpANull25.secondarySelectedColor, equals(Colors.white.withAlpha(0x0f)));
455
    expect(lerpANull25.shadowColor, equals(Colors.white.withAlpha(0x40)));
456
    expect(lerpANull25.surfaceTintColor, equals(Colors.white.withAlpha(0x40)));
457
    expect(lerpANull25.selectedShadowColor, equals(Colors.white.withAlpha(0x40)));
458
    expect(lerpANull25.showCheckmark, equals(true));
459
    expect(lerpANull25.labelPadding, equals(const EdgeInsets.only(top: 2.0, bottom: 2.0)));
460
    expect(lerpANull25.padding, equals(const EdgeInsets.all(0.5)));
461 462
    expect(lerpANull25.side!.color, equals(Colors.white.withAlpha(0x3f)));
    expect(lerpANull25.shape, isA<BeveledRectangleBorder>());
463 464
    expect(lerpANull25.labelStyle?.color, equals(Colors.black.withAlpha(0x38)));
    expect(lerpANull25.secondaryLabelStyle?.color, equals(Colors.white.withAlpha(0x38)));
465
    expect(lerpANull25.brightness, equals(Brightness.light));
466 467
    expect(lerpANull25.elevation, 1.25);
    expect(lerpANull25.pressElevation, 2.5);
468
    expect(lerpANull25.checkmarkColor, equals(Colors.white.withAlpha(0x40)));
469

470
    final ChipThemeData lerpANull75 = ChipThemeData.lerp(null, chipThemeWhite, 0.75)!;
471 472 473 474 475
    expect(lerpANull75.backgroundColor, equals(Colors.black.withAlpha(0x17)));
    expect(lerpANull75.deleteIconColor, equals(Colors.black.withAlpha(0xa7)));
    expect(lerpANull75.disabledColor, equals(Colors.black.withAlpha(0x09)));
    expect(lerpANull75.selectedColor, equals(Colors.black.withAlpha(0x2e)));
    expect(lerpANull75.secondarySelectedColor, equals(Colors.white.withAlpha(0x2e)));
476
    expect(lerpANull75.shadowColor, equals(Colors.white.withAlpha(0xbf)));
477
    expect(lerpANull75.surfaceTintColor, equals(Colors.white.withAlpha(0xbf)));
478
    expect(lerpANull75.selectedShadowColor, equals(Colors.white.withAlpha(0xbf)));
479
    expect(lerpANull75.showCheckmark, equals(true));
480
    expect(lerpANull75.labelPadding, equals(const EdgeInsets.only(top: 6.0, bottom: 6.0)));
481
    expect(lerpANull75.padding, equals(const EdgeInsets.all(1.5)));
482 483
    expect(lerpANull75.side!.color, equals(Colors.white.withAlpha(0xbf)));
    expect(lerpANull75.shape, isA<BeveledRectangleBorder>());
484 485
    expect(lerpANull75.labelStyle?.color, equals(Colors.black.withAlpha(0xa7)));
    expect(lerpANull75.secondaryLabelStyle?.color, equals(Colors.white.withAlpha(0xa7)));
486
    expect(lerpANull75.brightness, equals(Brightness.light));
487 488
    expect(lerpANull75.elevation, 3.75);
    expect(lerpANull75.pressElevation, 7.5);
489
    expect(lerpANull75.checkmarkColor, equals(Colors.white.withAlpha(0xbf)));
490

491
    final ChipThemeData lerpBNull25 = ChipThemeData.lerp(chipThemeBlack, null, 0.25)!;
492 493 494 495 496
    expect(lerpBNull25.backgroundColor, equals(Colors.white.withAlpha(0x17)));
    expect(lerpBNull25.deleteIconColor, equals(Colors.white.withAlpha(0xa7)));
    expect(lerpBNull25.disabledColor, equals(Colors.white.withAlpha(0x09)));
    expect(lerpBNull25.selectedColor, equals(Colors.white.withAlpha(0x2e)));
    expect(lerpBNull25.secondarySelectedColor, equals(Colors.black.withAlpha(0x2e)));
497
    expect(lerpBNull25.shadowColor, equals(Colors.black.withAlpha(0xbf)));
498
    expect(lerpBNull25.surfaceTintColor, equals(Colors.black.withAlpha(0xbf)));
499
    expect(lerpBNull25.selectedShadowColor, equals(Colors.black.withAlpha(0xbf)));
500
    expect(lerpBNull25.showCheckmark, equals(false));
501
    expect(lerpBNull25.labelPadding, equals(const EdgeInsets.only(left: 6.0, right: 6.0)));
502
    expect(lerpBNull25.padding, equals(const EdgeInsets.all(3.0)));
503
    expect(lerpBNull25.side!.color, equals(Colors.black.withAlpha(0x3f)));
Dan Field's avatar
Dan Field committed
504
    expect(lerpBNull25.shape, isA<StadiumBorder>());
505 506
    expect(lerpBNull25.labelStyle?.color, equals(Colors.white.withAlpha(0xa7)));
    expect(lerpBNull25.secondaryLabelStyle?.color, equals(Colors.black.withAlpha(0xa7)));
507
    expect(lerpBNull25.brightness, equals(Brightness.dark));
508 509
    expect(lerpBNull25.elevation, 0.75);
    expect(lerpBNull25.pressElevation, 3.0);
510
    expect(lerpBNull25.checkmarkColor, equals(Colors.black.withAlpha(0xbf)));
511

512
    final ChipThemeData lerpBNull75 = ChipThemeData.lerp(chipThemeBlack, null, 0.75)!;
513 514 515 516 517
    expect(lerpBNull75.backgroundColor, equals(Colors.white.withAlpha(0x08)));
    expect(lerpBNull75.deleteIconColor, equals(Colors.white.withAlpha(0x38)));
    expect(lerpBNull75.disabledColor, equals(Colors.white.withAlpha(0x03)));
    expect(lerpBNull75.selectedColor, equals(Colors.white.withAlpha(0x0f)));
    expect(lerpBNull75.secondarySelectedColor, equals(Colors.black.withAlpha(0x0f)));
518
    expect(lerpBNull75.shadowColor, equals(Colors.black.withAlpha(0x40)));
519
    expect(lerpBNull75.surfaceTintColor, equals(Colors.black.withAlpha(0x40)));
520
    expect(lerpBNull75.selectedShadowColor, equals(Colors.black.withAlpha(0x40)));
521
    expect(lerpBNull75.showCheckmark, equals(true));
522
    expect(lerpBNull75.labelPadding, equals(const EdgeInsets.only(left: 2.0, right: 2.0)));
523
    expect(lerpBNull75.padding, equals(const EdgeInsets.all(1.0)));
524
    expect(lerpBNull75.side!.color, equals(Colors.black.withAlpha(0xbf)));
Dan Field's avatar
Dan Field committed
525
    expect(lerpBNull75.shape, isA<StadiumBorder>());
526 527
    expect(lerpBNull75.labelStyle?.color, equals(Colors.white.withAlpha(0x38)));
    expect(lerpBNull75.secondaryLabelStyle?.color, equals(Colors.black.withAlpha(0x38)));
528
    expect(lerpBNull75.brightness, equals(Brightness.light));
529 530
    expect(lerpBNull75.elevation, 0.25);
    expect(lerpBNull75.pressElevation, 1.0);
531
    expect(lerpBNull75.checkmarkColor, equals(Colors.black.withAlpha(0x40)));
532
  });
533 534 535 536 537 538 539 540 541 542 543 544

  testWidgets('Chip uses stateful color from chip theme', (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) {
545
      if (states.contains(MaterialState.disabled)) {
546
        return disabledColor;
547
      }
548

549
      if (states.contains(MaterialState.pressed)) {
550
        return pressedColor;
551
      }
552

553
      if (states.contains(MaterialState.hovered)) {
554
        return hoverColor;
555
      }
556

557
      if (states.contains(MaterialState.focused)) {
558
        return focusedColor;
559
      }
560

561
      if (states.contains(MaterialState.selected)) {
562
        return selectedColor;
563
      }
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

      return defaultColor;
    }

    final TextStyle labelStyle =  TextStyle(
      color: MaterialStateColor.resolveWith(getTextColor),
    );
    Widget chipWidget({ bool enabled = true, bool selected = false }) {
      return MaterialApp(
        theme: ThemeData(
          chipTheme: ThemeData.light().chipTheme.copyWith(
            labelStyle: labelStyle,
            secondaryLabelStyle: labelStyle,
          ),
        ),
        home: Scaffold(
          body: Focus(
            focusNode: focusNode,
            child: ChoiceChip(
              label: const Text('Chip'),
              selected: selected,
              onSelected: enabled ? (_) {} : null,
            ),
          ),
        ),
      );
    }
    Color textColor() {
592
      return tester.renderObject<RenderParagraph>(find.text('Chip')).text.style!.color!;
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
    }

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

630 631 632 633 634 635 636
  testWidgets('Chip uses stateful border side from resolveWith pattern', (WidgetTester tester) async {
    const Color selectedColor = Color(0x00000001);
    const Color defaultColor = Color(0x00000002);

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

637
      if (states.contains(MaterialState.selected)) {
638
        color = selectedColor;
639
      }
640

641
      return BorderSide(color: color);
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    }

    Widget chipWidget({ bool selected = false }) {
      return MaterialApp(
        theme: ThemeData(
          chipTheme: ThemeData.light().chipTheme.copyWith(
            side: MaterialStateBorderSide.resolveWith(getBorderSide),
          ),
        ),
        home: Scaffold(
          body: ChoiceChip(
            label: const Text('Chip'),
            selected: selected,
            onSelected: (_) {},
          ),
        ),
      );
    }

    // Default.
    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));
  });

670 671 672 673 674 675
  testWidgets('Chip uses stateful border side from chip theme', (WidgetTester tester) async {
    const Color selectedColor = Color(0x00000001);
    const Color defaultColor = Color(0x00000002);

    BorderSide getBorderSide(Set<MaterialState> states) {
      Color color = defaultColor;
676
      if (states.contains(MaterialState.selected)) {
677
        color = selectedColor;
678
      }
679
      return BorderSide(color: color);
680 681
    }

682 683 684 685 686 687 688 689
    final ChipThemeData chipTheme = ChipThemeData.fromDefaults(
      brightness: Brightness.light,
      secondaryColor: Colors.blue,
      labelStyle: const TextStyle(),
    ).copyWith(
      side: _MaterialStateBorderSide(getBorderSide),
    );

690 691
    Widget chipWidget({ bool selected = false }) {
      return MaterialApp(
692
        theme: ThemeData(chipTheme: chipTheme),
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
        home: Scaffold(
          body: ChoiceChip(
            label: const Text('Chip'),
            selected: selected,
            onSelected: (_) {},
          ),
        ),
      );
    }

    // Default.
    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));
  });

  testWidgets('Chip uses stateful shape from chip theme', (WidgetTester tester) async {
    OutlinedBorder? getShape(Set<MaterialState> states) {
714
      if (states.contains(MaterialState.selected)) {
715
        return const RoundedRectangleBorder();
716
      }
717 718 719 720

      return null;
    }

721 722 723 724 725 726 727 728 729
    final ChipThemeData chipTheme = ChipThemeData.fromDefaults(
      brightness: Brightness.light,
      secondaryColor: Colors.blue,
      labelStyle: const TextStyle(),
    ).copyWith(
      shape: _MaterialStateOutlinedBorder(getShape),
    );


730 731
    Widget chipWidget({ bool selected = false }) {
      return MaterialApp(
732
        theme: ThemeData(chipTheme: chipTheme),
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
        home: Scaffold(
          body: ChoiceChip(
            label: const Text('Chip'),
            selected: selected,
            onSelected: (_) {},
          ),
        ),
      );
    }

    // Default.
    await tester.pumpWidget(chipWidget());
    expect(getMaterial(tester).shape, isA<StadiumBorder>());

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

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