data_table_test.dart 7.75 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1 2 3 4 5 6 7
// Copyright 2017 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';

8
import 'data_table_test_utils.dart';
Adam Barth's avatar
Adam Barth committed
9 10 11

void main() {
  testWidgets('DataTable control test', (WidgetTester tester) async {
12
    final List<String> log = <String>[];
Adam Barth's avatar
Adam Barth committed
13

14
    Widget buildTable({ int sortColumnIndex, bool sortAscending = true }) {
15
      return DataTable(
Adam Barth's avatar
Adam Barth committed
16 17 18 19 20 21
        sortColumnIndex: sortColumnIndex,
        sortAscending: sortAscending,
        onSelectAll: (bool value) {
          log.add('select-all: $value');
        },
        columns: <DataColumn>[
22
          const DataColumn(
23
            label: Text('Name'),
Adam Barth's avatar
Adam Barth committed
24 25
            tooltip: 'Name',
          ),
26
          DataColumn(
27
            label: const Text('Calories'),
Adam Barth's avatar
Adam Barth committed
28 29 30 31 32 33 34
            tooltip: 'Calories',
            numeric: true,
            onSort: (int columnIndex, bool ascending) {
              log.add('column-sort: $columnIndex $ascending');
            }
          ),
        ],
35
        rows: kDesserts.map<DataRow>((Dessert dessert) {
36 37
          return DataRow(
            key: Key(dessert.name),
Adam Barth's avatar
Adam Barth committed
38 39 40 41
            onSelectChanged: (bool selected) {
              log.add('row-selected: ${dessert.name}');
            },
            cells: <DataCell>[
42 43
              DataCell(
                Text(dessert.name),
Adam Barth's avatar
Adam Barth committed
44
              ),
45 46
              DataCell(
                Text('${dessert.calories}'),
Adam Barth's avatar
Adam Barth committed
47 48 49 50 51 52 53 54 55 56 57
                showEditIcon: true,
                onTap: () {
                  log.add('cell-tap: ${dessert.calories}');
                },
              ),
            ],
          );
        }).toList(),
      );
    }

58 59
    await tester.pumpWidget(MaterialApp(
      home: Material(child: buildTable())
Adam Barth's avatar
Adam Barth committed
60 61 62 63 64 65 66
    ));

    await tester.tap(find.byType(Checkbox).first);

    expect(log, <String>['select-all: true']);
    log.clear();

67
    await tester.tap(find.text('Cupcake'));
Adam Barth's avatar
Adam Barth committed
68

69
    expect(log, <String>['row-selected: Cupcake']);
Adam Barth's avatar
Adam Barth committed
70 71
    log.clear();

72
    await tester.tap(find.text('Calories'));
Adam Barth's avatar
Adam Barth committed
73 74 75 76

    expect(log, <String>['column-sort: 1 true']);
    log.clear();

77 78
    await tester.pumpWidget(MaterialApp(
      home: Material(child: buildTable(sortColumnIndex: 1))
Adam Barth's avatar
Adam Barth committed
79
    ));
80
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
81
    await tester.tap(find.text('Calories'));
Adam Barth's avatar
Adam Barth committed
82 83 84 85

    expect(log, <String>['column-sort: 1 false']);
    log.clear();

86 87
    await tester.pumpWidget(MaterialApp(
      home: Material(child: buildTable(sortColumnIndex: 1, sortAscending: false))
Adam Barth's avatar
Adam Barth committed
88
    ));
89
    await tester.pumpAndSettle(const Duration(milliseconds: 200));
Adam Barth's avatar
Adam Barth committed
90 91 92 93 94 95 96 97

    await tester.tap(find.text('375'));

    expect(log, <String>['cell-tap: 375']);
    log.clear();

    await tester.tap(find.byType(Checkbox).last);

98
    expect(log, <String>['row-selected: KitKat']);
Adam Barth's avatar
Adam Barth committed
99 100
    log.clear();
  });
101 102 103

  testWidgets('DataTable overflow test - header', (WidgetTester tester) async {
    await tester.pumpWidget(
104 105 106
      MaterialApp(
        home: Material(
          child: DataTable(
107
            columns: <DataColumn>[
108 109
              DataColumn(
                label: Text('X' * 2000),
110 111 112
              ),
            ],
            rows: const <DataRow>[
113 114 115 116
              DataRow(
                cells: <DataCell>[
                  DataCell(
                    Text('X'),
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byType(Text).first).size.width, greaterThan(800.0));
    expect(tester.renderObject<RenderBox>(find.byType(Row).first).size.width, greaterThan(800.0));
    expect(tester.takeException(), isNull); // column overflows table, but text doesn't overflow cell
  });

  testWidgets('DataTable overflow test - header with spaces', (WidgetTester tester) async {
    await tester.pumpWidget(
132 133 134
      MaterialApp(
        home: Material(
          child: DataTable(
135
            columns: <DataColumn>[
136 137
              DataColumn(
                label: Text('X ' * 2000), // has soft wrap points, but they should be ignored
138 139 140
              ),
            ],
            rows: const <DataRow>[
141 142 143 144
              DataRow(
                cells: <DataCell>[
                  DataCell(
                    Text('X'),
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byType(Text).first).size.width, greaterThan(800.0));
    expect(tester.renderObject<RenderBox>(find.byType(Row).first).size.width, greaterThan(800.0));
    expect(tester.takeException(), isNull); // column overflows table, but text doesn't overflow cell
  }, skip: true); // https://github.com/flutter/flutter/issues/13512

  testWidgets('DataTable overflow test', (WidgetTester tester) async {
    await tester.pumpWidget(
160 161 162
      MaterialApp(
        home: Material(
          child: DataTable(
163
            columns: const <DataColumn>[
164 165
              DataColumn(
                label: Text('X'),
166 167 168
              ),
            ],
            rows: <DataRow>[
169
              DataRow(
170
                cells: <DataCell>[
171 172
                  DataCell(
                    Text('X' * 2000),
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byType(Text).first).size.width, lessThan(800.0));
    expect(tester.renderObject<RenderBox>(find.byType(Row).first).size.width, greaterThan(800.0));
    expect(tester.takeException(), isNull); // cell overflows table, but text doesn't overflow cell
  });

  testWidgets('DataTable overflow test', (WidgetTester tester) async {
    await tester.pumpWidget(
188 189 190
      MaterialApp(
        home: Material(
          child: DataTable(
191
            columns: const <DataColumn>[
192 193
              DataColumn(
                label: Text('X'),
194 195 196
              ),
            ],
            rows: <DataRow>[
197
              DataRow(
198
                cells: <DataCell>[
199 200
                  DataCell(
                    Text('X ' * 2000), // wraps
201 202 203 204 205 206 207 208 209 210 211 212
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    expect(tester.renderObject<RenderBox>(find.byType(Text).first).size.width, lessThan(800.0));
    expect(tester.renderObject<RenderBox>(find.byType(Row).first).size.width, lessThan(800.0));
    expect(tester.takeException(), isNull);
  });
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

  testWidgets('DataTable column onSort test', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: DataTable(
            columns: const <DataColumn>[
              DataColumn(
                label: Text('Dessert'),
              ),
            ],
            rows: const <DataRow>[
              DataRow(
                cells: <DataCell>[
                  DataCell(
                    Text('Lollipop'), // wraps
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    await tester.tap(find.text('Dessert'));
    await tester.pump();
    expect(tester.takeException(), isNull);
  });

  testWidgets('DataTable row onSelectChanged test', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Material(
          child: DataTable(
            columns: const <DataColumn>[
              DataColumn(
                label: Text('Dessert'),
              ),
            ],
            rows: const <DataRow>[
              DataRow(
                cells: <DataCell>[
                  DataCell(
                    Text('Lollipop'), // wraps
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
    await tester.tap(find.text('Lollipop'));
    await tester.pump();
    expect(tester.takeException(), isNull);
  });

Adam Barth's avatar
Adam Barth committed
270
}