circle_avatar_test.dart 12.3 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 9
library;

10 11
import 'dart:typed_data';

12
import 'package:flutter/material.dart';
13
import 'package:flutter/rendering.dart';
14
import 'package:flutter_test/flutter_test.dart';
15
import '../image_data.dart';
16
import '../painting/mocks_for_image_cache.dart';
17

18
void main() {
19
  testWidgets('CircleAvatar with dark background color', (WidgetTester tester) async {
20
    final Color backgroundColor = Colors.blue.shade900;
21
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
22
      wrap(
23
        child: CircleAvatar(
24
          backgroundColor: backgroundColor,
25
          radius: 50.0,
26
          child: const Text('Z'),
27 28
        ),
      ),
29 30
    );

31
    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
32
    expect(box.size, equals(const Size(100.0, 100.0)));
33
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
34
    final BoxDecoration decoration = child.decoration as BoxDecoration;
35
    expect(decoration.color, equals(backgroundColor));
36

37
    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
38
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
39 40
  });

41
  testWidgets('CircleAvatar with light background color', (WidgetTester tester) async {
42 43 44
    final Color backgroundColor = Colors.blue.shade100;
    await tester.pumpWidget(
      wrap(
45
        child: CircleAvatar(
46 47 48 49 50 51 52 53
          backgroundColor: backgroundColor,
          radius: 50.0,
          child: const Text('Z'),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
54
    expect(box.size, equals(const Size(100.0, 100.0)));
55
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
56
    final BoxDecoration decoration = child.decoration as BoxDecoration;
57 58 59
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
60
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorDark));
61 62
  });

63
  testWidgets('CircleAvatar with image background', (WidgetTester tester) async {
64 65
    await tester.pumpWidget(
      wrap(
66 67
        child: CircleAvatar(
          backgroundImage: MemoryImage(Uint8List.fromList(kTransparentImage)),
68 69 70 71 72 73 74
          radius: 50.0,
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
    expect(box.size, equals(const Size(100.0, 100.0)));
75
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
76
    final BoxDecoration decoration = child.decoration as BoxDecoration;
77
    expect(decoration.image!.fit, equals(BoxFit.cover));
78 79
  });

80
  testWidgets('CircleAvatar with image foreground', (WidgetTester tester) async {
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    await tester.pumpWidget(
      wrap(
        child: CircleAvatar(
          foregroundImage: MemoryImage(Uint8List.fromList(kBlueRectPng)),
          radius: 50.0,
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
    expect(box.size, equals(const Size(100.0, 100.0)));
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
    final BoxDecoration decoration = child.decoration as BoxDecoration;
    expect(decoration.image!.fit, equals(BoxFit.cover));
  });

97
  testWidgets('CircleAvatar backgroundImage is used as a fallback for foregroundImage', (WidgetTester tester) async {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    final ErrorImageProvider errorImage = ErrorImageProvider();
    bool caughtForegroundImageError = false;
    await tester.pumpWidget(
      wrap(
        child: RepaintBoundary(
          child: CircleAvatar(
          foregroundImage: errorImage,
          backgroundImage: MemoryImage(Uint8List.fromList(kBlueRectPng)),
          radius: 50.0,
          onForegroundImageError: (_,__) => caughtForegroundImageError = true,
          ),
        ),
      ),
    );

    expect(caughtForegroundImageError, true);
    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
    expect(box.size, equals(const Size(100.0, 100.0)));
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
    final BoxDecoration decoration = child.decoration as BoxDecoration;
    expect(decoration.image!.fit, equals(BoxFit.cover));
    await expectLater(
      find.byType(CircleAvatar),
      matchesGoldenFile('circle_avatar.fallback.png'),
    );
  });

125
  testWidgets('CircleAvatar with foreground color', (WidgetTester tester) async {
126 127
    final Color foregroundColor = Colors.red.shade100;
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
128
      wrap(
129
        child: CircleAvatar(
130
          foregroundColor: foregroundColor,
131
          child: const Text('Z'),
132 133 134 135
        ),
      ),
    );

136
    final ThemeData fallback = ThemeData.fallback();
137 138

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
139
    expect(box.size, equals(const Size(40.0, 40.0)));
140
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
141
    final BoxDecoration decoration = child.decoration as BoxDecoration;
142
    expect(decoration.color, equals(fallback.primaryColorDark));
143 144

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
145
    expect(paragraph.text.style!.color, equals(foregroundColor));
146 147
  });

148
  testWidgets('Material3 - CircleAvatar default colors', (WidgetTester tester) async {
149
    final ThemeData theme = ThemeData();
150 151
    await tester.pumpWidget(
      wrap(
152
        child: Theme(
153 154
          data: theme,
          child: const CircleAvatar(
155
            child: Text('Z'),
156 157 158 159 160 161
          ),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
162
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
163
    final BoxDecoration decoration = child.decoration as BoxDecoration;
164
    expect(decoration.color, equals(theme.colorScheme.primaryContainer));
165 166

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
167
    expect(paragraph.text.style!.color, equals(theme.colorScheme.onPrimaryContainer));
168
  });
169

170
  testWidgets('CircleAvatar text does not expand with textScaleFactor', (WidgetTester tester) async {
171 172 173
    final Color foregroundColor = Colors.red.shade100;
    await tester.pumpWidget(
      wrap(
174
        child: CircleAvatar(
175 176 177 178 179 180
          foregroundColor: foregroundColor,
          child: const Text('Z'),
        ),
      ),
    );

181
    expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
182 183 184

    await tester.pumpWidget(
      wrap(
185
        child: MediaQuery(
186 187
          data: const MediaQueryData(
            textScaleFactor: 2.0,
188
            size: Size(111.0, 111.0),
189
            devicePixelRatio: 1.1,
190 191
            padding: EdgeInsets.all(11.0),
          ),
192 193
          child: CircleAvatar(
            child: Builder(
194
              builder: (BuildContext context) {
195
                final MediaQueryData data = MediaQuery.of(context);
196 197 198 199 200 201 202 203 204

                // These should not change.
                expect(data.size, equals(const Size(111.0, 111.0)));
                expect(data.devicePixelRatio, equals(1.1));
                expect(data.padding, equals(const EdgeInsets.all(11.0)));

                // This should be overridden to 1.0.
                expect(data.textScaleFactor, equals(1.0));
                return const Text('Z');
205
              },
206
            ),
207 208 209 210
          ),
        ),
      ),
    );
211
    expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
212
  });
213

214
  testWidgets('CircleAvatar respects minRadius', (WidgetTester tester) async {
215 216 217
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
218 219
        child: UnconstrainedBox(
          child: CircleAvatar(
220 221 222 223 224 225 226 227 228
            backgroundColor: backgroundColor,
            minRadius: 50.0,
            child: const Text('Z'),
          ),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
229
    expect(box.size, equals(const Size(100.0, 100.0)));
230
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
231
    final BoxDecoration decoration = child.decoration as BoxDecoration;
232 233 234
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
235
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
236 237
  });

238
  testWidgets('CircleAvatar respects maxRadius', (WidgetTester tester) async {
239 240 241
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
242
        child: CircleAvatar(
243 244 245 246 247 248 249 250
          backgroundColor: backgroundColor,
          maxRadius: 50.0,
          child: const Text('Z'),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
251
    expect(box.size, equals(const Size(100.0, 100.0)));
252
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
253
    final BoxDecoration decoration = child.decoration as BoxDecoration;
254 255 256
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
257
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
258 259
  });

260
  testWidgets('CircleAvatar respects setting both minRadius and maxRadius', (WidgetTester tester) async {
261 262 263
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
264
        child: CircleAvatar(
265 266 267 268 269 270 271 272 273
          backgroundColor: backgroundColor,
          maxRadius: 50.0,
          minRadius: 50.0,
          child: const Text('Z'),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
274
    expect(box.size, equals(const Size(100.0, 100.0)));
275
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
276
    final BoxDecoration decoration = child.decoration as BoxDecoration;
277 278 279
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
280
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
281
  });
282 283

  group('Material 2', () {
284 285 286
    // These tests are only relevant for Material 2. Once Material 2
    // support is deprecated and the APIs are removed, these tests
    // can be deleted.
287

288
    testWidgets('Material2 - CircleAvatar default colors with light theme', (WidgetTester tester) async {
289
      final ThemeData theme = ThemeData(useMaterial3: false, primaryColor: Colors.grey.shade100);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
      await tester.pumpWidget(
        wrap(
          child: Theme(
            data: theme,
            child: const CircleAvatar(
              child: Text('Z'),
            ),
          ),
        ),
      );

      final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
      final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
      final BoxDecoration decoration = child.decoration as BoxDecoration;
      expect(decoration.color, equals(theme.primaryColorLight));

      final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
      expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.titleLarge!.color));
    });

310
    testWidgets('Material2 - CircleAvatar default colors with dark theme', (WidgetTester tester) async {
311
      final ThemeData theme = ThemeData(useMaterial3: false, primaryColor: Colors.grey.shade800);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
      await tester.pumpWidget(
        wrap(
          child: Theme(
            data: theme,
            child: const CircleAvatar(
              child: Text('Z'),
            ),
          ),
        ),
      );

      final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
      final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
      final BoxDecoration decoration = child.decoration as BoxDecoration;
      expect(decoration.color, equals(theme.primaryColorDark));

      final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
      expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.titleLarge!.color));
    });
  });
332
}
Ian Hickson's avatar
Ian Hickson committed
333

334
Widget wrap({ required Widget child }) {
335
  return Directionality(
Ian Hickson's avatar
Ian Hickson committed
336
    textDirection: TextDirection.ltr,
337
    child: MediaQuery(
338
      data: const MediaQueryData(),
339 340 341
      child: MaterialApp(
        theme: ThemeData(useMaterial3: false),
        home: Center(child: child)),
342
    ),
Ian Hickson's avatar
Ian Hickson committed
343 344
  );
}