list_tile_test.dart 13.1 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:ui' show SemanticsFlags;

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 52
    // See https://material.io/guidelines/components/lists.html

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 }) {
60 61
      hasSubtitle = isTwoLine || isThreeLine;
      return new MaterialApp(
62
        home: new MediaQuery(
63 64 65 66
          data: new MediaQueryData(
            padding: const EdgeInsets.only(left: leftPadding, right: rightPadding),
            textScaleFactor: textScaleFactor,
          ),
67 68 69 70 71 72 73 74 75 76
          child: new Material(
            child: new Center(
              child: new ListTile(
                leading: new Container(key: leadingKey, width: 24.0, height: 24.0),
                title: const Text('title'),
                subtitle: hasSubtitle ? const Text('subtitle') : null,
                trailing: new Container(key: trailingKey, width: 24.0, height: 24.0),
                dense: dense,
                isThreeLine: isThreeLine,
              ),
77 78 79 80 81 82 83
            ),
          ),
        ),
      );
    }

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

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

    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;

100 101

    // 16.0 padding to the left and right of the leading and trailing widgets
102
    // plus the media padding.
103
    void testHorizontalGeometry() {
104 105
      expect(leftKey(leadingKey), 16.0 + leftPadding);
      expect(left('title'), 72.0 + leftPadding);
106
      if (hasSubtitle)
107
        expect(left('subtitle'), 72.0 + leftPadding);
108
      expect(left('title'), rightKey(leadingKey) + 32.0);
109
      expect(rightKey(trailingKey), 800.0 - 16.0 - rightPadding);
110
      expect(widthKey(trailingKey), 24.0);
111 112 113 114 115 116
    }

    void testVerticalGeometry(double expectedHeight) {
      expect(tester.getSize(find.byType(ListTile)), new Size(800.0, expectedHeight));
      if (hasSubtitle)
        expect(top('subtitle'), bottom('title'));
117
      expect(heightKey(trailingKey), 24.0);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    }

    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();
    testVerticalGeometry(60.0);

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

    await tester.pumpWidget(buildFrame(isThreeLine: true, dense: true));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(76.0);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    await tester.pumpWidget(buildFrame(textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(64.0);

    await tester.pumpWidget(buildFrame(dense: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(64.0);

    await tester.pumpWidget(buildFrame(isTwoLine: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(120.0);

    await tester.pumpWidget(buildFrame(isTwoLine: true, dense: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(120.0);

    await tester.pumpWidget(buildFrame(isThreeLine: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(120.0);

    await tester.pumpWidget(buildFrame(isThreeLine: true, dense: true, textScaleFactor: 4.0));
    testChildren();
    testHorizontalGeometry();
    testVerticalGeometry(120.0);
179 180
  });

181

182
  testWidgets('ListTile geometry (RTL)', (WidgetTester tester) async {
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    const double leftPadding = 10.0;
    const double rightPadding = 20.0;
    await tester.pumpWidget(const MediaQuery(
      data: const MediaQueryData(
        padding: const EdgeInsets.only(left: leftPadding, right: rightPadding),
      ),
      child: const Directionality(
        textDirection: TextDirection.rtl,
        child: const Material(
          child: const Center(
            child: const ListTile(
              leading: const Text('leading'),
              title: const Text('title'),
              trailing: const Text('trailing'),
            ),
198 199 200 201 202 203 204 205 206
          ),
        ),
      ),
    ));

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

    void testHorizontalGeometry() {
207 208
      expect(right('leading'), 800.0 - 16.0 - rightPadding);
      expect(right('title'), 800.0 - 72.0 - rightPadding);
209
      expect(left('leading') - right('title'), 16.0);
210
      expect(left('trailing'), 16.0 + leftPadding);
211 212 213 214 215
    }

    testHorizontalGeometry();
  });

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
  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();
242 243
    final Key leadingKey = new UniqueKey();
    final Key trailingKey = new UniqueKey();
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    ThemeData theme;

    Widget buildFrame({
      bool enabled: true,
      bool dense: false,
      bool selected: false,
      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,
268 269
                    leading: new TestIcon(key: leadingKey),
                    trailing: new TestIcon(key: trailingKey),
270 271 272 273 274 275 276 277 278 279 280 281 282 283
                    title: new TestText('title', key: titleKey),
                    subtitle: new TestText('subtitle', key: subtitleKey),
                  );
                }
              ),
            ),
          ),
        ),
      );
    }

    const Color green = const Color(0xFF00FF00);
    const Color red = const Color(0xFFFF0000);

284
    Color iconColor(Key key) => tester.state<TestIconState>(find.byKey(key)).iconTheme.color;
285 286
    Color textColor(Key key) => tester.state<TestTextState>(find.byKey(key)).textStyle.color;

287
    // A selected ListTile's leading, trailing, and text get the primary color by default
288 289
    await(tester.pumpWidget(buildFrame(selected: true)));
    await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
290 291
    expect(iconColor(leadingKey), theme.primaryColor);
    expect(iconColor(trailingKey), theme.primaryColor);
292 293 294
    expect(textColor(titleKey), theme.primaryColor);
    expect(textColor(subtitleKey), theme.primaryColor);

295
    // A selected ListTile's leading, trailing, and text get the ListTileTheme's selectedColor
296 297
    await(tester.pumpWidget(buildFrame(selected: true, selectedColor: green)));
    await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
298 299
    expect(iconColor(leadingKey), green);
    expect(iconColor(trailingKey), green);
300 301 302
    expect(textColor(titleKey), green);
    expect(textColor(subtitleKey), green);

303
    // An unselected ListTile's leading and trailing get the ListTileTheme's iconColor
304 305 306
    // An unselected ListTile's title texts get the ListTileTheme's textColor
    await(tester.pumpWidget(buildFrame(iconColor: red, textColor: green)));
    await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
307 308
    expect(iconColor(leadingKey), red);
    expect(iconColor(trailingKey), red);
309 310 311 312 313 314
    expect(textColor(titleKey), green);
    expect(textColor(subtitleKey), green);

    // If the item is disabled it's rendered with the theme's disabled color.
    await(tester.pumpWidget(buildFrame(enabled: false)));
    await(tester.pump(const Duration(milliseconds: 300)));  // DefaultTextStyle changes animate
315 316
    expect(iconColor(leadingKey), theme.disabledColor);
    expect(iconColor(trailingKey), theme.disabledColor);
317 318 319 320 321 322 323
    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.
    await(tester.pumpWidget(buildFrame(enabled: false, selected: true)));
    await(tester.pump(const Duration(milliseconds: 300)));  // DefaultTextStyle changes animate
324 325
    expect(iconColor(leadingKey), theme.disabledColor);
    expect(iconColor(trailingKey), theme.disabledColor);
326 327 328 329
    expect(textColor(titleKey), theme.disabledColor);
    expect(textColor(subtitleKey), theme.disabledColor);
  });

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  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(
              children: <Widget>[
                const ListTile(
                  title: const Text('one'),
                ),
                const ListTile(
                  title: const Text('two'),
                  selected: true,
                ),
                const ListTile(
                  title: const Text('three'),
350
                  enabled: false,
351 352 353 354 355 356 357 358 359 360 361 362 363
                ),
              ],
            ),
          ),
        )
      ),
    );

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            label: 'one',
364 365 366 367
            flags: <SemanticsFlags>[
              SemanticsFlags.hasEnabledState,
              SemanticsFlags.isEnabled,
            ],
368 369 370
          ),
          new TestSemantics.rootChild(
            label: 'two',
371 372 373 374 375
            flags: <SemanticsFlags>[
              SemanticsFlags.isSelected,
              SemanticsFlags.hasEnabledState,
              SemanticsFlags.isEnabled,
            ],
376 377 378
          ),
          new TestSemantics.rootChild(
            label: 'three',
379 380 381
            flags: <SemanticsFlags>[
              SemanticsFlags.hasEnabledState,
            ],
382 383 384 385 386 387 388 389
          ),
        ]
      ),
      ignoreTransform: true, ignoreId: true, ignoreRect: true),
    );

    semantics.dispose();
  });
390
}