circle_avatar_test.dart 9.09 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: new 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(new ThemeData.fallback().primaryColorLight));
34 35
  });

36 37 38 39 40 41 42 43 44 45 46 47 48
  testWidgets('CircleAvatar with light background color', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade100;
    await tester.pumpWidget(
      wrap(
        child: new CircleAvatar(
          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(new ThemeData.fallback().primaryColorDark));
56 57
  });

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  testWidgets('CircleAvatar with image background', (WidgetTester tester) async {
    await tester.pumpWidget(
      wrap(
        child: new CircleAvatar(
          backgroundImage: new MemoryImage(new Uint8List.fromList(kTransparentImage)),
          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 80
        child: new CircleAvatar(
          foregroundColor: foregroundColor,
81
          child: const Text('Z'),
82 83 84 85 86 87 88
        ),
      ),
    );

    final ThemeData fallback = new ThemeData.fallback();

    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 100 101 102 103
    final ThemeData theme = new ThemeData(
      primaryColor: Colors.grey.shade100,
      primaryColorBrightness: Brightness.light,
    );
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
104 105 106
      wrap(
        child: new Theme(
          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 124 125 126 127 128 129 130 131 132
    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 {
    final ThemeData theme = new ThemeData(
      primaryColor: Colors.grey.shade800,
      primaryColorBrightness: Brightness.dark,
    );
    await tester.pumpWidget(
      wrap(
        child: new Theme(
          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 152 153 154 155 156 157 158

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

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

    await tester.pumpWidget(
      wrap(
        child: new 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
          child: new CircleAvatar(
170 171 172 173 174 175 176 177 178 179 180 181 182 183
            child: new Builder(
              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
  });
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

  testWidgets('CircleAvatar respects minRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
        child: new UnconstrainedBox(
          child: new CircleAvatar(
            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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    expect(decoration.color, equals(backgroundColor));

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

  testWidgets('CircleAvatar respects maxRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
        child: new CircleAvatar(
          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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    expect(decoration.color, equals(backgroundColor));

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

  testWidgets('CircleAvatar respects setting both minRadius and maxRadius', (WidgetTester tester) async {
    final Color backgroundColor = Colors.blue.shade900;
    await tester.pumpWidget(
      wrap(
        child: new CircleAvatar(
          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 257 258
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
    expect(decoration.color, equals(backgroundColor));

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
    expect(paragraph.text.style.color, equals(new ThemeData.fallback().primaryColorLight));
  });
259
}
Ian Hickson's avatar
Ian Hickson committed
260 261 262 263

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