expansion_tile_test.dart 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';

void main() {
10 11
  final Color _dividerColor = const Color(0x1f333333);

12 13 14 15 16 17 18 19 20
  testWidgets('ExpansionTile initial state', (WidgetTester tester) async {
    final Key topKey = new UniqueKey();
    final Key expandedKey = const PageStorageKey<String>('expanded');
    final Key collapsedKey = const PageStorageKey<String>('collapsed');
    final Key defaultKey = const PageStorageKey<String>('default');

    final Key tileKey = new UniqueKey();

    await tester.pumpWidget(new MaterialApp(
21 22 23 24
      theme: new ThemeData(
        platform: TargetPlatform.iOS,
        dividerColor: _dividerColor,
      ),
25 26 27 28 29 30 31 32 33
      home: new Material(
        child: new SingleChildScrollView(
          child: new Column(
            children: <Widget>[
              new ListTile(title: const Text('Top'), key: topKey),
              new ExpansionTile(
                key: expandedKey,
                initiallyExpanded: true,
                title: const Text('Expanded'),
34
                backgroundColor: Colors.red,
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
                children: <Widget>[
                  new ListTile(
                    key: tileKey,
                    title: const Text('0')
                  )
                ]
              ),
              new ExpansionTile(
                key: collapsedKey,
                initiallyExpanded: false,
                title: const Text('Collapsed'),
                children: <Widget>[
                  new ListTile(
                    key: tileKey,
                    title: const Text('0')
                  )
                ]
              ),
              new ExpansionTile(
                key: defaultKey,
                title: const Text('Default'),
                children: <Widget>[
                  const ListTile(title: const Text('0')),
                ]
              )
            ]
          )
        )
      )
    ));

    double getHeight(Key key) => tester.getSize(find.byKey(key)).height;
67 68 69 70
    Container getContainer(Key key) => tester.firstWidget(find.descendant(
      of: find.byKey(key),
      matching: find.byType(Container),
    ));
71 72 73 74 75

    expect(getHeight(topKey), getHeight(expandedKey) - getHeight(tileKey) - 2.0);
    expect(getHeight(topKey), getHeight(collapsedKey) - 2.0);
    expect(getHeight(topKey), getHeight(defaultKey) - 2.0);

76 77
    BoxDecoration expandedContainerDecoration = getContainer(expandedKey).decoration;
    expect(expandedContainerDecoration.color, Colors.red);
78 79
    expect(expandedContainerDecoration.border.top.color, _dividerColor);
    expect(expandedContainerDecoration.border.bottom.color, _dividerColor);
80 81 82 83 84 85

    BoxDecoration collapsedContainerDecoration = getContainer(collapsedKey).decoration;
    expect(collapsedContainerDecoration.color, Colors.transparent);
    expect(collapsedContainerDecoration.border.top.color, Colors.transparent);
    expect(collapsedContainerDecoration.border.bottom.color, Colors.transparent);

86 87 88 89 90
    await tester.tap(find.text('Expanded'));
    await tester.tap(find.text('Collapsed'));
    await tester.tap(find.text('Default'));

    await tester.pump();
91 92 93 94 95 96 97 98 99 100

    // Pump to the middle of the animation for expansion.
    await tester.pump(const Duration(milliseconds: 100));
    final BoxDecoration collapsingContainerDecoration = getContainer(collapsedKey).decoration;
    expect(collapsingContainerDecoration.color, Colors.transparent);
    // Opacity should change but color component should remain the same.
    expect(collapsingContainerDecoration.border.top.color, const Color(0x15333333));
    expect(collapsingContainerDecoration.border.bottom.color, const Color(0x15333333));

    // Pump all the way to the end now.
101 102 103 104 105
    await tester.pump(const Duration(seconds: 1));

    expect(getHeight(topKey), getHeight(expandedKey) - 2.0);
    expect(getHeight(topKey), getHeight(collapsedKey) - getHeight(tileKey) - 2.0);
    expect(getHeight(topKey), getHeight(defaultKey) - getHeight(tileKey) - 2.0);
106 107 108 109 110 111 112 113 114 115

    // Expanded should be collapsed now.
    expandedContainerDecoration = getContainer(expandedKey).decoration;
    expect(expandedContainerDecoration.color, Colors.transparent);
    expect(expandedContainerDecoration.border.top.color, Colors.transparent);
    expect(expandedContainerDecoration.border.bottom.color, Colors.transparent);

    // Collapsed should be expanded now.
    collapsedContainerDecoration = getContainer(collapsedKey).decoration;
    expect(collapsedContainerDecoration.color, Colors.transparent);
116 117
    expect(collapsedContainerDecoration.border.top.color, _dividerColor);
    expect(collapsedContainerDecoration.border.bottom.color, _dividerColor);
118 119
  });
}