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

    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 {
    final GlobalKey<AnimatedListState> listKey = new GlobalKey<AnimatedListState>();

    await tester.pumpWidget(
42 43 44 45 46 47 48 49 50 51 52 53 54 55
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new AnimatedList(
          key: listKey,
          itemBuilder: (BuildContext context, int index, Animation<double> animation) {
            return new SizeTransition(
              key: new ValueKey<int>(index),
              axis: Axis.vertical,
              sizeFactor: animation,
              child: new SizedBox(
                height: 100.0,
                child: new Center(
                  child: new Text('item $index'),
                ),
Hans Muller's avatar
Hans Muller committed
56
              ),
57 58 59
            );
          },
        ),
Hans Muller's avatar
Hans Muller committed
60 61 62 63 64 65 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 113 114 115 116 117 118 119 120 121 122 123
      ),
    );

    double itemHeight(int index) => tester.getSize(find.byKey(new ValueKey<int>(index))).height;
    double itemTop(int index) => tester.getTopLeft(find.byKey(new ValueKey<int>(index))).dy;
    double itemBottom(int index) => tester.getBottomLeft(find.byKey(new ValueKey<int>(index))).dy;

    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 {
    final GlobalKey<AnimatedListState> listKey = new GlobalKey<AnimatedListState>();
    final List<int> items = <int>[0, 1, 2];

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

    await tester.pumpWidget(
131 132 133 134 135 136 137 138 139
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new AnimatedList(
          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 145 146 147 148 149 150 151 152 153 154 155
      ),
    );

    double itemTop(int index) => tester.getTopLeft(find.byKey(new ValueKey<int>(index))).dy;
    double itemBottom(int index) => tester.getBottomLeft(find.byKey(new ValueKey<int>(index))).dy;

    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);
   });
}