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

7
import 'package:flutter/material.dart';
8
import 'package:flutter/rendering.dart';
9 10
import 'package:flutter_test/flutter_test.dart';

11
import '../image_data.dart';
12
import '../painting/mocks_for_image_cache.dart';
13

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

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

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

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

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

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

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

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

76 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
  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'),
    );
  });

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

132
    final ThemeData fallback = ThemeData.fallback();
133 134

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

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
141
    expect(paragraph.text.style!.color, equals(foregroundColor));
142 143
  });

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

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

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

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

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

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
191
    expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.headline6!.color));
192
  });
193 194 195 196 197

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

205
    expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
206 207 208

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

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

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

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

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

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

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

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

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

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

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

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