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

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import '../widgets/semantics_tester.dart';

const Key avatarA = const Key('A');
const Key avatarC = const Key('C');
const Key avatarD = const Key('D');

Future<Null> pumpTestWidget(WidgetTester tester, {
  bool withName: true,
  bool withEmail: true,
  bool withOnDetailsPressedHandler: true,
}) async {
  await tester.pumpWidget(
    new MaterialApp(
      home: new MediaQuery(
        data: const MediaQueryData(
          padding: const EdgeInsets.only(
            left: 10.0,
            top: 20.0,
            right: 30.0,
            bottom: 40.0,
29
          ),
30 31 32 33 34 35 36
        ),
        child: new Material(
          child: new Center(
            child: new UserAccountsDrawerHeader(
              onDetailsPressed: withOnDetailsPressedHandler ? () {} : null,
              currentAccountPicture: const ExcludeSemantics(
                child: const CircleAvatar(
37 38
                  key: avatarA,
                  child: const Text('A'),
39
                ),
40
              ),
41
              otherAccountsPictures: const <Widget>[
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
                const CircleAvatar(
                  child: const Text('B'),
                ),
                const CircleAvatar(
                  key: avatarC,
                  child: const Text('C'),
                ),
                const CircleAvatar(
                  key: avatarD,
                  child: const Text('D'),
                ),
                const CircleAvatar(
                  child: const Text('E'),
                )
              ],
              accountName: withName ? const Text('name') : null,
              accountEmail: withEmail ? const Text('email') : null,
59
            ),
60 61 62
          ),
        ),
      ),
63 64 65
    ),
  );
}
66

67 68 69
void main() {
  testWidgets('UserAccountsDrawerHeader test', (WidgetTester tester) async {
    await pumpTestWidget(tester);
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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));

88
    // Verify height = height + top padding + bottom margin + bottom edge)
89
    box = tester.renderObject(find.byType(UserAccountsDrawerHeader));
90
    expect(box.size.height, equals(160.0 + 20.0 + 8.0 + 1.0));
91

92 93 94 95 96 97 98
    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));

99 100 101 102
    expect(avatarATopLeft.dx - topLeft.dx, equals(16.0 + 10.0)); // left padding
    expect(avatarATopLeft.dy - topLeft.dy, equals(16.0 + 20.0)); // add top padding
    expect(topRight.dx - avatarDTopRight.dx, equals(16.0 + 30.0)); // right padding
    expect(avatarDTopRight.dy - topRight.dy, equals(16.0 + 20.0)); // add top padding
103
    expect(avatarDTopRight.dx - avatarCTopRight.dx, equals(40.0 + 16.0)); // size + space between
104
  });
105 106


107
  testWidgets('UserAccountsDrawerHeader null parameters LTR', (WidgetTester tester) async {
108 109 110 111 112 113
    Widget buildFrame({
      Widget currentAccountPicture,
      List<Widget> otherAccountsPictures,
      Widget accountName,
      Widget accountEmail,
      VoidCallback onDetailsPressed,
114
      EdgeInsets margin,
115
    }) {
116 117 118 119 120 121 122 123 124 125 126
      return new MaterialApp(
        home: new Material(
          child: new Center(
            child: new UserAccountsDrawerHeader(
              currentAccountPicture: currentAccountPicture,
              otherAccountsPictures: otherAccountsPictures,
              accountName: accountName,
              accountEmail: accountEmail,
              onDetailsPressed: onDetailsPressed,
              margin: margin,
            ),
127
          ),
128 129 130 131 132
        ),
      );
    }

    await tester.pumpWidget(buildFrame());
133 134
    final RenderBox box = tester.renderObject(find.byType(UserAccountsDrawerHeader));
    expect(box.size.height, equals(160.0 + 1.0)); // height + bottom edge)
135 136 137 138 139 140 141 142
    expect(find.byType(Icon), findsNothing);

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

    await tester.pumpWidget(buildFrame(
143
      accountName: const Text('accountName'),
144 145 146
      onDetailsPressed: () { },
    ));
    expect(
147 148
      tester.getCenter(find.text('accountName')).dy,
      tester.getCenter(find.byType(Icon)).dy
149
    );
150 151 152 153
    expect(
      tester.getCenter(find.text('accountName')).dx,
      lessThan(tester.getCenter(find.byType(Icon)).dx)
    );
154 155

    await tester.pumpWidget(buildFrame(
156
      accountEmail: const Text('accountEmail'),
157 158 159
      onDetailsPressed: () { },
    ));
    expect(
160 161
      tester.getCenter(find.text('accountEmail')).dy,
      tester.getCenter(find.byType(Icon)).dy
162
    );
163 164 165 166
    expect(
      tester.getCenter(find.text('accountEmail')).dx,
      lessThan(tester.getCenter(find.byType(Icon)).dx)
    );
167 168

    await tester.pumpWidget(buildFrame(
169 170
      accountName: const Text('accountName'),
      accountEmail: const Text('accountEmail'),
171 172 173
      onDetailsPressed: () { },
    ));
    expect(
174
      tester.getCenter(find.text('accountEmail')).dy,
175
      tester.getCenter(find.byType(Icon)).dy
176
    );
177 178 179 180
    expect(
      tester.getCenter(find.text('accountEmail')).dx,
      lessThan(tester.getCenter(find.byType(Icon)).dx)
    );
181
    expect(
182 183
      tester.getBottomLeft(find.text('accountEmail')).dy,
      greaterThan(tester.getBottomLeft(find.text('accountName')).dy)
184 185
    );
    expect(
186 187
      tester.getBottomLeft(find.text('accountEmail')).dx,
      tester.getBottomLeft(find.text('accountName')).dx
188 189 190
    );

    await tester.pumpWidget(buildFrame(
191
      currentAccountPicture: const CircleAvatar(child: const Text('A')),
192 193 194 195
    ));
    expect(find.text('A'), findsOneWidget);

    await tester.pumpWidget(buildFrame(
196
      otherAccountsPictures: <Widget>[const CircleAvatar(child: const Text('A'))],
197 198 199
    ));
    expect(find.text('A'), findsOneWidget);

200
    const Key avatarA = const Key('A');
201
    await tester.pumpWidget(buildFrame(
202
      currentAccountPicture: const CircleAvatar(key: avatarA, child: const Text('A')),
203
      accountName: const Text('accountName'),
204 205
    ));
    expect(
206 207
      tester.getBottomLeft(find.byKey(avatarA)).dx,
      tester.getBottomLeft(find.text('accountName')).dx
208 209
    );
    expect(
210 211
      tester.getBottomLeft(find.text('accountName')).dy,
      greaterThan(tester.getBottomLeft(find.byKey(avatarA)).dy)
212 213
    );
  });
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

  testWidgets('UserAccountsDrawerHeader null parameters RTL', (WidgetTester tester) async {
    Widget buildFrame({
      Widget currentAccountPicture,
      List<Widget> otherAccountsPictures,
      Widget accountName,
      Widget accountEmail,
      VoidCallback onDetailsPressed,
      EdgeInsets margin,
    }) {
      return new MaterialApp(
        home: new Directionality(
          textDirection: TextDirection.rtl,
          child: new Material(
            child: new Center(
              child: new UserAccountsDrawerHeader(
                currentAccountPicture: currentAccountPicture,
                otherAccountsPictures: otherAccountsPictures,
                accountName: accountName,
                accountEmail: accountEmail,
                onDetailsPressed: onDetailsPressed,
                margin: margin,
              ),
            ),
          ),
        ),
      );
    }

    await tester.pumpWidget(buildFrame());
    final RenderBox box = tester.renderObject(find.byType(UserAccountsDrawerHeader));
    expect(box.size.height, equals(160.0 + 1.0)); // height + bottom edge)
    expect(find.byType(Icon), findsNothing);

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

    await tester.pumpWidget(buildFrame(
      accountName: const Text('accountName'),
      onDetailsPressed: () { },
    ));
    expect(
      tester.getCenter(find.text('accountName')).dy,
      tester.getCenter(find.byType(Icon)).dy
    );
    expect(
      tester.getCenter(find.text('accountName')).dx,
      greaterThan(tester.getCenter(find.byType(Icon)).dx)
    );

    await tester.pumpWidget(buildFrame(
      accountEmail: const Text('accountEmail'),
      onDetailsPressed: () { },
    ));
    expect(
      tester.getCenter(find.text('accountEmail')).dy,
      tester.getCenter(find.byType(Icon)).dy
    );
    expect(
      tester.getCenter(find.text('accountEmail')).dx,
      greaterThan(tester.getCenter(find.byType(Icon)).dx)
    );

    await tester.pumpWidget(buildFrame(
      accountName: const Text('accountName'),
      accountEmail: const Text('accountEmail'),
      onDetailsPressed: () { },
    ));
    expect(
      tester.getCenter(find.text('accountEmail')).dy,
      tester.getCenter(find.byType(Icon)).dy
    );
    expect(
      tester.getCenter(find.text('accountEmail')).dx,
      greaterThan(tester.getCenter(find.byType(Icon)).dx)
    );
    expect(
      tester.getBottomLeft(find.text('accountEmail')).dy,
      greaterThan(tester.getBottomLeft(find.text('accountName')).dy)
    );
    expect(
      tester.getBottomRight(find.text('accountEmail')).dx,
      tester.getBottomRight(find.text('accountName')).dx
    );

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

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

311
    const Key avatarA = const Key('A');
312
    await tester.pumpWidget(buildFrame(
313
      currentAccountPicture: const CircleAvatar(key: avatarA, child: const Text('A')),
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
      accountName: const Text('accountName'),
    ));
    expect(
      tester.getBottomRight(find.byKey(avatarA)).dx,
      tester.getBottomRight(find.text('accountName')).dx
    );
    expect(
      tester.getBottomLeft(find.text('accountName')).dy,
      greaterThan(tester.getBottomLeft(find.byKey(avatarA)).dy)
    );
  });

  testWidgets('UserAccountsDrawerHeader provides semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);
    await pumpTestWidget(tester);

    expect(
      semantics,
      hasSemantics(
        new TestSemantics(
          children: <TestSemantics>[
            new TestSemantics(
              children: <TestSemantics>[
                new TestSemantics(
338
                  flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
339 340
                  children: <TestSemantics>[
                    new TestSemantics(
341
                      label: 'Signed in\nname\nemail',
342
                      textDirection: TextDirection.ltr,
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
                      children: <TestSemantics>[
                        new TestSemantics(
                          label: r'B',
                          textDirection: TextDirection.ltr,
                        ),
                        new TestSemantics(
                          label: r'C',
                          textDirection: TextDirection.ltr,
                        ),
                        new TestSemantics(
                          label: r'D',
                          textDirection: TextDirection.ltr,
                        ),
                        new TestSemantics(
                          flags: <SemanticsFlag>[SemanticsFlag.isButton],
                          actions: <SemanticsAction>[SemanticsAction.tap],
                          label: r'Show accounts',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
363 364
                    ),
                  ],
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
                ),
              ],
            ),
          ],
        ),
        ignoreId: true, ignoreTransform: true, ignoreRect: true,
      ),
    );

    semantics.dispose();
  });

  testWidgets('UserAccountsDrawerHeader provides semantics with missing properties', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);
    await pumpTestWidget(
      tester,
      withEmail: false,
      withName: false,
      withOnDetailsPressedHandler: false,
    );

    expect(
      semantics,
      hasSemantics(
        new TestSemantics(
          children: <TestSemantics>[
            new TestSemantics(
              children: <TestSemantics>[
                new TestSemantics(
394
                  flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
395 396
                  children: <TestSemantics>[
                    new TestSemantics(
397
                      label: 'Signed in',
398
                      textDirection: TextDirection.ltr,
399 400 401 402 403 404 405 406 407 408 409 410 411 412
                      children: <TestSemantics>[
                        new TestSemantics(
                          label: r'B',
                          textDirection: TextDirection.ltr,
                        ),
                        new TestSemantics(
                          label: r'C',
                          textDirection: TextDirection.ltr,
                        ),
                        new TestSemantics(
                          label: r'D',
                          textDirection: TextDirection.ltr,
                        ),
                      ],
413 414
                    ),
                  ],
415 416 417 418 419 420 421 422 423 424 425
                ),
              ],
            ),
          ],
        ),
        ignoreId: true, ignoreTransform: true, ignoreRect: true,
      ),
    );

    semantics.dispose();
  });
426
}