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

10 11
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
12
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
13 14 15 16 17 18 19

void main() {
  test('CardTheme copyWith, ==, hashCode basics', () {
    expect(const CardTheme(), const CardTheme().copyWith());
    expect(const CardTheme().hashCode, const CardTheme().copyWith().hashCode);
  });

20 21 22 23 24 25
  test('CardTheme lerp special cases', () {
    expect(CardTheme.lerp(null, null, 0), const CardTheme());
    const CardTheme theme = CardTheme();
    expect(identical(CardTheme.lerp(theme, theme, 0.5), theme), true);
  });

26
  testWidgetsWithLeakTracking('Material3 - Passing no CardTheme returns defaults', (WidgetTester tester) async {
27
    final ThemeData theme = ThemeData(useMaterial3: true);
28
    await tester.pumpWidget(MaterialApp(
29
      theme: theme,
30
      home: const Scaffold(
31
        body: Card(),
32 33 34 35 36 37 38
      ),
    ));

    final Container container = _getCardContainer(tester);
    final Material material = _getCardMaterial(tester);

    expect(material.clipBehavior, Clip.none);
39 40 41
    expect(material.color, theme.colorScheme.surface);
    expect(material.shadowColor, theme.colorScheme.shadow);
    expect(material.surfaceTintColor, theme.colorScheme.surfaceTint); // Default primary color
42 43 44
    expect(material.elevation, 1.0);
    expect(container.margin, const EdgeInsets.all(4.0));
    expect(material.shape, const RoundedRectangleBorder(
45
      borderRadius: BorderRadius.all(Radius.circular(12.0)),
46 47 48
    ));
  });

49
  testWidgetsWithLeakTracking('Card uses values from CardTheme', (WidgetTester tester) async {
50 51 52 53 54
    final CardTheme cardTheme = _cardTheme();

    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(cardTheme: cardTheme),
      home: const Scaffold(
55
        body: Card(),
56 57 58 59 60 61 62 63
      ),
    ));

    final Container container = _getCardContainer(tester);
    final Material material = _getCardMaterial(tester);

    expect(material.clipBehavior, cardTheme.clipBehavior);
    expect(material.color, cardTheme.color);
64
    expect(material.shadowColor, cardTheme.shadowColor);
65
    expect(material.surfaceTintColor, cardTheme.surfaceTintColor);
66 67 68 69 70
    expect(material.elevation, cardTheme.elevation);
    expect(container.margin, cardTheme.margin);
    expect(material.shape, cardTheme.shape);
  });

71
  testWidgetsWithLeakTracking('Card widget properties take priority over theme', (WidgetTester tester) async {
72 73
    const Clip clip = Clip.hardEdge;
    const Color color = Colors.orange;
74
    const Color shadowColor = Colors.pink;
75 76 77 78 79 80 81 82 83 84 85 86
    const double elevation = 7.0;
    const EdgeInsets margin = EdgeInsets.all(3.0);
    const ShapeBorder shape = RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(9.0)),
    );

    await tester.pumpWidget(MaterialApp(
      theme: _themeData().copyWith(cardTheme: _cardTheme()),
      home: const Scaffold(
        body: Card(
          clipBehavior: clip,
          color: color,
87
          shadowColor: shadowColor,
88 89 90
          elevation: elevation,
          margin: margin,
          shape: shape,
91
        ),
92 93 94 95 96 97 98 99
      ),
    ));

    final Container container = _getCardContainer(tester);
    final Material material = _getCardMaterial(tester);

    expect(material.clipBehavior, clip);
    expect(material.color, color);
100
    expect(material.shadowColor, shadowColor);
101 102 103 104 105
    expect(material.elevation, elevation);
    expect(container.margin, margin);
    expect(material.shape, shape);
  });

106
  testWidgetsWithLeakTracking('CardTheme properties take priority over ThemeData properties', (WidgetTester tester) async {
107 108 109 110 111 112
    final CardTheme cardTheme = _cardTheme();
    final ThemeData themeData = _themeData().copyWith(cardTheme: cardTheme);

    await tester.pumpWidget(MaterialApp(
      theme: themeData,
      home: const Scaffold(
113
        body: Card(),
114 115 116 117 118 119 120
      ),
    ));

    final Material material = _getCardMaterial(tester);
    expect(material.color, cardTheme.color);
  });

121
  testWidgetsWithLeakTracking('Material3 - ThemeData properties are used when no CardTheme is set', (WidgetTester tester) async {
122
    final ThemeData themeData = ThemeData(useMaterial3: true);
123 124 125 126

    await tester.pumpWidget(MaterialApp(
      theme: themeData,
      home: const Scaffold(
127
        body: Card(),
128 129 130 131
      ),
    ));

    final Material material = _getCardMaterial(tester);
132
    expect(material.color, themeData.colorScheme.surface);
133 134
  });

135
  testWidgetsWithLeakTracking('Material3 - CardTheme customizes shape', (WidgetTester tester) async {
136 137 138 139 140 141 142 143 144
    const CardTheme cardTheme = CardTheme(
      color: Colors.white,
      shape: BeveledRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(7))),
      elevation: 1.0,
    );

    final Key painterKey = UniqueKey();

    await tester.pumpWidget(MaterialApp(
145
      theme: ThemeData(cardTheme: cardTheme, useMaterial3: true),
146 147 148 149 150
      home: Scaffold(
        body: RepaintBoundary(
          key: painterKey,
          child: Center(
            child: Card(
151
              child: SizedBox.fromSize(size: const Size(200, 300)),
152 153 154
            ),
          ),
        ),
155 156 157 158 159
      ),
    ));

    await expectLater(
      find.byKey(painterKey),
160
      matchesGoldenFile('card_theme.custom_shape.png'),
161
    );
162
  });
163 164

  group('Material 2', () {
165 166 167
    // These tests are only relevant for Material 2. Once Material 2
    // support is deprecated and the APIs are removed, these tests
    // can be deleted.
168

169
    testWidgetsWithLeakTracking('Material2 - ThemeData properties are used when no CardTheme is set', (WidgetTester tester) async {
170 171 172 173 174 175 176 177 178 179 180 181 182
      final ThemeData themeData = ThemeData(useMaterial3: false);

      await tester.pumpWidget(MaterialApp(
        theme: themeData,
        home: const Scaffold(
          body: Card(),
        ),
      ));

      final Material material = _getCardMaterial(tester);
      expect(material.color, themeData.cardColor);
    });

183
    testWidgetsWithLeakTracking('Material2 - Passing no CardTheme returns defaults', (WidgetTester tester) async {
184 185 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(useMaterial3: false),
        home: const Scaffold(
          body: Card(),
        ),
      ));

      final Container container = _getCardContainer(tester);
      final Material material = _getCardMaterial(tester);

      expect(material.clipBehavior, Clip.none);
      expect(material.color, Colors.white);
      expect(material.shadowColor, Colors.black);
      expect(material.surfaceTintColor, null);
      expect(material.elevation, 1.0);
      expect(container.margin, const EdgeInsets.all(4.0));
      expect(material.shape, const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(4.0)),
      ));
    });

205
    testWidgetsWithLeakTracking('Material2 - CardTheme customizes shape', (WidgetTester tester) async {
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
      const CardTheme cardTheme = CardTheme(
        color: Colors.white,
        shape: BeveledRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(7))),
        elevation: 1.0,
      );

      final Key painterKey = UniqueKey();

      await tester.pumpWidget(MaterialApp(
        theme: ThemeData(cardTheme: cardTheme, useMaterial3: false),
        home: Scaffold(
          body: RepaintBoundary(
            key: painterKey,
            child: Center(
              child: Card(
                child: SizedBox.fromSize(size: const Size(200, 300)),
              ),
            ),
          ),
        ),
      ));

      await expectLater(
        find.byKey(painterKey),
        matchesGoldenFile('card_theme.custom_shape_m2.png'),
      );
    });
  });
234 235 236 237 238 239
}

CardTheme _cardTheme() {
  return const CardTheme(
    clipBehavior: Clip.antiAlias,
    color: Colors.green,
240
    shadowColor: Colors.red,
241
    surfaceTintColor: Colors.purple,
242 243 244 245
    elevation: 6.0,
    margin: EdgeInsets.all(7.0),
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.all(Radius.circular(5.0)),
246
    ),
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  );
}

ThemeData _themeData() {
  return ThemeData(
    cardColor: Colors.pink,
  );
}

Material _getCardMaterial(WidgetTester tester) {
  return tester.widget<Material>(
    find.descendant(
      of: find.byType(Card),
      matching: find.byType(Material),
    ),
  );
}

Container _getCardContainer(WidgetTester tester) {
  return tester.widget<Container>(
    find.descendant(
      of: find.byType(Card),
      matching: find.byType(Container),
    ),
  );
}