animated_container_test.dart 9.67 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 = new 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 17 18 19 20 21 22 23 24 25 26
      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
    final GlobalKey key = new 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 42 43 44 45 46 47
      new AnimatedContainer(
        key: key,
        duration: const Duration(milliseconds: 200),
        decoration: decorationA
      )
    );

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

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

    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 102
      new AnimatedContainer(
        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 111
      new AnimatedContainer(
        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 120
      new AnimatedContainer(
        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 129
      new AnimatedContainer(
        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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  testWidgets('AnimatedContainer padding visual-to-directional animation', (WidgetTester tester) async {
    final Key target = new UniqueKey();

    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          padding: const EdgeInsets.only(right: 50.0),
          child: new SizedBox.expand(key: target),
        ),
      ),
    );

    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(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: new AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          padding: const EdgeInsetsDirectional.only(start: 100.0),
          child: new SizedBox.expand(key: target),
        ),
      ),
    );

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

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

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

    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
      new Center(
        child: new AnimatedContainer(
Adam Barth's avatar
Adam Barth committed
224
          duration: const Duration(milliseconds: 200),
225 226
          width: 100.0,
          height: 100.0,
Ian Hickson's avatar
Ian Hickson committed
227
          child: const Text('X', textDirection: TextDirection.ltr)
Adam Barth's avatar
Adam Barth committed
228
        )
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
      new Center(
        child: new AnimatedContainer(
244
          duration: const Duration(milliseconds: 200),
245 246
          width: 200.0,
          height: 200.0,
Ian Hickson's avatar
Ian Hickson committed
247
          child: const Text('X', textDirection: TextDirection.ltr)
248
        )
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 268 269 270
      new Center(
        child: new AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          width: 200.0,
          height: 100.0,
Ian Hickson's avatar
Ian Hickson committed
271
          child: const Text('X', textDirection: TextDirection.ltr)
272
        )
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
}