list_tile_test.dart 10.7 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

class TestIcon extends StatefulWidget {
9
  const TestIcon({ Key key }) : super(key: key);
10 11 12 13 14 15 16 17 18 19 20

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

class TestIconState extends State<TestIcon> {
  IconThemeData iconTheme;

  @override
  Widget build(BuildContext context) {
    iconTheme = IconTheme.of(context);
21
    return const Icon(Icons.add);
22 23 24 25
  }
}

class TestText extends StatefulWidget {
26
  const TestText(this.text, { Key key }) : super(key: key);
27 28 29 30 31 32 33 34 35 36 37 38 39

  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;
40
    return new Text(widget.text);
41 42 43 44
  }
}

void main() {
45
  testWidgets('ListTile geometry (LTR)', (WidgetTester tester) async {
46 47
    // See https://material.io/guidelines/components/lists.html

48 49
    final Key leadingKey = new GlobalKey();
    final Key trailingKey = new GlobalKey();
50 51
    bool hasSubtitle;

52
    Widget buildFrame({ bool dense: false, bool isTwoLine: false, bool isThreeLine: false, double textScaleFactor: 1.0 }) {
53 54
      hasSubtitle = isTwoLine || isThreeLine;
      return new MaterialApp(
55 56 57 58 59 60 61 62 63 64 65 66
        home: new MediaQuery(
          data: new MediaQueryData(textScaleFactor: textScaleFactor),
          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,
              ),
67 68 69 70 71 72 73
            ),
          ),
        ),
      );
    }

    void testChildren() {
74
      expect(find.byKey(leadingKey), findsOneWidget);
75 76 77
      expect(find.text('title'), findsOneWidget);
      if (hasSubtitle)
        expect(find.text('subtitle'), findsOneWidget);
78
      expect(find.byKey(trailingKey), findsOneWidget);
79 80
    }

81 82 83
    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;
84 85 86 87 88 89

    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;

90 91 92

    // 16.0 padding to the left and right of the leading and trailing widgets
    void testHorizontalGeometry() {
93
      expect(leftKey(leadingKey), 16.0);
94 95 96
      expect(left('title'), 72.0);
      if (hasSubtitle)
        expect(left('subtitle'), 72.0);
97 98 99
      expect(left('title'), rightKey(leadingKey) + 32.0);
      expect(rightKey(trailingKey), 800.0 - 16.0);
      expect(widthKey(trailingKey), 24.0);
100 101 102 103 104 105
    }

    void testVerticalGeometry(double expectedHeight) {
      expect(tester.getSize(find.byType(ListTile)), new Size(800.0, expectedHeight));
      if (hasSubtitle)
        expect(top('subtitle'), bottom('title'));
106
      expect(heightKey(trailingKey), 24.0);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }

    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);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    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);
168 169
  });

170

171
  testWidgets('ListTile geometry (RTL)', (WidgetTester tester) async {
Adam Barth's avatar
Adam Barth committed
172
    await tester.pumpWidget(const Directionality(
173
      textDirection: TextDirection.rtl,
Adam Barth's avatar
Adam Barth committed
174 175 176
      child: const Material(
        child: const Center(
          child: const ListTile(
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
            leading: const Text('leading'),
            title: const Text('title'),
            trailing: const Text('trailing'),
          ),
        ),
      ),
    ));

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

    void testHorizontalGeometry() {
      expect(right('leading'), 800.0 - 16.0);
      expect(right('title'), 800.0 - 72.0);
      expect(left('leading') - right('title'), 16.0);
      expect(left('trailing'), 16.0);
    }

    testHorizontalGeometry();
  });

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  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();
224 225
    final Key leadingKey = new UniqueKey();
    final Key trailingKey = new UniqueKey();
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    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,
250 251
                    leading: new TestIcon(key: leadingKey),
                    trailing: new TestIcon(key: trailingKey),
252 253 254 255 256 257 258 259 260 261 262 263 264 265
                    title: new TestText('title', key: titleKey),
                    subtitle: new TestText('subtitle', key: subtitleKey),
                  );
                }
              ),
            ),
          ),
        ),
      );
    }

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

266
    Color iconColor(Key key) => tester.state<TestIconState>(find.byKey(key)).iconTheme.color;
267 268
    Color textColor(Key key) => tester.state<TestTextState>(find.byKey(key)).textStyle.color;

269
    // A selected ListTile's leading, trailing, and text get the primary color by default
270 271
    await(tester.pumpWidget(buildFrame(selected: true)));
    await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
272 273
    expect(iconColor(leadingKey), theme.primaryColor);
    expect(iconColor(trailingKey), theme.primaryColor);
274 275 276
    expect(textColor(titleKey), theme.primaryColor);
    expect(textColor(subtitleKey), theme.primaryColor);

277
    // A selected ListTile's leading, trailing, and text get the ListTileTheme's selectedColor
278 279
    await(tester.pumpWidget(buildFrame(selected: true, selectedColor: green)));
    await(tester.pump(const Duration(milliseconds: 300))); // DefaultTextStyle changes animate
280 281
    expect(iconColor(leadingKey), green);
    expect(iconColor(trailingKey), green);
282 283 284
    expect(textColor(titleKey), green);
    expect(textColor(subtitleKey), green);

285
    // An unselected ListTile's leading and trailing get the ListTileTheme's iconColor
286 287 288
    // 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
289 290
    expect(iconColor(leadingKey), red);
    expect(iconColor(trailingKey), red);
291 292 293 294 295 296
    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
297 298
    expect(iconColor(leadingKey), theme.disabledColor);
    expect(iconColor(trailingKey), theme.disabledColor);
299 300 301 302 303 304 305
    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
306 307
    expect(iconColor(leadingKey), theme.disabledColor);
    expect(iconColor(trailingKey), theme.disabledColor);
308 309 310 311 312
    expect(textColor(titleKey), theme.disabledColor);
    expect(textColor(subtitleKey), theme.disabledColor);
  });

}