data_table_test.dart 2.88 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 15 16 17 18 19 20 21

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

    await tester.pumpWidget(new MaterialApp(
      home: new Material(child: buildTable())
    ));

    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 77 78 79

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

    await tester.pumpWidget(new MaterialApp(
      home: new Material(child: buildTable(sortColumnIndex: 1))
    ));
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 86 87 88

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

    await tester.pumpWidget(new MaterialApp(
      home: new Material(child: buildTable(sortColumnIndex: 1, sortAscending: false))
    ));
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 101
    log.clear();
  });
}