animated_list_test.dart 6.27 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6 7 8 9 10 11 12
// 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';

void main() {
  testWidgets('AnimatedList initialItemCount', (WidgetTester tester) async {
    final Map<int, Animation<double>> animations = <int, Animation<double>>{};

    await tester.pumpWidget(
13
      Directionality(
14
        textDirection: TextDirection.ltr,
15
        child: AnimatedList(
16 17 18
          initialItemCount: 2,
          itemBuilder: (BuildContext context, int index, Animation<double> animation) {
            animations[index] = animation;
19
            return SizedBox(
20
              height: 100.0,
21 22
              child: Center(
                child: Text('item $index'),
23 24 25 26
              ),
            );
          },
        ),
Hans Muller's avatar
Hans Muller committed
27 28 29 30 31 32 33 34 35 36 37 38
      ),
    );

    expect(find.text('item 0'), findsOneWidget);
    expect(find.text('item 1'), findsOneWidget);
    expect(animations.containsKey(0), true);
    expect(animations.containsKey(1), true);
    expect(animations[0].value, 1.0);
    expect(animations[1].value, 1.0);
  });

  testWidgets('AnimatedList insert', (WidgetTester tester) async {
39
    final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();
Hans Muller's avatar
Hans Muller committed
40 41

    await tester.pumpWidget(
42
      Directionality(
43
        textDirection: TextDirection.ltr,
44
        child: AnimatedList(
45 46
          key: listKey,
          itemBuilder: (BuildContext context, int index, Animation<double> animation) {
47 48
            return SizeTransition(
              key: ValueKey<int>(index),
49 50
              axis: Axis.vertical,
              sizeFactor: animation,
51
              child: SizedBox(
52
                height: 100.0,
53 54
                child: Center(
                  child: Text('item $index'),
55
                ),
Hans Muller's avatar
Hans Muller committed
56
              ),
57 58 59
            );
          },
        ),
Hans Muller's avatar
Hans Muller committed
60 61 62
      ),
    );

63 64 65
    double itemHeight(int index) => tester.getSize(find.byKey(ValueKey<int>(index), skipOffstage: false)).height;
    double itemTop(int index) => tester.getTopLeft(find.byKey(ValueKey<int>(index), skipOffstage: false)).dy;
    double itemBottom(int index) => tester.getBottomLeft(find.byKey(ValueKey<int>(index), skipOffstage: false)).dy;
Hans Muller's avatar
Hans Muller committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    listKey.currentState.insertItem(0, duration: const Duration(milliseconds: 100));
    await tester.pump();

    // Newly inserted item 0's height should animate from 0 to 100
    expect(itemHeight(0), 0.0);
    await tester.pump(const Duration(milliseconds: 50));
    expect(itemHeight(0), 50.0);
    await tester.pump(const Duration(milliseconds: 50));
    expect(itemHeight(0), 100.0);

    // The list now contains one fully expanded item at the top:
    expect(find.text('item 0'), findsOneWidget);
    expect(itemTop(0), 0.0);
    expect(itemBottom(0), 100.0);

    listKey.currentState.insertItem(0, duration: const Duration(milliseconds: 100));
    listKey.currentState.insertItem(0, duration: const Duration(milliseconds: 100));
    await tester.pump();

    // The height of the newly inserted items at index 0 and 1 should animate from 0 to 100.
    // The height of the original item, now at index 2, should remain 100.
    expect(itemHeight(0), 0.0);
    expect(itemHeight(1), 0.0);
    expect(itemHeight(2), 100.0);
    await tester.pump(const Duration(milliseconds: 50));
    expect(itemHeight(0), 50.0);
    expect(itemHeight(1), 50.0);
    expect(itemHeight(2), 100.0);
    await tester.pump(const Duration(milliseconds: 50));
    expect(itemHeight(0), 100.0);
    expect(itemHeight(1), 100.0);
    expect(itemHeight(2), 100.0);

    // The newly inserted "item 1" and "item 2" appear above "item 0"
    expect(find.text('item 0'), findsOneWidget);
    expect(find.text('item 1'), findsOneWidget);
    expect(find.text('item 2'), findsOneWidget);
    expect(itemTop(0), 0.0);
    expect(itemBottom(0), 100.0);
    expect(itemTop(1), 100.0);
    expect(itemBottom(1), 200.0);
    expect(itemTop(2), 200.0);
    expect(itemBottom(2), 300.0);
  });

  testWidgets('AnimatedList remove', (WidgetTester tester) async {
113
    final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();
Hans Muller's avatar
Hans Muller committed
114 115 116
    final List<int> items = <int>[0, 1, 2];

    Widget buildItem(BuildContext context, int item, Animation<double> animation) {
117 118
      return SizeTransition(
        key: ValueKey<int>(item),
Hans Muller's avatar
Hans Muller committed
119 120
        axis: Axis.vertical,
        sizeFactor: animation,
121
        child: SizedBox(
Hans Muller's avatar
Hans Muller committed
122
          height: 100.0,
123 124
          child: Center(
            child: Text('item $item', textDirection: TextDirection.ltr),
Hans Muller's avatar
Hans Muller committed
125 126 127 128 129 130
          ),
        ),
      );
    }

    await tester.pumpWidget(
131
      Directionality(
132
        textDirection: TextDirection.ltr,
133
        child: AnimatedList(
134 135 136 137 138 139
          key: listKey,
          initialItemCount: 3,
          itemBuilder: (BuildContext context, int index, Animation<double> animation) {
            return buildItem(context, items[index], animation);
          },
        ),
Hans Muller's avatar
Hans Muller committed
140 141 142
      ),
    );

143 144
    double itemTop(int index) => tester.getTopLeft(find.byKey(ValueKey<int>(index))).dy;
    double itemBottom(int index) => tester.getBottomLeft(find.byKey(ValueKey<int>(index))).dy;
Hans Muller's avatar
Hans Muller committed
145 146 147 148 149 150 151 152 153 154 155

    expect(find.text('item 0'), findsOneWidget);
    expect(find.text('item 1'), findsOneWidget);
    expect(find.text('item 2'), findsOneWidget);

    items.removeAt(0);
    listKey.currentState.removeItem(0,
      (BuildContext context, Animation<double> animation) => buildItem(context, 0, animation),
      duration: const Duration(milliseconds: 100),
    );

156
    // Items 0, 1, 2 at 0, 100, 200. All heights 100.
Hans Muller's avatar
Hans Muller committed
157 158 159 160 161 162 163 164 165
    expect(itemTop(0), 0.0);
    expect(itemBottom(0), 100.0);
    expect(itemTop(1), 100.0);
    expect(itemBottom(1), 200.0);
    expect(itemTop(2), 200.0);
    expect(itemBottom(2), 300.0);

    // Newly removed item 0's height should animate from 100 to 0 over 100ms

166
    // Items 0, 1, 2 at 0, 50, 150. Item 0's height is 50.
Hans Muller's avatar
Hans Muller committed
167 168 169 170 171 172 173 174 175
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 50));
    expect(itemTop(0), 0.0);
    expect(itemBottom(0), 50.0);
    expect(itemTop(1), 50.0);
    expect(itemBottom(1), 150.0);
    expect(itemTop(2), 150.0);
    expect(itemBottom(2), 250.0);

176
    // Items 1, 2 at 0, 100.
Hans Muller's avatar
Hans Muller committed
177 178 179 180 181 182 183
    await tester.pumpAndSettle();
    expect(itemTop(1), 0.0);
    expect(itemBottom(1), 100.0);
    expect(itemTop(2), 100.0);
    expect(itemBottom(2), 200.0);
   });
}