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

import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9 10 11 12 13

class TestPaintingContext implements PaintingContext {
  final List<Invocation> invocations = <Invocation>[];

  @override
14 15 16
  void noSuchMethod(Invocation invocation) {
    invocations.add(invocation);
  }
17 18 19
}

void main() {
20
  group('AnimatedSize', () {
21
    testWidgetsWithLeakTracking('animates forwards then backwards with stable-sized children', (WidgetTester tester) async {
22
      await tester.pumpWidget(
23
        const Center(
24
          child: AnimatedSize(
25 26
            duration: Duration(milliseconds: 200),
            child: SizedBox(
27 28 29
              width: 100.0,
              height: 100.0,
            ),
30 31
          ),
        ),
32 33 34 35 36 37 38
      );

      RenderBox box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(100.0));
      expect(box.size.height, equals(100.0));

      await tester.pumpWidget(
39
        const Center(
40
          child: AnimatedSize(
41 42
            duration: Duration(milliseconds: 200),
            child: SizedBox(
43 44 45
              width: 200.0,
              height: 200.0,
            ),
46 47
          ),
        ),
48 49 50 51 52 53 54
      );

      await tester.pump(const Duration(milliseconds: 100));
      box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(150.0));
      expect(box.size.height, equals(150.0));

55
      TestPaintingContext context = TestPaintingContext();
56 57 58 59 60 61 62 63 64
      box.paint(context, Offset.zero);
      expect(context.invocations.first.memberName, equals(#pushClipRect));

      await tester.pump(const Duration(milliseconds: 100));
      box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(200.0));
      expect(box.size.height, equals(200.0));

      await tester.pumpWidget(
65
        const Center(
66
          child: AnimatedSize(
67 68
            duration: Duration(milliseconds: 200),
            child: SizedBox(
69 70 71 72 73 74 75 76 77 78 79 80
              width: 100.0,
              height: 100.0,
            ),
          ),
        ),
      );

      await tester.pump(const Duration(milliseconds: 100));
      box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(150.0));
      expect(box.size.height, equals(150.0));

81
      context = TestPaintingContext();
82 83 84 85 86 87 88 89 90
      box.paint(context, Offset.zero);
      expect(context.invocations.first.memberName, equals(#paintChild));

      await tester.pump(const Duration(milliseconds: 100));
      box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(100.0));
      expect(box.size.height, equals(100.0));
    });

91
    testWidgetsWithLeakTracking('clamps animated size to constraints', (WidgetTester tester) async {
92
      await tester.pumpWidget(
93
        const Center(
94
          child: SizedBox (
95
            width: 100.0,
96
            height: 100.0,
97
            child: AnimatedSize(
98 99
              duration: Duration(milliseconds: 200),
              child: SizedBox(
100 101 102 103
                width: 100.0,
                height: 100.0,
              ),
            ),
104 105
          ),
        ),
106 107 108 109 110 111 112 113
      );

      RenderBox box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(100.0));
      expect(box.size.height, equals(100.0));

      // Attempt to animate beyond the outer SizedBox.
      await tester.pumpWidget(
114
        const Center(
115
          child: SizedBox (
116 117
            width: 100.0,
            height: 100.0,
118
            child: AnimatedSize(
119 120
              duration: Duration(milliseconds: 200),
              child: SizedBox(
121 122 123 124 125 126 127 128 129 130 131 132 133 134
                width: 200.0,
                height: 200.0,
              ),
            ),
          ),
        ),
      );

      // Verify that animated size is the same as the outer SizedBox.
      await tester.pump(const Duration(milliseconds: 100));
      box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(100.0));
      expect(box.size.height, equals(100.0));
    });
135

136
    testWidgetsWithLeakTracking('tracks unstable child, then resumes animation when child stabilizes', (WidgetTester tester) async {
137
      Future<void> pumpMillis(int millis) async {
138
        await tester.pump(Duration(milliseconds: millis));
139 140
      }

141
      void verify({ double? size, RenderAnimatedSizeState? state }) {
142 143 144 145 146 147 148 149 150 151 152 153
        assert(size != null || state != null);
        final RenderAnimatedSize box = tester.renderObject(find.byType(AnimatedSize));
        if (size != null) {
          expect(box.size.width, size);
          expect(box.size.height, size);
        }
        if (state != null) {
          expect(box.state, state);
        }
      }

      await tester.pumpWidget(
154 155
        Center(
          child: AnimatedSize(
156
            duration: const Duration(milliseconds: 200),
157
            child: AnimatedContainer(
158
              duration: const Duration(milliseconds: 100),
159
              width: 100.0,
160 161 162 163
              height: 100.0,
            ),
          ),
        ),
164 165 166 167 168 169
      );

      verify(size: 100.0, state: RenderAnimatedSizeState.stable);

      // Animate child size from 100 to 200 slowly (100ms).
      await tester.pumpWidget(
170 171
        Center(
          child: AnimatedSize(
172
            duration: const Duration(milliseconds: 200),
173
            child: AnimatedContainer(
174
              duration: const Duration(milliseconds: 100),
175
              width: 200.0,
176 177 178 179
              height: 200.0,
            ),
          ),
        ),
180
      );
181

182 183 184
      // Make sure animation proceeds at child's pace, with AnimatedSize
      // tightly tracking the child's size.
      verify(state: RenderAnimatedSizeState.stable);
185
      await pumpMillis(1); // register change
186 187 188 189 190
      verify(state: RenderAnimatedSizeState.changed);
      await pumpMillis(49);
      verify(size: 150.0, state: RenderAnimatedSizeState.unstable);
      await pumpMillis(50);
      verify(size: 200.0, state: RenderAnimatedSizeState.unstable);
191

192 193 194 195 196 197
      // Stabilize size
      await pumpMillis(50);
      verify(size: 200.0, state: RenderAnimatedSizeState.stable);

      // Quickly (in 1ms) change size back to 100
      await tester.pumpWidget(
198 199
        Center(
          child: AnimatedSize(
200
            duration: const Duration(milliseconds: 200),
201
            child: AnimatedContainer(
202 203 204 205
              duration: const Duration(milliseconds: 1),
              width: 100.0,
              height: 100.0,
            ),
206 207
          ),
        ),
208 209 210
      );

      verify(size: 200.0, state: RenderAnimatedSizeState.stable);
211
      await pumpMillis(1); // register change
212 213 214 215 216 217 218
      verify(state: RenderAnimatedSizeState.changed);
      await pumpMillis(100);
      verify(size: 150.0, state: RenderAnimatedSizeState.stable);
      await pumpMillis(100);
      verify(size: 100.0, state: RenderAnimatedSizeState.stable);
    });

219
    testWidgetsWithLeakTracking('resyncs its animation controller', (WidgetTester tester) async {
220 221
      await tester.pumpWidget(
        const Center(
222 223 224
          child: AnimatedSize(
            duration: Duration(milliseconds: 200),
            child: SizedBox(
225 226 227
              width: 100.0,
              height: 100.0,
            ),
228 229
          ),
        ),
230
      );
231

232
      await tester.pumpWidget(
233
        const Center(
234
          child: AnimatedSize(
235 236
            duration: Duration(milliseconds: 200),
            child: SizedBox(
237 238 239
              width: 200.0,
              height: 100.0,
            ),
240 241
          ),
        ),
242 243 244 245 246 247 248 249
      );

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

      final RenderBox box = tester.renderObject(find.byType(AnimatedSize));
      expect(box.size.width, equals(150.0));
    });

250
    testWidgetsWithLeakTracking('does not run animation unnecessarily', (WidgetTester tester) async {
251
      await tester.pumpWidget(
252
        const Center(
253
          child: AnimatedSize(
254 255
            duration: Duration(milliseconds: 200),
            child: SizedBox(
256 257 258
              width: 100.0,
              height: 100.0,
            ),
259 260
          ),
        ),
261
      );
262

263 264 265 266 267 268 269 270 271
      for (int i = 0; i < 20; i++) {
        final RenderAnimatedSize box = tester.renderObject(find.byType(AnimatedSize));
        expect(box.size.width, 100.0);
        expect(box.size.height, 100.0);
        expect(box.state, RenderAnimatedSizeState.stable);
        expect(box.isAnimating, false);
        await tester.pump(const Duration(milliseconds: 10));
      }
    });
272

273
    testWidgetsWithLeakTracking('can set and update clipBehavior', (WidgetTester tester) async {
274
      await tester.pumpWidget(
275
        const Center(
276
          child: AnimatedSize(
277 278
            duration: Duration(milliseconds: 200),
            child: SizedBox(
279 280 281 282 283 284 285 286 287 288 289
              width: 100.0,
              height: 100.0,
            ),
          ),
        ),
      );

      // By default, clipBehavior should be Clip.hardEdge
      final RenderAnimatedSize renderObject = tester.renderObject(find.byType(AnimatedSize));
      expect(renderObject.clipBehavior, equals(Clip.hardEdge));

290
      for (final Clip clip in Clip.values) {
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        await tester.pumpWidget(
          Center(
            child: AnimatedSize(
              duration: const Duration(milliseconds: 200),
              clipBehavior: clip,
              child: const SizedBox(
                width: 100.0,
                height: 100.0,
              ),
            ),
          ),
        );
        expect(renderObject.clipBehavior, clip);
      }
    });
306

307
    testWidgetsWithLeakTracking('works wrapped in IntrinsicHeight and Wrap', (WidgetTester tester) async {
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
      Future<void> pumpWidget(Size size, [Duration? duration]) async {
        return tester.pumpWidget(
          Center(
            child: IntrinsicHeight(
              child: Wrap(
                textDirection: TextDirection.ltr,
                children: <Widget>[
                  AnimatedSize(
                    duration: const Duration(milliseconds: 200),
                    curve: Curves.easeInOutBack,
                    child: SizedBox(
                      width: size.width,
                      height: size.height,
                    ),
                  ),
                ],
              ),
            ),
          ),
          duration,
        );
      }

      await pumpWidget(const Size(100, 100));
      expect(tester.renderObject<RenderBox>(find.byType(IntrinsicHeight)).size, const Size(100, 100));

      await pumpWidget(const Size(150, 200));
      expect(tester.renderObject<RenderBox>(find.byType(IntrinsicHeight)).size, const Size(100, 100));

      // Each pump triggers verification of dry layout.
      for (int total = 0; total < 200; total += 10) {
        await tester.pump(const Duration(milliseconds: 10));
      }
      expect(tester.renderObject<RenderBox>(find.byType(IntrinsicHeight)).size, const Size(150, 200));

      // Change every pump
      await pumpWidget(const Size(100, 100));
      expect(tester.renderObject<RenderBox>(find.byType(IntrinsicHeight)).size, const Size(150, 200));

      await pumpWidget(const Size(111, 111), const Duration(milliseconds: 10));
      expect(tester.renderObject<RenderBox>(find.byType(IntrinsicHeight)).size, const Size(111, 111));

      await pumpWidget(const Size(222, 222), const Duration(milliseconds: 10));
      expect(tester.renderObject<RenderBox>(find.byType(IntrinsicHeight)).size, const Size(222, 222));
    });
353

354
    testWidgetsWithLeakTracking('re-attach with interrupted animation', (WidgetTester tester) async {
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
      const Key key1 = ValueKey<String>('key1');
      const Key key2 = ValueKey<String>('key2');
      late StateSetter setState;
      Size childSize = const Size.square(100);
      final Widget animatedSize = Center(
        key: GlobalKey(debugLabel: 'animated size'),
        // This SizedBox creates a relayout boundary so _cleanRelayoutBoundary
        // does not mark the descendant render objects below the relayout boundary
        // dirty.
        child: SizedBox.fromSize(
          size: const Size.square(200),
          child: Center(
            child: AnimatedSize(
              duration: const Duration(seconds: 1),
              child: StatefulBuilder(
                builder: (BuildContext context, StateSetter stateSetter) {
                  setState = stateSetter;
                  return SizedBox.fromSize(size: childSize);
                },
              ),
            ),
          ),
        ),
      );

      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Row(
            children: <Widget>[
              SizedBox(
                key: key1,
                height: 200,
                child: animatedSize,
              ),
              const SizedBox(
                key: key2,
                height: 200,
393
              ),
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
            ],
          ),
        )
      );

      setState(() {
        childSize = const Size.square(150);
      });
      // Kick off the resizing animation.
      await tester.pump();

      // Immediately reparent the AnimatedSize subtree to a different parent
      // with the same incoming constraints.
      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Row(
            children: <Widget>[
              const SizedBox(
                key: key1,
                height: 200,
              ),
              SizedBox(
                key: key2,
                height: 200,
                child: animatedSize,
420
              ),
421 422
            ],
          ),
423
        ),
424 425 426 427 428 429 430 431 432 433 434 435 436
      );

      expect(
        tester.renderObject<RenderBox>(find.byType(AnimatedSize)).size,
        const Size.square(100),
      );
      await tester.pumpAndSettle();
      // The animatedSize widget animates to the right size.
      expect(
        tester.renderObject<RenderBox>(find.byType(AnimatedSize)).size,
        const Size.square(150),
      );
    });
437

438
    testWidgetsWithLeakTracking('disposes animation and controller', (WidgetTester tester) async {
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      await tester.pumpWidget(
        const Center(
          child: AnimatedSize(
            duration: Duration(milliseconds: 200),
            child: SizedBox(
              width: 100.0,
              height: 100.0,
            ),
          ),
        ),
      );

      final RenderAnimatedSize box = tester.renderObject(find.byType(AnimatedSize));

      await tester.pumpWidget(
        const Center(),
      );

      expect(box.debugAnimation, isNotNull);
      expect(box.debugAnimation!.isDisposed, isTrue);
      expect(box.debugController, isNotNull);
      expect(
        () => box.debugController!.dispose(),
        throwsA(isA<AssertionError>().having(
          (AssertionError error) => error.message,
          'message',
          equalsIgnoringHashCodes(
            'AnimationController.dispose() called more than once.\n'
            'A given AnimationController cannot be disposed more than once.\n'
            'The following AnimationController object was disposed multiple times:\n'
            '  AnimationController#00000(⏮ 0.000; paused; DISPOSED)',
          ),
        )),
      );
    });
474
  });
475
}