animated_container_test.dart 4.86 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// Copyright 2015 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6 7
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Adam Barth's avatar
Adam Barth committed
8 9 10
import 'package:test/test.dart';

void main() {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  testWidgets('AnimatedContainer control test', (WidgetTester tester) {
    GlobalKey key = new GlobalKey();

    BoxDecoration decorationA = new BoxDecoration(
      backgroundColor: new Color(0xFF00FF00)
    );

    BoxDecoration decorationB = new BoxDecoration(
      backgroundColor: new Color(0xFF0000FF)
    );

    BoxDecoration actualDecoration;

    tester.pumpWidget(
      new AnimatedContainer(
        key: key,
        duration: const Duration(milliseconds: 200),
        decoration: decorationA
      )
    );

    RenderDecoratedBox box = key.currentContext.findRenderObject();
    actualDecoration = box.decoration;
    expect(actualDecoration.backgroundColor, equals(decorationA.backgroundColor));

    tester.pumpWidget(
      new AnimatedContainer(
        key: key,
        duration: const Duration(milliseconds: 200),
        decoration: decorationB
      )
    );

    expect(key.currentContext.findRenderObject(), equals(box));
    actualDecoration = box.decoration;
    expect(actualDecoration.backgroundColor, equals(decorationA.backgroundColor));

    tester.pump(const Duration(seconds: 1));

    actualDecoration = box.decoration;
    expect(actualDecoration.backgroundColor, equals(decorationB.backgroundColor));
  });
53

54 55 56 57 58 59
  testWidgets('AnimatedContainer overanimate test', (WidgetTester tester) {
    tester.pumpWidget(
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        decoration: new BoxDecoration(
          backgroundColor: new Color(0xFF00FF00)
Adam Barth's avatar
Adam Barth 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
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
    tester.pump(new Duration(seconds: 1));
    expect(tester.binding.transientCallbackCount, 0);
    tester.pumpWidget(
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        decoration: new BoxDecoration(
          backgroundColor: new Color(0xFF00FF00)
        )
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
    tester.pump(new Duration(seconds: 1));
    expect(tester.binding.transientCallbackCount, 0);
    tester.pumpWidget(
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        decoration: new BoxDecoration(
          backgroundColor: new Color(0xFF0000FF)
        )
      )
    );
    expect(tester.binding.transientCallbackCount, 1); // this is the only time an animation should have started!
    tester.pump(new Duration(seconds: 1));
    expect(tester.binding.transientCallbackCount, 0);
    tester.pumpWidget(
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        decoration: new BoxDecoration(
          backgroundColor: new Color(0xFF0000FF)
        )
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
  });
Adam Barth's avatar
Adam Barth committed
98

99 100 101 102
  testWidgets('Animation rerun', (WidgetTester tester) {
    tester.pumpWidget(
      new Center(
        child: new AnimatedContainer(
Adam Barth's avatar
Adam Barth committed
103
          duration: const Duration(milliseconds: 200),
104 105 106
          width: 100.0,
          height: 100.0,
          child: new Text('X')
Adam Barth's avatar
Adam Barth committed
107
        )
108 109
      )
    );
Adam Barth's avatar
Adam Barth committed
110

111 112
    tester.pump();
    tester.pump(new Duration(milliseconds: 100));
Adam Barth's avatar
Adam Barth committed
113

114 115 116
    RenderBox text = tester.renderObject(find.text('X'));
    expect(text.size.width, equals(100.0));
    expect(text.size.height, equals(100.0));
Adam Barth's avatar
Adam Barth committed
117

118
    tester.pump(new Duration(milliseconds: 1000));
119

120 121 122
    tester.pumpWidget(
      new Center(
        child: new AnimatedContainer(
123
          duration: const Duration(milliseconds: 200),
124 125 126
          width: 200.0,
          height: 200.0,
          child: new Text('X')
127
        )
128 129 130 131
      )
    );
    tester.pump();
    tester.pump(new Duration(milliseconds: 100));
132

133 134 135 136 137
    text = tester.renderObject(find.text('X'));
    expect(text.size.width, greaterThan(110.0));
    expect(text.size.width, lessThan(190.0));
    expect(text.size.height, greaterThan(110.0));
    expect(text.size.height, lessThan(190.0));
138

139
    tester.pump(new Duration(milliseconds: 1000));
140

141 142
    expect(text.size.width, equals(200.0));
    expect(text.size.height, equals(200.0));
143

144 145 146 147 148 149 150
    tester.pumpWidget(
      new Center(
        child: new AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          width: 200.0,
          height: 100.0,
          child: new Text('X')
151
        )
152 153 154 155
      )
    );
    tester.pump();
    tester.pump(new Duration(milliseconds: 100));
156

157 158 159
    expect(text.size.width, equals(200.0));
    expect(text.size.height, greaterThan(110.0));
    expect(text.size.height, lessThan(190.0));
160

161
    tester.pump(new Duration(milliseconds: 1000));
162

163 164
    expect(text.size.width, equals(200.0));
    expect(text.size.height, equals(100.0));
165
  });
Adam Barth's avatar
Adam Barth committed
166
}