circle_avatar_test.dart 9 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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 12
import '../painting/image_data.dart';

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 29
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
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 51 52 53 54
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    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 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)));
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    expect(decoration.image.fit, equals(BoxFit.cover));
  });

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 91
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
92
    expect(decoration.color, equals(fallback.primaryColorDark));
93 94 95 96 97

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

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 115 116
          ),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
117 118 119 120 121 122 123
    expect(decoration.color, equals(theme.primaryColorLight));

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

  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 140 141 142
          ),
        ),
      ),
    );

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

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
    expect(paragraph.text.style.color, equals(theme.primaryTextTheme.title.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 172 173 174 175 176 177 178 179 180 181 182 183
              builder: (BuildContext context) {
                final MediaQueryData data = MediaQuery.of(context);

                // 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
  }, skip: isBrowser);
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 208 209 210 211
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    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 230 231 232 233
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    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 253 254 255 256
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    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({ 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
  );
}