list_tile_test.dart 19.9 KB
Newer Older
1 2 3 4
// Copyright 2015 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:math' as math;

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

11 12
import '../widgets/semantics_tester.dart';

13
class TestIcon extends StatefulWidget {
14
  const TestIcon({ Key key }) : super(key: key);
15 16 17 18 19 20 21 22 23 24 25

  @override
  TestIconState createState() => new TestIconState();
}

class TestIconState extends State<TestIcon> {
  IconThemeData iconTheme;

  @override
  Widget build(BuildContext context) {
    iconTheme = IconTheme.of(context);
26
    return const Icon(Icons.add);
27 28 29 30
  }
}

class TestText extends StatefulWidget {
31
  const TestText(this.text, { Key key }) : super(key: key);
32 33 34 35 36 37 38 39 40 41 42 43 44

  final String text;

  @override
  TestTextState createState() => new TestTextState();
}

class TestTextState extends State<TestText> {
  TextStyle textStyle;

  @override
  Widget build(BuildContext context) {
    textStyle = DefaultTextStyle.of(context).style;
45
    return new Text(widget.text);
46 47 48 49
  }
}

void main() {
50
  testWidgets('ListTile geometry (LTR)', (WidgetTester tester) async {
51
    // See https://material.io/go/design-lists
52

53 54
    final Key leadingKey = new GlobalKey();
    final Key trailingKey = new GlobalKey();
55 56
    bool hasSubtitle;

57 58
    const double leftPadding = 10.0;
    const double rightPadding = 20.0;
59
    Widget buildFrame({ bool dense = false, bool isTwoLine = false, bool isThreeLine = false, double textScaleFactor = 1.0, double subtitleScaleFactor }) {
60
      hasSubtitle = isTwoLine || isThreeLine;
61
      subtitleScaleFactor ??= textScaleFactor;
62
      return new MaterialApp(
63
        home: new MediaQuery(
64 65 66 67
          data: new MediaQueryData(
            padding: const EdgeInsets.only(left: leftPadding, right: rightPadding),
            textScaleFactor: textScaleFactor,
          ),
68 69 70 71 72
          child: new Material(
            child: new Center(
              child: new ListTile(
                leading: new Container(key: leadingKey, width: 24.0, height: 24.0),
                title: const Text('title'),
73
                subtitle: hasSubtitle ? new Text('subtitle', textScaleFactor: subtitleScaleFactor) : null,
74 75 76 77
                trailing: new Container(key: trailingKey, width: 24.0, height: 24.0),
                dense: dense,
                isThreeLine: isThreeLine,
              ),
78 79 80 81 82 83 84
            ),
          ),
        ),
      );
    }

    void testChildren() {
85
      expect(find.byKey(leadingKey), findsOneWidget);
86 87 88
      expect(find.text('title'), findsOneWidget);
      if (hasSubtitle)
        expect(find.text('subtitle'), findsOneWidget);
89
      expect(find.byKey(trailingKey), findsOneWidget);
90 91
    }

92 93 94
    double left(String text) => tester.getTopLeft(find.text(text)).dx;
    double top(String text) => tester.getTopLeft(find.text(text)).dy;
    double bottom(String text) => tester.getBottomLeft(find.text(text)).dy;
95
    double height(String text) => tester.getRect(find.text(text)).height;
96 97 98 99 100 101

    double leftKey(Key key) => tester.getTopLeft(find.byKey(key)).dx;
    double rightKey(Key key) => tester.getTopRight(find.byKey(key)).dx;
    double widthKey(Key key) => tester.getSize(find.byKey(key)).width;
    double heightKey(Key key) => tester.getSize(find.byKey(key)).height;

102 103 104
    // ListTiles are contained by a SafeArea defined like this:
    // SafeArea(top: false, bottom: false, minimum: contentPadding)
    // The default contentPadding is 16.0 on the left and right.
105
    void testHorizontalGeometry() {
106 107
      expect(leftKey(leadingKey), math.max(16.0, leftPadding));
      expect(left('title'), 56.0 + math.max(16.0, leftPadding));
108
      if (hasSubtitle)
109
        expect(left('subtitle'), 56.0 + math.max(16.0, leftPadding));
110
      expect(left('title'), rightKey(leadingKey) + 32.0);
111
      expect(rightKey(trailingKey), 800.0 - math.max(16.0, rightPadding));
112
      expect(widthKey(trailingKey), 24.0);
113 114 115
    }

    void testVerticalGeometry(double expectedHeight) {
116 117 118 119 120 121 122 123 124
      final Rect tileRect = tester.getRect(find.byType(ListTile));
      expect(tileRect.size, new Size(800.0, expectedHeight));
      expect(top('title'), greaterThanOrEqualTo(tileRect.top));
      if (hasSubtitle) {
        expect(top('subtitle'), greaterThanOrEqualTo(bottom('title')));
        expect(bottom('subtitle'), lessThan(tileRect.bottom));
      } else {
        expect(top('title'), equals(tileRect.top + (tileRect.height - height('title')) / 2.0));
      }
125
      expect(heightKey(trailingKey), 24.0);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    }

    await tester.pumpWidget(buildFrame());
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(56.0);

    await tester.pumpWidget(buildFrame(dense: true));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(48.0);

    await tester.pumpWidget(buildFrame(isTwoLine: true));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(72.0);

    await tester.pumpWidget(buildFrame(isTwoLine: true, dense: true));
    testChildren();
    testHorizontalGeometry();
146
    testVerticalGeometry(64.0);
147 148 149 150 151 152 153 154 155 156

    await tester.pumpWidget(buildFrame(isThreeLine: true));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(88.0);

    await tester.pumpWidget(buildFrame(isThreeLine: true, dense: true));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(76.0);
157 158 159 160

    await tester.pumpWidget(buildFrame(textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
161
    testVerticalGeometry(72.0);
162 163 164 165

    await tester.pumpWidget(buildFrame(dense: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
166
    testVerticalGeometry(72.0);
167 168 169 170

    await tester.pumpWidget(buildFrame(isTwoLine: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
171 172 173 174 175 176 177
    testVerticalGeometry(128.0);

    // Make sure that the height of a large subtitle is taken into account.
    await tester.pumpWidget(buildFrame(isTwoLine: true, textScaleFactor: 0.5, subtitleScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(72.0);
178 179 180 181

    await tester.pumpWidget(buildFrame(isTwoLine: true, dense: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
182
    testVerticalGeometry(128.0);
183 184 185 186

    await tester.pumpWidget(buildFrame(isThreeLine: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
187
    testVerticalGeometry(128.0);
188 189 190 191

    await tester.pumpWidget(buildFrame(isThreeLine: true, dense: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
192
    testVerticalGeometry(128.0);
193 194
  });

195
  testWidgets('ListTile geometry (RTL)', (WidgetTester tester) async {
196 197 198
    const double leftPadding = 10.0;
    const double rightPadding = 20.0;
    await tester.pumpWidget(const MediaQuery(
199 200
      data: const MediaQueryData(
        padding: const EdgeInsets.only(left: leftPadding, right: rightPadding),
201
      ),
202
      child: const Directionality(
203
        textDirection: TextDirection.rtl,
204 205 206 207 208 209
        child: const Material(
          child: const Center(
            child: const ListTile(
              leading: const Text('L'),
              title: const Text('title'),
              trailing: const Text('T'),
210
            ),
211 212 213 214 215 216 217 218 219
          ),
        ),
      ),
    ));

    double left(String text) => tester.getTopLeft(find.text(text)).dx;
    double right(String text) => tester.getTopRight(find.text(text)).dx;

    void testHorizontalGeometry() {
220 221 222
      expect(right('L'), 800.0 - math.max(16.0, rightPadding));
      expect(right('title'), 800.0 - 56.0 - math.max(16.0, rightPadding));
      expect(left('T'), math.max(16.0, leftPadding));
223 224 225 226 227
    }

    testHorizontalGeometry();
  });

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
  testWidgets('ListTile.divideTiles', (WidgetTester tester) async {
    final List<String> titles = <String>[ 'first', 'second', 'third' ];

    await tester.pumpWidget(new MaterialApp(
      home: new Material(
        child: new Builder(
          builder: (BuildContext context) {
            return new ListView(
              children: ListTile.divideTiles(
                context: context,
                tiles: titles.map((String title) => new ListTile(title: new Text(title))),
              ).toList(),
            );
          },
        ),
      ),
    ));

    expect(find.text('first'), findsOneWidget);
    expect(find.text('second'), findsOneWidget);
    expect(find.text('third'), findsOneWidget);
  });

  testWidgets('ListTileTheme', (WidgetTester tester) async {
    final Key titleKey = new UniqueKey();
    final Key subtitleKey = new UniqueKey();
254 255
    final Key leadingKey = new UniqueKey();
    final Key trailingKey = new UniqueKey();
256 257 258
    ThemeData theme;

    Widget buildFrame({
259 260 261
      bool enabled = true,
      bool dense = false,
      bool selected = false,
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      Color selectedColor,
      Color iconColor,
      Color textColor,
    }) {
      return new MaterialApp(
        home: new Material(
          child: new Center(
            child: new ListTileTheme(
              dense: dense,
              selectedColor: selectedColor,
              iconColor: iconColor,
              textColor: textColor,
              child: new Builder(
                builder: (BuildContext context) {
                  theme = Theme.of(context);
                  return new ListTile(
                    enabled: enabled,
                    selected: selected,
280 281
                    leading: new TestIcon(key: leadingKey),
                    trailing: new TestIcon(key: trailingKey),
282 283 284 285 286 287 288 289 290 291 292
                    title: new TestText('title', key: titleKey),
                    subtitle: new TestText('subtitle', key: subtitleKey),
                  );
                }
              ),
            ),
          ),
        ),
      );
    }

293 294
    const Color green = const Color(0xFF00FF00);
    const Color red = const Color(0xFFFF0000);
295

296
    Color iconColor(Key key) => tester.state<TestIconState>(find.byKey(key)).iconTheme.color;
297 298
    Color textColor(Key key) => tester.state<TestTextState>(find.byKey(key)).textStyle.color;

299
    // A selected ListTile's leading, trailing, and text get the primary color by default
300 301
    await tester.pumpWidget(buildFrame(selected: true));
    await tester.pump(const Duration(milliseconds: 300)); // DefaultTextStyle changes animate
302 303
    expect(iconColor(leadingKey), theme.primaryColor);
    expect(iconColor(trailingKey), theme.primaryColor);
304 305 306
    expect(textColor(titleKey), theme.primaryColor);
    expect(textColor(subtitleKey), theme.primaryColor);

307
    // A selected ListTile's leading, trailing, and text get the ListTileTheme's selectedColor
308 309
    await tester.pumpWidget(buildFrame(selected: true, selectedColor: green));
    await tester.pump(const Duration(milliseconds: 300)); // DefaultTextStyle changes animate
310 311
    expect(iconColor(leadingKey), green);
    expect(iconColor(trailingKey), green);
312 313 314
    expect(textColor(titleKey), green);
    expect(textColor(subtitleKey), green);

315
    // An unselected ListTile's leading and trailing get the ListTileTheme's iconColor
316
    // An unselected ListTile's title texts get the ListTileTheme's textColor
317 318
    await tester.pumpWidget(buildFrame(iconColor: red, textColor: green));
    await tester.pump(const Duration(milliseconds: 300)); // DefaultTextStyle changes animate
319 320
    expect(iconColor(leadingKey), red);
    expect(iconColor(trailingKey), red);
321 322 323 324
    expect(textColor(titleKey), green);
    expect(textColor(subtitleKey), green);

    // If the item is disabled it's rendered with the theme's disabled color.
325 326
    await tester.pumpWidget(buildFrame(enabled: false));
    await tester.pump(const Duration(milliseconds: 300)); // DefaultTextStyle changes animate
327 328
    expect(iconColor(leadingKey), theme.disabledColor);
    expect(iconColor(trailingKey), theme.disabledColor);
329 330 331 332 333
    expect(textColor(titleKey), theme.disabledColor);
    expect(textColor(subtitleKey), theme.disabledColor);

    // If the item is disabled it's rendered with the theme's disabled color.
    // Even if it's selected.
334 335
    await tester.pumpWidget(buildFrame(enabled: false, selected: true));
    await tester.pump(const Duration(milliseconds: 300)); // DefaultTextStyle changes animate
336 337
    expect(iconColor(leadingKey), theme.disabledColor);
    expect(iconColor(trailingKey), theme.disabledColor);
338 339 340 341
    expect(textColor(titleKey), theme.disabledColor);
    expect(textColor(subtitleKey), theme.disabledColor);
  });

342 343 344 345 346 347 348 349 350 351
  testWidgets('ListTile semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    await tester.pumpWidget(
      new Material(
        child: new Directionality(
          textDirection: TextDirection.ltr,
          child: new MediaQuery(
            data: const MediaQueryData(),
            child: new Column(
352
              children: const <Widget>[
353 354
                const ListTile(
                  title: const Text('one'),
355
                ),
356 357
                const ListTile(
                  title: const Text('two'),
358 359
                  selected: true,
                ),
360 361
                const ListTile(
                  title: const Text('three'),
362
                  enabled: false,
363 364 365 366 367 368 369 370 371 372 373 374 375
                ),
              ],
            ),
          ),
        )
      ),
    );

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            label: 'one',
376 377 378
            flags: <SemanticsFlag>[
              SemanticsFlag.hasEnabledState,
              SemanticsFlag.isEnabled,
379
            ],
380 381 382
          ),
          new TestSemantics.rootChild(
            label: 'two',
383 384 385 386
            flags: <SemanticsFlag>[
              SemanticsFlag.isSelected,
              SemanticsFlag.hasEnabledState,
              SemanticsFlag.isEnabled,
387
            ],
388 389 390
          ),
          new TestSemantics.rootChild(
            label: 'three',
391 392
            flags: <SemanticsFlag>[
              SemanticsFlag.hasEnabledState,
393
            ],
394 395 396 397 398 399 400 401
          ),
        ]
      ),
      ignoreTransform: true, ignoreId: true, ignoreRect: true),
    );

    semantics.dispose();
  });
402 403 404 405 406 407 408 409 410 411 412 413 414 415

  testWidgets('ListTile contentPadding', (WidgetTester tester) async {
    Widget buildFrame(TextDirection textDirection) {
      return new MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          textScaleFactor: 1.0
        ),
        child: new Directionality(
          textDirection: textDirection,
          child: new Material(
            child: new Container(
              alignment: Alignment.topLeft,
              child: const ListTile(
416
                contentPadding: const EdgeInsetsDirectional.only(
417 418 419 420 421
                  start: 10.0,
                  end: 20.0,
                  top: 30.0,
                  bottom: 40.0,
                ),
422 423 424
                leading: const Text('L'),
                title: const Text('title'),
                trailing: const Text('T'),
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
              ),
            ),
          ),
        ),
      );
    }

    double left(String text) => tester.getTopLeft(find.text(text)).dx;
    double right(String text) => tester.getTopRight(find.text(text)).dx;

    await tester.pumpWidget(buildFrame(TextDirection.ltr));

    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 126.0)); // 126 = 56 + 30 + 40
    expect(left('L'), 10.0); // contentPadding.start = 10
    expect(right('T'), 780.0); // 800 - contentPadding.end

    await tester.pumpWidget(buildFrame(TextDirection.rtl));

    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 126.0)); // 126 = 56 + 30 + 40
    expect(left('T'), 20.0); // contentPadding.end = 20
    expect(right('L'), 790.0); // 800 - contentPadding.start
  });

  testWidgets('ListTile contentPadding', (WidgetTester tester) async {
    Widget buildFrame(TextDirection textDirection) {
      return new MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          textScaleFactor: 1.0
        ),
        child: new Directionality(
          textDirection: textDirection,
          child: new Material(
            child: new Container(
              alignment: Alignment.topLeft,
              child: const ListTile(
461
                contentPadding: const EdgeInsetsDirectional.only(
462 463 464 465 466
                  start: 10.0,
                  end: 20.0,
                  top: 30.0,
                  bottom: 40.0,
                ),
467 468 469
                leading: const Text('L'),
                title: const Text('title'),
                trailing: const Text('T'),
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
              ),
            ),
          ),
        ),
      );
    }

    double left(String text) => tester.getTopLeft(find.text(text)).dx;
    double right(String text) => tester.getTopRight(find.text(text)).dx;

    await tester.pumpWidget(buildFrame(TextDirection.ltr));

    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 126.0)); // 126 = 56 + 30 + 40
    expect(left('L'), 10.0); // contentPadding.start = 10
    expect(right('T'), 780.0); // 800 - contentPadding.end

    await tester.pumpWidget(buildFrame(TextDirection.rtl));

    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 126.0)); // 126 = 56 + 30 + 40
    expect(left('T'), 20.0); // contentPadding.end = 20
    expect(right('L'), 790.0); // 800 - contentPadding.start
  });

  testWidgets('ListTileTheme wide leading Widget', (WidgetTester tester) async {
494
    const Key leadingKey = const ValueKey<String>('L');
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559

    Widget buildFrame(double leadingWidth, TextDirection textDirection) {
      return new MediaQuery(
        data: const MediaQueryData(
          padding: EdgeInsets.zero,
          textScaleFactor: 1.0
        ),
        child: new Directionality(
          textDirection: textDirection,
          child: new Material(
            child: new Container(
              alignment: Alignment.topLeft,
              child: new ListTile(
                contentPadding: EdgeInsets.zero,
                leading: new SizedBox(key: leadingKey, width: leadingWidth, height: 32.0),
                title: const Text('title'),
                subtitle: const Text('subtitle'),
              ),
            ),
          ),
        ),
      );
    }

    double left(String text) => tester.getTopLeft(find.text(text)).dx;
    double right(String text) => tester.getTopRight(find.text(text)).dx;

    // textDirection = LTR

    // Two-line tile's height = 72, leading 24x32 widget is vertically centered
    await tester.pumpWidget(buildFrame(24.0, TextDirection.ltr));
    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 72.0));
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(0.0, 20.0));
    expect(tester.getBottomRight(find.byKey(leadingKey)), const Offset(24.0, 52.0));

    // Leading widget's width is 20, so default layout: the left edges of the
    // title and subtitle are at 56dps (contentPadding is zero).
    expect(left('title'), 56.0);
    expect(left('subtitle'), 56.0);

    // If the leading widget is wider than 40 it is separated from the
    // title and subtitle by 16.
    await tester.pumpWidget(buildFrame(56.0, TextDirection.ltr));
    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 72.0));
    expect(tester.getTopLeft(find.byKey(leadingKey)), const Offset(0.0, 20.0));
    expect(tester.getBottomRight(find.byKey(leadingKey)), const Offset(56.0, 52.0));
    expect(left('title'), 72.0);
    expect(left('subtitle'), 72.0);

    // Same tests, textDirection = RTL

    await tester.pumpWidget(buildFrame(24.0, TextDirection.rtl));
    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 72.0));
    expect(tester.getTopRight(find.byKey(leadingKey)), const Offset(800.0, 20.0));
    expect(tester.getBottomLeft(find.byKey(leadingKey)), const Offset(800.0 - 24.0, 52.0));
    expect(right('title'), 800.0 - 56.0);
    expect(right('subtitle'), 800.0 - 56.0);

    await tester.pumpWidget(buildFrame(56.0, TextDirection.rtl));
    expect(tester.getSize(find.byType(ListTile)), const Size(800.0, 72.0));
    expect(tester.getTopRight(find.byKey(leadingKey)), const Offset(800.0, 20.0));
    expect(tester.getBottomLeft(find.byKey(leadingKey)), const Offset(800.0 - 56.0, 52.0));
    expect(right('title'), 800.0 - 72.0);
    expect(right('subtitle'), 800.0 - 72.0);
  });
560
}