expansion_panel_test.dart 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('ExpansionPanelList test', (WidgetTester tester) async {
    int index;
    bool isExpanded;

    await tester.pumpWidget(
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
      new MaterialApp(
        home: new SingleChildScrollView(
          child: new ExpansionPanelList(
            expansionCallback: (int _index, bool _isExpanded) {
              index = _index;
              isExpanded = _isExpanded;
            },
            children: <ExpansionPanel>[
              new ExpansionPanel(
                headerBuilder: (BuildContext context, bool isExpanded) {
                  return new Text(isExpanded ? 'B' : 'A');
                },
                body: const SizedBox(height: 100.0),
              ),
            ],
          ),
        ),
      ),
32 33 34 35 36
    );

    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsNothing);
    RenderBox box = tester.renderObject(find.byType(ExpansionPanelList));
37
    final double oldHeight = box.size.height;
38 39 40 41 42 43 44
    expect(find.byType(ExpandIcon), findsOneWidget);
    await tester.tap(find.byType(ExpandIcon));
    expect(index, 0);
    expect(isExpanded, isFalse);
    box = tester.renderObject(find.byType(ExpansionPanelList));
    expect(box.size.height, equals(oldHeight));

45
    // now expand the child panel
46
    await tester.pumpWidget(
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      new MaterialApp(
        home: new SingleChildScrollView(
          child: new ExpansionPanelList(
            expansionCallback: (int _index, bool _isExpanded) {
              index = _index;
              isExpanded = _isExpanded;
            },
            children: <ExpansionPanel>[
              new ExpansionPanel(
                headerBuilder: (BuildContext context, bool isExpanded) {
                  return new Text(isExpanded ? 'B' : 'A');
                },
                body: const SizedBox(height: 100.0),
                isExpanded: true, // this is the addition
              ),
            ],
          ),
        ),
      ),
66 67 68 69 70 71 72 73
    );
    await tester.pump(const Duration(milliseconds: 200));

    expect(find.text('A'), findsNothing);
    expect(find.text('B'), findsOneWidget);
    box = tester.renderObject(find.byType(ExpansionPanelList));
    expect(box.size.height - oldHeight, greaterThanOrEqualTo(100.0)); // 100 + some margin
  });
74
  testWidgets('Multiple Panel List test', (WidgetTester tester) async {
75 76 77 78 79 80 81 82 83 84
    await tester.pumpWidget(
      new MaterialApp(
        home: new ListView(
          children: <ExpansionPanelList>[
            new ExpansionPanelList(
              children: <ExpansionPanel>[
                new ExpansionPanel(
                  headerBuilder: (BuildContext context, bool isExpanded) {
                    return new Text(isExpanded ? 'B' : 'A');
                  },
85
                  body: const SizedBox(height: 100.0),
86 87 88 89 90 91 92 93 94 95
                  isExpanded: true,
                ),
              ],
            ),
            new ExpansionPanelList(
              children: <ExpansionPanel>[
                new ExpansionPanel(
                  headerBuilder: (BuildContext context, bool isExpanded){
                    return new Text(isExpanded ? 'D' : 'C');
                  },
96
                  body: const SizedBox(height: 100.0),
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                  isExpanded: true,
                ),
              ],
            ),
          ],
        ),
      ),
    );
    await tester.pump(const Duration(milliseconds: 200));

    expect(find.text('A'), findsNothing);
    expect(find.text('B'), findsOneWidget);
    expect(find.text('C'), findsNothing);
    expect(find.text('D'), findsOneWidget);
  });
112
}