circle_avatar_test.dart 11.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 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
import 'dart:typed_data';

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

14
import '../image_data.dart';
15
import '../painting/mocks_for_image_cache.dart';
16

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

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

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

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

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

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

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

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

79 80 81 82 83 84 85 86 87 88 89 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 116 117 118 119 120 121 122 123
  testWidgets('CircleAvatar with image foreground', (WidgetTester tester) async {
    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));
  });

  testWidgets('CircleAvatar backgroundImage is used as a fallback for foregroundImage', (WidgetTester tester) async {
    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'),
    );
  });

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

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

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

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

147
  testWidgets('CircleAvatar with light theme', (WidgetTester tester) async {
148
    final ThemeData theme = ThemeData(
149 150 151 152
      primaryColor: Colors.grey.shade100,
      primaryColorBrightness: Brightness.light,
    );
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
153
      wrap(
154
        child: Theme(
Ian Hickson's avatar
Ian Hickson committed
155
          data: theme,
156
          child: const CircleAvatar(
157
            child: Text('Z'),
158 159 160 161 162 163
          ),
        ),
      ),
    );

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

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

  testWidgets('CircleAvatar with dark theme', (WidgetTester tester) async {
173
    final ThemeData theme = ThemeData(
174 175 176 177 178
      primaryColor: Colors.grey.shade800,
      primaryColorBrightness: Brightness.dark,
    );
    await tester.pumpWidget(
      wrap(
179
        child: Theme(
180 181
          data: theme,
          child: const CircleAvatar(
182
            child: Text('Z'),
183 184 185 186 187 188
          ),
        ),
      ),
    );

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

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
194
    expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.titleLarge!.color));
195
  });
196 197 198 199 200

  testWidgets('CircleAvatar text does not expand with textScaleFactor', (WidgetTester tester) async {
    final Color foregroundColor = Colors.red.shade100;
    await tester.pumpWidget(
      wrap(
201
        child: CircleAvatar(
202 203 204 205 206 207
          foregroundColor: foregroundColor,
          child: const Text('Z'),
        ),
      ),
    );

208
    expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
209 210 211

    await tester.pumpWidget(
      wrap(
212
        child: MediaQuery(
213 214
          data: const MediaQueryData(
            textScaleFactor: 2.0,
215
            size: Size(111.0, 111.0),
216
            devicePixelRatio: 1.1,
217 218
            padding: EdgeInsets.all(11.0),
          ),
219 220
          child: CircleAvatar(
            child: Builder(
221
              builder: (BuildContext context) {
222
                final MediaQueryData data = MediaQuery.of(context);
223 224 225 226 227 228 229 230 231

                // 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');
232
              },
233
            ),
234 235 236 237
          ),
        ),
      ),
    );
238
    expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
239
  });
240 241 242 243 244

  testWidgets('CircleAvatar respects minRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
245 246
        child: UnconstrainedBox(
          child: CircleAvatar(
247 248 249 250 251 252 253 254 255
            backgroundColor: backgroundColor,
            minRadius: 50.0,
            child: const Text('Z'),
          ),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
256
    expect(box.size, equals(const Size(100.0, 100.0)));
257
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
258
    final BoxDecoration decoration = child.decoration as BoxDecoration;
259 260 261
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
262
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
263 264 265 266 267 268
  });

  testWidgets('CircleAvatar respects maxRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
269
        child: CircleAvatar(
270 271 272 273 274 275 276 277
          backgroundColor: backgroundColor,
          maxRadius: 50.0,
          child: const Text('Z'),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
278
    expect(box.size, equals(const Size(100.0, 100.0)));
279
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
280
    final BoxDecoration decoration = child.decoration as BoxDecoration;
281 282 283
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
284
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
285 286 287 288 289 290
  });

  testWidgets('CircleAvatar respects setting both minRadius and maxRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
291
        child: CircleAvatar(
292 293 294 295 296 297 298 299 300
          backgroundColor: backgroundColor,
          maxRadius: 50.0,
          minRadius: 50.0,
          child: const Text('Z'),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
301
    expect(box.size, equals(const Size(100.0, 100.0)));
302
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
303
    final BoxDecoration decoration = child.decoration as BoxDecoration;
304 305 306
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
307
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
308
  });
309
}
Ian Hickson's avatar
Ian Hickson committed
310

311
Widget wrap({ required Widget child }) {
312
  return Directionality(
Ian Hickson's avatar
Ian Hickson committed
313
    textDirection: TextDirection.ltr,
314
    child: MediaQuery(
315
      data: const MediaQueryData(),
316
      child: Center(child: child),
317
    ),
Ian Hickson's avatar
Ian Hickson committed
318 319
  );
}