circle_avatar_test.dart 9.35 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

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

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

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

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

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

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

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

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

75 76 77
  testWidgets('CircleAvatar with foreground color', (WidgetTester tester) async {
    final Color foregroundColor = Colors.red.shade100;
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
78
      wrap(
79
        child: CircleAvatar(
80
          foregroundColor: foregroundColor,
81
          child: const Text('Z'),
82 83 84 85
        ),
      ),
    );

86
    final ThemeData fallback = ThemeData.fallback();
87 88

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
89
    expect(box.size, equals(const Size(40.0, 40.0)));
90
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
91
    final BoxDecoration decoration = child.decoration as BoxDecoration;
92
    expect(decoration.color, equals(fallback.primaryColorDark));
93 94

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
95
    expect(paragraph.text.style!.color, equals(foregroundColor));
96 97
  });

98
  testWidgets('CircleAvatar with light theme', (WidgetTester tester) async {
99
    final ThemeData theme = ThemeData(
100 101 102 103
      primaryColor: Colors.grey.shade100,
      primaryColorBrightness: Brightness.light,
    );
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
104
      wrap(
105
        child: Theme(
Ian Hickson's avatar
Ian Hickson committed
106
          data: theme,
107
          child: const CircleAvatar(
108
            child: Text('Z'),
109 110 111 112 113 114
          ),
        ),
      ),
    );

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

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
120
    expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.headline6!.color));
121 122 123
  });

  testWidgets('CircleAvatar with dark theme', (WidgetTester tester) async {
124
    final ThemeData theme = ThemeData(
125 126 127 128 129
      primaryColor: Colors.grey.shade800,
      primaryColorBrightness: Brightness.dark,
    );
    await tester.pumpWidget(
      wrap(
130
        child: Theme(
131 132
          data: theme,
          child: const CircleAvatar(
133
            child: Text('Z'),
134 135 136 137 138 139
          ),
        ),
      ),
    );

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

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
145
    expect(paragraph.text.style!.color, equals(theme.primaryTextTheme.headline6!.color));
146
  });
147 148 149 150 151

  testWidgets('CircleAvatar text does not expand with textScaleFactor', (WidgetTester tester) async {
    final Color foregroundColor = Colors.red.shade100;
    await tester.pumpWidget(
      wrap(
152
        child: CircleAvatar(
153 154 155 156 157 158
          foregroundColor: foregroundColor,
          child: const Text('Z'),
        ),
      ),
    );

159
    expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
160 161 162

    await tester.pumpWidget(
      wrap(
163
        child: MediaQuery(
164 165
          data: const MediaQueryData(
            textScaleFactor: 2.0,
166
            size: Size(111.0, 111.0),
167
            devicePixelRatio: 1.1,
168
            padding: EdgeInsets.all(11.0)),
169 170
          child: CircleAvatar(
            child: Builder(
171
              builder: (BuildContext context) {
172
                final MediaQueryData data = MediaQuery.of(context);
173 174 175 176 177 178 179 180 181 182 183

                // 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');
              }
            ),
184 185 186 187
          ),
        ),
      ),
    );
188
    expect(tester.getSize(find.text('Z')), equals(const Size(16.0, 16.0)));
189
  });
190 191 192 193 194

  testWidgets('CircleAvatar respects minRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
195 196
        child: UnconstrainedBox(
          child: CircleAvatar(
197 198 199 200 201 202 203 204 205
            backgroundColor: backgroundColor,
            minRadius: 50.0,
            child: const Text('Z'),
          ),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
206
    expect(box.size, equals(const Size(100.0, 100.0)));
207
    final RenderDecoratedBox child = box.child! as RenderDecoratedBox;
208
    final BoxDecoration decoration = child.decoration as BoxDecoration;
209 210 211
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
212
    expect(paragraph.text.style!.color, equals(ThemeData.fallback().primaryColorLight));
213 214 215 216 217 218
  });

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

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

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

  testWidgets('CircleAvatar respects setting both minRadius and maxRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
241
        child: CircleAvatar(
242 243 244 245 246 247 248 249 250
          backgroundColor: backgroundColor,
          maxRadius: 50.0,
          minRadius: 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
}
Ian Hickson's avatar
Ian Hickson committed
260

261
Widget wrap({ required Widget child }) {
262
  return Directionality(
Ian Hickson's avatar
Ian Hickson committed
263
    textDirection: TextDirection.ltr,
264
    child: MediaQuery(
265
      data: const MediaQueryData(),
266
      child: Center(child: child),
267
    ),
Ian Hickson's avatar
Ian Hickson committed
268 269
  );
}