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

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

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

13
import '../rendering/mock_canvas.dart';
14
import '../widgets/test_border.dart' show TestBorder;
15

16
class NotifyMaterial extends StatelessWidget {
17
  const NotifyMaterial({ super.key });
18 19
  @override
  Widget build(BuildContext context) {
20
    const LayoutChangedNotification().dispatch(context);
21
    return Container();
22 23 24
  }
}

25 26 27
Widget buildMaterial({
  double elevation = 0.0,
  Color shadowColor = const Color(0xFF00FF00),
28
  Color? surfaceTintColor,
29
  Color color = const Color(0xFF0000FF),
30
}) {
31 32
  return Center(
    child: SizedBox(
33 34
      height: 100.0,
      width: 100.0,
35
      child: Material(
36
        color: color,
37
        shadowColor: shadowColor,
38
        surfaceTintColor: surfaceTintColor,
39
        elevation: elevation,
40
        shape: const CircleBorder(),
41 42 43 44 45
      ),
    ),
  );
}

46
RenderPhysicalShape getModel(WidgetTester tester) {
47
  return tester.renderObject(find.byType(PhysicalShape));
48 49
}

50 51 52
class PaintRecorder extends CustomPainter {
  PaintRecorder(this.log);

53
  final List<Size> log;
54 55 56 57

  @override
  void paint(Canvas canvas, Size size) {
    log.add(size);
58
    final Paint paint = Paint()..color = const Color(0xFF0000FF);
59
    canvas.drawRect(Offset.zero & size, paint);
60 61 62 63 64 65
  }

  @override
  bool shouldRepaint(PaintRecorder oldDelegate) => false;
}

66 67 68 69 70 71
class ElevationColor {
  const ElevationColor(this.elevation, this.color);
  final double elevation;
  final Color color;
}

72
void main() {
73 74 75 76 77 78 79 80 81 82 83 84 85 86
  // Regression test for https://github.com/flutter/flutter/issues/81504
  testWidgets('MaterialApp.home nullable and update test', (WidgetTester tester) async {
    // _WidgetsAppState._usesNavigator == true
    await tester.pumpWidget(const MaterialApp(home: SizedBox.shrink()));

    // _WidgetsAppState._usesNavigator == false
    await tester.pumpWidget(const MaterialApp()); // Do not crash!

    // _WidgetsAppState._usesNavigator == true
    await tester.pumpWidget(const MaterialApp(home: SizedBox.shrink())); // Do not crash!

    expect(tester.takeException(), null);
  });

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  testWidgets('default Material debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const Material().debugFillProperties(builder);

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

    expect(description, <String>['type: canvas']);
  });

  testWidgets('Material implements debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const Material(
      color: Color(0xFFFFFFFF),
103
      shadowColor: Color(0xffff0000),
104
      surfaceTintColor: Color(0xff0000ff),
105 106 107 108 109 110 111 112 113 114 115 116
      textStyle: TextStyle(color: Color(0xff00ff00)),
      borderRadius: BorderRadiusDirectional.all(Radius.circular(10)),
    ).debugFillProperties(builder);

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

    expect(description, <String>[
      'type: canvas',
      'color: Color(0xffffffff)',
117
      'shadowColor: Color(0xffff0000)',
118
      'surfaceTintColor: Color(0xff0000ff)',
119 120
      'textStyle.inherit: true',
      'textStyle.color: Color(0xff00ff00)',
121
      'borderRadius: BorderRadiusDirectional.circular(10.0)',
122 123 124
    ]);
  });

125
  testWidgets('LayoutChangedNotification test', (WidgetTester tester) async {
126
    await tester.pumpWidget(
127
      const Material(
128
        child: NotifyMaterial(),
129
      ),
130 131
    );
  });
132 133

  testWidgets('ListView scroll does not repaint', (WidgetTester tester) async {
134
    final List<Size> log = <Size>[];
135 136

    await tester.pumpWidget(
137
      Directionality(
138
        textDirection: TextDirection.ltr,
139
        child: Column(
140
          children: <Widget>[
141
            SizedBox(
142 143
              width: 150.0,
              height: 150.0,
144 145
              child: CustomPaint(
                painter: PaintRecorder(log),
146
              ),
147
            ),
148 149 150
            Expanded(
              child: Material(
                child: Column(
151
                  children: <Widget>[
152 153
                    Expanded(
                      child: ListView(
154
                        children: <Widget>[
155
                          Container(
156 157 158 159 160
                            height: 2000.0,
                            color: const Color(0xFF00FF00),
                          ),
                        ],
                      ),
161
                    ),
162
                    SizedBox(
163 164
                      width: 100.0,
                      height: 100.0,
165 166
                      child: CustomPaint(
                        painter: PaintRecorder(log),
167
                      ),
168
                    ),
169 170
                  ],
                ),
171 172
              ),
            ),
173 174
          ],
        ),
175 176 177 178 179 180 181 182 183 184 185
      ),
    );

    // We paint twice because we have two CustomPaint widgets in the tree above
    // to test repainting both inside and outside the Material widget.
    expect(log, equals(<Size>[
      const Size(150.0, 150.0),
      const Size(100.0, 100.0),
    ]));
    log.clear();

186
    await tester.drag(find.byType(ListView), const Offset(0.0, -300.0));
187 188 189 190
    await tester.pump();

    expect(log, isEmpty);
  });
191 192

  testWidgets('Shadows animate smoothly', (WidgetTester tester) async {
193 194
    // This code verifies that the PhysicalModel's elevation animates over
    // a kThemeChangeDuration time interval.
195

196
    await tester.pumpWidget(buildMaterial());
197
    final RenderPhysicalShape modelA = getModel(tester);
198 199
    expect(modelA.elevation, equals(0.0));

200
    await tester.pumpWidget(buildMaterial(elevation: 9.0));
201
    final RenderPhysicalShape modelB = getModel(tester);
202
    expect(modelB.elevation, equals(0.0));
203 204

    await tester.pump(const Duration(milliseconds: 1));
205
    final RenderPhysicalShape modelC = getModel(tester);
206
    expect(modelC.elevation, moreOrLessEquals(0.0, epsilon: 0.001));
207 208

    await tester.pump(kThemeChangeDuration ~/ 2);
209
    final RenderPhysicalShape modelD = getModel(tester);
210
    expect(modelD.elevation, isNot(moreOrLessEquals(0.0, epsilon: 0.001)));
211 212

    await tester.pump(kThemeChangeDuration);
213
    final RenderPhysicalShape modelE = getModel(tester);
214
    expect(modelE.elevation, equals(9.0));
215
  });
216 217

  testWidgets('Shadow colors animate smoothly', (WidgetTester tester) async {
218
    // This code verifies that the PhysicalModel's shadowColor animates over
219 220
    // a kThemeChangeDuration time interval.

221
    await tester.pumpWidget(buildMaterial());
222
    final RenderPhysicalShape modelA = getModel(tester);
223 224 225
    expect(modelA.shadowColor, equals(const Color(0xFF00FF00)));

    await tester.pumpWidget(buildMaterial(shadowColor: const Color(0xFFFF0000)));
226
    final RenderPhysicalShape modelB = getModel(tester);
227 228 229
    expect(modelB.shadowColor, equals(const Color(0xFF00FF00)));

    await tester.pump(const Duration(milliseconds: 1));
230
    final RenderPhysicalShape modelC = getModel(tester);
231
    expect(modelC.shadowColor, within<Color>(distance: 1, from: const Color(0xFF00FF00)));
232 233

    await tester.pump(kThemeChangeDuration ~/ 2);
234
    final RenderPhysicalShape modelD = getModel(tester);
235
    expect(modelD.shadowColor, isNot(within<Color>(distance: 1, from: const Color(0xFF00FF00))));
236 237

    await tester.pump(kThemeChangeDuration);
238
    final RenderPhysicalShape modelE = getModel(tester);
239 240
    expect(modelE.shadowColor, equals(const Color(0xFFFF0000)));
  });
241

242 243 244 245 246 247 248 249
  testWidgets('Transparent material widget does not absorb hit test', (WidgetTester tester) async {
    // This is a regression test for https://github.com/flutter/flutter/issues/58665.
    bool pressed = false;
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Stack(
            children: <Widget>[
250
              ElevatedButton(
251 252 253
                onPressed: () {
                  pressed = true;
                },
254
                child: null,
255
              ),
256
              const Material(
257
                type: MaterialType.transparency,
258
                child: SizedBox(
259 260 261 262 263 264 265 266 267
                  width: 400.0,
                  height: 500.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );
268
    await tester.tap(find.byType(ElevatedButton));
269 270 271
    expect(pressed, isTrue);
  });

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  group('Surface Tint Overlay', () {
    testWidgets('applyElevationOverlayColor does not effect anything with useMaterial3 set to true', (WidgetTester tester) async {
      const Color surfaceColor = Color(0xFF121212);
      await tester.pumpWidget(Theme(
        data: ThemeData(
          useMaterial3: true,
          applyElevationOverlayColor: true,
          colorScheme: const ColorScheme.dark().copyWith(surface: surfaceColor),
        ),
        child: buildMaterial(color: surfaceColor, elevation: 8.0),
      ));
      final RenderPhysicalShape model = getModel(tester);
      expect(model.color, equals(surfaceColor));
    });

    testWidgets('surfaceTintColor is used to as an overlay to indicate elevation', (WidgetTester tester) async {
      const Color baseColor = Color(0xFF121212);
      const Color surfaceTintColor = Color(0xff44CCFF);
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      // With no surfaceTintColor specified, it should not apply an overlay
      await tester.pumpWidget(
        Theme(
          data: ThemeData(
            useMaterial3: true,
          ),
          child: buildMaterial(
            color: baseColor,
            elevation: 12.0,
          ),
        ),
      );
      await tester.pumpAndSettle();
      final RenderPhysicalShape noTintModel = getModel(tester);
      expect(noTintModel.color, equals(baseColor));

      // With surfaceTintColor specified, it should not apply an overlay based
      // on the elevation.
      await tester.pumpWidget(
        Theme(
          data: ThemeData(
            useMaterial3: true,
          ),
          child: buildMaterial(
            color: baseColor,
            surfaceTintColor: surfaceTintColor,
            elevation: 12.0,
          ),
        ),
      );
      await tester.pumpAndSettle();
      final RenderPhysicalShape tintModel = getModel(tester);

      // Final color should be the base with a tint of 0.14 opacity or 0xff192c33
      expect(tintModel.color, equals(const Color(0xff192c33)));
    });

  }); // Surface Tint Overlay group

  group('Elevation Overlay M2', () {
    // These tests only apply to the Material 2 overlay mechanism. This group
    // can be removed after migration to Material 3 is complete.
333 334 335 336
    testWidgets('applyElevationOverlayColor set to false does not change surface color', (WidgetTester tester) async {
      const Color surfaceColor = Color(0xFF121212);
      await tester.pumpWidget(Theme(
          data: ThemeData(
337
            useMaterial3: false,
338 339 340
            applyElevationOverlayColor: false,
            colorScheme: const ColorScheme.dark().copyWith(surface: surfaceColor),
          ),
341 342
          child: buildMaterial(color: surfaceColor, elevation: 8.0),
      ));
343 344 345 346
      final RenderPhysicalShape model = getModel(tester);
      expect(model.color, equals(surfaceColor));
    });

347 348 349 350
    testWidgets('applyElevationOverlayColor set to true applies a semi-transparent onSurface color to the surface color', (WidgetTester tester) async {
      const Color surfaceColor = Color(0xFF121212);
      const Color onSurfaceColor = Colors.greenAccent;

351
      // The colors we should get with a base surface color of 0xFF121212 for
352
      // and a given elevation
353 354
      const List<ElevationColor> elevationColors = <ElevationColor>[
        ElevationColor(0.0, Color(0xFF121212)),
355 356 357 358 359 360 361 362 363
        ElevationColor(1.0, Color(0xFF161D19)),
        ElevationColor(2.0, Color(0xFF18211D)),
        ElevationColor(3.0, Color(0xFF19241E)),
        ElevationColor(4.0, Color(0xFF1A2620)),
        ElevationColor(6.0, Color(0xFF1B2922)),
        ElevationColor(8.0, Color(0xFF1C2C24)),
        ElevationColor(12.0, Color(0xFF1D3027)),
        ElevationColor(16.0, Color(0xFF1E3329)),
        ElevationColor(24.0, Color(0xFF20362B)),
364 365
      ];

366
      for (final ElevationColor test in elevationColors) {
367 368 369
        await tester.pumpWidget(
            Theme(
              data: ThemeData(
370
                useMaterial3: false,
371
                applyElevationOverlayColor: true,
372 373 374 375
                colorScheme: const ColorScheme.dark().copyWith(
                  surface: surfaceColor,
                  onSurface: onSurfaceColor,
                ),
376 377 378 379 380
              ),
              child: buildMaterial(
                color: surfaceColor,
                elevation: test.elevation,
              ),
381
            ),
382 383 384 385 386 387 388
        );
        await tester.pumpAndSettle(); // wait for the elevation animation to finish
        final RenderPhysicalShape model = getModel(tester);
        expect(model.color, equals(test.color));
      }
    });

389 390 391 392
    testWidgets('overlay will not apply to materials using a non-surface color', (WidgetTester tester) async {
      await tester.pumpWidget(
        Theme(
          data: ThemeData(
393
            useMaterial3: false,
394 395 396 397 398 399 400 401 402 403 404 405 406 407
            applyElevationOverlayColor: true,
            colorScheme: const ColorScheme.dark(),
          ),
          child: buildMaterial(
            color: Colors.cyan,
            elevation: 8.0,
          ),
        ),
      );
      final RenderPhysicalShape model = getModel(tester);
      // Shouldn't change, as it is not using a ColorScheme.surface color
      expect(model.color, equals(Colors.cyan));
    });

408
    testWidgets('overlay will not apply to materials using a light theme', (WidgetTester tester) async {
409 410 411
      await tester.pumpWidget(
          Theme(
            data: ThemeData(
412
              useMaterial3: false,
413
              applyElevationOverlayColor: true,
414
              colorScheme: const ColorScheme.light(),
415 416
            ),
            child: buildMaterial(
417 418
              color: Colors.cyan,
              elevation: 8.0,
419
            ),
420
          ),
421 422
      );
      final RenderPhysicalShape model = getModel(tester);
423
      // Shouldn't change, as it was under a light color scheme.
424 425 426
      expect(model.color, equals(Colors.cyan));
    });

427 428 429 430 431 432 433
    testWidgets('overlay will apply to materials with a non-opaque surface color', (WidgetTester tester) async {
      const Color surfaceColor = Color(0xFF121212);
      const Color surfaceColorWithOverlay = Color(0xC6353535);

      await tester.pumpWidget(
        Theme(
          data: ThemeData(
434
            useMaterial3: false,
435
            applyElevationOverlayColor: true,
436
            colorScheme: const ColorScheme.dark(),
437 438 439 440 441 442 443 444 445 446 447 448
          ),
          child: buildMaterial(
            color: surfaceColor.withOpacity(.75),
            elevation: 8.0,
          ),
        ),
      );

      final RenderPhysicalShape model = getModel(tester);
      expect(model.color, equals(surfaceColorWithOverlay));
      expect(model.color, isNot(equals(surfaceColor)));
    });
449 450 451 452 453 454 455 456 457 458 459 460

    testWidgets('Expected overlay color can be computed using colorWithOverlay', (WidgetTester tester) async {
      const Color surfaceColor = Color(0xFF123456);
      const Color onSurfaceColor = Color(0xFF654321);
      const double elevation = 8.0;

      final Color surfaceColorWithOverlay =
        ElevationOverlay.colorWithOverlay(surfaceColor, onSurfaceColor, elevation);

      await tester.pumpWidget(
        Theme(
          data: ThemeData(
461
            useMaterial3: false,
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
            applyElevationOverlayColor: true,
            colorScheme: const ColorScheme.dark(
              surface: surfaceColor,
              onSurface: onSurfaceColor,
            ),
          ),
          child: buildMaterial(
            color: surfaceColor,
            elevation: elevation,
          ),
        ),
      );

      final RenderPhysicalShape model = getModel(tester);
      expect(model.color, equals(surfaceColorWithOverlay));
      expect(model.color, isNot(equals(surfaceColor)));
    });
479 480

  }); // Elevation Overlay M2 group
481

482
  group('Transparency clipping', () {
483
    testWidgets('No clip by default', (WidgetTester tester) async {
484
      final GlobalKey materialKey = GlobalKey();
485
      await tester.pumpWidget(
486
          Material(
487 488 489
            key: materialKey,
            type: MaterialType.transparency,
            child: const SizedBox(width: 100.0, height: 100.0),
490
          ),
491 492
      );

493 494
      final RenderClipPath renderClip = tester.allRenderObjects.whereType<RenderClipPath>().first;
      expect(renderClip.clipBehavior, equals(Clip.none));
495
    });
496 497

    testWidgets('clips to bounding rect by default given Clip.antiAlias', (WidgetTester tester) async {
498
      final GlobalKey materialKey = GlobalKey();
499
      await tester.pumpWidget(
500
        Material(
501 502
          key: materialKey,
          type: MaterialType.transparency,
503
          clipBehavior: Clip.antiAlias,
504
          child: const SizedBox(width: 100.0, height: 100.0),
505
        ),
506 507
      );

508
      expect(find.byKey(materialKey), clipsWithBoundingRect);
509 510
    });

511
    testWidgets('clips to rounded rect when borderRadius provided given Clip.antiAlias', (WidgetTester tester) async {
512
      final GlobalKey materialKey = GlobalKey();
513
      await tester.pumpWidget(
514
        Material(
515 516
          key: materialKey,
          type: MaterialType.transparency,
517
          borderRadius: const BorderRadius.all(Radius.circular(10.0)),
518
          clipBehavior: Clip.antiAlias,
519
          child: const SizedBox(width: 100.0, height: 100.0),
520
        ),
521 522 523 524 525
      );

      expect(
        find.byKey(materialKey),
        clipsWithBoundingRRect(
526
          borderRadius: const BorderRadius.all(Radius.circular(10.0)),
527 528 529
        ),
      );
    });
530

531
    testWidgets('clips to shape when provided given Clip.antiAlias', (WidgetTester tester) async {
532
      final GlobalKey materialKey = GlobalKey();
533
      await tester.pumpWidget(
534
        Material(
535 536 537
          key: materialKey,
          type: MaterialType.transparency,
          shape: const StadiumBorder(),
538
          clipBehavior: Clip.antiAlias,
539
          child: const SizedBox(width: 100.0, height: 100.0),
540
        ),
541 542 543 544 545 546 547 548 549
      );

      expect(
        find.byKey(materialKey),
        clipsWithShapeBorder(
          shape: const StadiumBorder(),
        ),
      );
    });
550 551 552 553 554 555 556 557 558

    testWidgets('supports directional clips', (WidgetTester tester) async {
      final List<String> logs = <String>[];
      final ShapeBorder shape = TestBorder((String message) { logs.add(message); });
      Widget buildMaterial() {
        return Material(
          type: MaterialType.transparency,
          shape: shape,
          clipBehavior: Clip.antiAlias,
559
          child: const SizedBox(width: 100.0, height: 100.0),
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
        );
      }
      final Widget material = buildMaterial();
      // verify that a regular clip works as one would expect
      logs.add('--0');
      await tester.pumpWidget(material);
      // verify that pumping again doesn't recompute the clip
      // even though the widget itself is new (the shape doesn't change identity)
      logs.add('--1');
      await tester.pumpWidget(buildMaterial());
      // verify that Material passes the TextDirection on to its shape when it's transparent
      logs.add('--2');
      await tester.pumpWidget(Directionality(
        textDirection: TextDirection.ltr,
        child: material,
      ));
      // verify that changing the text direction from LTR to RTL has an effect
      // even though the widget itself is identical
      logs.add('--3');
      await tester.pumpWidget(Directionality(
        textDirection: TextDirection.rtl,
        child: material,
      ));
      // verify that pumping again with a text direction has no effect
      logs.add('--4');
      await tester.pumpWidget(Directionality(
        textDirection: TextDirection.rtl,
        child: buildMaterial(),
      ));
      logs.add('--5');
      // verify that changing the text direction and the widget at the same time
      // works as expected
      await tester.pumpWidget(Directionality(
        textDirection: TextDirection.ltr,
        child: material,
      ));
      expect(logs, <String>[
        '--0',
        'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) null',
        'paint Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) null',
        '--1',
        '--2',
        'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.ltr',
        'paint Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.ltr',
        '--3',
        'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.rtl',
        'paint Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.rtl',
        '--4',
        '--5',
        'getOuterPath Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.ltr',
        'paint Rect.fromLTRB(0.0, 0.0, 800.0, 600.0) TextDirection.ltr',
      ]);
    });
613 614 615 616
  });

  group('PhysicalModels', () {
    testWidgets('canvas', (WidgetTester tester) async {
617
      final GlobalKey materialKey = GlobalKey();
618
      await tester.pumpWidget(
619
        Material(
620
          key: materialKey,
621
          child: const SizedBox(width: 100.0, height: 100.0),
622
        ),
623 624 625 626 627 628 629 630 631 632
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.zero,
          elevation: 0.0,
      ));
    });

    testWidgets('canvas with borderRadius and elevation', (WidgetTester tester) async {
633
      final GlobalKey materialKey = GlobalKey();
634
      await tester.pumpWidget(
635
        Material(
636
          key: materialKey,
637
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
638
          elevation: 1.0,
639
          child: const SizedBox(width: 100.0, height: 100.0),
640
        ),
641 642 643 644
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
645
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
646 647 648 649
          elevation: 1.0,
      ));
    });

650
    testWidgets('canvas with shape and elevation', (WidgetTester tester) async {
651
      final GlobalKey materialKey = GlobalKey();
652
      await tester.pumpWidget(
653
        Material(
654 655 656
          key: materialKey,
          shape: const StadiumBorder(),
          elevation: 1.0,
657
          child: const SizedBox(width: 100.0, height: 100.0),
658
        ),
659 660 661 662 663 664 665 666
      );

      expect(find.byKey(materialKey), rendersOnPhysicalShape(
          shape: const StadiumBorder(),
          elevation: 1.0,
      ));
    });

667
    testWidgets('card', (WidgetTester tester) async {
668
      final GlobalKey materialKey = GlobalKey();
669
      await tester.pumpWidget(
670
        Material(
671 672 673
          key: materialKey,
          type: MaterialType.card,
          child: const SizedBox(width: 100.0, height: 100.0),
674
        ),
675 676 677 678
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
679
          borderRadius: const BorderRadius.all(Radius.circular(2.0)),
680 681 682 683 684
          elevation: 0.0,
      ));
    });

    testWidgets('card with borderRadius and elevation', (WidgetTester tester) async {
685
      final GlobalKey materialKey = GlobalKey();
686
      await tester.pumpWidget(
687
        Material(
688 689
          key: materialKey,
          type: MaterialType.card,
690
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
691 692
          elevation: 5.0,
          child: const SizedBox(width: 100.0, height: 100.0),
693
        ),
694 695 696 697
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
698
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
699 700 701 702
          elevation: 5.0,
      ));
    });

703
    testWidgets('card with shape and elevation', (WidgetTester tester) async {
704
      final GlobalKey materialKey = GlobalKey();
705
      await tester.pumpWidget(
706
        Material(
707 708 709 710 711
          key: materialKey,
          type: MaterialType.card,
          shape: const StadiumBorder(),
          elevation: 5.0,
          child: const SizedBox(width: 100.0, height: 100.0),
712
        ),
713 714 715 716 717 718 719 720
      );

      expect(find.byKey(materialKey), rendersOnPhysicalShape(
          shape: const StadiumBorder(),
          elevation: 5.0,
      ));
    });

721
    testWidgets('circle', (WidgetTester tester) async {
722
      final GlobalKey materialKey = GlobalKey();
723
      await tester.pumpWidget(
724
        Material(
725 726 727
          key: materialKey,
          type: MaterialType.circle,
          color: const Color(0xFF0000FF),
728
          child: const SizedBox(width: 100.0, height: 100.0),
729
        ),
730 731 732 733 734 735 736 737 738
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.circle,
          elevation: 0.0,
      ));
    });

    testWidgets('button', (WidgetTester tester) async {
739
      final GlobalKey materialKey = GlobalKey();
740
      await tester.pumpWidget(
741
        Material(
742 743 744
          key: materialKey,
          type: MaterialType.button,
          color: const Color(0xFF0000FF),
745
          child: const SizedBox(width: 100.0, height: 100.0),
746
        ),
747 748 749 750
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
751
          borderRadius: const BorderRadius.all(Radius.circular(2.0)),
752 753 754 755 756
          elevation: 0.0,
      ));
    });

    testWidgets('button with elevation and borderRadius', (WidgetTester tester) async {
757
      final GlobalKey materialKey = GlobalKey();
758
      await tester.pumpWidget(
759
        Material(
760 761 762
          key: materialKey,
          type: MaterialType.button,
          color: const Color(0xFF0000FF),
763
          borderRadius: const BorderRadius.all(Radius.circular(6.0)),
764
          elevation: 4.0,
765
          child: const SizedBox(width: 100.0, height: 100.0),
766
        ),
767 768 769 770
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
771
          borderRadius: const BorderRadius.all(Radius.circular(6.0)),
772 773 774
          elevation: 4.0,
      ));
    });
775 776

    testWidgets('button with elevation and shape', (WidgetTester tester) async {
777
      final GlobalKey materialKey = GlobalKey();
778
      await tester.pumpWidget(
779
        Material(
780 781 782 783 784
          key: materialKey,
          type: MaterialType.button,
          color: const Color(0xFF0000FF),
          shape: const StadiumBorder(),
          elevation: 4.0,
785
          child: const SizedBox(width: 100.0, height: 100.0),
786
        ),
787 788 789 790 791 792 793
      );

      expect(find.byKey(materialKey), rendersOnPhysicalShape(
          shape: const StadiumBorder(),
          elevation: 4.0,
      ));
    });
794
  });
795 796 797

  group('Border painting', () {
    testWidgets('border is painted on physical layers', (WidgetTester tester) async {
798
      final GlobalKey materialKey = GlobalKey();
799
      await tester.pumpWidget(
800
        Material(
801 802 803 804
          key: materialKey,
          type: MaterialType.button,
          color: const Color(0xFF0000FF),
          shape: const CircleBorder(
805
            side: BorderSide(
806
              width: 2.0,
807
              color: Color(0xFF0000FF),
808
            ),
809
          ),
810
          child: const SizedBox(width: 100.0, height: 100.0),
811
        ),
812 813 814 815 816 817 818
      );

      final RenderBox box = tester.renderObject(find.byKey(materialKey));
      expect(box, paints..circle());
    });

    testWidgets('border is painted for transparent material', (WidgetTester tester) async {
819
      final GlobalKey materialKey = GlobalKey();
820
      await tester.pumpWidget(
821
        Material(
822 823 824
          key: materialKey,
          type: MaterialType.transparency,
          shape: const CircleBorder(
825
            side: BorderSide(
826
              width: 2.0,
827
              color: Color(0xFF0000FF),
828
            ),
829
          ),
830
          child: const SizedBox(width: 100.0, height: 100.0),
831
        ),
832 833 834 835 836 837 838
      );

      final RenderBox box = tester.renderObject(find.byKey(materialKey));
      expect(box, paints..circle());
    });

    testWidgets('border is not painted for when border side is none', (WidgetTester tester) async {
839
      final GlobalKey materialKey = GlobalKey();
840
      await tester.pumpWidget(
841
        Material(
842 843 844
          key: materialKey,
          type: MaterialType.transparency,
          shape: const CircleBorder(),
845
          child: const SizedBox(width: 100.0, height: 100.0),
846
        ),
847 848 849 850 851
      );

      final RenderBox box = tester.renderObject(find.byKey(materialKey));
      expect(box, isNot(paints..circle()));
    });
852 853 854 855 856 857 858 859 860 861 862 863 864 865

    testWidgets('border is painted above child by default', (WidgetTester tester) async {
      final Key painterKey = UniqueKey();

      await tester.pumpWidget(MaterialApp(
        home: Scaffold(
          body: RepaintBoundary(
            key: painterKey,
            child: Card(
              child: SizedBox(
                width: 200,
                height: 300,
                child: Material(
                  clipBehavior: Clip.hardEdge,
866 867 868
                  shape: const RoundedRectangleBorder(
                    side: BorderSide(color: Colors.grey, width: 6),
                    borderRadius: BorderRadius.all(Radius.circular(8)),
869 870 871 872 873 874
                  ),
                  child: Column(
                    children: <Widget>[
                      Container(
                        color: Colors.green,
                        height: 150,
875
                      ),
876 877 878 879
                    ],
                  ),
                ),
              ),
880 881
            ),
          ),
882 883 884 885 886
        ),
      ));

      await expectLater(
        find.byKey(painterKey),
887
        matchesGoldenFile('material.border_paint_above.png'),
888
      );
889
    });
890 891 892 893 894 895 896 897 898 899 900 901 902 903

    testWidgets('border is painted below child when specified', (WidgetTester tester) async {
      final Key painterKey = UniqueKey();

      await tester.pumpWidget(MaterialApp(
        home: Scaffold(
          body: RepaintBoundary(
            key: painterKey,
            child: Card(
              child: SizedBox(
                width: 200,
                height: 300,
                child: Material(
                  clipBehavior: Clip.hardEdge,
904 905 906
                  shape: const RoundedRectangleBorder(
                    side: BorderSide(color: Colors.grey, width: 6),
                    borderRadius: BorderRadius.all(Radius.circular(8)),
907 908 909 910 911 912 913
                  ),
                  borderOnForeground: false,
                  child: Column(
                    children: <Widget>[
                      Container(
                        color: Colors.green,
                        height: 150,
914
                      ),
915 916 917 918
                    ],
                  ),
                ),
              ),
919 920
            ),
          ),
921 922 923 924 925
        ),
      ));

      await expectLater(
        find.byKey(painterKey),
926
        matchesGoldenFile('material.border_paint_below.png'),
927
      );
928
    });
929
  });
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977

  testWidgets('InkFeature skips painting if intermediate node skips', (WidgetTester tester) async {
    final GlobalKey sizedBoxKey = GlobalKey();
    final GlobalKey materialKey = GlobalKey();
    await tester.pumpWidget(Material(
      key: materialKey,
      child: Offstage(
        child: SizedBox(key: sizedBoxKey, width: 20, height: 20),
      ),
    ));
    final MaterialInkController controller = Material.of(sizedBoxKey.currentContext!)!;

    final TrackPaintInkFeature tracker = TrackPaintInkFeature(
      controller: controller,
      referenceBox: sizedBoxKey.currentContext!.findRenderObject()! as RenderBox,
    );
    controller.addInkFeature(tracker);
    expect(tracker.paintCount, 0);

    // Force a repaint. Since it's offstage, the ink feture should not get painted.
    materialKey.currentContext!.findRenderObject()!.paint(PaintingContext(ContainerLayer(), Rect.largest), Offset.zero);
    expect(tracker.paintCount, 0);

    await tester.pumpWidget(Material(
      key: materialKey,
      child: Offstage(
        offstage: false,
        child: SizedBox(key: sizedBoxKey, width: 20, height: 20),
      ),
    ));
    // Gets a paint because the global keys have reused the elements and it is
    // now onstage.
    expect(tracker.paintCount, 1);

    // Force a repaint again. This time, it gets repainted because it is onstage.
    materialKey.currentContext!.findRenderObject()!.paint(PaintingContext(ContainerLayer(), Rect.largest), Offset.zero);
    expect(tracker.paintCount, 2);
  });
}

class TrackPaintInkFeature extends InkFeature {
  TrackPaintInkFeature({required super.controller, required super.referenceBox});

  int paintCount = 0;
  @override
  void paintFeature(Canvas canvas, Matrix4 transform) {
    paintCount += 1;
  }
978
}