two_level_list_test.dart 3.63 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6 7 8 9
// Copyright 2016 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_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';

void main() {
10
  testWidgets('TwoLevelList basics', (WidgetTester tester) async {
Hans Muller's avatar
Hans Muller committed
11 12 13 14
    final Key topKey = new UniqueKey();
    final Key sublistKey = new UniqueKey();
    final Key bottomKey = new UniqueKey();

15
    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
Hans Muller's avatar
Hans Muller committed
16 17
      '/': (_) {
        return new Material(
18
          child: new SingleChildScrollView(
19
            child: new Column(
20
              children: <Widget>[
21 22
                new ListTile(title: const Text('Top'), key: topKey),
                new ExpansionTile(
Hans Muller's avatar
Hans Muller committed
23
                  key: sublistKey,
24
                  title: const Text('Sublist'),
Hans Muller's avatar
Hans Muller committed
25
                  children: <Widget>[
26 27
                    const ListTile(title: const Text('0')),
                    const ListTile(title: const Text('1'))
Hans Muller's avatar
Hans Muller committed
28 29
                  ]
                ),
30
                new ListTile(title: const Text('Bottom'), key: bottomKey)
Hans Muller's avatar
Hans Muller committed
31 32 33 34 35 36 37
              ]
            )
          )
        );
      }
    };

38
    await tester.pumpWidget(new MaterialApp(routes: routes));
Hans Muller's avatar
Hans Muller committed
39

40 41 42
    expect(find.text('Top'), findsOneWidget);
    expect(find.text('Sublist'), findsOneWidget);
    expect(find.text('Bottom'), findsOneWidget);
Hans Muller's avatar
Hans Muller committed
43

44
    double getY(Key key) => tester.getTopLeft(find.byKey(key)).dy;
45
    double getHeight(Key key) => tester.getSize(find.byKey(key)).height;
Hans Muller's avatar
Hans Muller committed
46

47 48
    expect(getY(topKey), lessThan(getY(sublistKey)));
    expect(getY(sublistKey), lessThan(getY(bottomKey)));
Hans Muller's avatar
Hans Muller committed
49

50 51 52
    // The sublist has a one pixel border above and below.
    expect(getHeight(topKey), equals(getHeight(sublistKey) - 2.0));
    expect(getHeight(bottomKey), equals(getHeight(sublistKey) - 2.0));
Hans Muller's avatar
Hans Muller committed
53

54 55 56
    await tester.tap(find.text('Sublist'));
    await tester.pump(const Duration(seconds: 1));
    await tester.pump(const Duration(seconds: 1));
Hans Muller's avatar
Hans Muller committed
57

58 59 60 61 62
    expect(find.text('Top'), findsOneWidget);
    expect(find.text('Sublist'), findsOneWidget);
    expect(find.text('0'), findsOneWidget);
    expect(find.text('1'), findsOneWidget);
    expect(find.text('Bottom'), findsOneWidget);
Hans Muller's avatar
Hans Muller committed
63

64 65 66 67
    expect(getY(topKey), lessThan(getY(sublistKey)));
    expect(getY(sublistKey), lessThan(getY(bottomKey)));
    expect(getY(bottomKey) - getY(sublistKey), greaterThan(getHeight(topKey)));
    expect(getY(bottomKey) - getY(sublistKey), greaterThan(getHeight(bottomKey)));
Hans Muller's avatar
Hans Muller committed
68
  });
69

70
  testWidgets('onExpansionChanged callback', (WidgetTester tester) async {
71 72 73 74 75
    bool didChangeOpen;

    final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
      '/': (_) {
        return new Material(
76
          child: new SingleChildScrollView(
77
            child: new Column(
78
              children: <Widget>[
79
                new ExpansionTile(
80
                  title: const Text('Sublist'),
81
                  onExpansionChanged: (bool opened) {
82 83 84
                    didChangeOpen = opened;
                  },
                  children: <Widget>[
85 86
                    const ListTile(title: const Text('0')),
                    const ListTile(title: const Text('1'))
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                  ]
                ),
              ]
            )
          )
        );
      }
    };

    await tester.pumpWidget(new MaterialApp(routes: routes));

    expect(didChangeOpen, isNull);
    await tester.tap(find.text('Sublist'));
    expect(didChangeOpen, isTrue);
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    await tester.tap(find.text('Sublist'));
    expect(didChangeOpen, isFalse);
  });
Hans Muller's avatar
Hans Muller committed
106
}