animated_container_test.dart 9.56 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
  testWidgets('AnimatedContainer.debugFillProperties', (WidgetTester tester) async {
11
    final AnimatedContainer container = AnimatedContainer(
12
      constraints: const BoxConstraints.tightFor(width: 17.0, height: 23.0),
13 14
      decoration: const BoxDecoration(color: Color(0xFF00FF00)),
      foregroundDecoration: const BoxDecoration(color: Color(0x7F0000FF)),
15 16
      margin: const EdgeInsets.all(10.0),
      padding: const EdgeInsets.all(7.0),
17
      transform: Matrix4.translationValues(4.0, 3.0, 0.0),
18 19 20 21 22 23 24 25 26
      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
    final GlobalKey key = GlobalKey();
29

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

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

    BoxDecoration actualDecoration;

40
    await tester.pumpWidget(
41
      AnimatedContainer(
42 43
        key: key,
        duration: const Duration(milliseconds: 200),
44
        decoration: decorationA,
45 46 47
      )
    );

48
    final RenderDecoratedBox box = key.currentContext.findRenderObject();
49
    actualDecoration = box.decoration;
50
    expect(actualDecoration.color, equals(decorationA.color));
51

52
    await tester.pumpWidget(
53
      AnimatedContainer(
54 55
        key: key,
        duration: const Duration(milliseconds: 200),
56
        decoration: decorationB,
57 58 59 60 61
      )
    );

    expect(key.currentContext.findRenderObject(), equals(box));
    actualDecoration = box.decoration;
62
    expect(actualDecoration.color, equals(decorationA.color));
63

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

    actualDecoration = box.decoration;
67
    expect(actualDecoration.color, equals(decorationB.color));
68 69 70

    expect(box, hasAGoodToStringDeep);
    expect(
71
      box.toStringDeep(minLevel: DiagnosticLevel.info),
72 73 74 75 76
      equalsIgnoringHashCodes(
        'RenderDecoratedBox#00000\n'
        ' │ parentData: <none>\n'
        ' │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        ' │ size: Size(800.0, 600.0)\n'
77
        ' │ decoration: BoxDecoration:\n'
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        ' │   color: Color(0xff0000ff)\n'
        ' │ configuration: ImageConfiguration(bundle:\n'
        ' │   PlatformAssetBundle#00000(), devicePixelRatio: 1.0, platform:\n'
        ' │   android)\n'
        ' │\n'
        ' └─child: RenderLimitedBox#00000\n'
        '   │ parentData: <none> (can use size)\n'
        '   │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '   │ size: Size(800.0, 600.0)\n'
        '   │ maxWidth: 0.0\n'
        '   │ maxHeight: 0.0\n'
        '   │\n'
        '   └─child: RenderConstrainedBox#00000\n'
        '       parentData: <none> (can use size)\n'
        '       constraints: BoxConstraints(w=800.0, h=600.0)\n'
        '       size: Size(800.0, 600.0)\n'
        '       additionalConstraints: BoxConstraints(biggest)\n',
      ),
    );
97
  });
98

99 100
  testWidgets('AnimatedContainer overanimate test', (WidgetTester tester) async {
    await tester.pumpWidget(
101
      AnimatedContainer(
102
        duration: const Duration(milliseconds: 200),
103
        color: const Color(0xFF00FF00),
104 105 106
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
107
    await tester.pump(const Duration(seconds: 1));
108
    expect(tester.binding.transientCallbackCount, 0);
109
    await tester.pumpWidget(
110
      AnimatedContainer(
111
        duration: const Duration(milliseconds: 200),
112
        color: const Color(0xFF00FF00),
113 114 115
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
116
    await tester.pump(const Duration(seconds: 1));
117
    expect(tester.binding.transientCallbackCount, 0);
118
    await tester.pumpWidget(
119
      AnimatedContainer(
120
        duration: const Duration(milliseconds: 200),
121
        color: const Color(0xFF0000FF),
122 123 124
      )
    );
    expect(tester.binding.transientCallbackCount, 1); // this is the only time an animation should have started!
125
    await tester.pump(const Duration(seconds: 1));
126
    expect(tester.binding.transientCallbackCount, 0);
127
    await tester.pumpWidget(
128
      AnimatedContainer(
129
        duration: const Duration(milliseconds: 200),
130
        color: const Color(0xFF0000FF),
131 132 133 134
      )
    );
    expect(tester.binding.transientCallbackCount, 0);
  });
Adam Barth's avatar
Adam Barth committed
135

136
  testWidgets('AnimatedContainer padding visual-to-directional animation', (WidgetTester tester) async {
137
    final Key target = UniqueKey();
138 139

    await tester.pumpWidget(
140
      Directionality(
141
        textDirection: TextDirection.rtl,
142
        child: AnimatedContainer(
143 144
          duration: const Duration(milliseconds: 200),
          padding: const EdgeInsets.only(right: 50.0),
145
          child: SizedBox.expand(key: target),
146 147 148 149 150 151 152 153
        ),
      ),
    );

    expect(tester.getSize(find.byKey(target)), const Size(750.0, 600.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(750.0, 0.0));

    await tester.pumpWidget(
154
      Directionality(
155
        textDirection: TextDirection.rtl,
156
        child: AnimatedContainer(
157 158
          duration: const Duration(milliseconds: 200),
          padding: const EdgeInsetsDirectional.only(start: 100.0),
159
          child: SizedBox.expand(key: target),
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        ),
      ),
    );

    expect(tester.getSize(find.byKey(target)), const Size(750.0, 600.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(750.0, 0.0));

    await tester.pump(const Duration(milliseconds: 100));

    expect(tester.getSize(find.byKey(target)), const Size(725.0, 600.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(725.0, 0.0));

    await tester.pump(const Duration(milliseconds: 500));

    expect(tester.getSize(find.byKey(target)), const Size(700.0, 600.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(700.0, 0.0));
  });

  testWidgets('AnimatedContainer alignment visual-to-directional animation', (WidgetTester tester) async {
179
    final Key target = UniqueKey();
180 181

    await tester.pumpWidget(
182
      Directionality(
183
        textDirection: TextDirection.rtl,
184
        child: AnimatedContainer(
185
          duration: const Duration(milliseconds: 200),
186
          alignment: Alignment.topRight,
187
          child: SizedBox(key: target, width: 100.0, height: 200.0),
188 189 190 191 192 193 194 195
        ),
      ),
    );

    expect(tester.getSize(find.byKey(target)), const Size(100.0, 200.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(800.0, 0.0));

    await tester.pumpWidget(
196
      Directionality(
197
        textDirection: TextDirection.rtl,
198
        child: AnimatedContainer(
199
          duration: const Duration(milliseconds: 200),
200
          alignment: AlignmentDirectional.bottomStart,
201
          child: SizedBox(key: target, width: 100.0, height: 200.0),
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        ),
      ),
    );

    expect(tester.getSize(find.byKey(target)), const Size(100.0, 200.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(800.0, 0.0));

    await tester.pump(const Duration(milliseconds: 100));

    expect(tester.getSize(find.byKey(target)), const Size(100.0, 200.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(800.0, 200.0));

    await tester.pump(const Duration(milliseconds: 500));

    expect(tester.getSize(find.byKey(target)), const Size(100.0, 200.0));
    expect(tester.getTopRight(find.byKey(target)), const Offset(800.0, 400.0));
  });

220 221
  testWidgets('Animation rerun', (WidgetTester tester) async {
    await tester.pumpWidget(
222 223
      Center(
        child: AnimatedContainer(
Adam Barth's avatar
Adam Barth committed
224
          duration: const Duration(milliseconds: 200),
225 226
          width: 100.0,
          height: 100.0,
227 228
          child: const Text('X', textDirection: TextDirection.ltr),
        ),
229 230
      )
    );
Adam Barth's avatar
Adam Barth committed
231

232
    await tester.pump();
233
    await tester.pump(const Duration(milliseconds: 100));
Adam Barth's avatar
Adam Barth committed
234

235 236 237
    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
238

239
    await tester.pump(const Duration(milliseconds: 1000));
240

241
    await tester.pumpWidget(
242 243
      Center(
        child: AnimatedContainer(
244
          duration: const Duration(milliseconds: 200),
245 246
          width: 200.0,
          height: 200.0,
247 248
          child: const Text('X', textDirection: TextDirection.ltr),
        ),
249 250
      )
    );
251
    await tester.pump();
252
    await tester.pump(const Duration(milliseconds: 100));
253

254 255 256 257 258
    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));
259

260
    await tester.pump(const Duration(milliseconds: 1000));
261

262 263
    expect(text.size.width, equals(200.0));
    expect(text.size.height, equals(200.0));
264

265
    await tester.pumpWidget(
266 267
      Center(
        child: AnimatedContainer(
268 269 270
          duration: const Duration(milliseconds: 200),
          width: 200.0,
          height: 100.0,
271 272
          child: const Text('X', textDirection: TextDirection.ltr),
        ),
273 274
      )
    );
275
    await tester.pump();
276
    await tester.pump(const Duration(milliseconds: 100));
277

278 279 280
    expect(text.size.width, equals(200.0));
    expect(text.size.height, greaterThan(110.0));
    expect(text.size.height, lessThan(190.0));
281

282
    await tester.pump(const Duration(milliseconds: 1000));
283

284 285
    expect(text.size.width, equals(200.0));
    expect(text.size.height, equals(100.0));
286
  });
Adam Barth's avatar
Adam Barth committed
287
}