user_accounts_drawer_header_test.dart 5.91 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
10
  testWidgets('UserAccountsDrawerHeader test', (WidgetTester tester) async {
11 12 13
    final Key avatarA = const Key('A');
    final Key avatarC = const Key('C');
    final Key avatarD = const Key('D');
14 15

    await tester.pumpWidget(
16 17 18 19 20 21 22
      new MaterialApp(
        home: new Material(
          child: new Center(
            child: new UserAccountsDrawerHeader(
              currentAccountPicture: new CircleAvatar(
                key: avatarA,
                child: const Text('A'),
23
              ),
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
              otherAccountsPictures: <Widget>[
                const CircleAvatar(
                  child: const Text('B'),
                ),
                new CircleAvatar(
                  key: avatarC,
                  child: const Text('C'),
                ),
                new CircleAvatar(
                  key: avatarD,
                  child: const Text('D'),
                ),
                const CircleAvatar(
                  child: const Text('E'),
                )
              ],
              accountName: const Text("name"),
              accountEmail: const Text("email"),
            ),
43 44 45
          ),
        ),
      ),
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    );

    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsOneWidget);
    expect(find.text('D'), findsOneWidget);
    expect(find.text('E'), findsNothing);

    expect(find.text('name'), findsOneWidget);
    expect(find.text('email'), findsOneWidget);

    RenderBox box = tester.renderObject(find.byKey(avatarA));
    expect(box.size.width, equals(72.0));
    expect(box.size.height, equals(72.0));

    box = tester.renderObject(find.byKey(avatarC));
    expect(box.size.width, equals(40.0));
    expect(box.size.height, equals(40.0));

65 66 67
    box = tester.renderObject(find.byType(UserAccountsDrawerHeader));
    expect(box.size.height, equals(160.0 + 8.0 + 1.0)); // height + bottom margin + bottom edge)

68 69 70 71 72 73 74 75 76 77 78 79
    final Offset topLeft = tester.getTopLeft(find.byType(UserAccountsDrawerHeader));
    final Offset topRight = tester.getTopRight(find.byType(UserAccountsDrawerHeader));

    final Offset avatarATopLeft = tester.getTopLeft(find.byKey(avatarA));
    final Offset avatarDTopRight = tester.getTopRight(find.byKey(avatarD));
    final Offset avatarCTopRight = tester.getTopRight(find.byKey(avatarC));

    expect(avatarATopLeft.dx - topLeft.dx, equals(16.0));
    expect(avatarATopLeft.dy - topLeft.dy, equals(16.0));
    expect(topRight.dx - avatarDTopRight.dx, equals(16.0));
    expect(avatarDTopRight.dy - topRight.dy, equals(16.0));
    expect(avatarDTopRight.dx - avatarCTopRight.dx, equals(40.0 + 16.0)); // size + space between
80
  });
81 82 83 84 85 86 87 88 89


  testWidgets('UserAccountsDrawerHeader null parameters', (WidgetTester tester) async {
    Widget buildFrame({
      Widget currentAccountPicture,
      List<Widget> otherAccountsPictures,
      Widget accountName,
      Widget accountEmail,
      VoidCallback onDetailsPressed,
90
      EdgeInsets margin,
91
    }) {
92 93 94 95 96 97 98 99 100 101 102
      return new MaterialApp(
        home: new Material(
          child: new Center(
            child: new UserAccountsDrawerHeader(
              currentAccountPicture: currentAccountPicture,
              otherAccountsPictures: otherAccountsPictures,
              accountName: accountName,
              accountEmail: accountEmail,
              onDetailsPressed: onDetailsPressed,
              margin: margin,
            ),
103
          ),
104 105 106 107 108
        ),
      );
    }

    await tester.pumpWidget(buildFrame());
109 110
    final RenderBox box = tester.renderObject(find.byType(UserAccountsDrawerHeader));
    expect(box.size.height, equals(160.0 + 1.0)); // height + bottom edge)
111 112 113 114 115 116 117 118
    expect(find.byType(Icon), findsNothing);

    await tester.pumpWidget(buildFrame(
      onDetailsPressed: () { },
    ));
    expect(find.byType(Icon), findsOneWidget);

    await tester.pumpWidget(buildFrame(
119
      accountName: const Text('accountName'),
120 121 122
      onDetailsPressed: () { },
    ));
    expect(
123 124
      tester.getCenter(find.text('accountName')).dy,
      tester.getCenter(find.byType(Icon)).dy
125 126 127
    );

    await tester.pumpWidget(buildFrame(
128
      accountEmail: const Text('accountEmail'),
129 130 131
      onDetailsPressed: () { },
    ));
    expect(
132 133
      tester.getCenter(find.text('accountEmail')).dy,
      tester.getCenter(find.byType(Icon)).dy
134 135 136
    );

    await tester.pumpWidget(buildFrame(
137 138
      accountName: const Text('accountName'),
      accountEmail: const Text('accountEmail'),
139 140 141
      onDetailsPressed: () { },
    ));
    expect(
142 143
      tester.getCenter(find.text('accountEmail')).dy,
      tester.getCenter(find.byType(Icon)).dy
144 145
    );
    expect(
146 147
      tester.getBottomLeft(find.text('accountEmail')).dy,
      greaterThan(tester.getBottomLeft(find.text('accountName')).dy)
148 149
    );
    expect(
150 151
      tester.getBottomLeft(find.text('accountEmail')).dx,
      tester.getBottomLeft(find.text('accountName')).dx
152 153 154
    );

    await tester.pumpWidget(buildFrame(
155
      currentAccountPicture: const CircleAvatar(child: const Text('A')),
156 157 158 159
    ));
    expect(find.text('A'), findsOneWidget);

    await tester.pumpWidget(buildFrame(
160
      otherAccountsPictures: <Widget>[const CircleAvatar(child: const Text('A'))],
161 162 163
    ));
    expect(find.text('A'), findsOneWidget);

164
    final Key avatarA = const Key('A');
165
    await tester.pumpWidget(buildFrame(
166 167
      currentAccountPicture: new CircleAvatar(key: avatarA, child: const Text('A')),
      accountName: const Text('accountName'),
168 169
    ));
    expect(
170 171
      tester.getBottomLeft(find.byKey(avatarA)).dx,
      tester.getBottomLeft(find.text('accountName')).dx
172 173
    );
    expect(
174 175
      tester.getBottomLeft(find.text('accountName')).dy,
      greaterThan(tester.getBottomLeft(find.byKey(avatarA)).dy)
176 177
    );
  });
178
}