animated_container_test.dart 5.71 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

void main() {
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  testWidgets('AnimatedContainer.debugFillDescription', (WidgetTester tester) async {
    AnimatedContainer container = new AnimatedContainer(
      constraints: const BoxConstraints.tightFor(width: 17.0, height: 23.0),
      decoration: const BoxDecoration(backgroundColor: const Color(0xFF00FF00)),
      foregroundDecoration: const BoxDecoration(backgroundColor: const Color(0x7F0000FF)),
      margin: const EdgeInsets.all(10.0),
      padding: const EdgeInsets.all(7.0),
      transform: new Matrix4.translationValues(4.0, 3.0, 0.0),
      width: 50.0,
      height: 75.0,
      curve: Curves.ease,
      duration: const Duration(milliseconds: 200),
    );

    expect(container, hasOneLineDescription);
  });

27
  testWidgets('AnimatedContainer control test', (WidgetTester tester) async {
28 29
    GlobalKey key = new GlobalKey();

30 31
    BoxDecoration decorationA = const BoxDecoration(
      backgroundColor: const Color(0xFF00FF00)
32 33
    );

34 35
    BoxDecoration decorationB = const BoxDecoration(
      backgroundColor: const Color(0xFF0000FF)
36 37 38 39
    );

    BoxDecoration actualDecoration;

40
    await tester.pumpWidget(
41 42 43 44 45 46 47 48 49 50 51
      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));

52
    await tester.pumpWidget(
53 54 55 56 57 58 59 60 61 62 63
      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));

64
    await tester.pump(const Duration(seconds: 1));
65 66 67 68

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

70 71
  testWidgets('AnimatedContainer overanimate test', (WidgetTester tester) async {
    await tester.pumpWidget(
72 73
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
74 75
        decoration: const BoxDecoration(
          backgroundColor: const Color(0xFF00FF00)
Adam Barth's avatar
Adam Barth committed
76
        )
77 78 79
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
80
    await tester.pump(const Duration(seconds: 1));
81
    expect(tester.binding.transientCallbackCount, 0);
82
    await tester.pumpWidget(
83 84
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
85 86
        decoration: const BoxDecoration(
          backgroundColor: const Color(0xFF00FF00)
87 88 89 90
        )
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
91
    await tester.pump(const Duration(seconds: 1));
92
    expect(tester.binding.transientCallbackCount, 0);
93
    await tester.pumpWidget(
94 95
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
96 97
        decoration: const BoxDecoration(
          backgroundColor: const Color(0xFF0000FF)
98 99 100 101
        )
      )
    );
    expect(tester.binding.transientCallbackCount, 1); // this is the only time an animation should have started!
102
    await tester.pump(const Duration(seconds: 1));
103
    expect(tester.binding.transientCallbackCount, 0);
104
    await tester.pumpWidget(
105 106
      new AnimatedContainer(
        duration: const Duration(milliseconds: 200),
107 108
        decoration: const BoxDecoration(
          backgroundColor: const Color(0xFF0000FF)
109 110 111 112 113
        )
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
  });
Adam Barth's avatar
Adam Barth committed
114

115 116
  testWidgets('Animation rerun', (WidgetTester tester) async {
    await tester.pumpWidget(
117 118
      new Center(
        child: new AnimatedContainer(
Adam Barth's avatar
Adam Barth committed
119
          duration: const Duration(milliseconds: 200),
120 121 122
          width: 100.0,
          height: 100.0,
          child: new Text('X')
Adam Barth's avatar
Adam Barth committed
123
        )
124 125
      )
    );
Adam Barth's avatar
Adam Barth committed
126

127
    await tester.pump();
128
    await tester.pump(const Duration(milliseconds: 100));
Adam Barth's avatar
Adam Barth committed
129

130 131 132
    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
133

134
    await tester.pump(const Duration(milliseconds: 1000));
135

136
    await tester.pumpWidget(
137 138
      new Center(
        child: new AnimatedContainer(
139
          duration: const Duration(milliseconds: 200),
140 141 142
          width: 200.0,
          height: 200.0,
          child: new Text('X')
143
        )
144 145
      )
    );
146
    await tester.pump();
147
    await tester.pump(const Duration(milliseconds: 100));
148

149 150 151 152 153
    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));
154

155
    await tester.pump(const Duration(milliseconds: 1000));
156

157 158
    expect(text.size.width, equals(200.0));
    expect(text.size.height, equals(200.0));
159

160
    await tester.pumpWidget(
161 162 163 164 165 166
      new Center(
        child: new AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          width: 200.0,
          height: 100.0,
          child: new Text('X')
167
        )
168 169
      )
    );
170
    await tester.pump();
171
    await tester.pump(const Duration(milliseconds: 100));
172

173 174 175
    expect(text.size.width, equals(200.0));
    expect(text.size.height, greaterThan(110.0));
    expect(text.size.height, lessThan(190.0));
176

177
    await tester.pump(const Duration(milliseconds: 1000));
178

179 180
    expect(text.size.width, equals(200.0));
    expect(text.size.height, equals(100.0));
181
  });
Adam Barth's avatar
Adam Barth committed
182
}