animated_container_test.dart 9.66 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

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

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

    expect(container, hasOneLineDescription);
  });

29
  testWidgets('AnimatedContainer control test', (WidgetTester tester) async {
30
    final GlobalKey key = GlobalKey();
31

32 33
    const BoxDecoration decorationA = BoxDecoration(
      color: Color(0xFF00FF00),
34 35
    );

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

    BoxDecoration actualDecoration;

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

50 51
    final RenderDecoratedBox box = key.currentContext.findRenderObject() as RenderDecoratedBox;
    actualDecoration = box.decoration as BoxDecoration;
52
    expect(actualDecoration.color, equals(decorationA.color));
53

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

    expect(key.currentContext.findRenderObject(), equals(box));
63
    actualDecoration = box.decoration as BoxDecoration;
64
    expect(actualDecoration.color, equals(decorationA.color));
65

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

68
    actualDecoration = box.decoration as BoxDecoration;
69
    expect(actualDecoration.color, equals(decorationB.color));
70 71 72

    expect(box, hasAGoodToStringDeep);
    expect(
73
      box.toStringDeep(minLevel: DiagnosticLevel.info),
74 75 76 77 78
      equalsIgnoringHashCodes(
        'RenderDecoratedBox#00000\n'
        ' │ parentData: <none>\n'
        ' │ constraints: BoxConstraints(w=800.0, h=600.0)\n'
        ' │ size: Size(800.0, 600.0)\n'
79
        ' │ decoration: BoxDecoration:\n'
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        ' │   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',
      ),
    );
99
  });
100

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

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

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

    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(
156
      Directionality(
157
        textDirection: TextDirection.rtl,
158
        child: AnimatedContainer(
159 160
          duration: const Duration(milliseconds: 200),
          padding: const EdgeInsetsDirectional.only(start: 100.0),
161
          child: SizedBox.expand(key: target),
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        ),
      ),
    );

    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 {
181
    final Key target = UniqueKey();
182 183

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

    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(
198
      Directionality(
199
        textDirection: TextDirection.rtl,
200
        child: AnimatedContainer(
201
          duration: const Duration(milliseconds: 200),
202
          alignment: AlignmentDirectional.bottomStart,
203
          child: SizedBox(key: target, width: 100.0, height: 200.0),
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        ),
      ),
    );

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

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

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

237 238 239
    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
240

241
    await tester.pump(const Duration(milliseconds: 1000));
242

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

256 257 258 259 260
    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));
261

262
    await tester.pump(const Duration(milliseconds: 1000));
263

264 265
    expect(text.size.width, equals(200.0));
    expect(text.size.height, equals(200.0));
266

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

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

284
    await tester.pump(const Duration(milliseconds: 1000));
285

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