circle_avatar_test.dart 5.16 KB
Newer Older
1 2 3 4 5
// 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.

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

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

22
    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
23 24
    expect(box.size.width, equals(100.0));
    expect(box.size.height, equals(100.0));
25 26
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
27
    expect(decoration.color, equals(backgroundColor));
28

29 30 31 32
    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
    expect(paragraph.text.style.color, equals(Colors.white));
  });

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  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));
    expect(box.size.width, equals(100.0));
    expect(box.size.height, equals(100.0));
    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(Colors.black));
  });

56 57 58
  testWidgets('CircleAvatar with foreground color', (WidgetTester tester) async {
    final Color foregroundColor = Colors.red.shade100;
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
59
      wrap(
60 61
        child: new CircleAvatar(
          foregroundColor: foregroundColor,
62
          child: const Text('Z'),
63 64 65 66 67 68 69 70 71 72 73
        ),
      ),
    );

    final ThemeData fallback = new ThemeData.fallback();

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
    expect(box.size.width, equals(40.0));
    expect(box.size.height, equals(40.0));
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
74
    expect(decoration.color, equals(fallback.primaryColor));
75 76 77 78 79 80 81 82 83 84 85

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

  testWidgets('CircleAvatar with theme', (WidgetTester tester) async {
    final ThemeData theme = new ThemeData(
      primaryColor: Colors.grey.shade100,
      primaryColorBrightness: Brightness.light,
    );
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
86 87 88
      wrap(
        child: new Theme(
          data: theme,
89
          child: const CircleAvatar(
90
            child: const Text('Z'),
91 92 93 94 95 96 97 98
          ),
        ),
      ),
    );

    final RenderConstrainedBox box = tester.renderObject(find.byType(CircleAvatar));
    final RenderDecoratedBox child = box.child;
    final BoxDecoration decoration = child.decoration;
99
    expect(decoration.color, equals(theme.primaryColor));
100 101 102

    final RenderParagraph paragraph = tester.renderObject(find.text('Z'));
    expect(paragraph.text.style.color, equals(theme.primaryTextTheme.title.color));
103
  });
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

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

    expect(tester.getSize(find.text('Z')), equals(const Size(20.0, 20.0)));

    await tester.pumpWidget(
      wrap(
        child: new MediaQuery(
121 122 123 124 125
          data: const MediaQueryData(
            textScaleFactor: 2.0,
            size: const Size(111.0, 111.0),
            devicePixelRatio: 1.1,
            padding: const EdgeInsets.all(11.0)),
126
          child: new CircleAvatar(
127 128 129 130 131 132 133 134 135 136 137 138 139 140
            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');
              }
            ),
141 142 143 144 145 146
          ),
        ),
      ),
    );
    expect(tester.getSize(find.text('Z')), equals(const Size(20.0, 20.0)));
  });
147
}
Ian Hickson's avatar
Ian Hickson committed
148 149 150 151

Widget wrap({ Widget child }) {
  return new Directionality(
    textDirection: TextDirection.ltr,
152 153 154 155
    child: new MediaQuery(
      data: const MediaQueryData(),
      child: new Center(child: child),
    ),
Ian Hickson's avatar
Ian Hickson committed
156 157
  );
}